KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > server > ra > EasyBeansResourceAdapter


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EasyBeansResourceAdapter.java 1134 2006-10-04 17:06:43Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.server.ra;
27
28 import java.io.File JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.resource.ResourceException JavaDoc;
37 import javax.resource.spi.ActivationSpec JavaDoc;
38 import javax.resource.spi.BootstrapContext JavaDoc;
39 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
40 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
41 import javax.rmi.PortableRemoteObject JavaDoc;
42 import javax.transaction.xa.XAResource JavaDoc;
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 /**
56  * Defines a resource adapter which allow to embed the EJB3 server in Application Server.
57  * application server.
58  * @author Florent Benoit
59  */

60 public class EasyBeansResourceAdapter implements javax.resource.spi.ResourceAdapter JavaDoc {
61
62     /**
63      * Default XML file.
64      */

65     public static final String JavaDoc DEFAULT_XML_FILE = "org/objectweb/easybeans/server/ra/easybeans-ra.xml";
66
67     /**
68      * JOnAS ADM interface.
69      */

70     public static final String JavaDoc JONAS_ADM_ITF = "org.objectweb.jonas.adm.AdmInterface";
71
72
73     /**
74      * Logger.
75      */

76     private static JLog logger = JLogFactory.getLog(EasyBeansResourceAdapter.class);
77
78     /**
79      * Embedded instance.
80      */

81     private Embedded embedded = null;
82
83     /**
84      * Starts the embedded server.
85      * @param ctx - a bootstrap context containing references to useful
86      * facilities that could be used by a resource adapter instance.
87      * @throws ResourceAdapterInternalException indicates bootstrap failure.
88      */

89     public void start(final BootstrapContext JavaDoc ctx) throws ResourceAdapterInternalException JavaDoc {
90         // user configuration ?
91
URL JavaDoc 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 JavaDoc("Cannot create the embedded server", e);
101         }
102
103         // Update configuration
104
ServerConfig serverConfig = embedded.getServerConfig();
105         String JavaDoc jBase = System.getProperty("jonas.base");
106         if (jBase == null) {
107             throw new ResourceAdapterInternalException JavaDoc("No JONAS_BASE found, cannot continue.");
108         }
109         File JavaDoc workDir = new File JavaDoc(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         // Defines JOnAS security
119
System.setProperty(EZBSecurityCurrent.SECURITY_CURRENT_PROPERTY, JOnASSecurityCurrent.class.getName());
120
121         // start of the embedded object will be delayed until JOnAS is started.
122
// This is because Datasource could be deployed after this adapter and
123
// then datasource won't be found.
124
new EmbeddedStarter().start();
125
126     }
127
128     /**
129      * Stopp the embedded server.
130      */

131     public void stop() {
132         try {
133             embedded.stop();
134         } catch (EmbeddedException e) {
135             throw new IllegalStateException JavaDoc("Cannot stop the embedded server", e);
136         } finally {
137             embedded = null;
138         }
139     }
140
141     /**
142      * This is called during the activation of a message endpoint.
143      * @param endpointFactory - a message endpoint factory instance.
144      * @param spec an activation spec JavaBean instance.
145      * @throws ResourceException - indicates message endpoint activation
146      * rejection due to incorrect activation setup information.
147      */

148     public void endpointActivation(final MessageEndpointFactory JavaDoc endpointFactory, final ActivationSpec JavaDoc spec)
149             throws ResourceException JavaDoc {
150
151     }
152
153     /**
154      * This is called when a message endpoint is deactivated. The instances
155      * passed as arguments to this method call should be identical to those
156      * passed in for the corresponding endpointActivation call. This causes the
157      * resource adapter to stop delivering messages to the message endpoint. Any
158      * exception thrown by this method is ignored. After this method call, the
159      * endpoint is deemed inactive.
160      * @param endpointFactory a message endpoint factory instance.
161      * @param spec an activation spec JavaBean instance.
162      */

163     public void endpointDeactivation(final MessageEndpointFactory JavaDoc endpointFactory, final ActivationSpec JavaDoc spec) {
164
165     }
166
167     /**
168      * This method is called by the application server during crash recovery.
169      * @param specs an array of ActivationSpec JavaBeans each of which
170      * corresponds to an deployed endpoint application that was active
171      * prior to the system crash.
172      * @return an array of XAResource objects each of which represents a unique
173      * resource manager.
174      * @throws ResourceException generic exception if operation fails due to an
175      * error condition.
176      */

177     public XAResource JavaDoc[] getXAResources(final ActivationSpec JavaDoc[] specs) throws ResourceException JavaDoc {
178
179         return null;
180     }
181
182     /**
183      * Gets the embedded object.
184      * @return embedded server.
185      */

186     protected Embedded getEmbedded() {
187         return embedded;
188     }
189
190     /**
191      * This inner class will wait until the JOnAS server is ready and start the
192      * embedded server.
193      * @author Florent Benoit
194      */

195     public class EmbeddedStarter extends Thread JavaDoc {
196
197         /**
198          * Sleep time for this waiter thread (1 second).
199          */

200         private static final int SLEEP_TIME = 1000;
201
202         /**
203          * Logger.
204          */

205         private JLog logger = JLogFactory.getLog(EmbeddedStarter.class);
206
207         /**
208          * Wait until JOnAS is started.<br>
209          * Then, start embedded object.
210          */

211         @Override JavaDoc
212         public void run() {
213             boolean embeddedStarted = false;
214
215             while (!embeddedStarted) {
216
217                 try {
218                     Thread.sleep(SLEEP_TIME);
219                 } catch (InterruptedException JavaDoc e) {
220                     throw new RuntimeException JavaDoc("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         /**
235          * @return true if the JOnAS server is ready, else false
236          */

237         private boolean jonasIsReady() {
238             String JavaDoc jonasName = System.getProperty("jonas.name", "jonas");
239
240             // Get Adm object
241
String JavaDoc admName = jonasName + "_Adm";
242             Context JavaDoc initialContext = null;
243             try {
244                 initialContext = new InitialContext JavaDoc();
245             } catch (NamingException JavaDoc e) {
246                 logger.error("Cannot get an initial context", e);
247                 return false;
248             }
249             Object JavaDoc adm = null;
250             try {
251                 adm = initialContext.lookup(admName);
252             } catch (NamingException JavaDoc e) {
253                 logger.error("No {0} object found in the initial context", admName, e);
254                 return false;
255             }
256
257
258             // Cast object into Adm object
259
// Load Adm interface
260
Class JavaDoc admItfClass;
261             try {
262                 admItfClass = ClassUtils.forName(JONAS_ADM_ITF);
263             } catch (ClassNotFoundException JavaDoc 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             // Try to see if there is a method getServerState on this object
270
Method JavaDoc getStateMethod;
271             String JavaDoc methodName = "getServerState";
272             try {
273                 getStateMethod = adm.getClass().getMethod(methodName);
274             } catch (SecurityException JavaDoc e) {
275                 logger.error("Cannot get method name {0}", methodName, e);
276                 return false;
277             } catch (NoSuchMethodException JavaDoc 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 JavaDoc state = null;
288             try {
289                 state = (Integer JavaDoc) getStateMethod.invoke(adm);
290             } catch (IllegalArgumentException JavaDoc e) {
291                 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e);
292                 return false;
293             } catch (IllegalAccessException JavaDoc e) {
294                 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e);
295                 return false;
296             } catch (InvocationTargetException JavaDoc e) {
297                 logger.error("Cannot call method {0} on object {1}.", methodName, adm, e);
298                 return false;
299             }
300
301             // check the status
302
if (state.intValue() == 1) {
303                 logger.info("JOnAS was started, EasyBeans embedded server can be started.");
304                 // JOnAS is ready, can start embedded !
305
return true;
306             }
307
308             return false;
309
310         }
311
312     }
313
314 }
315
Popular Tags