1 25 26 27 package org.objectweb.jonas.service; 28 29 import java.util.ArrayList ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.List ; 33 import java.util.Map ; 34 35 import javax.naming.Context ; 36 import javax.naming.NamingException ; 37 38 import org.objectweb.jonas.common.JProp; 39 import org.objectweb.jonas.common.Log; 40 import org.objectweb.jonas.naming.CompNamingContext; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 45 64 65 public class ServiceManager { 66 67 70 static final String SERVICES_PROP_NAME = JProp.JONASPREFIX + ".services"; 71 72 75 static final String PREFIX_SERVICE_PROP_NAME = JProp.JONASPREFIX + ".service"; 76 77 80 private static Logger logger = null; 81 82 85 private static ServiceManager unique = null; 86 87 90 private JProp props = null; 91 92 95 private static List services = null; 96 97 100 private Map servicesByName = null; 101 102 105 private Map contextsByService = null; 106 107 111 private ServiceManager() throws Exception { 112 logger = Log.getLogger(Log.JONAS_SERVER_PREFIX); 113 props = JProp.getInstance(); 115 readServices(); 117 } 118 119 125 public static ServiceManager getInstance() throws Exception { 126 if (unique == null) { 127 unique = new ServiceManager(); 128 } 129 return unique; 130 } 131 132 138 public Service getService(String name) throws ServiceException { 139 if (servicesByName == null) { 140 throw new ServiceException("No service has been initialized yet"); 141 } 142 Service s = (Service) servicesByName.get(name); 143 if (s == null) { 144 throw new ServiceException("Unknown service '" + name + "'"); 145 } 146 return s; 147 } 148 149 154 public Service getEjbService() throws ServiceException { 155 return getService("ejb"); 156 } 157 158 163 public Service getEarService() throws ServiceException { 164 return getService("ear"); 165 } 166 167 172 public Service getRarService() throws ServiceException { 173 return getService("resource"); 174 } 175 176 181 public Service getWebContainerService() throws ServiceException { 182 return getService("web"); 183 } 184 185 190 public Service getWebServicesService() throws ServiceException { 191 return getService("ws"); 192 } 193 194 199 public Service getMailService() throws ServiceException { 200 return getService("mail"); 201 } 202 203 208 public Service getDataBaseService() throws ServiceException { 209 return getService("dbm"); 210 } 211 212 217 public Service getTransactionService() throws ServiceException { 218 return getService("jtm"); 219 } 220 221 226 public Service getJmsService() throws ServiceException { 227 return getService("jms"); 228 } 229 230 235 public Service getSecurityService() throws ServiceException { 236 return getService("security"); 237 } 238 239 244 public Service getJmxService() throws ServiceException { 245 return getService("jmx"); 246 } 247 248 253 public Service getDiscoveryService() throws ServiceException { 254 return getService("discovery"); 255 } 256 257 262 public Service getRegistryService() throws ServiceException { 263 return getService("registry"); 264 } 265 266 271 public Service getResourceService() throws ServiceException { 272 return getService("resource"); 273 } 274 275 279 public void startRegistry() throws ServiceException { 280 if (logger.isLoggable(BasicLevel.DEBUG)) { 281 logger.log(BasicLevel.DEBUG, ""); 282 } 283 Service reg = getRegistryService(); 284 try { 285 reg.init((Context ) contextsByService.get(reg)); 286 reg.start(); 287 logger.log(BasicLevel.INFO, "registry service started"); 288 } catch (ServiceException e) { 289 throw new ServiceException("Cannot init/start registry" + e); 290 } 291 } 292 293 297 public void startJmx(String idMBeanServer) throws ServiceException { 298 if (logger.isLoggable(BasicLevel.DEBUG)) { 299 logger.log(BasicLevel.DEBUG, ""); 300 } 301 Service jmxService = getJmxService(); 302 try { 303 Context paramCtx = (Context ) contextsByService.get(jmxService); 304 if (idMBeanServer != null) { 305 try { 306 paramCtx.bind("idMBeanServer", idMBeanServer); 307 } catch (NamingException ne) { 308 throw new ServiceException("Cannot bind idMBeanServer = '" + idMBeanServer + "' for JMX service.", ne); 309 } 310 } 311 jmxService.init(paramCtx); 312 jmxService.start(); 313 logger.log(BasicLevel.INFO, "jmx service started"); 314 } catch (ServiceException e) { 315 throw e; 316 } 317 } 318 319 323 public void startServices() throws ServiceException { 324 if (logger.isLoggable(BasicLevel.DEBUG)) { 325 logger.log(BasicLevel.DEBUG, ""); 326 } 327 Service[] services = getServices(); 328 Context serviceContext = null; 329 for (int i = 2; i < services.length; i++) { 332 Service service = services[i]; 333 try { 334 serviceContext = (Context ) contextsByService.get(service); 335 service.init(serviceContext); 336 service.start(); 337 logger.log(BasicLevel.INFO, service.getName() + " service started"); 338 } catch (ServiceException e) { 339 throw new ServiceException("Cannot init/start service '" + service.getName() + "': " + e.getMessage(), e); 340 } 341 } 342 } 343 344 349 public Service[] getServices() throws ServiceException { 350 Service[] ss = new Service[services.size()]; 351 for (int i = 0; i < services.size(); i++) { 352 ss[i] = (Service) services.get(i); 353 } 354 return ss; 355 } 356 357 361 public String [] getServiceNames() { 362 if (logger.isLoggable(BasicLevel.DEBUG)) { 363 logger.log(BasicLevel.DEBUG, ""); 364 } 365 String [] serviceNames; 366 serviceNames = props.getValueAsArray(SERVICES_PROP_NAME); 367 if (serviceNames == null) { 368 return null; 369 } 370 if (serviceNames[0].equals("registry") && serviceNames[1].equals("jmx")) { 371 return serviceNames; 372 } 373 ArrayList services = new ArrayList (); 374 services.add(0, "registry"); 375 services.add(1, "jmx"); 376 int nbServices = 2; 377 for (int i = 0; i < serviceNames.length; i++) { 378 if (!serviceNames[i].equals("registry") && !serviceNames[i].equals("jmx")) { 379 services.add(serviceNames[i]); 380 nbServices++; 381 } 382 } 383 serviceNames = new String [nbServices]; 384 if (logger.isLoggable(BasicLevel.DEBUG)) { 385 logger.log(BasicLevel.DEBUG, "Created new array of String of size " + services.size()); 386 } 387 for (int i = 0; i < nbServices; i++) { 388 serviceNames[i] = (String ) services.get(i); 389 } 390 return serviceNames; 391 } 392 393 398 protected void readServices() throws ServiceException { 399 if (logger.isLoggable(BasicLevel.DEBUG)) { 400 logger.log(BasicLevel.DEBUG, ""); 401 } 402 services = new ArrayList (); 403 contextsByService = new Hashtable (); 404 servicesByName = new Hashtable (); 405 String [] serviceNames; 406 serviceNames = getServiceNames(); 407 if (serviceNames == null) { 408 throw new ServiceException("Property '" + SERVICES_PROP_NAME + "' is missing in '" 409 + JProp.JONASPREFIX + "' properties file"); 410 } 411 for (int i = 0; i < serviceNames.length; i++) { 412 String serviceName = serviceNames[i]; 413 Service serviceObj = createServiceFrom(serviceName); 414 if (serviceObj != null) { 415 try { 416 Context ctx = createServiceContextFor(serviceName); 417 services.add(serviceObj); 418 contextsByService.put(serviceObj, ctx); 419 servicesByName.put(serviceName, serviceObj); 420 } catch (NamingException e) { 421 throw new ServiceException("cannot create the context name of the service '" 422 + serviceName + "'", 423 e); 424 } 425 } 426 } 427 } 428 429 436 protected Service createServiceFrom(String serviceName) throws ServiceException { 437 if (logger.isLoggable(BasicLevel.DEBUG)) { 438 logger.log(BasicLevel.DEBUG, serviceName); 439 } 440 String prefixPropName = PREFIX_SERVICE_PROP_NAME + "." + serviceName; 441 String serviceClassName = props.getValue(prefixPropName + ".class"); 442 if (serviceClassName == null) { 443 throw new ServiceException("Property '" + prefixPropName + ".class' missing in '" 444 + JProp.JONASPREFIX + "' properties file"); 445 } 446 try { 447 ClassLoader classLoader = this.getClass().getClassLoader(); 448 if (classLoader == null) { 449 classLoader = Thread.currentThread().getContextClassLoader(); 450 } 451 Class serviceClass = classLoader.loadClass(serviceClassName); 452 Service service = (Service) serviceClass.newInstance(); 453 service.setName(serviceName); 454 if (logger.isLoggable(BasicLevel.DEBUG)) { 455 logger.log(BasicLevel.DEBUG, "class used: " + serviceClassName); 456 } 457 return (service); 458 } catch (NoClassDefFoundError ncdfe) { 459 logger.log(BasicLevel.WARN, "WARNING : The service '" + serviceName 460 + "' is disabled because a class for this service is missing." 461 + " Check your services in jonas.properties file and your environment variables. Missing class : '" 462 + ncdfe.getMessage() + "'", ncdfe); 463 return null; 465 } catch (ClassNotFoundException cnfe) { 466 logger.log(BasicLevel.WARN, "WARNING : The service '" + serviceName 467 + "' is disabled because a class for this service is missing." 468 + " Check your services in jonas.properties file and your environment variables. Missing class : '" 469 + cnfe.getMessage() + "'", cnfe); 470 return null; 472 } catch (Exception e) { 473 throw new ServiceException("Error when creating the service '" + serviceName + "'", e); 474 } 475 } 476 477 485 protected Context createServiceContextFor(String serviceName) throws NamingException { 486 if (logger.isLoggable(BasicLevel.DEBUG)) { 487 logger.log(BasicLevel.DEBUG, serviceName); 488 } 489 String prefixPropName = PREFIX_SERVICE_PROP_NAME + "." + serviceName; 490 CompNamingContext ctx = new CompNamingContext(serviceName); 491 for (Enumeration e = props.getEnv().propertyNames(); e.hasMoreElements();) { 492 String propName = (String ) e.nextElement(); 493 if (propName.startsWith(prefixPropName + ".")) { 494 ctx.rebind(propName, props.getValue(propName)); 495 } 496 } 497 ctx.rebind(JProp.JONAS_NAME, props.getValue(JProp.JONAS_NAME)); 499 ctx.rebind(JProp.DOMAIN_NAME, props.getValue(JProp.DOMAIN_NAME)); 500 if (serviceName.equals("jmx")) { 503 String prop = props.getValue("host.name"); 504 if (prop != null) { 505 ctx.rebind("host.name", prop); 506 } 507 prop = props.getValue("jmxconnector.port"); 508 if (prop != null) { 509 ctx.rebind("jmxconnector.port", prop); 510 } 511 } 512 return ctx; 513 } 514 515 522 public void stopServices() throws ServiceException { 523 String msgError = new String (); 524 String sepError = ""; 525 Service[] services = getServices(); 526 for (int i = services.length - 1; i >= 0; i--) { 527 Service service = services[i]; 528 if (service.isStarted()) { 529 try { 530 service.stop(); 531 if (logger.isLoggable(BasicLevel.DEBUG)) { 532 logger.log(BasicLevel.DEBUG, service.getName() + " service stopped"); 533 } 534 } catch (ServiceException e) { 535 msgError = msgError.concat(sepError + "Cannot stop the service '" 536 + service.getName() + "': " + e); 537 sepError = "\n"; 538 } 539 } 540 } 541 if (msgError.length() != 0) { 542 throw new ServiceException(msgError); 543 } 544 } 545 } 546 | Popular Tags |