KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jms > JmsServiceImpl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@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  * $Id: JmsServiceImpl.java,v 1.29 2005/04/28 08:43:26 benoitf Exp $
22  * --------------------------------------------------------------------------
23  */

24
25
26 package org.objectweb.jonas.jms;
27
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import javax.jms.Queue JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.objectweb.jonas.common.Log;
40 import org.objectweb.jonas.jmx.JmxService;
41 import org.objectweb.jonas.jmx.JonasObjectName;
42 import org.objectweb.jonas.jtm.TransactionService;
43 import org.objectweb.jonas.management.ReconfiguredProp;
44 import org.objectweb.jonas.service.AbsServiceImpl;
45 import org.objectweb.jonas.service.ServiceException;
46 import org.objectweb.jonas.service.ServiceManager;
47 import org.objectweb.jonas_jms.JmsJmxManagement;
48 import org.objectweb.jonas_jms.JmsManagerImpl;
49 import org.objectweb.jonas_jms.api.JmsManager;
50 import org.objectweb.util.monolog.api.BasicLevel;
51 import org.objectweb.util.monolog.api.Logger;
52
53 /**
54  * JMS Service implementation.
55  * @author Philippe Coq
56  * Contributor(s):
57  * Adriana Danes: highlight configuration properties
58  * 03.05.27 - Add support for monitoring of JMS destinations
59  */

60
61 public class JmsServiceImpl extends AbsServiceImpl implements JmsService, JmsServiceImplMBean {
62
63     // loggers
64
static private Logger serverlog = null;
65     static private Logger loaderlog = null;
66
67     // Service name as used to label configuration properties
68
public static final String JavaDoc SERVICE_NAME = "jms";
69
70     // Configuration information used when starting the JMS service
71
String JavaDoc momProviderClassName;
72     Vector JavaDoc queueNames = new Vector JavaDoc();
73     Vector JavaDoc topicNames = new Vector JavaDoc();
74     boolean isMomLocal;
75     String JavaDoc url;
76     String JavaDoc jmsClass;
77     TransactionService transactionService = null;
78
79     // JMS manager
80
JmsManager jonasjms = null;
81     JmsJmxManagement jmsjmx = null;
82
83     // JMS service configuration properties
84
public static final String JavaDoc COLLOCATED = "jonas.service.jms.collocated";
85     public static final String JavaDoc MOM = "jonas.service.jms.mom";
86     public static final String JavaDoc TOPICS = "jonas.service.jms.topics";
87     public static final String JavaDoc QUEUES = "jonas.service.jms.queues";
88     public static final String JavaDoc DESTINATIONS = "jonas.service.jms.dest";
89     public static final String JavaDoc URL = "jonas.service.jms.url";
90     public static final String JavaDoc CLASS = "jonas.service.jms.class";
91
92     // Value used as sequence number by reconfiguration notifications
93
long sequenceNumber = 0;
94
95     // -------------------------------------------------------------------
96
// Service Implementation
97
// -------------------------------------------------------------------
98

99     /**
100      * Init the Service.
101      * Configuration information is passed thru a Context object.
102      */

103     public void doInit(Context JavaDoc ctx) throws ServiceException {
104
105     // get loggers
106
serverlog = Log.getLogger(Log.JONAS_SERVER_PREFIX);
107     loaderlog = Log.getLogger(Log.JONAS_LOADER_PREFIX);
108     super.initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX));
109
110     // Read the configuration information
111
try {
112         momProviderClassName = (String JavaDoc) ctx.lookup(MOM);
113     } catch (NamingException JavaDoc e) {
114         serverlog.log(BasicLevel.ERROR, "JMS Service Cannot read configuration "+e);
115         throw new ServiceException("JMS Service Cannot read configuration ", e);
116     }
117
118     String JavaDoc ql = null;
119     try {
120         ql = (String JavaDoc) ctx.lookup(QUEUES);
121     } catch (NamingException JavaDoc e) {
122         // No queue predefined
123
}
124     if (ql != null) {
125         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ql, ",");
126         while (st.hasMoreTokens()) {
127         queueNames.add(st.nextToken().trim());
128         }
129     }
130
131     String JavaDoc tl = null;
132     try {
133         tl = (String JavaDoc) ctx.lookup(TOPICS);
134     } catch (NamingException JavaDoc e) {
135         // No topic predefined
136
}
137     if (tl != null) {
138         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(tl, ",");
139         while (st.hasMoreTokens()) {
140         topicNames.add(st.nextToken().trim());
141         }
142     }
143
144     String JavaDoc local = "true";
145     try {
146         local = (String JavaDoc) ctx.lookup(COLLOCATED);
147     } catch (NamingException JavaDoc e) {
148         // default value is collocated
149
}
150     isMomLocal = local.equalsIgnoreCase("true");
151
152     url = "";
153     try {
154         url = (String JavaDoc) ctx.lookup(URL);
155     } catch (NamingException JavaDoc e) {
156         // default value is null string
157
}
158
159     // Get the transaction service
160
try {
161         transactionService =
162         (TransactionService) ServiceManager.getInstance().getTransactionService();
163     } catch (Exception JavaDoc e) {
164         serverlog.log(BasicLevel.ERROR, "Error when starting the JMS service "+e);
165         throw new ServiceException("Error when starting the JMS service ", e);
166     }
167
168     if (serverlog.isLoggable(BasicLevel.DEBUG)) {
169         serverlog.log(BasicLevel.DEBUG, "JMS service initialized");
170     }
171     }
172
173     /**
174      * Start the Service
175      * Initialization of the service is already done.
176      * For administrating objects we use the class specified in the jms.provider property
177      * by default we use the Joram implementation
178      */

179     public void doStart() throws ServiceException {
180
181     if (isMomLocal) {
182         if (serverlog.isLoggable(BasicLevel.DEBUG)) {
183             serverlog.log(BasicLevel.DEBUG, "Starting JMS Service with collocated MOM");
184         }
185     } else {
186         if (serverlog.isLoggable(BasicLevel.DEBUG)) {
187             serverlog.log(BasicLevel.DEBUG, "Starting JMS Service with MOM at url "+url);
188         }
189     }
190
191     jonasjms = JmsManagerImpl.getJmsManager();
192     jmsjmx = JmsManagerImpl.getJmsJmxManagement();
193
194     Class JavaDoc c = null;
195     if (loaderlog.isLoggable(BasicLevel.DEBUG)) {
196         loaderlog.log(BasicLevel.DEBUG, "JmsServiceImpl classloader="+getClass().getClassLoader());
197     }
198     try {
199         ClassLoader JavaDoc loader = JmsManagerImpl.class.getClassLoader();
200         if (loader == null) {
201         loader = Thread.currentThread().getContextClassLoader();
202         }
203         c = loader.loadClass(momProviderClassName);
204         if (loaderlog.isLoggable(BasicLevel.DEBUG)) {
205             loaderlog.log(BasicLevel.DEBUG, momProviderClassName+" classloader="+loader);
206         }
207     } catch (Exception JavaDoc e) {
208         serverlog.log(BasicLevel.ERROR, "JMS Service Cannot load admin class "+e);
209         throw new ServiceException("JMS Service Cannot load admin class", e);
210     }
211
212     // initialization of the JmsManager with implementation class for Administration
213
try {
214         jonasjms.init(c, isMomLocal, url, transactionService.getTransactionManager());
215     } catch (Exception JavaDoc e) {
216         serverlog.log(BasicLevel.ERROR, "JMS Service Cannot init the JMS manager "+e);
217         throw new ServiceException("JMS Service Cannot init the JMS manager", e);
218     }
219
220     // Create Topics and Queues
221
try {
222         // Create administered objects for Queues
223
for (int i = 0; i < queueNames.size(); i++) {
224         Queue JavaDoc q = jonasjms.createQueue((String JavaDoc) queueNames.elementAt(i));
225         }
226
227         // Create administered objects for Topics
228
for (int i = 0; i < topicNames.size(); i++) {
229         Topic JavaDoc t = jonasjms.createTopic((String JavaDoc) topicNames.elementAt(i));
230         }
231     } catch (Exception JavaDoc ex) {
232         serverlog.log(BasicLevel.ERROR, "JMS Service Cannot create administered object : "+ex.toString());
233         try {
234         jonasjms.stop();
235         jonasjms = null;
236         } catch (Exception JavaDoc e) {
237         }
238         throw new ServiceException("JMS Service Cannot create administered object", ex);
239     }
240
241     try {
242         // Register JmsService MBean : JmxJmsService
243
((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().registerMBean(this,JonasObjectName.jmsService());
244     } catch (ServiceException se) {
245         // Jmx Service not available, do nothing
246
} catch (Exception JavaDoc e) {
247         serverlog.log(BasicLevel.ERROR, "JmsService: Cannot start the JMS service:\n"+e);
248         throw new ServiceException("JmsService: Cannot start the JMS service",
249                        e);
250     }
251
252     }
253
254     /**
255      * Stop the JMS Service
256      */

257     public void doStop() throws ServiceException {
258
259     try {
260         jonasjms.stop();
261     } catch (Exception JavaDoc e) {
262         serverlog.log(BasicLevel.ERROR, "JMS Service Cannot stop the JMS manager "+e);
263         throw new ServiceException("JMS Service Cannot stop the JMS manager", e);
264     }
265     // unregister MBean
266
try {
267         // unregister jms Service MBean
268
((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().unregisterMBean(JonasObjectName.jmsService());
269     } catch (ServiceException se) {
270         // Jmx Service not available, do nothing
271
} catch (Exception JavaDoc e) {
272         serverlog.log(BasicLevel.ERROR, "Cannot stop the JMS service:\n"+e);
273         throw new ServiceException("Cannot stop the JMS service",e);
274     }
275     serverlog.log(BasicLevel.DEBUG, "JMS Service stopped");
276     }
277
278
279     // -------------------------------------------------------------------
280
// JMS Service Implementation
281
// -------------------------------------------------------------------
282

283     public JmsManager getJmsManager() {
284     return jonasjms;
285     }
286
287     /**
288      * MBean method
289      * @return the current number of Jms Connection Factory
290      */

291     public Integer JavaDoc getCurrentNumberOfJmsConnectionFactory(){
292         return new Integer JavaDoc(jmsjmx.getCurrentNumberOfJmsConnectionFactory());
293     }
294
295     /**
296      * MBean method
297      * @return the current number of Topic Jms Connection Factory
298      */

299     public Integer JavaDoc getCurrentNumberOfJmsTopicConnectionFactory(){
300     return new Integer JavaDoc(jmsjmx.getCurrentNumberOfJmsTopicConnectionFactory());
301
302     }
303
304     /**
305      * MBean method
306      * @return the current number of Queue Jms Connection Factory
307      */

308     public Integer JavaDoc getCurrentNumberOfJmsQueueConnectionFactory(){
309         return new Integer JavaDoc(jmsjmx.getCurrentNumberOfJmsQueueConnectionFactory());
310     }
311
312     /**
313      * MBean method
314      * @return the current number of Topic Jms Destination
315      */

316     public Integer JavaDoc getCurrentNumberOfJmsTopicDestination(){
317     return new Integer JavaDoc(jmsjmx.getCurrentNumberOfJmsTopicDestination());
318     }
319
320     /**
321      * MBean method
322      * @return the current number of Queue Jms Destination
323      */

324     public Integer JavaDoc getCurrentNumberOfJmsQueueDestination(){
325     return new Integer JavaDoc(jmsjmx.getCurrentNumberOfJmsQueueDestination());
326     }
327
328     /**
329      * MBean method
330      * Create a new Jms queue destination
331      * @param String jndi Name
332      * not yet implement with a remote host
333      */

334     public void createJmsQueueDestination(String JavaDoc jndiName){
335     try {
336         jonasjms.createQueue(jndiName);
337
338         // Send to the listner MBean a notification containing the new value of this property and
339
// the indication that the new value has to be added to a list of values
340
ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false);
341         p.setAdd(true);
342         sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
343     } catch (Exception JavaDoc e) {
344         serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e);
345     }
346     }
347
348     /**
349      * MBean method
350      * Create a new Jms topic destination
351      * @param String jndi Name
352      * not yet implement with a remote host
353      */

354     public void createJmsTopicDestination(String JavaDoc jndiName){
355         try {
356             jonasjms.createTopic(jndiName);
357
358             // Send to the listner MBean a notification containing the new value of this property and
359
// the indication that the new value has to be added to a list of values
360
ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false);
361             p.setAdd(true);
362             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
363         } catch (Exception JavaDoc e) {
364             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e);
365         }
366     }
367
368     /**
369      * MBean method
370      * Remove a Jms destination
371      * @param String jndi name
372      */

373     public void removeJmsTopicDestination(String JavaDoc jndiName){
374         try{
375             jmsjmx.removeJmsDestination(jndiName);
376
377             // Send to the listner MBean a notification containing the new value of this property and
378
// the indication that the new value has to be removed from a list of values
379
ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false);
380             p.setAdd(false);
381             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
382         } catch (Exception JavaDoc e){
383             // remove problem
384
serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
385         }
386     }
387
388     /**
389      * MBean method
390      * Remove a Jms destination
391      * @param String jndi name
392      */

393     public void removeJmsQueueDestination(String JavaDoc jndiName){
394         try{
395             jmsjmx.removeJmsDestination(jndiName);
396
397             // Send to the listner MBean a notification containing the new value of this property and
398
// the indication that the new value has to be removed from a list of values
399
ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false);
400             p.setAdd(false);
401             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
402         } catch (Exception JavaDoc e){
403             // remove problem
404
serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
405         }
406     }
407
408
409    /**
410      * Remove a Jms destination
411      * @param String jndi name
412      */

413     public void removeJmsDestination(String JavaDoc jndiName) {
414         try{
415             String JavaDoc destType = jmsjmx.removeJmsDestination(jndiName);
416
417             // Send to the listner MBean a notification containing the new value of this property and
418
// the indication that the new value has to be removed from a list of values
419
ReconfiguredProp p = null;
420             if (destType.equals("queue")) {
421                 p = new ReconfiguredProp(QUEUES, jndiName, false);
422             } else if (destType.equals("topic")) {
423                 p = new ReconfiguredProp(TOPICS, jndiName, false);
424             } else {
425                 return;
426             }
427             p.setAdd(false);
428             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
429         } catch (Exception JavaDoc e){
430             // remove problem
431
serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
432         }
433     }
434
435     /**
436      * MBean method
437      * return Set of Queue Destinations Names
438      */

439     public Set JavaDoc getAllJmsQueueDestinationNames() {
440         HashSet JavaDoc result = new HashSet JavaDoc();
441         for (Enumeration JavaDoc enu = jonasjms.getQueuesNames() ; enu.hasMoreElements() ;) {
442             result.add(enu.nextElement());
443         }
444         return result;
445     }
446
447     /**
448      * MBean method
449      * return Set of Topic Destination Names
450      */

451     public Set JavaDoc getAllJmsTopicDestinationNames(){
452         HashSet JavaDoc result = new HashSet JavaDoc();
453         for (Enumeration JavaDoc enu = jonasjms.getTopicsNames() ; enu.hasMoreElements() ;) {
454             result.add(enu.nextElement());
455         }
456         return result;
457     }
458
459     /**
460      * MBean method
461      * return Set of Connection Factory Names
462      */

463     public Set JavaDoc getAllJmsConnectionFactoryNames() {
464         HashSet JavaDoc result = new HashSet JavaDoc();
465         return result;
466     }
467
468     /**
469      * MBean method
470      * return Set of Queue Connection Factory Names
471      */

472     public Set JavaDoc getAllJmsQueueConnectionFactoryNames() {
473         HashSet JavaDoc result = new HashSet JavaDoc();
474         return result;
475     }
476
477     /**
478      * MBean method
479      * return Set of Topic Connection Factory Names
480      */

481     public Set JavaDoc getAllJmsTopicConnectionFactoryNames() {
482         HashSet JavaDoc result = new HashSet JavaDoc();
483         return result;
484     }
485
486     /**
487      * MBean method
488      * @return String name of default Queue Connection factory
489      */

490     public String JavaDoc getDefaultQueueConnectionFactoryName() {
491         return jmsjmx.getDefaultQueueConnectionFactoryName();
492     }
493
494     /**
495      * MBean method
496      * @return String name of default Topic Connection factory
497      */

498     public String JavaDoc getDefaultTopicConnectionFactoryName() {
499         return jmsjmx.getDefaultTopicConnectionFactoryName();
500     }
501
502     /**
503      * @return String name of default Connection factory
504      */

505     public String JavaDoc getDefaultConnectionFactoryName() {
506         return jmsjmx.getDefaultConnectionFactoryName();
507     }
508
509     /**
510      * MBean method:
511      * save updated configuration
512      */

513     public void saveConfig() {
514         sendSaveNotification(++sequenceNumber, SERVICE_NAME);
515     }
516
517     // Monitoring methods
518

519     /**
520      * Get the messaging mode a connection factory belongs to
521      * @param jndiName connection factory name
522      * @return messaging mode
523      */

524     public String JavaDoc getConnectionFactoryMode(String JavaDoc jndiName) {
525         serverlog.log(BasicLevel.DEBUG, "");
526         String JavaDoc m = null;
527         try{
528             m = jmsjmx.getConnectionFactoryMode(jndiName);
529         } catch (IllegalStateException JavaDoc se) {
530             // nothing to do
531
} catch (Exception JavaDoc e){
532             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
533         }
534         return m;
535     }
536
537     /**
538      * Get number of pending messages on a queue
539      * @param jndiName queue name
540      * @return number of pending messages
541      */

542     public Integer JavaDoc getPendingMessages(String JavaDoc jndiName) {
543         serverlog.log(BasicLevel.DEBUG, "");
544         Integer JavaDoc nb = null;
545         int n;
546         try{
547             n = jmsjmx.getPendingMessages(jndiName);
548             nb = new Integer JavaDoc(n);
549         } catch (IllegalStateException JavaDoc se) {
550             // nothing to do
551
} catch (Exception JavaDoc e){
552             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
553         }
554         return nb;
555     }
556
557     /**
558      * Get number of pending requests on a queue
559      * @param jndiName queue name
560      * @return number of pending requests. If nb null, could not get this information.
561      */

562     public Integer JavaDoc getPendingRequests(String JavaDoc jndiName) {
563         if (serverlog.isLoggable(BasicLevel.DEBUG)) {
564             serverlog.log(BasicLevel.DEBUG, "");
565         }
566         Integer JavaDoc nb = null;
567         int n;
568         try{
569             n = jmsjmx.getPendingRequests(jndiName);
570             nb = new Integer JavaDoc(n);
571         } catch (IllegalStateException JavaDoc se) {
572             // nothing to do
573
} catch (Exception JavaDoc e){
574             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
575         }
576         return nb;
577     }
578
579     /**
580      * Get number of subscriptions on a topic
581      * @param jndiName topic name
582      * @return number of subscriptions
583      */

584     public Integer JavaDoc getSubscriptions(String JavaDoc jndiName) {
585         if (serverlog.isLoggable(BasicLevel.DEBUG)) {
586             serverlog.log(BasicLevel.DEBUG, "");
587         }
588         Integer JavaDoc nb = null;
589         int n;
590         try{
591             n = jmsjmx.getSubscriptions(jndiName);
592             nb = new Integer JavaDoc(n);
593         } catch (IllegalStateException JavaDoc se) {
594             // nothing to do
595
} catch (Exception JavaDoc e){
596             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
597         }
598         return nb;
599     }
600
601     public Boolean JavaDoc isMomLocal() {
602         return new Boolean JavaDoc(isMomLocal);
603     }
604     /**
605      * @return the MOM's URL if distant or "" if collocated
606      */

607     public String JavaDoc getUrl() {
608         return url;
609     }
610
611     public String JavaDoc getMom() {
612         return momProviderClassName;
613     }
614 }
615
Popular Tags