KickJava   Java API By Example, From Geeks To Geeks.

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


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.RemoteException JavaDoc;
13 import java.rmi.registry.LocateRegistry JavaDoc;
14 import java.rmi.registry.Registry JavaDoc;
15 import java.rmi.server.ExportException JavaDoc;
16 import java.rmi.server.RemoteStub JavaDoc;
17 import java.rmi.server.UnicastRemoteObject JavaDoc;
18 import java.util.Map JavaDoc;
19 import org.jboss.remoting.InvokerLocator;
20 import org.jboss.remoting.ServerInvoker;
21 import org.jboss.remoting.marshal.serializable.SerializableMarshaller;
22
23 /**
24  * RMIServerInvoker
25  *
26  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
27  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
28  * @version $Revision: 1.4 $
29  */

30 public class RMIServerInvoker extends ServerInvoker implements RMIServerInvokerInf
31 {
32    private RemoteStub JavaDoc stub;
33
34    public static final int DEFAULT_REGISTRY_PORT = 3455;
35    public static final String JavaDoc REGISTRY_PORT_KEY = "registryPort";
36
37    public RMIServerInvoker(InvokerLocator locator)
38    {
39       super(locator);
40    }
41
42    protected void setup() throws Exception JavaDoc
43    {
44       super.setup();
45
46       int bindPort = getServerBindPort();
47       Registry JavaDoc registry = null;
48       registry = getRegistry();
49       stub = (RemoteStub JavaDoc) UnicastRemoteObject.exportObject(this, bindPort);
50
51       log.debug("Binding registry to " + "remoting/RMIServerInvoker/" + bindPort);
52
53       registry.rebind("remoting/RMIServerInvoker/" + bindPort, this);
54    }
55
56    public RMIServerInvoker(InvokerLocator locator, Map JavaDoc configuration)
57    {
58       super(locator, configuration);
59    }
60
61    private Registry JavaDoc getRegistry() throws Exception JavaDoc
62    {
63       Registry JavaDoc registry = null;
64
65       int port = DEFAULT_REGISTRY_PORT;
66
67       // See if locator contains a specific registry port
68
Map JavaDoc params = getConfiguration();
69       if(params != null)
70       {
71          String JavaDoc value = (String JavaDoc) params.get(REGISTRY_PORT_KEY);
72          if(value != null)
73          {
74             try
75             {
76                port = Integer.parseInt(value);
77                log.debug("Using port " + port + " for rmi registry.");
78             }
79             catch(NumberFormatException JavaDoc e)
80             {
81                throw new Exception JavaDoc("Can not set the RMIServerInvoker RMI registry to port " + value + ". This is not a valid port number.");
82             }
83          }
84       }
85
86       try
87       {
88          log.debug("Creating registry for " + port);
89
90          registry = LocateRegistry.createRegistry(port);
91       }
92       catch(ExportException JavaDoc exportEx)
93       {
94          log.debug("Locating registry for " + port);
95
96          // Probably means that the registry already exists, so just get it.
97
registry = LocateRegistry.getRegistry(port);
98       }
99       if(log.isTraceEnabled())
100       {
101          log.trace("Got registry: " + registry);
102       }
103       return registry;
104    }
105
106    protected String JavaDoc getDefaultDataType()
107    {
108       return SerializableMarshaller.DATATYPE;
109    }
110
111    /**
112     * destroy the RMI Server Invoker, which will unexport the RMI server
113     */

114    public void destroy()
115    {
116       super.destroy();
117       try
118       {
119          try
120          {
121             log.debug("unbinding " + "remoting/RMIServerInvoker/" + locator.getPort() + " from registry running on port " + (locator.getPort() + 1));
122             Registry JavaDoc registry = getRegistry();
123             registry.unbind("remoting/RMIServerInvoker/" + locator.getPort());
124          }
125          catch(Exception JavaDoc e)
126          {
127             log.error("Error unbinding RMIServerInvoker from RMI registry.", e);
128          }
129
130          UnicastRemoteObject.unexportObject(this, true);
131
132       }
133       catch(java.rmi.NoSuchObjectException JavaDoc e)
134       {
135
136       }
137    }
138
139    protected void finalize() throws Throwable JavaDoc
140    {
141       destroy();
142       super.finalize();
143    }
144
145    /**
146     * returns true if the transport is bi-directional in nature, for example,
147     * SOAP in unidirectional and SOCKETs are bi-directional (unless behind a firewall
148     * for example).
149     *
150     * @return
151     */

152    public boolean isTransportBiDirectional()
153    {
154       return true;
155    }
156
157    public final RemoteStub JavaDoc getStub()
158    {
159       return stub;
160    }
161
162    public Object JavaDoc transport(Object JavaDoc invocation)
163          throws RemoteException JavaDoc, IOException JavaDoc
164    {
165       return invoke(invocation);
166    }
167 }
168
Popular Tags