KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > rmi > registry > Registry


1 /***
2  * Fractal RMI: a binder for remote method calls between Fractal components.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Bruno Dillenseger, Eric Bruneton
22  */

23
24 package org.objectweb.fractal.rmi.registry;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.factory.GenericFactory;
28 import org.objectweb.fractal.api.type.ComponentType;
29 import org.objectweb.fractal.api.type.InterfaceType;
30 import org.objectweb.fractal.api.type.TypeFactory;
31
32 import org.objectweb.fractal.adl.Factory;
33 import org.objectweb.fractal.adl.FactoryFactory;
34
35 import org.objectweb.fractal.util.Fractal;
36
37 import org.objectweb.fractal.jonathan.JContextFactory;
38 import org.objectweb.jonathan.apis.binding.Identifier;
39 import org.objectweb.jonathan.apis.binding.NamingContext;
40 import org.objectweb.jonathan.apis.kernel.Context;
41 import org.objectweb.jonathan.apis.kernel.JonathanException;
42
43 import java.net.InetAddress JavaDoc;
44
45 /**
46  * Provides static methods to launch and get access to a Fractal registry.
47  */

48
49 public class Registry {
50
51   /**
52    * The default port used to export the naming service interface.
53    */

54
55   public final static int DEFAULT_PORT = 1234;
56
57   /**
58    * Private constructor (uninstantiable class).
59    */

60
61   private Registry () {
62   }
63
64   /**
65    * Launches a Fractal registry on the local host.
66    *
67    * @param args a single argument setting the port number where to pick the
68    * naming service interface.
69    * @throws Exception if something goes wrong.
70    */

71
72   public static void main (final String JavaDoc[] args)
73     throws Exception JavaDoc
74   {
75     // port number to export the registry interface
76
int port = DEFAULT_PORT;
77     if (args.length == 1) {
78       try {
79         port = Integer.parseInt(args[0]);
80       } catch (NumberFormatException JavaDoc nfe) {
81       }
82     }
83     createRegistry(port);
84   }
85
86   /**
87    * Creates {@link NamingService} component and exports its server interface.
88    *
89    * @param port the port number to be used to export the naming service
90    * interface.
91    * @throws Exception if something goes wrong.
92    */

93
94   public static void createRegistry (final int port)
95     throws Exception JavaDoc
96   {
97     // the RMI class loader does not work if there is no security manager
98
System.setSecurityManager(new SecurityManager JavaDoc());
99
100     // constructs the Fractal RMI binder
101
Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
102     Component comp =
103       (Component)f.newComponent("org.objectweb.fractal.rmi.ORB", null);
104     Fractal.getLifeCycleController(comp).startFc();
105     NamingContext binder = (NamingContext)comp.getFcInterface("context");
106
107     // creates the name service component
108
Component boot = Fractal.getBootstrapComponent();
109     TypeFactory tf = Fractal.getTypeFactory(boot);
110     ComponentType nsType = tf.createFcType(
111       new InterfaceType[] {
112         tf.createFcItfType(
113           NamingService.NAMING_SERVICE,
114           "org.objectweb.fractal.rmi.registry.NamingService",
115           TypeFactory.SERVER,
116           TypeFactory.MANDATORY,
117           TypeFactory.SINGLE),
118         tf.createFcItfType(
119           "attribute-controller",
120           "org.objectweb.fractal.rmi.registry.NamingServiceAttributes",
121           TypeFactory.SERVER,
122           TypeFactory.MANDATORY,
123           TypeFactory.SINGLE)});
124     GenericFactory cf = Fractal.getGenericFactory(boot);
125     Component nsId = cf.newFcInstance(
126       nsType,
127       "primitive",
128       "org.objectweb.fractal.rmi.registry.NamingServiceImpl");
129     Fractal.getLifeCycleController(nsId).startFc();
130
131     // exports the NamingService interface of the naming service component
132
Context ctxt = new JContextFactory().newContext();
133     ctxt.addElement("port", Integer JavaDoc.class, new Integer JavaDoc(port), (char)0);
134     ctxt.addElement("key", Integer JavaDoc.class, new Integer JavaDoc(-1), (char)0);
135     binder.export(nsId.getFcInterface(NamingService.NAMING_SERVICE), ctxt);
136
137     System.out.println("Fractal registry is ready.");
138   }
139
140   /**
141    * Returns a reference to the {@link NamingService} on the local host, for the
142    * default {@link #DEFAULT_PORT port}.
143    *
144    * @return a reference to the {@link NamingService} on the local host, for the
145    * default {@link #DEFAULT_PORT port}.
146    * @throws Exception if something goes wrong.
147    */

148
149   public static NamingService getRegistry ()
150     throws Exception JavaDoc
151   {
152     String JavaDoc host = InetAddress.getLocalHost().getHostName();
153     return getRegistry(host);
154   }
155
156   /**
157    * Returns a reference to the {@link NamingService} on the given host, for the
158    * default {@link #DEFAULT_PORT port}.
159    *
160    * @param host the host where the naming service is located.
161    * @return a reference to the {@link NamingService} on the given host, for the
162    * default {@link #DEFAULT_PORT port}.
163    * @throws Exception if something goes wrong.
164    */

165
166   public static NamingService getRegistry (final String JavaDoc host)
167     throws Exception JavaDoc
168   {
169     return getRegistry(host, DEFAULT_PORT);
170   }
171
172   /**
173    * Returns a reference to the {@link NamingService} on the given host and
174    * port.
175    *
176    * @param host the host where the naming service is located.
177    * @param port the port that was used to export the naming service on the
178    * given host.
179    * @return a reference to the {@link NamingService} on the given host and
180    * port.
181    * @throws Exception if something goes wrong.
182    */

183
184   public static NamingService getRegistry (
185     final String JavaDoc host,
186     final int port) throws Exception JavaDoc
187   {
188     // constructs the Fractal RMI binder
189
Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
190     Component comp =
191       (Component)f.newComponent("org.objectweb.fractal.rmi.ORB", null);
192     Fractal.getLifeCycleController(comp).startFc();
193     NamingContext binder = (NamingContext)comp.getFcInterface("context");
194     return getRegistry(host, port, binder);
195   }
196
197   /**
198    * Returns a reference to the {@link NamingService} on the given host, for the
199    * default {@link #DEFAULT_PORT port}.
200    *
201    * @param host the host where the naming service is located.
202    * @param binder the binder to be used to create the binding to the naming
203    * service interface.
204    * @return a reference to the {@link NamingService} on the given host, for the
205    * default {@link #DEFAULT_PORT port}.
206    * @throws JonathanException if something goes wrong.
207    */

208
209   public static NamingService getRegistry (
210     final String JavaDoc host,
211     final NamingContext binder) throws JonathanException
212   {
213     return getRegistry(host, DEFAULT_PORT, binder);
214   }
215
216   /**
217    * Returns a reference to the {@link NamingService} on the given host and
218    * port.
219    *
220    * @param host the host where the naming service is located.
221    * @param port the port that was used to export the naming service on the
222    * given host.
223    * @param binder the binder to be used to create the binding to the naming
224    * service interface.
225    * @return a reference to the {@link NamingService} on the given host and
226    * port.
227    * @throws JonathanException if something goes wrong.
228    */

229
230   public static NamingService getRegistry (
231     final String JavaDoc host,
232     final int port,
233     final NamingContext binder) throws JonathanException
234   {
235     short len = (short)host.length();
236     byte[] b = new byte[len + 10];
237     // port
238
b[0] = (byte)((port >>> 24) & 0xFF);
239     b[1] = (byte)((port >>> 16) & 0xFF);
240     b[2] = (byte)((port >>> 8) & 0xFF);
241     b[3] = (byte)(port & 0xFF);
242     // host
243
b[4] = (byte)((len >>> 8) & 0xFF);
244     b[5] = (byte)(len & 0xFF);
245     for (int i = 0 ; i < len; ++i) {
246       b[i + 6] = (byte)host.charAt(i);
247     }
248     // key
249
int key = -1;
250     b[len + 6] = (byte)((key >>> 24) & 0xFF);
251     b[len + 7] = (byte)((key >>> 16) & 0xFF);
252     b[len + 8] = (byte)((key >>> 8) & 0xFF);
253     b[len + 9] = (byte)(key & 0xFF);
254
255     Identifier id = binder.decode(b, 0, b.length);
256
257     Context hints = new JContextFactory().newContext();
258     hints.addElement(
259       "interface_type",
260       String JavaDoc.class,
261       "org.objectweb.fractal.rmi.registry.NamingService",
262       (char)0);
263
264     return (NamingService)id.bind(new Identifier[] {id}, hints);
265   }
266 }
267
Popular Tags