1 28 29 package com.caucho.resources.rmi; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.resource.spi.ResourceAdapterInternalException ; 36 import java.rmi.Naming ; 37 import java.rmi.Remote ; 38 import java.rmi.server.UnicastRemoteObject ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class RmiService { 46 static protected final Logger log = Log.open(RmiService.class); 47 static final L10N L = new L10N(RmiService.class); 48 49 private RmiRegistry _registry; 50 51 private String _serviceName; 52 private String _serviceClass; 53 private Class _serviceClassClass; 54 55 private String _boundName; 56 private Remote _boundObject; 57 58 64 public void setServiceName(String name) 65 66 { 67 _serviceName = name; 68 } 69 70 public String getServiceName() 71 { 72 return _serviceName; 73 } 74 75 78 public void setServiceClass(String serviceClass) 79 { 80 _serviceClass = serviceClass; 81 } 82 83 86 public String getServiceClass() 87 { 88 return _serviceClass; 89 } 90 91 public void setParent(Object parent) 92 { 93 if (parent instanceof RmiRegistry) 94 _registry = (RmiRegistry) parent; 95 } 96 97 public void init() 98 throws ConfigException 99 { 100 if (_registry == null) 101 throw new ConfigException(L.l("{0} must be used as a child of {1}","RmiService","RmiRegistry")); 102 103 if (_serviceClass == null) 104 throw new ConfigException(L.l("`{0}' is required","service-class")); 105 if (_serviceName == null) 106 throw new ConfigException(L.l("`{0}' is required","service-name")); 107 108 try { 109 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 110 111 if (loader != null) 112 _serviceClassClass = Class.forName(_serviceClass, false, loader); 113 else 114 _serviceClassClass = Class.forName(_serviceClass); 115 } catch (ClassNotFoundException ex) { 116 throw new ConfigException(L.l("no class found with name `{0}'",_serviceClass)); 117 } 118 } 119 120 121 124 public void start() 125 throws ResourceAdapterInternalException 126 { 127 String fullName = _registry.makeFullName(_serviceName); 128 129 if (_boundName != null) 130 throw new ResourceAdapterInternalException (L.l("cannot bind rmi service with name `{0}', already bound with name `{1}'", fullName, _boundName)); 131 132 try { 133 _boundObject = (Remote ) _serviceClassClass.newInstance(); 134 135 if (log.isLoggable(Level.FINE)) 136 log.fine(L.l("binding rmi name `{0}' to object `{1}'",fullName,_boundObject.getClass().getName())); 137 138 Naming.rebind(fullName, _boundObject); 139 _boundName = fullName; 140 } 141 catch (Exception ex) { 142 throw new ResourceAdapterInternalException (L.l("error binding rmi service with name `{0}'", fullName),ex); 143 } 144 145 } 146 147 150 void stop() 151 { 152 if (_boundName != null) { 153 try { 154 if (log.isLoggable(Level.FINE)) 155 log.fine(L.l("unbinding rmi name `{0}'", _boundName)); 156 157 Naming.unbind(_boundName); 158 159 } catch (Exception ex) { 160 if (log.isLoggable(Level.INFO)) 161 log.log(Level.INFO,L.l("error unbinding rmi name `{0}'", _boundName),ex); 162 } 163 _boundName = null; 164 } 165 166 if (_boundObject != null) { 167 try { 168 if (log.isLoggable(Level.FINEST)) 169 log.finest(L.l("unexporting rmi object `{0}'", _boundObject.getClass().getName())); 170 171 UnicastRemoteObject.unexportObject(_boundObject, true); 172 } catch (Exception ex) { 173 if (log.isLoggable(Level.FINE)) 174 log.log(Level.FINE,L.l("error unexporting rmi object `{0}'", _boundObject.getClass().getName()),ex); 175 } 176 _boundObject = null; 177 } 178 } 179 } 180 181 | Popular Tags |