1 25 26 package org.objectweb.easybeans.server.ra; 27 28 import java.io.File ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.net.URL ; 32 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import javax.resource.ResourceException ; 37 import javax.resource.spi.ActivationSpec ; 38 import javax.resource.spi.BootstrapContext ; 39 import javax.resource.spi.ResourceAdapterInternalException ; 40 import javax.resource.spi.endpoint.MessageEndpointFactory ; 41 import javax.rmi.PortableRemoteObject ; 42 import javax.transaction.xa.XAResource ; 43 44 import org.objectweb.easybeans.log.JLog; 45 import org.objectweb.easybeans.log.JLogFactory; 46 import org.objectweb.easybeans.security.api.EZBSecurityCurrent; 47 import org.objectweb.easybeans.security.propagation.jonas.JOnASSecurityCurrent; 48 import org.objectweb.easybeans.server.EasyBeans; 49 import org.objectweb.easybeans.server.Embedded; 50 import org.objectweb.easybeans.server.EmbeddedConfigurator; 51 import org.objectweb.easybeans.server.EmbeddedException; 52 import org.objectweb.easybeans.server.ServerConfig; 53 import org.objectweb.easybeans.util.loader.ClassUtils; 54 55 60 public class EasyBeansResourceAdapter implements javax.resource.spi.ResourceAdapter { 61 62 65 public static final String DEFAULT_XML_FILE = "org/objectweb/easybeans/server/ra/easybeans-ra.xml"; 66 67 70 public static final String JONAS_ADM_ITF = "org.objectweb.jonas.adm.AdmInterface"; 71 72 73 76 private static JLog logger = JLogFactory.getLog(EasyBeansResourceAdapter.class); 77 78 81 private Embedded embedded = null; 82 83 89 public void start(final BootstrapContext ctx) throws ResourceAdapterInternalException { 90 URL xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(EasyBeans.USER_XML_FILE); 92 93 if (xmlConfigurationURL == null) { 94 logger.warn("No {0} resource found in classpath, use default settings", EasyBeans.USER_XML_FILE); 95 xmlConfigurationURL = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_XML_FILE); 96 } 97 try { 98 embedded = EmbeddedConfigurator.create(xmlConfigurationURL); 99 } catch (EmbeddedException e) { 100 throw new IllegalStateException ("Cannot create the embedded server", e); 101 } 102 103 ServerConfig serverConfig = embedded.getServerConfig(); 105 String jBase = System.getProperty("jonas.base"); 106 if (jBase == null) { 107 throw new ResourceAdapterInternalException ("No JONAS_BASE found, cannot continue."); 108 } 109 File workDir = new File (jBase + File.separator + "ejb3s"); 110 workDir.mkdir(); 111 serverConfig.setEjb3Path(workDir.getAbsolutePath()); 112 serverConfig.setShouldWait(false); 113 serverConfig.setUseMBeans(false); 114 serverConfig.setUseNaming(false); 115 serverConfig.setInitJACC(false); 116 117 118 System.setProperty(EZBSecurityCurrent.SECURITY_CURRENT_PROPERTY, JOnASSecurityCurrent.class.getName()); 120 121 new EmbeddedStarter().start(); 125 126 } 127 128 131 public void stop() { 132 try { 133 embedded.stop(); 134 } catch (EmbeddedException e) { 135 throw new IllegalStateException ("Cannot stop the embedded server", e); 136 } finally { 137 embedded = null; 138 } 139 } 140 141 148 public void endpointActivation(final MessageEndpointFactory endpointFactory, final ActivationSpec spec) 149 throws ResourceException { 150 151 } 152 153 163 public void endpointDeactivation(final MessageEndpointFactory endpointFactory, final ActivationSpec spec) { 164 165 } 166 167 177 public XAResource [] getXAResources(final ActivationSpec [] specs) throws ResourceException { 178 179 return null; 180 } 181 182 186 protected Embedded getEmbedded() { 187 return embedded; 188 } 189 190 195 public class EmbeddedStarter extends Thread { 196 197 200 private static final int SLEEP_TIME = 1000; 201 202 205 private JLog logger = JLogFactory.getLog(EmbeddedStarter.class); 206 207 211 @Override 212 public void run() { 213 boolean embeddedStarted = false; 214 215 while (!embeddedStarted) { 216 217 try { 218 Thread.sleep(SLEEP_TIME); 219 } catch (InterruptedException e) { 220 throw new RuntimeException ("Thread fail to sleep"); 221 } 222 223 if (jonasIsReady()) { 224 try { 225 getEmbedded().start(); 226 } catch (EmbeddedException e) { 227 logger.error("Cannot start the embedded server", e); 228 } 229 embeddedStarted = true; 230 } 231 } 232 } 233 234 237 private boolean jonasIsReady() { 238 String jonasName = System.getProperty("jonas.name", "jonas"); 239 240 String admName = jonasName + "_Adm"; 242 Context initialContext = null; 243 try { 244 initialContext = new InitialContext (); 245 } catch (NamingException e) { 246 logger.error("Cannot get an initial context", e); 247 return false; 248 } 249 Object adm = null; 250 try { 251 adm = initialContext.lookup(admName); 252 } catch (NamingException e) { 253 logger.error("No {0} object found in the initial context", admName, e); 254 return false; 255 } 256 257 258 Class admItfClass; 261 try { 262 admItfClass = ClassUtils.forName(JONAS_ADM_ITF); 263 } catch (ClassNotFoundException e) { 264 logger.error("Cannot load the class ''{0}''", JONAS_ADM_ITF, e); 265 return false; 266 } 267 adm = PortableRemoteObject.narrow(adm, admItfClass); 268 269 Method getStateMethod; 271 String methodName = "getServerState"; 272 try { 273 getStateMethod = adm.getClass().getMethod(methodName); 274 } catch (SecurityException e) { 275 logger.error("Cannot get method name {0}", methodName, e); 276 return false; 277 } catch (NoSuchMethodException e) { 278 logger.error("Cannot get method name {0}", methodName, e); 279 return false; 280 } 281 282 if (getStateMethod == null) { 283 logger.warn("No method getStateMethod() found on object {0}", adm); 284 return false; 285 } 286 287 Integer state = null; 288 try { 289 state = (Integer ) getStateMethod.invoke(adm); 290 } catch (IllegalArgumentException e) { 291 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e); 292 return false; 293 } catch (IllegalAccessException e) { 294 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e); 295 return false; 296 } catch (InvocationTargetException e) { 297 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e); 298 return false; 299 } 300 301 if (state.intValue() == 1) { 303 logger.info("JOnAS was started, EasyBeans embedded server can be started."); 304 return true; 306 } 307 308 return false; 309 310 } 311 312 } 313 314 } 315 | Popular Tags |