KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > joram > JoramComponent


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:JoramComponent.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.joram;
27
28 import java.net.ConnectException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.jms.ConnectionFactory JavaDoc;
34 import javax.jms.QueueConnectionFactory JavaDoc;
35 import javax.jms.TopicConnectionFactory JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.resource.ResourceException JavaDoc;
39 import javax.resource.spi.ActivationSpec JavaDoc;
40 import javax.resource.spi.BootstrapContext JavaDoc;
41 import javax.resource.spi.ResourceAdapter JavaDoc;
42 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
43 import javax.resource.spi.XATerminator JavaDoc;
44 import javax.transaction.TransactionManager JavaDoc;
45 import javax.transaction.xa.XAException JavaDoc;
46
47 import org.objectweb.easybeans.component.api.EZBComponentException;
48 import org.objectweb.easybeans.component.itf.JMSComponent;
49 import org.objectweb.easybeans.jca.workmanager.ResourceWorkManager;
50 import org.objectweb.easybeans.log.JLog;
51 import org.objectweb.easybeans.log.JLogFactory;
52 import org.objectweb.easybeans.transaction.JTransactionManager;
53 import org.objectweb.joram.client.connector.ActivationSpecImpl;
54 import org.objectweb.joram.client.connector.JoramAdapter;
55 import org.objectweb.joram.client.jms.ConnectionMetaData;
56 import org.objectweb.joram.client.jms.admin.AdminException;
57 import org.objectweb.joram.client.jms.admin.AdminModule;
58 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory;
59 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory;
60 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory;
61 import org.objectweb.joram.mom.proxies.ConnectionManager;
62 import org.objectweb.joram.mom.proxies.tcp.TcpProxyService;
63 import org.objectweb.jotm.Current;
64 import org.objectweb.util.monolog.Monolog;
65
66 import fr.dyade.aaa.agent.AgentServer;
67 import fr.dyade.aaa.agent.ServerConfigHelper;
68 import fr.dyade.aaa.util.NullTransaction;
69
70 /**
71  * Class that start/stop a simple collocated JORAM server.
72  * It also creates some initial Topics/Queues.
73  * @author Florent Benoit
74  */

75 public class JoramComponent implements JMSComponent {
76
77     /**
78      * Logger.
79      */

80     private static JLog logger = JLogFactory.getLog(JoramComponent.class);
81
82
83     /**
84      * Queue connection factory (for external components).
85      */

86     private static final String JavaDoc QUEUE_CONN_FACT_NAME="JQCF";
87
88     /**
89      * Topic connection factory (for external components).
90      */

91     private static final String JavaDoc TOPIC_CONN_FACT_NAME="JTCF";
92
93     /**
94      * Connection factory (for external components).
95      */

96     private static final String JavaDoc CONN_FACT_NAME="JCF";
97
98     /**
99      * Default port number.
100      */

101     private static final int DEFAULT_PORT_NUMBER = 16010;
102
103     /**
104      * Port number.
105      */

106     private int port = DEFAULT_PORT_NUMBER;
107
108     /**
109      * Default hostname.
110      */

111     private static final String JavaDoc DEFAULT_HOST_NAME = "localhost";
112
113     /**
114      * Host.
115      */

116     private String JavaDoc host = DEFAULT_HOST_NAME;
117
118     /**
119      * ID of the JORAM server.
120      */

121     private static final short ID = 0;
122
123     /**
124      * Transaction property (Set to remove persistence settings : transient). It
125      * avoids the creation of a directory.
126      */

127     private static final String JavaDoc TRANSACTION_PROPERTY = "Transaction";
128
129     /**
130      * Default name of the persistence directory (won't be used).
131      */

132     private static final String JavaDoc DEFAULT_PERSISTENCE_DIRECTORY = "joram-persistence-s" + ID;
133
134     /**
135      * Server is started ?
136      */

137     private boolean started = false;
138
139     /**
140      * Minimum threads.
141      */

142     private static final int MIN_THREADS = 1;
143
144     /**
145      * Maximum threads.
146      */

147     private static final int MAX_THREADS = 2;
148
149     /**
150      * Thread timeout.
151      */

152     private static final int THREAD_TIMEOUT = 20;
153
154     /**
155      * Instance of the resource adapter.
156      */

157     private JoramAdapter joramAdapter = null;
158
159     /**
160      * List of topics to create.
161      */

162     private List JavaDoc<String JavaDoc> topics = null;
163
164     /**
165      * List of queues to create.
166      */

167     private List JavaDoc<String JavaDoc> queues = null;
168
169     /**
170      * WorkManager used by this component.
171      */

172     private ResourceWorkManager workManager = null;
173
174     /**
175      * Initial Context.
176      */

177     private InitialContext JavaDoc ictx = null;
178
179     /**
180      * Default constructor.
181      */

182     public JoramComponent() {
183         topics = new ArrayList JavaDoc<String JavaDoc>();
184         queues = new ArrayList JavaDoc<String JavaDoc>();
185     }
186
187
188     /**
189      * Init method.<br/>
190      * This method is called before the start method.
191      * @throws EZBComponentException if the initialization has failed.
192      */

193     public void init() throws EZBComponentException {
194
195     }
196
197     /**
198      * Starts a Joram Server without persistence.
199      * @throws EZBComponentException if start fails
200      */

201     public void start() throws EZBComponentException {
202
203         // Initialize Monolog
204
Properties JavaDoc properties = new Properties JavaDoc();
205         properties.put(Monolog.MONOLOG_CLASS_NAME, "org.objectweb.util.monolog.wrapper.javaLog.LoggerFactory");
206         properties.put("logger.org.objectweb.joram.level", "ERROR");
207         properties.put("logger.com.scalagent.level", "ERROR");
208         properties.put("logger.fr.dyade.aaa.level", "ERROR");
209         Monolog.init(properties);
210
211
212         // Build initial context
213
try {
214             ictx = new InitialContext JavaDoc();
215         } catch (NamingException JavaDoc e) {
216             throw new EZBComponentException("Cannot create an initial context.", e);
217         }
218
219
220         TransactionManager JavaDoc transactionManager = JTransactionManager.getTransactionManager();
221         workManager = new ResourceWorkManager(transactionManager, MIN_THREADS, MAX_THREADS, THREAD_TIMEOUT);
222
223         // Create BootstrapContext
224
XATerminator JavaDoc xaTerminator = null;
225         try {
226             xaTerminator = ((Current) transactionManager).getXATerminator();
227         } catch (XAException JavaDoc e) {
228             throw new EZBComponentException("Cannot get the XA terminator", e);
229         }
230
231         BootstrapContext JavaDoc bootstrapContext = new JoramBootstrapContext(workManager, xaTerminator);
232
233         // Create JORAM adapter
234
joramAdapter = new JoramAdapter();
235
236         // Set host/port
237
joramAdapter.setHostName(host);
238         joramAdapter.setServerPort(new Integer JavaDoc(port));
239
240         // configure it (in fact the server will be collocated but started by this service and not by the resource adapter).
241
joramAdapter.setCollocatedServer(Boolean.FALSE);
242
243         // Set properties (transient)
244
System.setProperty(TRANSACTION_PROPERTY, NullTransaction.class.getName());
245
246         // Init
247
try {
248             AgentServer.init(ID, DEFAULT_PERSISTENCE_DIRECTORY, null);
249         } catch (Exception JavaDoc e) {
250             throw new EZBComponentException("Cannot initialize a new collocated Joram server", e);
251         }
252
253
254         // start Agent
255
try {
256            AgentServer.start();
257         } catch (Exception JavaDoc e) {
258             throw new EZBComponentException("Cannot start collocated Joram server", e);
259         }
260
261         // Add services
262
try {
263             new ServerConfigHelper(false).addService(ID, ConnectionManager.class.getName(), "root root");
264         } catch (Exception JavaDoc e) {
265             throw new EZBComponentException("Cannot add connection manager service", e);
266         }
267         // To enable TCP listener
268
try {
269             new ServerConfigHelper(false).addService(ID, TcpProxyService.class.getName(), String.valueOf(port));
270         } catch (Exception JavaDoc e) {
271             throw new EZBComponentException("Cannot add TcpProxy service", e);
272         }
273
274
275
276         // connect to the collocated server
277
try {
278             connectToCollocated();
279         } catch (JoramException e) {
280             throw new EZBComponentException("Cannot connect to the collocated server", e);
281         }
282
283
284         // Create anonymous user
285
try {
286             joramAdapter.createUser("anonymous", "anonymous");
287         } catch (AdminException e) {
288             throw new EZBComponentException("Cannot create anonymous user", e);
289
290         }
291
292
293         // start resource adapter
294
try {
295             joramAdapter.start(bootstrapContext);
296         } catch (ResourceAdapterInternalException JavaDoc e) {
297             throw new EZBComponentException("Cannot start the resource adapter of JORAM", e);
298         }
299
300         // create factories
301
try {
302             createConnectionFactories();
303         } catch (JoramException e) {
304             throw new EZBComponentException("Cannot create connection factories", e);
305         }
306
307
308         // initial topics and queues
309
try {
310             createInitialTopics();
311         } catch (JoramException e) {
312             throw new EZBComponentException("Cannot create initial topics", e);
313         }
314
315         try {
316             createInitialQueues();
317         } catch (JoramException e) {
318             throw new EZBComponentException("Cannot create initial queues", e);
319         }
320
321         // Create and bind ActivationSpec
322
ActivationSpec JavaDoc activationSpec = new ActivationSpecImpl();
323         try {
324             activationSpec.setResourceAdapter(joramAdapter);
325         } catch (ResourceException JavaDoc e) {
326             throw new EZBComponentException("Cannot set resource adapter on activation spec object", e);
327         }
328         try {
329             ictx.rebind("joramActivationSpec", activationSpec);
330         } catch (NamingException JavaDoc e) {
331             throw new EZBComponentException("Cannot bind activation spec object", e);
332         }
333
334         logger.info("Joram version ''{0}'' started on {1}:{2}.", ConnectionMetaData.providerVersion, host, String.valueOf(port));
335         started = true;
336     }
337
338     /**
339      * Stops the JORAM server (if started).
340      * @throws EZBComponentException if stop is failing
341      */

342     public void stop() throws EZBComponentException {
343         if (!started) {
344             throw new IllegalStateException JavaDoc("Cannot stop a server as it was not started");
345         }
346
347         // Stop adapter
348
joramAdapter.stop();
349
350         // disconnect
351
disconnectFromCollocated();
352
353         // stop
354
AgentServer.stop();
355
356         // Stop threads of the workmanager
357
workManager.stopThreads();
358     }
359
360     /**
361      * Connect to the collocated server to performg administration tasks. It
362      * needs to be called before any admin task.
363      * @throws JoramException if the connection to the collocated server fails
364      */

365     private void connectToCollocated() throws JoramException {
366         try {
367             AdminModule.collocatedConnect("root", "root");
368         } catch (ConnectException JavaDoc e) {
369             throw new JoramException("Cannot connect to the collocated server for the administration", e);
370         } catch (AdminException e) {
371             throw new JoramException("Cannot connect to the collocated server for the administration", e);
372         }
373     }
374
375     /**
376      * Disconnect from the collocated server. It needs to be called when
377      * stopping to use admin task.
378      * @throws AdminException
379      * @throws ConnectException
380      */

381     private void disconnectFromCollocated() {
382         AdminModule.disconnect();
383     }
384
385     /**
386      * Create connection factories.
387      * @throws JoramException if factories are not created.
388      */

389     private void createConnectionFactories() throws JoramException {
390         // managed
391
joramAdapter.createCF("CF");
392         joramAdapter.createQCF("QCF");
393         joramAdapter.createTCF("TCF");
394
395         // Create connection factories that will be used by a pure JMS Client
396
ConnectionFactory JavaDoc jcf = null;
397         TopicConnectionFactory JavaDoc jtcf = null;
398         QueueConnectionFactory JavaDoc jqcf = null;
399
400         String JavaDoc name = CONN_FACT_NAME;
401         try {
402             jcf = TcpConnectionFactory.create(host, port);
403             ictx.rebind(name, jcf);
404         } catch (NamingException JavaDoc e) {
405             throw new JoramException("Cannot create a factory with the name '" + name + "'.", e);
406         }
407
408         name = QUEUE_CONN_FACT_NAME;
409         try {
410             jqcf = QueueTcpConnectionFactory.create(host, port);
411             ictx.rebind(name, jqcf);
412         } catch (NamingException JavaDoc e) {
413             throw new JoramException("Cannot create a factory with the name '" + name + "'.", e);
414         }
415
416         name = TOPIC_CONN_FACT_NAME;
417         try {
418             jtcf = TopicTcpConnectionFactory.create(host, port);
419             ictx.rebind(name, jtcf);
420         } catch (NamingException JavaDoc e) {
421             throw new JoramException("Cannot create a factory with the name '" + name + "'.", e);
422         }
423     }
424
425     /**
426      * Creates a topic with a given name.
427      * @param name the topic's name.
428      * @throws JoramException if the topic can't be created
429      */

430     private void createTopic(final String JavaDoc name) throws JoramException {
431         try {
432             joramAdapter.createTopic(name);
433         } catch (AdminException e) {
434             throw new JoramException("Cannot create a topic with the name '" + name + "'.", e);
435         }
436     }
437
438     /**
439      * Creates a queue with a given name.
440      * @param name the topic's name.
441      * @throws JoramException if the queue can't be created
442      */

443     private void createQueue(final String JavaDoc name) throws JoramException {
444         try {
445             joramAdapter.createQueue(name);
446         } catch (AdminException e) {
447             throw new JoramException("Cannot create a queue with the name '" + name + "'.", e);
448         }
449     }
450
451     /**
452      * Create the list of the defined topics.
453      * @throws JoramException if topics can't be created
454      */

455     private void createInitialTopics() throws JoramException {
456         for (String JavaDoc topic : topics) {
457             createTopic(topic);
458         }
459     }
460
461     /**
462      * Create the list of the defined queues.
463      * @throws JoramException if queues can't be created
464      */

465     private void createInitialQueues() throws JoramException {
466         for (String JavaDoc queue : queues) {
467             createQueue(queue);
468         }
469     }
470
471     /**
472      * Sets the initial queues of the Joram server.
473      * @param queues the list of the name of the queues.
474      */

475     public void setQueues(final List JavaDoc<String JavaDoc> queues) {
476         this.queues = queues;
477     }
478
479     /**
480      * Sets the initial topics of the Joram server.
481      * @param topics the list of the name of the topics.
482      */

483     public void setTopics(final List JavaDoc<String JavaDoc> topics) {
484         this.topics = topics;
485     }
486
487
488     /**
489      * Gets the resource adapter instance.
490      * @return resource adapter instance.
491      */

492     public ResourceAdapter JavaDoc getResourceAdapter() {
493         return joramAdapter;
494     }
495
496
497     /**
498      * Sets the hostname to use.
499      * @param host the host to use.
500      */

501     public void setHostname(final String JavaDoc host) {
502         this.host = host;
503     }
504
505
506     /**
507      * Sets the port number to use.
508      * @param port the given port.
509      */

510     public void setPort(final int port) {
511         this.port = port;
512     }
513
514
515 }
516
Popular Tags