JmsServiceImpl.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  * --------------------------------------------------------------------------
00021  * $Id: JmsServiceImpl.java,v 1.28 2004/03/25 15:58:20 sauthieg Exp $
00022  * --------------------------------------------------------------------------
00023  */
00024 
00025 
00026 package org.objectweb.jonas.jms;
00027 
00028 import java.util.Enumeration;
00029 import java.util.HashSet;
00030 import java.util.Set;
00031 import java.util.StringTokenizer;
00032 import java.util.Vector;
00033 
00034 import javax.jms.Queue;
00035 import javax.jms.Topic;
00036 import javax.naming.Context;
00037 import javax.naming.NamingException;
00038 
00039 import org.objectweb.jonas.common.Log;
00040 import org.objectweb.jonas.jmx.JmxService;
00041 import org.objectweb.jonas.jmx.JonasObjectName;
00042 import org.objectweb.jonas.jtm.TransactionService;
00043 import org.objectweb.jonas.management.ReconfiguredProp;
00044 import org.objectweb.jonas.service.AbsServiceImpl;
00045 import org.objectweb.jonas.service.ServiceException;
00046 import org.objectweb.jonas.service.ServiceManager;
00047 import org.objectweb.jonas_jms.JmsJmxManagement;
00048 import org.objectweb.jonas_jms.JmsManagerImpl;
00049 import org.objectweb.jonas_jms.api.JmsManager;
00050 import org.objectweb.util.monolog.api.BasicLevel;
00051 import org.objectweb.util.monolog.api.Logger;
00052 
00061 public class JmsServiceImpl extends AbsServiceImpl implements JmsService, JmsServiceImplMBean {
00062 
00063     // loggers
00064     static private Logger serverlog = null;
00065     static private Logger loaderlog = null;
00066 
00067     // Service name as used to label configuration properties
00068     public static final String SERVICE_NAME = "jms";
00069 
00070     // Configuration information used when starting the JMS service
00071     String momProviderClassName;
00072     Vector queueNames = new Vector();
00073     Vector topicNames = new Vector();
00074     boolean isMomLocal;
00075     String url;
00076     String jmsClass;
00077     TransactionService transactionService = null;
00078     
00079     // JMS manager
00080     JmsManager jonasjms = null;
00081     JmsJmxManagement jmsjmx = null;
00082 
00083     // JMS service configuration properties
00084     public static final String COLLOCATED = "jonas.service.jms.collocated";
00085     public static final String MOM = "jonas.service.jms.mom";
00086     public static final String TOPICS = "jonas.service.jms.topics";
00087     public static final String QUEUES = "jonas.service.jms.queues";
00088     public static final String DESTINATIONS = "jonas.service.jms.dest";
00089     public static final String URL = "jonas.service.jms.url";
00090     public static final String CLASS = "jonas.service.jms.class";
00091 
00092     // Value used as sequence number by reconfiguration notifications
00093     long sequenceNumber = 0;
00094 
00095     // -------------------------------------------------------------------
00096     // Service Implementation
00097     // -------------------------------------------------------------------
00098 
00103     public void doInit(Context ctx) throws ServiceException {
00104 
00105         // get loggers
00106         serverlog = Log.getLogger(Log.JONAS_SERVER_PREFIX);
00107         loaderlog = Log.getLogger(Log.JONAS_LOADER_PREFIX);
00108     super.initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX));
00109 
00110         // Read the configuration information
00111         try {
00112             momProviderClassName = (String) ctx.lookup(MOM);
00113         } catch (NamingException e) {
00114             serverlog.log(BasicLevel.ERROR, "JMS Service Cannot read configuration "+e);
00115             throw new ServiceException("JMS Service Cannot read configuration ", e);
00116         }
00117 
00118         String ql = null;
00119         try {
00120             ql = (String) ctx.lookup(QUEUES);
00121         } catch (NamingException e) {
00122             // No queue predefined
00123         }
00124         if (ql != null) {
00125             StringTokenizer st = new StringTokenizer(ql, ",");
00126             while (st.hasMoreTokens()) {
00127                 queueNames.add(st.nextToken().trim());
00128             }
00129         }
00130 
00131         String tl = null;
00132         try {
00133             tl = (String) ctx.lookup(TOPICS);
00134         } catch (NamingException e) {
00135             // No topic predefined
00136         }
00137         if (tl != null) {
00138             StringTokenizer st = new StringTokenizer(tl, ",");
00139             while (st.hasMoreTokens()) {
00140                 topicNames.add(st.nextToken().trim());
00141             }
00142         }
00143 
00144         String local = "true";
00145         try {
00146             local = (String) ctx.lookup(COLLOCATED);
00147         } catch (NamingException e) {
00148             // default value is collocated
00149         }
00150         isMomLocal = local.equalsIgnoreCase("true");
00151 
00152         url = "";
00153         try {
00154             url = (String) ctx.lookup(URL);
00155         } catch (NamingException e) {
00156             // default value is null string
00157         }
00158 
00159         // Get the transaction service
00160         try {
00161             transactionService =
00162                 (TransactionService) ServiceManager.getInstance().getTransactionService();
00163         } catch (Exception e) {
00164             serverlog.log(BasicLevel.ERROR, "Error when starting the JMS service "+e);
00165             throw new ServiceException("Error when starting the JMS service ", e);
00166         }
00167 
00168         serverlog.log(BasicLevel.DEBUG, "JMS service initialized");
00169     }
00170 
00177     public void doStart() throws ServiceException {
00178 
00179         if (isMomLocal) {
00180             serverlog.log(BasicLevel.DEBUG, "Starting JMS Service with collocated MOM");
00181         } else {
00182             serverlog.log(BasicLevel.DEBUG, "Starting JMS Service with MOM at url "+url);
00183         }
00184         
00185         jonasjms = JmsManagerImpl.getJmsManager();
00186         jmsjmx = JmsManagerImpl.getJmsJmxManagement();
00187         
00188         Class c = null;
00189         loaderlog.log(BasicLevel.DEBUG, "JmsServiceImpl classloader="+getClass().getClassLoader());
00190         try {
00191             ClassLoader loader = JmsManagerImpl.class.getClassLoader();
00192             if (loader == null) {
00193                 loader = Thread.currentThread().getContextClassLoader();
00194             }
00195             c = loader.loadClass(momProviderClassName);
00196             loaderlog.log(BasicLevel.DEBUG, momProviderClassName+" classloader="+loader);
00197         } catch (Exception e) {
00198             serverlog.log(BasicLevel.ERROR, "JMS Service Cannot load admin class "+e);
00199             throw new ServiceException("JMS Service Cannot load admin class", e);
00200         }
00201         
00202         // initialization of the JmsManager with implementation class for Administration
00203         try {
00204             jonasjms.init(c, isMomLocal, url, transactionService.getTransactionManager());
00205         } catch (Exception e) {
00206             serverlog.log(BasicLevel.ERROR, "JMS Service Cannot init the JMS manager "+e);
00207             throw new ServiceException("JMS Service Cannot init the JMS manager", e);
00208         }
00209         
00210         // Create Topics and Queues
00211         try {
00212             // Create administered objects for Queues
00213             for (int i = 0; i < queueNames.size(); i++) {
00214                 Queue q = jonasjms.createQueue((String) queueNames.elementAt(i));
00215             }
00216 
00217             // Create administered objects for Topics
00218             for (int i = 0; i < topicNames.size(); i++) {
00219                 Topic t = jonasjms.createTopic((String) topicNames.elementAt(i));
00220             }
00221         } catch (Exception ex) {
00222             serverlog.log(BasicLevel.ERROR, "JMS Service Cannot create administered object : "+ex.toString());
00223             try {
00224                 jonasjms.stop();
00225                 jonasjms = null;
00226             } catch (Exception e) {
00227             }
00228             throw new ServiceException("JMS Service Cannot create administered object", ex);
00229         }
00230 
00231         try {
00232             // Register JmsService MBean : JmxJmsService 
00233             ((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().registerMBean(this,JonasObjectName.jmsService());
00234         } catch (ServiceException se) {
00235             // Jmx Service not available, do nothing
00236         } catch (Exception e) {
00237             serverlog.log(BasicLevel.ERROR, "JmsService: Cannot start the JMS service:\n"+e);
00238             throw new ServiceException("JmsService: Cannot start the JMS service",
00239                                        e);
00240         }
00241 
00242     }
00243 
00247     public void doStop() throws ServiceException {
00248 
00249         try {
00250             jonasjms.stop();
00251         } catch (Exception e) {
00252             serverlog.log(BasicLevel.ERROR, "JMS Service Cannot stop the JMS manager "+e);
00253             throw new ServiceException("JMS Service Cannot stop the JMS manager", e);
00254         }
00255         // unregister MBean     
00256         try {
00257             // unregister jms Service MBean
00258             ((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().unregisterMBean(JonasObjectName.jmsService());
00259         } catch (ServiceException se) {
00260             // Jmx Service not available, do nothing
00261         } catch (Exception e) {
00262             serverlog.log(BasicLevel.ERROR, "Cannot stop the JMS service:\n"+e);
00263             throw new ServiceException("Cannot stop the JMS service",e);
00264         }
00265         serverlog.log(BasicLevel.DEBUG, "JMS Service stopped");
00266     }
00267 
00268 
00269     // -------------------------------------------------------------------
00270     // JMS Service Implementation
00271     // -------------------------------------------------------------------
00272 
00273     public JmsManager  getJmsManager() {
00274         return jonasjms;
00275     }
00276 
00281     public Integer getCurrentNumberOfJmsConnectionFactory(){
00282             return new Integer(jmsjmx.getCurrentNumberOfJmsConnectionFactory());
00283     }
00284 
00289     public Integer getCurrentNumberOfJmsTopicConnectionFactory(){
00290         return new Integer(jmsjmx.getCurrentNumberOfJmsTopicConnectionFactory());
00291 
00292     }
00293 
00298     public Integer getCurrentNumberOfJmsQueueConnectionFactory(){
00299                 return new Integer(jmsjmx.getCurrentNumberOfJmsQueueConnectionFactory());
00300     }
00301 
00306     public Integer getCurrentNumberOfJmsTopicDestination(){
00307         return new Integer(jmsjmx.getCurrentNumberOfJmsTopicDestination());
00308     }
00309 
00314     public Integer getCurrentNumberOfJmsQueueDestination(){
00315         return new Integer(jmsjmx.getCurrentNumberOfJmsQueueDestination());
00316     }
00317 
00324     public void createJmsQueueDestination(String jndiName){
00325         try {
00326             jonasjms.createQueue(jndiName);
00327             
00328         // Send to the listner MBean a notification containing the new value of this property and 
00329         // the indication that the new value has to be added to a list of values 
00330         ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false);
00331         p.setAdd(true); 
00332         sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
00333         } catch (Exception e) {
00334             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e);
00335         }
00336     } 
00337 
00344     public void createJmsTopicDestination(String jndiName){
00345         try {
00346             jonasjms.createTopic(jndiName);
00347 
00348             // Send to the listner MBean a notification containing the new value of this property and 
00349             // the indication that the new value has to be added to a list of values 
00350             ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false);
00351             p.setAdd(true);
00352             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
00353         } catch (Exception e) {
00354             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e);
00355         }   
00356     } 
00357 
00363     public void removeJmsTopicDestination(String jndiName){
00364         try{
00365             jmsjmx.removeJmsDestination(jndiName);
00366 
00367             // Send to the listner MBean a notification containing the new value of this property and 
00368             // the indication that the new value has to be removed from a list of values 
00369             ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false);
00370             p.setAdd(false);
00371             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
00372         } catch (Exception e){
00373             // remove problem
00374             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
00375         }
00376     }
00377 
00383     public void removeJmsQueueDestination(String jndiName){
00384         try{
00385             jmsjmx.removeJmsDestination(jndiName);
00386 
00387             // Send to the listner MBean a notification containing the new value of this property and 
00388             // the indication that the new value has to be removed from a list of values 
00389             ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false);
00390             p.setAdd(false);
00391             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
00392         } catch (Exception e){
00393             // remove problem
00394             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
00395         }
00396     }
00397 
00398 
00403     public void removeJmsDestination(String jndiName) {        
00404         try{
00405             String destType = jmsjmx.removeJmsDestination(jndiName);
00406 
00407             // Send to the listner MBean a notification containing the new value of this property and 
00408             // the indication that the new value has to be removed from a list of values 
00409             ReconfiguredProp p = null;
00410             if (destType.equals("queue")) {
00411                 p = new ReconfiguredProp(QUEUES, jndiName, false);
00412             } else if (destType.equals("topic")) {
00413                 p = new ReconfiguredProp(TOPICS, jndiName, false);
00414             } else {
00415                 return;
00416             }
00417             p.setAdd(false);
00418             sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p);
00419         } catch (Exception e){
00420             // remove problem
00421             serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e);
00422         }
00423     }
00424 
00429     public Set getAllJmsQueueDestinationNames() {
00430         HashSet result = new HashSet();
00431         for (Enumeration enu = jonasjms.getQueuesNames() ; enu.hasMoreElements() ;) {
00432             result.add(enu.nextElement());
00433         }
00434         return result;
00435     }
00436 
00441     public Set getAllJmsTopicDestinationNames(){
00442         HashSet result = new HashSet();
00443         for (Enumeration enu = jonasjms.getTopicsNames() ; enu.hasMoreElements() ;) {
00444             result.add(enu.nextElement());
00445         }
00446         return result;
00447     }
00448 
00453     public Set getAllJmsConnectionFactoryNames() {
00454         HashSet result = new HashSet();
00455         return result;
00456     }
00457 
00462     public Set getAllJmsQueueConnectionFactoryNames() {
00463         HashSet result = new HashSet();
00464         return result;
00465     }
00466 
00471     public Set getAllJmsTopicConnectionFactoryNames() {
00472         HashSet result = new HashSet();
00473         return result;  
00474     }
00475 
00480     public String getDefaultQueueConnectionFactoryName() {
00481         return jmsjmx.getDefaultQueueConnectionFactoryName(); 
00482     }
00483 
00488     public String getDefaultTopicConnectionFactoryName() {
00489         return jmsjmx.getDefaultTopicConnectionFactoryName(); 
00490     }
00491 
00495     public String getDefaultConnectionFactoryName() {
00496         return jmsjmx.getDefaultConnectionFactoryName();
00497     }
00498 
00503     public void saveConfig() {
00504         sendSaveNotification(++sequenceNumber, SERVICE_NAME);
00505     }
00506 
00507     // Monitoring methods
00508 
00514     public String getConnectionFactoryMode(String jndiName) {
00515         serverlog.log(BasicLevel.DEBUG, "");
00516         String m = null;
00517         try{
00518             m = jmsjmx.getConnectionFactoryMode(jndiName);
00519         } catch (IllegalStateException se) {
00520             // nothing to do
00521         } catch (Exception e){
00522             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
00523         }    
00524         return m;
00525     }
00526  
00532     public Integer getPendingMessages(String jndiName) {
00533         serverlog.log(BasicLevel.DEBUG, "");
00534         Integer nb = null;
00535         int n;
00536         try{
00537             n = jmsjmx.getPendingMessages(jndiName);
00538             nb = new Integer(n);
00539         } catch (IllegalStateException se) {
00540             // nothing to do
00541         } catch (Exception e){
00542             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
00543         }    
00544         return nb;
00545     }
00546 
00552     public Integer getPendingRequests(String jndiName) {
00553         serverlog.log(BasicLevel.DEBUG, "");
00554         Integer nb = null;
00555         int n;
00556         try{
00557             n = jmsjmx.getPendingRequests(jndiName);
00558             nb = new Integer(n);
00559         } catch (IllegalStateException se) {
00560             // nothing to do
00561         } catch (Exception e){
00562             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
00563         }    
00564         return nb;
00565     }
00566 
00572     public Integer getSubscriptions(String jndiName) {
00573         serverlog.log(BasicLevel.DEBUG, "");
00574         Integer nb = null;
00575         int n;
00576         try{
00577             n = jmsjmx.getSubscriptions(jndiName);
00578             nb = new Integer(n);
00579         } catch (IllegalStateException se) {
00580             // nothing to do
00581         } catch (Exception e){
00582             serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e);
00583         }    
00584         return nb;
00585     } 
00586 
00587     public Boolean isMomLocal() {
00588         return new Boolean(isMomLocal);
00589     }
00593     public String getUrl() {
00594         return url;
00595     }
00596 
00597     public String getMom() {
00598         return momProviderClassName;
00599     }
00600 }

Generated on Tue Feb 15 15:05:16 2005 for JOnAS by  doxygen 1.3.9.1