KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > resources > rmi > RmiRegistry


1 /*
2  * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29 package com.caucho.resources.rmi;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.jca.AbstractResourceAdapter;
33 import com.caucho.log.Log;
34 import com.caucho.util.L10N;
35
36 import javax.resource.spi.BootstrapContext JavaDoc;
37 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
38 import java.rmi.registry.LocateRegistry JavaDoc;
39 import java.rmi.registry.Registry JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.LinkedList JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * An RMI registry and its services. This resource is used to register
47  * services with an RMI Registry. The Registry is either on localhost, in
48  * which case it is created in the local JVM unless it already exists, or the
49  * Registry is on a remote server, in which case it is assumed that the
50  * Registry has already been started.
51  */

52
53 public class RmiRegistry extends AbstractResourceAdapter
54 {
55   static protected final Logger JavaDoc log = Log.open(RmiRegistry.class);
56   static final L10N L = new L10N(RmiRegistry.class);
57
58   private String JavaDoc _server = "localhost";
59   private int _port = 1099;
60
61   private LinkedList JavaDoc<RmiService> _services = new LinkedList JavaDoc<RmiService>();
62
63   private String JavaDoc _namePrefix;
64
65   /**
66    * The server that runs the RMI registry. If this is `localhost' (the
67    * default), then the Registry is created on the localhost if it does not
68    * already exist. If the server is not localhost, then it is assumed that
69    * the remote Registry already exists.
70    */

71   public void setServer(String JavaDoc server)
72   {
73     _server = server;
74   }
75
76   public String JavaDoc getServer()
77   {
78     return _server;
79   }
80
81   /**
82    * The port for the Registry, default is 1099.
83    */

84   public void setPort(int port)
85   {
86     _port = port;
87   }
88
89   /**
90    * The port for the Registry.
91    */

92   public int getPort()
93   {
94     return _port;
95   }
96
97   /**
98    * Add an RMI service to register with this Registry.
99    */

100   public void addRmiService(RmiService service)
101     throws ConfigException
102   {
103     _services.add(service);
104     
105   }
106
107   public void init() throws ConfigException
108   {
109     if (System.getSecurityManager() == null)
110       throw new ConfigException("RMI requires a SecurityManager - add a <security-manager/> element to <resin>");
111
112     _namePrefix =("//" + _server + ':' + _port + '/');
113   }
114
115   public void start(BootstrapContext JavaDoc ctx)
116     throws ResourceAdapterInternalException JavaDoc
117   {
118     if (_server.equals("localhost"))
119         startRegistry();
120     else {
121       log.config(L.l("using remote RMI Registry `{0}:{1}'",_server,String.valueOf(_port)));
122     }
123
124     // start (export and bind) all services
125
for (Iterator JavaDoc<RmiService> i = _services.iterator(); i.hasNext(); ) {
126       RmiService s = i.next();
127
128       s.start();
129     }
130   }
131
132   /**
133    * Make a full name for a service with this registry and
134    * the given serviceName.
135    */

136   String JavaDoc makeFullName(String JavaDoc serviceName)
137   {
138     return _namePrefix + serviceName;
139   }
140
141   /**
142    * Start the RMI registry in the local JVM, if it has not been started
143    * already.
144    */

145   private void startRegistry()
146     throws ResourceAdapterInternalException JavaDoc
147   {
148     /**
149      * Some tricks are required here, the RMI Registry needs to be
150      * started from the system classloader.
151      */

152     Thread JavaDoc thread = Thread.currentThread();
153     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
154
155     try {
156       thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
157
158       try {
159         Registry JavaDoc reg = LocateRegistry.getRegistry(_port);
160         reg.list(); // Verify it's alive and well
161
if (log.isLoggable(Level.CONFIG))
162           log.config(L.l("found RMI Registry on port `{0}'",
163              _port));
164       }
165       catch (Exception JavaDoc e) {
166         // couldn't find a valid registry so create one
167
if (log.isLoggable(Level.CONFIG))
168           log.config(L.l("creating RMI Registry on port `{0}'", _port));
169         
170         LocateRegistry.createRegistry(_port);
171       }
172     } catch (Exception JavaDoc ex) {
173       throw new ResourceAdapterInternalException JavaDoc(ex);
174     } finally {
175       thread.setContextClassLoader(oldLoader);
176     }
177   }
178       
179   /**
180    * stop (unbind and unexport) all services
181    */

182   public void stop()
183     throws ResourceAdapterInternalException JavaDoc
184   {
185     // unbind and unexport all services
186
for (Iterator JavaDoc<RmiService> i = _services.iterator(); i.hasNext(); ) {
187       RmiService s = i.next();
188       s.stop();
189     }
190   }
191 }
192
Popular Tags