KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > transport > rmi > RMIClientInvoker


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting.transport.rmi;
10
11 import java.io.IOException JavaDoc;
12 import java.rmi.Remote JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import java.rmi.registry.LocateRegistry JavaDoc;
15 import java.rmi.registry.Registry JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.jboss.remoting.CannotConnectException;
18 import org.jboss.remoting.ConnectionFailedException;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.RemoteClientInvoker;
21 import org.jboss.remoting.marshal.Marshaller;
22 import org.jboss.remoting.marshal.UnMarshaller;
23 import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
24
25 /**
26  * RMIClientInvoker
27  *
28  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
29  * @author <a HREF="mailto:telrod@vocalocity.net">Tom Elrod</a>
30  * @version $Revision: 1.3 $
31  */

32 public class RMIClientInvoker extends RemoteClientInvoker
33 {
34    private RMIServerInvokerInf server;
35
36    /**
37     * Need flag to indicate if have been able to lookup registry and set stub.
38     * Can't do this in the constructor, as need to throw CannotConnectException so
39     * for clustering capability.
40     *
41     * @param locator
42     */

43    private boolean connected = false;
44
45    public RMIClientInvoker(InvokerLocator locator)
46    {
47       super(locator);
48    }
49
50    private int getRegistryPort(InvokerLocator locator)
51    {
52       int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT;
53
54       // See if locator contains a specific registry port
55
Map JavaDoc params = locator.getParameters();
56       if(params != null)
57       {
58          String JavaDoc value = (String JavaDoc) params.get(RMIServerInvoker.REGISTRY_PORT_KEY);
59          if(value != null)
60          {
61             try
62             {
63                port = Integer.parseInt(value);
64                log.debug("Using port " + port + " for rmi registry.");
65             }
66             catch(NumberFormatException JavaDoc e)
67             {
68                log.error("Can not set the RMIServerInvoker RMI registry to port " + value + ". This is not a valid port number.");
69             }
70          }
71       }
72       return port;
73    }
74
75    /**
76     * get the server stub
77     *
78     * @param server
79     */

80    public void setServerStub(RMIServerInvokerInf server)
81    {
82       this.server = server;
83       log.trace(this.server);
84    }
85
86    /**
87     * return the RMI server stub
88     *
89     * @return
90     */

91    public RMIServerInvokerInf getServerStub()
92    {
93       return this.server;
94    }
95
96    /**
97     * subclasses must implement this method to provide a hook to connect to the remote server, if this applies
98     * to the specific transport. However, in some transport implementations, this may not make must difference since
99     * the connection is not persistent among invocations, such as SOAP. In these cases, the method should
100     * silently return without any processing.
101     *
102     * @throws ConnectionFailedException
103     */

104    protected void handleConnect()
105          throws ConnectionFailedException
106    {
107       //TODO: -TME Need to figure this out a little better as am now dealing with
108
// with 2 ports, the rmi server and the registry.
109
try
110       {
111          String JavaDoc host = locator.getHost();
112          int port = getRegistryPort(locator);
113          Registry JavaDoc regsitry = LocateRegistry.getRegistry(host, port);
114          Remote JavaDoc remoteObj = regsitry.lookup("remoting/RMIServerInvoker/" + locator.getPort());
115          log.debug("Remote RMI Stub: " + remoteObj);
116          setServerStub((RMIServerInvokerInf) remoteObj);
117          connected = true;
118       }
119       catch(Exception JavaDoc e)
120       {
121          connected = false;
122          log.debug("Error connecting RMI invoker client.", e);
123          throw new CannotConnectException("Error connecting RMI invoker client", e);
124       }
125    }
126
127    /**
128     * subclasses must implement this method to provide a hook to disconnect from the remote server, if this applies
129     * to the specific transport. However, in some transport implementations, this may not make must difference since
130     * the connection is not persistent among invocations, such as SOAP. In these cases, the method should
131     * silently return without any processing.
132     */

133    protected void handleDisconnect()
134    {
135    }
136
137    protected String JavaDoc getDefaultDataType()
138    {
139       return SerializableMarshaller.DATATYPE;
140    }
141
142    protected Object JavaDoc transport(String JavaDoc sessionId, Object JavaDoc invocation, Map JavaDoc metadata, Marshaller marshaller, UnMarshaller unmarshaller)
143          throws IOException JavaDoc, ConnectionFailedException
144    {
145       if(this.server == null)
146       {
147          log.error("Server stub has not been set in RMI invoker client. See previous errors for details.");
148          //throw new IOException("Server stub hasn't been set!");
149
throw new CannotConnectException("Server stub has not been set.");
150       }
151       try
152       {
153          return server.transport(invocation);
154       }
155       catch(RemoteException JavaDoc e)
156       {
157          throw new CannotConnectException("Error making invocation in RMI client invoker.", e);
158       }
159    }
160 }
161
Popular Tags