/*********************************************************************
* RemotingService.java
* created on 01.12.2006 by netseeker
* $Id: RemotingService.java,v 1.3 2007/03/22 21:01:35 netseeker Exp $
* $Log: RemotingService.java,v $
* Revision 1.3 2007/03/22 21:01:35 netseeker
* *** empty log message ***
*
* Revision 1.2 2006/12/02 18:47:20 netseeker
* added javadoc
*
* Revision 1.1 2006/12/02 18:24:57 netseeker
* refactored and moved the clientside remote reflection parts to the main package
* addred a new generator for generating clientside dynamic proxies for doing remoting requests
*
*
* ====================================================================
*
* Copyright 2005-2006 netseeker aka Michael Manske
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This file is part of the EJOE framework.
* For more information on the author, please see
* <http://www.manskes.de/>.
*
*********************************************************************/
package de.netseeker.ejoe;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import de.netseeker.ejoe.request.RemotingRequest;
/**
* JDK Proxy factory and InvocationHandler
* This factory implementation can be used to do Remoting Requests in
* a very elegantly way by using Interfaces for the targeted remote service class.
*
* Basic usage:
* <pre>
* EJClient client = new EJClient(...);
* MyInterface service = (ISimpleTypes) RemotingService.createService(
* MyRemoteService.class.getName(), MyInterface.class, client );
* SomeResponse response = service.doSomething(someArgument,...);
* </pre>
*
* @author netseeker
* @since 0.3.9.2
*/
public class RemotingService implements InvocationHandler
{
private String _remoteClassName;
private EJClient _client;
private EJAsyncCallback _callback;
/**
* Creates a new JDK Proxy for doing Remoting Requests using a given Interface
* @param svRemoteClassName Targeted remote service class within the EJServer
* @param svInterface Interface according to the methods in targeted remote service class
* @param svClient EJClient to use for processing requests
* @return JDK Proxy implementing the given Interface
*/
public static Object createService( String svRemoteClassName, Class svInterface, EJClient svClient )
{
return Proxy.newProxyInstance( svClient.getClass().getClassLoader(), new Class[] { svInterface },
new RemotingService( svRemoteClassName, svClient, null ) );
}
/**
* Creates a new JDK Proxy for doing asynchronous Remoting Requests using a given Interface
* @param svRemoteClassName Targeted remote service class within the EJServer
* @param svInterface Interface according to the methods in targeted remote service class
* @param svClient EJClient to use for processing requests
* @param svCallback Callback handler to inform whenever a request is completed or an error occured
* @return JDK Proxy implementing the given Interface for processing asynchronous requests
*/
public static Object createAsyncService( String svRemoteClassName, Class svInterface, EJClient svClient,
EJAsyncCallback svCallback )
{
return Proxy.newProxyInstance( svClient.getClass().getClassLoader(), new Class[] { svInterface },
new RemotingService( svRemoteClassName, svClient, svCallback ) );
}
/**
* @param remoteClassName
* @param client
* @param callback
*/
protected RemotingService(String remoteClassName, EJClient client, EJAsyncCallback callback)
{
if ( client == null )
{
throw new IllegalArgumentException( "EJClient must not be null!" );
}
if ( remoteClassName == null || remoteClassName.length() == 0 )
{
throw new IllegalArgumentException( "RemoteClassName must not be null or empty!" );
}
this._remoteClassName = remoteClassName;
this._client = client;
this._callback = callback;
}
/*
* (non-Javadoc)
*
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
{
RemotingRequest request = new RemotingRequest( this._remoteClassName, method.getName(), args );
if ( this._callback == null )
{
return this._client.execute( request );
}
else
{
this._client.executeAsync( request, this._callback );
}
return null;
}
}