1 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 ; 44 45 48 49 public class Registry { 50 51 54 55 public final static int DEFAULT_PORT = 1234; 56 57 60 61 private Registry () { 62 } 63 64 71 72 public static void main (final String [] args) 73 throws Exception 74 { 75 int port = DEFAULT_PORT; 77 if (args.length == 1) { 78 try { 79 port = Integer.parseInt(args[0]); 80 } catch (NumberFormatException nfe) { 81 } 82 } 83 createRegistry(port); 84 } 85 86 93 94 public static void createRegistry (final int port) 95 throws Exception 96 { 97 System.setSecurityManager(new SecurityManager ()); 99 100 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 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 Context ctxt = new JContextFactory().newContext(); 133 ctxt.addElement("port", Integer .class, new Integer (port), (char)0); 134 ctxt.addElement("key", Integer .class, new Integer (-1), (char)0); 135 binder.export(nsId.getFcInterface(NamingService.NAMING_SERVICE), ctxt); 136 137 System.out.println("Fractal registry is ready."); 138 } 139 140 148 149 public static NamingService getRegistry () 150 throws Exception 151 { 152 String host = InetAddress.getLocalHost().getHostName(); 153 return getRegistry(host); 154 } 155 156 165 166 public static NamingService getRegistry (final String host) 167 throws Exception 168 { 169 return getRegistry(host, DEFAULT_PORT); 170 } 171 172 183 184 public static NamingService getRegistry ( 185 final String host, 186 final int port) throws Exception 187 { 188 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 208 209 public static NamingService getRegistry ( 210 final String host, 211 final NamingContext binder) throws JonathanException 212 { 213 return getRegistry(host, DEFAULT_PORT, binder); 214 } 215 216 229 230 public static NamingService getRegistry ( 231 final String 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 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 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 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 .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 |