1 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 ; 37 import javax.resource.spi.ResourceAdapterInternalException ; 38 import java.rmi.registry.LocateRegistry ; 39 import java.rmi.registry.Registry ; 40 import java.util.Iterator ; 41 import java.util.LinkedList ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 52 53 public class RmiRegistry extends AbstractResourceAdapter 54 { 55 static protected final Logger log = Log.open(RmiRegistry.class); 56 static final L10N L = new L10N(RmiRegistry.class); 57 58 private String _server = "localhost"; 59 private int _port = 1099; 60 61 private LinkedList <RmiService> _services = new LinkedList <RmiService>(); 62 63 private String _namePrefix; 64 65 71 public void setServer(String server) 72 { 73 _server = server; 74 } 75 76 public String getServer() 77 { 78 return _server; 79 } 80 81 84 public void setPort(int port) 85 { 86 _port = port; 87 } 88 89 92 public int getPort() 93 { 94 return _port; 95 } 96 97 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 ctx) 116 throws ResourceAdapterInternalException 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 for (Iterator <RmiService> i = _services.iterator(); i.hasNext(); ) { 126 RmiService s = i.next(); 127 128 s.start(); 129 } 130 } 131 132 136 String makeFullName(String serviceName) 137 { 138 return _namePrefix + serviceName; 139 } 140 141 145 private void startRegistry() 146 throws ResourceAdapterInternalException 147 { 148 152 Thread thread = Thread.currentThread(); 153 ClassLoader oldLoader = thread.getContextClassLoader(); 154 155 try { 156 thread.setContextClassLoader(ClassLoader.getSystemClassLoader()); 157 158 try { 159 Registry reg = LocateRegistry.getRegistry(_port); 160 reg.list(); if (log.isLoggable(Level.CONFIG)) 162 log.config(L.l("found RMI Registry on port `{0}'", 163 _port)); 164 } 165 catch (Exception e) { 166 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 ex) { 173 throw new ResourceAdapterInternalException (ex); 174 } finally { 175 thread.setContextClassLoader(oldLoader); 176 } 177 } 178 179 182 public void stop() 183 throws ResourceAdapterInternalException 184 { 185 for (Iterator <RmiService> i = _services.iterator(); i.hasNext(); ) { 187 RmiService s = i.next(); 188 s.stop(); 189 } 190 } 191 } 192 | Popular Tags |