1 25 26 package org.objectweb.easybeans.container.mdb; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 31 import javax.resource.ResourceException ; 32 import javax.resource.spi.ResourceAdapter ; 33 34 import org.objectweb.easybeans.component.api.EZBComponent; 35 import org.objectweb.easybeans.component.itf.JMSComponent; 36 import org.objectweb.easybeans.log.JLog; 37 import org.objectweb.easybeans.log.JLogFactory; 38 import org.objectweb.easybeans.server.Embedded; 39 40 45 public final class MDBResourceAdapterHelper { 46 47 50 private static JLog logger = JLogFactory.getLog(MDBResourceAdapterHelper.class); 51 52 55 private MDBResourceAdapterHelper() { 56 57 } 58 59 69 public static ResourceAdapter getResourceAdapter(final String jndiName, final Embedded embedded) 70 throws ResourceException { 71 EZBComponent component = embedded.getComponent("org.objectweb.easybeans.component.joram.JoramComponent"); 73 if (component != null) { 74 if (component instanceof JMSComponent) { 76 return ((JMSComponent) component).getResourceAdapter(); 77 } 78 throw new IllegalArgumentException ("The 'jms' component doesn't implement JMSComponent interface."); 79 } 80 81 String jRoot = System.getProperty("install.root"); 83 if (jRoot != null) { 84 logger.debug("JOnAS detected, trying with JOnAS Resource Adapter"); 85 return getJOnASResourceAdapter(jndiName); 86 } 87 88 throw new ResourceException ("MDB is used but no JMS resource service was started." 89 + "Also, no resource service found for the server."); 90 91 } 92 93 102 public static ResourceAdapter getJOnASResourceAdapter(final String jndiName) throws ResourceException { 103 Class rarClass = null; 104 try { 105 rarClass = Thread.currentThread().getContextClassLoader().loadClass("org.objectweb.jonas.resource.Rar"); 106 } catch (ClassNotFoundException e) { 107 throw new ResourceException ("Cannot find the JOnAS resource adapter class", e); 108 } 109 110 Method getRarMethod = null; 112 try { 113 getRarMethod = rarClass.getMethod("getRar", new Class [] {String .class}); 114 } catch (SecurityException e) { 115 throw new ResourceException ("Cannot get the getRar method on the class '" + rarClass + "'.", e); 116 } catch (NoSuchMethodException e) { 117 throw new ResourceException ("Cannot get the getRar method on the class '" + rarClass + "'.", e); 118 } 119 120 Object rarObject = null; 122 try { 123 rarObject = getRarMethod.invoke(null, jndiName); 124 } catch (IllegalArgumentException e) { 125 throw new ResourceException ("Cannot invoke method with jndiName '" + jndiName + "'.", e); 126 } catch (IllegalAccessException e) { 127 throw new ResourceException ("Cannot invoke method with jndiName '" + jndiName + "'.", e); 128 } catch (InvocationTargetException e) { 129 throw new ResourceException ("Cannot invoke method with jndiName '" + jndiName + "'.", e 130 .getTargetException()); 131 } 132 133 Method getResourceAdapterMethod = null; 135 try { 136 getResourceAdapterMethod = rarObject.getClass().getMethod("getResourceAdapter", new Class [] {}); 137 } catch (SecurityException e) { 138 throw new ResourceException ("Cannot get the getResourceAdapter method on the class '" 139 + rarObject.getClass() + "'.", e); 140 } catch (NoSuchMethodException e) { 141 throw new ResourceException ("Cannot get the getResourceAdapter method on the class '" 142 + rarObject.getClass() + "'.", e); 143 } 144 145 Object resourceAdapterObj = null; 147 try { 148 resourceAdapterObj = getResourceAdapterMethod.invoke(rarObject, new Object [] {}); 149 } catch (IllegalArgumentException e) { 150 throw new ResourceException ("Cannot invoke method getResourceAdapter on the rar object.", e); 151 } catch (IllegalAccessException e) { 152 throw new ResourceException ("Cannot invoke method getResourceAdapter on the rar object.", e); 153 } catch (InvocationTargetException e) { 154 throw new ResourceException ("Cannot invoke method getResourceAdapter on the rar object.", e); 155 } 156 157 ResourceAdapter resourceAdapter = null; 159 if (resourceAdapterObj instanceof ResourceAdapter ) { 160 resourceAdapter = (ResourceAdapter ) resourceAdapterObj; 161 } else { 162 throw new ResourceException ("Object found is not an instance of ResourceAdapter"); 163 } 164 return resourceAdapter; 165 166 } 167 } 168 | Popular Tags |