View Javadoc

1   /**********************************************************************
2    * RemotingService.java
3    * created on 01.12.2006 by netseeker
4    * $Id: RemotingService.java,v 1.3 2007/03/22 21:01:35 netseeker Exp $
5    * $Log: RemotingService.java,v $
6    * Revision 1.3  2007/03/22 21:01:35  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.2  2006/12/02 18:47:20  netseeker
10   * added javadoc
11   *
12   * Revision 1.1  2006/12/02 18:24:57  netseeker
13   * refactored and moved the clientside remote reflection parts to the main package
14   * addred a new generator for generating clientside dynamic proxies for doing remoting requests
15   *
16   *
17   * ====================================================================
18   *
19   *  Copyright 2005-2006 netseeker aka Michael Manske
20   *
21   *  Licensed under the Apache License, Version 2.0 (the "License");
22   *  you may not use this file except in compliance with the License.
23   *  You may obtain a copy of the License at
24   *
25   *      http://www.apache.org/licenses/LICENSE-2.0
26   *
27   *  Unless required by applicable law or agreed to in writing, software
28   *  distributed under the License is distributed on an "AS IS" BASIS,
29   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30   *  See the License for the specific language governing permissions and
31   *  limitations under the License.
32   * ====================================================================
33   *
34   * This file is part of the EJOE framework.
35   * For more information on the author, please see
36   * <http://www.manskes.de/>.
37   *
38   *********************************************************************/
39  package de.netseeker.ejoe;
40  
41  import java.lang.reflect.InvocationHandler;
42  import java.lang.reflect.Method;
43  import java.lang.reflect.Proxy;
44  
45  import de.netseeker.ejoe.request.RemotingRequest;
46  
47  /***
48   * JDK Proxy factory and InvocationHandler
49   * This factory implementation can be used to do Remoting Requests in
50   * a very elegantly way by using Interfaces for the targeted remote service class.
51   * 
52   * Basic usage:
53   * <pre>
54   * EJClient client = new EJClient(...);
55   * MyInterface service = (ISimpleTypes) RemotingService.createService( 
56   *                              MyRemoteService.class.getName(), MyInterface.class, client );
57   * SomeResponse response = service.doSomething(someArgument,...);
58   * </pre>
59   *  
60   * @author netseeker
61   * @since 0.3.9.2
62   */
63  public class RemotingService implements InvocationHandler
64  {
65      private String          _remoteClassName;
66  
67      private EJClient        _client;
68  
69      private EJAsyncCallback _callback;
70  
71      /***
72       * Creates a new JDK Proxy for doing Remoting Requests using a given Interface
73       * @param svRemoteClassName Targeted remote service class within the EJServer
74       * @param svInterface Interface according to the methods in targeted remote service class
75       * @param svClient EJClient to use for processing requests
76       * @return JDK Proxy implementing the given Interface
77       */
78      public static Object createService( String svRemoteClassName, Class svInterface, EJClient svClient )
79      {
80          return Proxy.newProxyInstance( svClient.getClass().getClassLoader(), new Class[] { svInterface },
81                                         new RemotingService( svRemoteClassName, svClient, null ) );
82      }
83  
84      /***
85       * Creates a new JDK Proxy for doing asynchronous Remoting Requests using a given Interface
86       * @param svRemoteClassName Targeted remote service class within the EJServer
87       * @param svInterface Interface according to the methods in targeted remote service class
88       * @param svClient EJClient to use for processing requests
89       * @param svCallback Callback handler to inform whenever a request is completed or an error occured
90       * @return JDK Proxy implementing the given Interface for processing asynchronous requests 
91       */
92      public static Object createAsyncService( String svRemoteClassName, Class svInterface, EJClient svClient,
93                                               EJAsyncCallback svCallback )
94      {
95          return Proxy.newProxyInstance( svClient.getClass().getClassLoader(), new Class[] { svInterface },
96                                         new RemotingService( svRemoteClassName, svClient, svCallback ) );
97      }
98  
99      /***
100      * @param remoteClassName
101      * @param client
102      * @param callback
103      */
104     protected RemotingService(String remoteClassName, EJClient client, EJAsyncCallback callback)
105     {
106         if ( client == null )
107         {
108             throw new IllegalArgumentException( "EJClient must not be null!" );
109         }
110         if ( remoteClassName == null || remoteClassName.length() == 0 )
111         {
112             throw new IllegalArgumentException( "RemoteClassName must not be null or empty!" );
113         }
114 
115         this._remoteClassName = remoteClassName;
116         this._client = client;
117         this._callback = callback;
118     }
119 
120     /*
121      * (non-Javadoc)
122      * 
123      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
124      */
125     public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
126     {
127         RemotingRequest request = new RemotingRequest( this._remoteClassName, method.getName(), args );
128         if ( this._callback == null )
129         {
130             return this._client.execute( request );
131         }
132         else
133         {
134             this._client.executeAsync( request, this._callback );
135         }
136 
137         return null;
138     }
139 }