1 26 27 package org.objectweb.jonas.server; 28 29 import java.rmi.RMISecurityManager ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.ObjectName ; 36 import javax.management.modelmbean.ModelMBean ; 37 import javax.management.remote.JMXServiceURL ; 38 import javax.security.jacc.PolicyContext ; 39 import javax.security.jacc.PolicyContextException ; 40 import javax.security.jacc.PolicyContextHandler ; 41 42 import org.apache.commons.modeler.ManagedBean; 43 import org.apache.commons.modeler.Registry; 44 45 import org.objectweb.carol.util.configuration.ConfigurationRepository; 46 import org.objectweb.carol.util.configuration.ProtocolConfiguration; 47 48 import org.objectweb.jonas_lib.I18n; 49 import org.objectweb.jonas_lib.deployment.work.WorkCleaner; 50 import org.objectweb.jonas_lib.version.Version; 51 52 import org.objectweb.jonas.adm.Adm; 53 import org.objectweb.jonas.common.JProp; 54 import org.objectweb.jonas.common.Log; 55 import org.objectweb.jonas.common.LogManagement; 56 import org.objectweb.jonas.jmx.J2eeObjectName; 57 import org.objectweb.jonas.jmx.JmxService; 58 import org.objectweb.jonas.jmx.JonasObjectName; 59 import org.objectweb.jonas.management.JonasMBeanTools; 60 import org.objectweb.jonas.management.ReconfigManager; 61 import org.objectweb.jonas.management.j2eemanagement.J2EEDomain; 62 import org.objectweb.jonas.security.PolicyProvider; 63 import org.objectweb.jonas.security.jacc.JPolicyContextHandler; 64 import org.objectweb.jonas.service.JonasAlreadyStartedException; 65 import org.objectweb.jonas.service.ServiceException; 66 import org.objectweb.jonas.service.ServiceManager; 67 68 import org.objectweb.util.monolog.api.BasicLevel; 69 import org.objectweb.util.monolog.api.Logger; 70 import org.objectweb.util.monolog.wrapper.javaLog.MonologFormatter; 71 72 85 public class Server { 86 87 88 91 private static final String DEF_LOG_CONFIGFILE = "trace"; 92 93 96 private static I18n i18n = I18n.getInstance(Server.class); 97 98 101 private static final String LOG_CONFIGFILE = "jonas.log.configfile"; 102 103 106 private static Logger logger = null; 107 108 111 private static final String SECURITY_PROPAGATION = "jonas.security.propagation"; 112 113 116 private static final String CSIV2_PROPAGATION = "jonas.csiv2.propagation"; 117 118 121 private static final String SECURITY_MANAGER = "jonas.security.manager"; 122 123 126 private static final String SERVERNAME = "JOnAS"; 127 128 131 private static final int SLEEP_VALUE = 10000; 132 133 136 private static final String TRANSACTION_PROPAGATION = "jonas.transaction.propagation"; 137 138 141 private static Server unique = null; 142 143 146 private String idMbeanServer = null; 147 148 151 private MBeanServer mbeanServer = null; 152 153 158 public static Server getInstance() throws Exception { 159 if (unique == null) { 160 unique = new Server(); 161 } 162 return unique; 163 } 164 165 170 public static void main(String [] args) throws Exception { 171 Server server = null; 172 173 System.setProperty(MonologFormatter.WRAPPERS_PROPERTY, "mx4j.log.CommonsLogger,mx4j.log.Logger,org.objectweb.carol.util.configuration.TraceCarol"); 177 178 try { 179 server = Server.getInstance(); 180 server.start(); 181 } catch (JonasAlreadyStartedException e) { 182 System.err.println(i18n.getMessage("Server.main.alreadyStarted", SERVERNAME)); 185 throw e; 187 } catch (Exception e) { 188 System.err.println("JOnAS error: " + e); 190 e.printStackTrace(System.err); 191 System.err.println(SERVERNAME + " halting"); 192 try { 193 if (server != null) { 194 server.stop(); 195 } 196 } catch (Exception he) { 197 System.err.println("Exception when halting" + he.getMessage()); 198 throw he; 199 } 200 throw e; 201 } 202 } 203 204 207 private Adm adm = null; 208 209 212 private String domainName = null; 213 214 217 private J2EEServer j2EEServer = null; 218 219 222 private String logConfigFile; 223 224 227 private JProp props = null; 228 229 232 private String serverName = null; 233 234 237 private ServiceManager sm = null; 238 239 240 244 private Server() throws Exception { 245 props = JProp.getInstance(); 247 248 logConfigFile = props.getValue(LOG_CONFIGFILE, DEF_LOG_CONFIGFILE); 250 Log.configure(logConfigFile); 251 252 logger = Log.getLogger(Log.JONAS_SERVER_PREFIX); 254 255 sm = ServiceManager.getInstance(); 257 258 logger.log(BasicLevel.INFO, jvmInfos()); 260 261 PolicyProvider.init(); 263 264 } 265 266 267 268 272 public J2EEServer getJ2EEServer() { 273 return j2EEServer; 274 } 275 276 280 private void initMBeanServer() throws Exception { 281 282 JonasObjectName.setDomain(domainName); 284 285 boolean existingMbeanServer = false; 286 List mbeanServers = MBeanServerFactory.findMBeanServer(null); 288 if (mbeanServers.size() > 0) { 289 mbeanServer = (MBeanServer ) mbeanServers.get(0); 290 existingMbeanServer = true; 291 } else { 292 mbeanServer = MBeanServerFactory.createMBeanServer(domainName); 294 } 295 296 ObjectName mbeanServerDelegate = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 297 idMbeanServer = (String ) mbeanServer.getAttribute(mbeanServerDelegate, "MBeanServerId"); 298 if (existingMbeanServer) { 299 logger.log(BasicLevel.INFO, "Use an existing MBean server with id '" + idMbeanServer + "'."); 300 } 301 } 302 303 304 311 public String initCarol(JProp props, String serverName) throws Exception { 312 313 ConfigurationRepository.init(domainName, serverName); 315 316 String sec = props.getValue(SECURITY_PROPAGATION, "false").trim(); 318 String trans = props.getValue(TRANSACTION_PROPAGATION, "false").trim(); 319 String csiv2 = props.getValue(CSIV2_PROPAGATION, "true").trim(); 320 321 if (sec.equals("true")) { 322 ConfigurationRepository.addInterceptors("jrmp", 324 "org.objectweb.jonas.security.interceptors.jrmp.SecurityInitializer"); 325 ConfigurationRepository.addInterceptors("cmi", 327 "org.objectweb.jonas.security.interceptors.jrmp.SecurityInitializer"); 328 ConfigurationRepository.addInterceptors("iiop", 330 "org.objectweb.jonas.security.interceptors.iiop.SecurityInitializer"); 331 332 } 333 334 if (csiv2.equals("true")) { 335 ConfigurationRepository.addInterceptors("iiop", "org.objectweb.jonas.security.iiop.Csiv2Initializer"); 336 } 337 338 339 if (trans.equals("true")) { 340 ConfigurationRepository.addInterceptors("jrmp", "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer"); 342 ConfigurationRepository.addInterceptors("cmi", "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer"); 344 ConfigurationRepository.addInterceptors("iiop", "org.objectweb.jotm.ots.OTSORBInitializer"); 346 } 347 348 System.setProperty("carol.server.mode", "true"); 350 351 String protocols = ""; 354 String prefix = "rmi/"; 355 ProtocolConfiguration[] protocolConfigurations = ConfigurationRepository.getConfigurations(); 356 for (int c = 0; c < protocolConfigurations.length; c++) { 357 if (protocols == "") { 358 protocols = prefix + protocolConfigurations[c].getName(); 359 } else { 360 protocols += "," + prefix + protocolConfigurations[c].getName(); 361 } 362 } 363 return protocols; 364 } 365 366 370 private void initJACCPolicyContextHandlers() throws PolicyContextException { 371 PolicyContextHandler policyContextHandler = new JPolicyContextHandler(); 372 String [] keys = policyContextHandler.getKeys(); 373 for (int k = 0; k < keys.length; k++) { 374 PolicyContext.registerHandler(keys[k], policyContextHandler, true); 375 } 376 } 377 378 382 public void kill() throws Exception { 383 if (adm != null) { 384 adm.killServer(); 385 } 386 } 387 388 392 public void start() throws Exception { 393 serverName = props.getValue(JProp.JONAS_NAME, JProp.JONAS_DEF_NAME); 395 396 domainName = props.getValue(JProp.DOMAIN_NAME, JProp.JONAS_DEF_NAME); 398 399 initMBeanServer(); 400 401 String protocols = initCarol(props, serverName); 403 404 boolean useSecurityManager = new Boolean (props.getValue(SECURITY_MANAGER, "true").trim()).booleanValue(); 406 407 if (useSecurityManager) { 408 if (System.getSecurityManager() == null) { 409 System.setSecurityManager(new RMISecurityManager ()); 410 } 411 } else { 412 logger.log(BasicLevel.INFO, "Security manager is not set."); 413 } 414 415 try { 417 initJACCPolicyContextHandlers(); 418 } catch (PolicyContextException pce) { 419 logger.log(BasicLevel.ERROR, "Could not register JOnAS Policy Context Handlers"); 420 throw pce; 421 } 422 423 sm.startRegistry(); 425 426 429 sm.startJmx(idMbeanServer); 430 431 WorkCleaner.getInstance().start(); 433 434 Registry oRegistry = JonasMBeanTools.getRegistry(); 436 437 domainName = props.getValue(JProp.DOMAIN_NAME); 439 440 ObjectName domainOn = null; 442 443 J2EEDomain j2eeDomain = null; 444 try { 445 domainOn = J2eeObjectName.J2EEDomain(domainName); 447 j2eeDomain = new J2EEDomain(domainOn.toString(), false, false, false); 448 ManagedBean oManaged = oRegistry.findManagedBean("J2EEDomain"); 449 ModelMBean oMBean = oManaged.createMBean(j2eeDomain); 450 mbeanServer.registerMBean(oMBean, domainOn); 451 } catch (javax.management.InstanceAlreadyExistsException ae) { 452 if (logger.isLoggable(BasicLevel.DEBUG)) { 454 logger.log(BasicLevel.DEBUG, ae); 455 } 456 } catch (Exception e) { 457 logger.log(BasicLevel.WARN, "Could not register J2EEDomain MBean"); 458 e.printStackTrace(); 459 } 460 461 ObjectName serverOn = null; 463 try { 464 ReconfigManager reconfigManagerMBean = new ReconfigManager(props.getConfigFileEnv(), mbeanServer); 466 mbeanServer.registerMBean(reconfigManagerMBean, JonasObjectName.serverConfig()); 467 468 serverOn = J2eeObjectName.J2EEServer(domainName, serverName); 470 j2EEServer = new J2EEServer(serverOn.toString(), this, serverName, Version.NUMBER, Version.VENDOR, 471 protocols); 472 ManagedBean oManaged = oRegistry.findManagedBean("J2EEServer"); 473 ModelMBean oMBean = oManaged.createMBean(j2EEServer); 474 mbeanServer.registerMBean(oMBean, serverOn); 475 476 try { 478 JmxService jmxService = ((JmxService) sm.getJmxService()); 479 JMXServiceURL [] connectorServerURLs = jmxService.getConnectorServerURLs(); 480 ArrayList urlsList = new ArrayList (); 481 for (int i = 0; i < connectorServerURLs.length; i++) { 482 if (connectorServerURLs[i] != null) { 487 urlsList.add(connectorServerURLs[i].toString()); 488 } 489 } 490 String [] urls = new String [urlsList.size()]; 491 for (int i = 0; i < urls.length; i++) { 492 urls[i] = (String ) urlsList.get(i); 493 } 494 j2eeDomain.addServer(serverOn.toString(), urls); 495 j2eeDomain.setJmxService(jmxService); 496 } catch (ServiceException e) { 497 j2eeDomain.addServer(serverOn.toString(), null); 499 } 500 501 LogManagement logMBean = LogManagement.getInstance(); 503 mbeanServer.registerMBean(logMBean, JonasObjectName.logService(logConfigFile)); 504 } catch (Exception e) { 505 logger.log(BasicLevel.WARN, "Could not create J2EEServer MBean : " + e.getMessage(), e); 506 } 507 508 ObjectName onJvm = null; 510 try { 511 onJvm = J2eeObjectName.JVM(domainName, serverName, serverName); 512 ManagedBean oManaged = oRegistry.findManagedBean("JVM"); 513 JavaVm oJvm = new JavaVm(onJvm.toString(), props); 514 ModelMBean oMBean = oManaged.createMBean(oJvm); 515 mbeanServer.registerMBean(oMBean, onJvm); 516 j2EEServer.addJavaVM(onJvm.toString()); 518 } catch (Exception e) { 519 logger.log(BasicLevel.WARN, "Could not create JVM MBean : " + e.getMessage(), e); 520 } 521 522 try { 524 adm = new Adm(props); 525 } catch (Exception e) { 526 e.printStackTrace(); 527 throw new Exception ("Cannot create Adm : " + e); 528 } 529 530 sm.startServices(); 532 533 boolean isEJB = true; 535 try { 536 sm.getEjbService(); 537 } catch (ServiceException se) { 538 isEJB = false; 539 } 540 adm.serverReady(isEJB); 541 542 System.out.println(i18n.getMessage("Server.start.serverIsReady", serverName, Version.NUMBER)); 543 logger.log(BasicLevel.INFO, i18n.getMessage("Server.start.serverStarted", serverName, protocols)); 544 545 547 while (true) { 548 Thread.sleep(SLEEP_VALUE); 549 } 550 } 551 552 555 public static String jvmInfos() { 556 StringBuffer sb = new StringBuffer (); 557 sb.append("JVM used is "); 558 sb.append(System.getProperty("java.version")); 559 sb.append(" version of "); 560 sb.append(System.getProperty("java.vendor")); 561 sb.append(" vendor on "); 562 sb.append(System.getProperty("os.name")); 563 sb.append(" "); 564 sb.append(System.getProperty("os.version")); 565 sb.append("/"); 566 sb.append(System.getProperty("os.arch")); 567 sb.append(" OS."); 568 return sb.toString(); 569 } 570 571 575 public void stop() throws Exception { 576 if (adm != null) { 577 System.out.println(i18n.getMessage("Server.stop.serverIsStopped", serverName)); 578 logger.log(BasicLevel.INFO, i18n.getMessage("Server.stop.serverStopped", serverName)); 579 adm.stopServer(); 580 } 581 } 582 583 586 public static boolean isStarted() { 587 return unique != null; 588 } 589 } | Popular Tags |