1 24 25 26 package org.objectweb.jonas.jms; 27 28 import java.util.Enumeration ; 29 import java.util.HashSet ; 30 import java.util.Set ; 31 import java.util.StringTokenizer ; 32 import java.util.Vector ; 33 34 import javax.jms.Queue ; 35 import javax.jms.Topic ; 36 import javax.naming.Context ; 37 import javax.naming.NamingException ; 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 60 61 public class JmsServiceImpl extends AbsServiceImpl implements JmsService, JmsServiceImplMBean { 62 63 static private Logger serverlog = null; 65 static private Logger loaderlog = null; 66 67 public static final String SERVICE_NAME = "jms"; 69 70 String momProviderClassName; 72 Vector queueNames = new Vector (); 73 Vector topicNames = new Vector (); 74 boolean isMomLocal; 75 String url; 76 String jmsClass; 77 TransactionService transactionService = null; 78 79 JmsManager jonasjms = null; 81 JmsJmxManagement jmsjmx = null; 82 83 public static final String COLLOCATED = "jonas.service.jms.collocated"; 85 public static final String MOM = "jonas.service.jms.mom"; 86 public static final String TOPICS = "jonas.service.jms.topics"; 87 public static final String QUEUES = "jonas.service.jms.queues"; 88 public static final String DESTINATIONS = "jonas.service.jms.dest"; 89 public static final String URL = "jonas.service.jms.url"; 90 public static final String CLASS = "jonas.service.jms.class"; 91 92 long sequenceNumber = 0; 94 95 99 103 public void doInit(Context ctx) throws ServiceException { 104 105 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 try { 112 momProviderClassName = (String ) ctx.lookup(MOM); 113 } catch (NamingException 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 ql = null; 119 try { 120 ql = (String ) ctx.lookup(QUEUES); 121 } catch (NamingException e) { 122 } 124 if (ql != null) { 125 StringTokenizer st = new StringTokenizer (ql, ","); 126 while (st.hasMoreTokens()) { 127 queueNames.add(st.nextToken().trim()); 128 } 129 } 130 131 String tl = null; 132 try { 133 tl = (String ) ctx.lookup(TOPICS); 134 } catch (NamingException e) { 135 } 137 if (tl != null) { 138 StringTokenizer st = new StringTokenizer (tl, ","); 139 while (st.hasMoreTokens()) { 140 topicNames.add(st.nextToken().trim()); 141 } 142 } 143 144 String local = "true"; 145 try { 146 local = (String ) ctx.lookup(COLLOCATED); 147 } catch (NamingException e) { 148 } 150 isMomLocal = local.equalsIgnoreCase("true"); 151 152 url = ""; 153 try { 154 url = (String ) ctx.lookup(URL); 155 } catch (NamingException e) { 156 } 158 159 try { 161 transactionService = 162 (TransactionService) ServiceManager.getInstance().getTransactionService(); 163 } catch (Exception 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 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 c = null; 195 if (loaderlog.isLoggable(BasicLevel.DEBUG)) { 196 loaderlog.log(BasicLevel.DEBUG, "JmsServiceImpl classloader="+getClass().getClassLoader()); 197 } 198 try { 199 ClassLoader 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 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 try { 214 jonasjms.init(c, isMomLocal, url, transactionService.getTransactionManager()); 215 } catch (Exception 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 try { 222 for (int i = 0; i < queueNames.size(); i++) { 224 Queue q = jonasjms.createQueue((String ) queueNames.elementAt(i)); 225 } 226 227 for (int i = 0; i < topicNames.size(); i++) { 229 Topic t = jonasjms.createTopic((String ) topicNames.elementAt(i)); 230 } 231 } catch (Exception 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 e) { 237 } 238 throw new ServiceException("JMS Service Cannot create administered object", ex); 239 } 240 241 try { 242 ((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().registerMBean(this,JonasObjectName.jmsService()); 244 } catch (ServiceException se) { 245 } catch (Exception 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 257 public void doStop() throws ServiceException { 258 259 try { 260 jonasjms.stop(); 261 } catch (Exception 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 try { 267 ((JmxService)ServiceManager.getInstance().getJmxService()).getJmxServer().unregisterMBean(JonasObjectName.jmsService()); 269 } catch (ServiceException se) { 270 } catch (Exception 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 283 public JmsManager getJmsManager() { 284 return jonasjms; 285 } 286 287 291 public Integer getCurrentNumberOfJmsConnectionFactory(){ 292 return new Integer (jmsjmx.getCurrentNumberOfJmsConnectionFactory()); 293 } 294 295 299 public Integer getCurrentNumberOfJmsTopicConnectionFactory(){ 300 return new Integer (jmsjmx.getCurrentNumberOfJmsTopicConnectionFactory()); 301 302 } 303 304 308 public Integer getCurrentNumberOfJmsQueueConnectionFactory(){ 309 return new Integer (jmsjmx.getCurrentNumberOfJmsQueueConnectionFactory()); 310 } 311 312 316 public Integer getCurrentNumberOfJmsTopicDestination(){ 317 return new Integer (jmsjmx.getCurrentNumberOfJmsTopicDestination()); 318 } 319 320 324 public Integer getCurrentNumberOfJmsQueueDestination(){ 325 return new Integer (jmsjmx.getCurrentNumberOfJmsQueueDestination()); 326 } 327 328 334 public void createJmsQueueDestination(String jndiName){ 335 try { 336 jonasjms.createQueue(jndiName); 337 338 ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false); 341 p.setAdd(true); 342 sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p); 343 } catch (Exception e) { 344 serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e); 345 } 346 } 347 348 354 public void createJmsTopicDestination(String jndiName){ 355 try { 356 jonasjms.createTopic(jndiName); 357 358 ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false); 361 p.setAdd(true); 362 sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p); 363 } catch (Exception e) { 364 serverlog.log(BasicLevel.ERROR, "JmsService: Exception when create destination " + jndiName + ": " + e); 365 } 366 } 367 368 373 public void removeJmsTopicDestination(String jndiName){ 374 try{ 375 jmsjmx.removeJmsDestination(jndiName); 376 377 ReconfiguredProp p = new ReconfiguredProp(TOPICS, jndiName, false); 380 p.setAdd(false); 381 sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p); 382 } catch (Exception e){ 383 serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e); 385 } 386 } 387 388 393 public void removeJmsQueueDestination(String jndiName){ 394 try{ 395 jmsjmx.removeJmsDestination(jndiName); 396 397 ReconfiguredProp p = new ReconfiguredProp(QUEUES, jndiName, false); 400 p.setAdd(false); 401 sendReconfigNotification(++sequenceNumber, SERVICE_NAME, p); 402 } catch (Exception e){ 403 serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e); 405 } 406 } 407 408 409 413 public void removeJmsDestination(String jndiName) { 414 try{ 415 String destType = jmsjmx.removeJmsDestination(jndiName); 416 417 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 e){ 430 serverlog.log(BasicLevel.ERROR, "JmsService: Exception when remove destination"+e); 432 } 433 } 434 435 439 public Set getAllJmsQueueDestinationNames() { 440 HashSet result = new HashSet (); 441 for (Enumeration enu = jonasjms.getQueuesNames() ; enu.hasMoreElements() ;) { 442 result.add(enu.nextElement()); 443 } 444 return result; 445 } 446 447 451 public Set getAllJmsTopicDestinationNames(){ 452 HashSet result = new HashSet (); 453 for (Enumeration enu = jonasjms.getTopicsNames() ; enu.hasMoreElements() ;) { 454 result.add(enu.nextElement()); 455 } 456 return result; 457 } 458 459 463 public Set getAllJmsConnectionFactoryNames() { 464 HashSet result = new HashSet (); 465 return result; 466 } 467 468 472 public Set getAllJmsQueueConnectionFactoryNames() { 473 HashSet result = new HashSet (); 474 return result; 475 } 476 477 481 public Set getAllJmsTopicConnectionFactoryNames() { 482 HashSet result = new HashSet (); 483 return result; 484 } 485 486 490 public String getDefaultQueueConnectionFactoryName() { 491 return jmsjmx.getDefaultQueueConnectionFactoryName(); 492 } 493 494 498 public String getDefaultTopicConnectionFactoryName() { 499 return jmsjmx.getDefaultTopicConnectionFactoryName(); 500 } 501 502 505 public String getDefaultConnectionFactoryName() { 506 return jmsjmx.getDefaultConnectionFactoryName(); 507 } 508 509 513 public void saveConfig() { 514 sendSaveNotification(++sequenceNumber, SERVICE_NAME); 515 } 516 517 519 524 public String getConnectionFactoryMode(String jndiName) { 525 serverlog.log(BasicLevel.DEBUG, ""); 526 String m = null; 527 try{ 528 m = jmsjmx.getConnectionFactoryMode(jndiName); 529 } catch (IllegalStateException se) { 530 } catch (Exception e){ 532 serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e); 533 } 534 return m; 535 } 536 537 542 public Integer getPendingMessages(String jndiName) { 543 serverlog.log(BasicLevel.DEBUG, ""); 544 Integer nb = null; 545 int n; 546 try{ 547 n = jmsjmx.getPendingMessages(jndiName); 548 nb = new Integer (n); 549 } catch (IllegalStateException se) { 550 } catch (Exception e){ 552 serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e); 553 } 554 return nb; 555 } 556 557 562 public Integer getPendingRequests(String jndiName) { 563 if (serverlog.isLoggable(BasicLevel.DEBUG)) { 564 serverlog.log(BasicLevel.DEBUG, ""); 565 } 566 Integer nb = null; 567 int n; 568 try{ 569 n = jmsjmx.getPendingRequests(jndiName); 570 nb = new Integer (n); 571 } catch (IllegalStateException se) { 572 } catch (Exception e){ 574 serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e); 575 } 576 return nb; 577 } 578 579 584 public Integer getSubscriptions(String jndiName) { 585 if (serverlog.isLoggable(BasicLevel.DEBUG)) { 586 serverlog.log(BasicLevel.DEBUG, ""); 587 } 588 Integer nb = null; 589 int n; 590 try{ 591 n = jmsjmx.getSubscriptions(jndiName); 592 nb = new Integer (n); 593 } catch (IllegalStateException se) { 594 } catch (Exception e){ 596 serverlog.log(BasicLevel.ERROR, "Exception when calling monitoring operation " + e); 597 } 598 return nb; 599 } 600 601 public Boolean isMomLocal() { 602 return new Boolean (isMomLocal); 603 } 604 607 public String getUrl() { 608 return url; 609 } 610 611 public String getMom() { 612 return momProviderClassName; 613 } 614 } 615 | Popular Tags |