00001
00027 package org.objectweb.jonas.server;
00028
00029 import java.rmi.RMISecurityManager;
00030 import java.util.Enumeration;
00031 import java.util.HashMap;
00032 import java.util.Iterator;
00033 import java.util.Properties;
00034 import java.util.StringTokenizer;
00035
00036 import javax.management.MBeanServer;
00037 import javax.management.ObjectName;
00038 import javax.management.modelmbean.ModelMBean;
00039 import javax.naming.InitialContext;
00040 import javax.security.jacc.PolicyContext;
00041 import javax.security.jacc.PolicyContextException;
00042 import javax.security.jacc.PolicyContextHandler;
00043
00044 import org.apache.commons.modeler.ManagedBean;
00045 import org.apache.commons.modeler.Registry;
00046 import org.objectweb.carol.util.configuration.CarolConfiguration;
00047 import org.objectweb.carol.util.configuration.CarolCurrentConfiguration;
00048 import org.objectweb.jonas.adm.Adm;
00049 import org.objectweb.jonas.common.JProp;
00050 import org.objectweb.jonas.common.Log;
00051 import org.objectweb.jonas.common.LogManagement;
00052 import org.objectweb.jonas.jmx.J2eeObjectName;
00053 import org.objectweb.jonas.jmx.JmxService;
00054 import org.objectweb.jonas.jmx.JonasObjectName;
00055 import org.objectweb.jonas.jtm.TransactionService;
00056 import org.objectweb.jonas.management.JonasMBeanTools;
00057 import org.objectweb.jonas.management.ReconfigManager;
00058 import org.objectweb.jonas.management.j2eemanagement.J2EEDomain;
00059 import org.objectweb.jonas.naming.JNDIResource;
00060 import org.objectweb.jonas.naming.NamingManager;
00061 import org.objectweb.jonas.security.PolicyProvider;
00062 import org.objectweb.jonas.security.jacc.JPolicyContextHandler;
00063 import org.objectweb.jonas.service.JonasAlreadyStartedException;
00064 import org.objectweb.jonas.service.ServiceException;
00065 import org.objectweb.jonas.service.ServiceManager;
00066 import org.objectweb.jonas_lib.I18n;
00067 import org.objectweb.jonas_lib.deployment.work.WorkCleaner;
00068 import org.objectweb.jonas_lib.version.Version;
00069 import org.objectweb.util.monolog.api.BasicLevel;
00070 import org.objectweb.util.monolog.api.Logger;
00071 import org.objectweb.util.monolog.wrapper.javaLog.MonologFormatter;
00072
00085 public class Server {
00086
00087
00091 private static final String DEF_LOG_CONFIGFILE = "trace";
00092
00096 private static final String DEF_ORB_PORT = "0";
00097
00101 private static I18n i18n = I18n.getInstance(Server.class);
00102
00106 private static final String LOG_CONFIGFILE = "jonas.log.configfile";
00107
00111 private static Logger logger = null;
00112
00116 private static final String ORB_PORT = "jonas.orb.port";
00117
00121 private static final String SECURITY_PROPAGATION = "jonas.security.propagation";
00122
00126 private static final String CSIV2_PROPAGATION = "jonas.csiv2.propagation";
00127
00131 private static final String SECURITY_MANAGER = "jonas.security.manager";
00132
00136 private static final String SERVERNAME = "JOnAS";
00137
00141 private static final int SLEEP_VALUE = 10000;
00142
00146 private static final String TRANSACTION_PROPAGATION = "jonas.transaction.propagation";
00147
00151 private static Server unique = null;
00152
00158 public static Server getInstance() throws Exception {
00159 if (unique == null) {
00160 unique = new Server();
00161 }
00162 return unique;
00163 }
00164
00170 public static void main(String[] args) throws Exception {
00171 Server server = null;
00172
00173
00174
00175 System.setProperty(MonologFormatter.WRAPPERS_PROPERTY, "mx4j.log.CommonsLogger,mx4j.log.Logger");
00176
00177 try {
00178 server = Server.getInstance();
00179 server.start();
00180 } catch (JonasAlreadyStartedException e) {
00181
00182
00183 System.err.println(i18n.getMessage("Server.main.alreadyStarted", SERVERNAME));
00184
00185 throw e;
00186 } catch (Exception e) {
00187
00188 System.err.println("JOnAS error: " + e);
00189 e.printStackTrace(System.err);
00190 System.err.println(SERVERNAME + " halting");
00191 try {
00192 if (server != null) {
00193 server.stop();
00194 }
00195 } catch (Exception he) {
00196 System.err.println("Exception when halting" + he.getMessage());
00197 throw he;
00198 }
00199 throw e;
00200 }
00201 }
00202
00206 private Adm adm = null;
00207
00211 private String domainName = null;
00212
00216 private J2EEServer j2EEServer = null;
00217
00221 private String logConfigFile;
00222
00226 private JProp props = null;
00227
00231 private String serverName = null;
00232
00236 private ServiceManager sm = null;
00237
00242 private HashMap environments = new HashMap();
00243
00248 private int nbCarolProtocols = 0;
00249
00254 private String theProtocol = null;
00255
00260 private Server() throws Exception {
00261
00262 props = JProp.getInstance();
00263
00264
00265 logConfigFile = props.getValue(LOG_CONFIGFILE, DEF_LOG_CONFIGFILE);
00266 Log.configure(logConfigFile);
00267
00268
00269 logger = Log.getLogger(Log.JONAS_SERVER_PREFIX);
00270
00271
00272 sm = ServiceManager.getInstance();
00273
00274
00275 PolicyProvider.init();
00276
00277 }
00278
00279
00280
00285 public J2EEServer getJ2EEServer() {
00286 return j2EEServer;
00287 }
00288
00295 public String initCarol(JProp props) throws Exception {
00296
00297
00298 CarolConfiguration.init(Log.getLoggerFactory());
00299
00300
00301 String sec = props.getValue(SECURITY_PROPAGATION, "false").trim();
00302 String trans = props.getValue(TRANSACTION_PROPAGATION, "false").trim();
00303 String csiv2 = props.getValue(CSIV2_PROPAGATION, "true").trim();
00304
00305 if (sec.equals("true")) {
00306
00307 CarolConfiguration.addInterceptors("jrmp",
00308 "org.objectweb.jonas.security.interceptors.jrmp.SecurityInitializer");
00309
00310 CarolConfiguration.addInterceptors("cmi",
00311 "org.objectweb.jonas.security.interceptors.jrmp.SecurityInitializer");
00312
00313 CarolConfiguration.addInterceptors("iiop",
00314 "org.objectweb.jonas.security.interceptors.iiop.SecurityInitializer");
00315
00316 }
00317
00318 if (csiv2.equals("true")) {
00319 CarolConfiguration.addInterceptors("iiop", "org.objectweb.jonas.security.iiop.Csiv2Initializer");
00320 }
00321
00322
00323 if (trans.equals("true")) {
00324
00325 CarolConfiguration.addInterceptors("jrmp", "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer");
00326
00327 CarolConfiguration.addInterceptors("cmi", "org.objectweb.jotm.jta.rmi.JTAInterceptorInitializer");
00328
00329 CarolConfiguration.addInterceptors("iiop", "org.objectweb.jotm.ots.OTSORBInitializer");
00330 }
00331
00332
00333 System.setProperty("rmi.server.port", props.getValue(ORB_PORT, DEF_ORB_PORT));
00334
00335
00336 CarolCurrentConfiguration carolConfig = CarolCurrentConfiguration.getCurrent();
00337 String s = CarolConfiguration.getProtocols();
00338 StringTokenizer st = new StringTokenizer(s, ",", false);
00339 String pn = " ";
00340
00341 int idx = 0;
00342 while (st.hasMoreElements()) {
00343 String token = st.nextToken().trim();
00344 if (idx != 0) {
00345 pn = pn + ", ";
00346 }
00347 pn = pn + "rmi/" + token;
00348 idx++;
00349
00350
00351 Properties protocolProps = carolConfig.getRMIProperties(token);
00352 if (protocolProps == null) {
00353 throw new Exception("no protocol properties for " + token);
00354 }
00355 Properties environment = new Properties();
00356 for (Enumeration pNames = protocolProps.propertyNames(); pNames.hasMoreElements();) {
00357 String pName = (String) pNames.nextElement();
00358 String pValue = protocolProps.getProperty(pName);
00359 if ((pName != null) && (pValue != null)) {
00360 environment.setProperty(pName, pValue);
00361 }
00362 }
00363 environments.put(token, environment);
00364 theProtocol = token;
00365 }
00366 nbCarolProtocols = idx;
00367 return pn;
00368 }
00369
00374 private void initJACCPolicyContextHandlers() throws PolicyContextException {
00375 PolicyContextHandler policyContextHandler = new JPolicyContextHandler();
00376 String[] keys = policyContextHandler.getKeys();
00377 for (int k = 0; k < keys.length; k++) {
00378 PolicyContext.registerHandler(keys[k], policyContextHandler, true);
00379 }
00380 }
00381
00386 public void kill() throws Exception {
00387 if (adm != null) {
00388 adm.killServer();
00389 }
00390 }
00391
00396 public void start() throws Exception {
00397
00398 serverName = props.getValue(JProp.JONAS_NAME, JProp.JONAS_DEF_NAME);
00399
00400
00401 String protocols = initCarol(props);
00402
00403
00404 boolean useSecurityManager = new Boolean(props.getValue(SECURITY_MANAGER, "true").trim()).booleanValue();
00405
00406 if (useSecurityManager) {
00407 if (System.getSecurityManager() == null) {
00408 System.setSecurityManager(new RMISecurityManager());
00409 }
00410 } else {
00411 logger.log(BasicLevel.INFO, "Security manager is not set.");
00412 }
00413
00414
00415 try {
00416 initJACCPolicyContextHandlers();
00417 } catch (PolicyContextException pce) {
00418 logger.log(BasicLevel.ERROR, "Could not register JOnAS Policy Context Handlers");
00419 throw pce;
00420 }
00421
00422
00423 sm.startRegistry();
00424
00425
00426
00427
00428 sm.startJmx();
00429
00430
00431
00432 WorkCleaner.getInstance().start();
00433
00434
00435 JmxService jmxService = null;
00436 MBeanServer mbeanServer = null;
00437 try {
00438 jmxService = (JmxService) ServiceManager.getInstance().getJmxService();
00439 mbeanServer = jmxService.getJmxServer();
00440 } catch (Exception se) {
00441
00442
00443
00444 logger.log(BasicLevel.ERROR, "Could not access the jmx service");
00445 throw se;
00446 }
00447
00448
00449
00450
00451
00452
00453 Registry oRegistry = JonasMBeanTools.getRegistry();
00454
00455
00456 domainName = props.getValue(JProp.DOMAIN_NAME);
00457
00458
00459 ObjectName domainOn = null;
00460 boolean domainOK = false;
00461 J2EEDomain j2eeDomain = null;
00462 try {
00463
00464 domainOn = J2eeObjectName.J2EEDomain(domainName);
00465 j2eeDomain = new J2EEDomain(domainOn.toString(), false, false, false);
00466 ManagedBean oManaged = oRegistry.findManagedBean("J2EEDomain");
00467 ModelMBean oMBean = oManaged.createMBean(j2eeDomain);
00468 mbeanServer.registerMBean(oMBean, domainOn);
00469 domainOK = true;
00470 } catch (javax.management.InstanceAlreadyExistsException ae) {
00471
00472 logger.log(BasicLevel.DEBUG, ae.getMessage());
00473 } catch (Exception e) {
00474 logger.log(BasicLevel.WARN, "Could not register J2EEDomain MBean");
00475 e.printStackTrace();
00476 domainOK = false;
00477 }
00478
00479
00480 ObjectName serverOn = null;
00481 try {
00482
00483 ReconfigManager reconfigManagerMBean = new ReconfigManager(props.getConfigFileEnv(), mbeanServer);
00484 mbeanServer.registerMBean(reconfigManagerMBean, JonasObjectName.serverConfig());
00485
00486
00487 serverOn = J2eeObjectName.J2EEServer(domainName, serverName);
00488 j2EEServer = new J2EEServer(serverOn.toString(), this, serverName, Version.NUMBER, Version.VENDOR,
00489 protocols);
00490 ManagedBean oManaged = oRegistry.findManagedBean("J2EEServer");
00491 ModelMBean oMBean = oManaged.createMBean(j2EEServer);
00492 mbeanServer.registerMBean(oMBean, serverOn);
00493
00494 j2eeDomain.addServer(serverOn.toString());
00495
00496
00497 LogManagement logMBean = LogManagement.getInstance();
00498 mbeanServer.registerMBean(logMBean, JonasObjectName.logService(logConfigFile));
00499 } catch (Exception e) {
00500
00501 jmxService.stop();
00502 }
00503
00504
00505 ObjectName onJvm = null;
00506 try {
00507 onJvm = J2eeObjectName.JVM(domainName, serverName, serverName);
00508 ManagedBean oManaged = oRegistry.findManagedBean("JVM");
00509 JavaVm oJvm = new JavaVm(onJvm.toString(), props);
00510 ModelMBean oMBean = oManaged.createMBean(oJvm);
00511 mbeanServer.registerMBean(oMBean, onJvm);
00512
00513 j2EEServer.addJavaVM(onJvm.toString());
00514 } catch (Exception e) {
00515 logger.log(BasicLevel.WARN, "Could not create JVM MBean : " + e.getMessage(), e);
00516 }
00517
00518 ObjectName on = null;
00519
00520 ManagedBean oManaged = oRegistry.findManagedBean("JNDIResource");
00521 if (nbCarolProtocols == 1) {
00522
00523 try {
00524 on = J2eeObjectName.JNDIResource(domainName, "jndi", serverName);
00525
00526 NamingManager naming = NamingManager.getInstance();
00527 JNDIResource resMBean = new JNDIResource(on.toString(), naming.getInitialContext(), theProtocol);
00528 ModelMBean oMBean = oManaged.createMBean(resMBean);
00529 mbeanServer.registerMBean(oMBean, on);
00530 } catch (Exception e) {
00531 logger.log(BasicLevel.WARN, "Problem with J2EEResource MBean " + on.toString()
00532 + " creation or registration: " + e.getMessage(), e);
00533 }
00534 } else {
00535
00536 InitialContext protocolContext = null;
00537 for (Iterator it = environments.keySet().iterator(); it.hasNext();) {
00538 String protocol = (String) it.next();
00539 on = J2eeObjectName.JNDIResource(domainName, "jndi_" + protocol, serverName);
00540 protocolContext = new InitialContext((Properties) environments.get(protocol));
00541 JNDIResource resMBean = new JNDIResource(on.toString(), protocolContext, protocol);
00542 ModelMBean oMBean = oManaged.createMBean(resMBean);
00543 mbeanServer.registerMBean(oMBean, on);
00544 }
00545 }
00546
00547
00548 try {
00549 adm = new Adm(props);
00550 } catch (Exception e) {
00551 e.printStackTrace();
00552 throw new Exception("Cannot create Adm : " + e);
00553 }
00554
00555
00556 sm.startServices();
00557
00558
00559 boolean isEJB = true;
00560 try {
00561 sm.getEjbService();
00562 } catch (ServiceException se) {
00563 isEJB = false;
00564 }
00565 adm.serverReady(isEJB);
00566
00567
00568 try {
00569 TransactionService ts = (TransactionService) sm.getTransactionService();
00570 ts.getCurrent().getTransactionRecovery().startResourceManagerRecovery();
00571 } catch (Exception ex) {
00572 logger.log(BasicLevel.DEBUG, "JOTM startResourceManagerRecovery failed: " + ex.getMessage());
00573 ex.printStackTrace();
00574 throw ex;
00575 }
00576
00577 System.out.println(i18n.getMessage("Server.start.serverIsReady", serverName, Version.NUMBER));
00578 logger.log(BasicLevel.INFO, i18n.getMessage("Server.start.serverStarted", serverName, protocols));
00579
00580
00581
00582 while (true) {
00583 Thread.sleep(SLEEP_VALUE);
00584 }
00585 }
00586
00591 public void stop() throws Exception {
00592 if (adm != null) {
00593 System.out.println(i18n.getMessage("Server.stop.serverIsStopped", serverName));
00594 logger.log(BasicLevel.INFO, i18n.getMessage("Server.stop.serverStopped", serverName));
00595 adm.stopServer();
00596 }
00597 }
00598
00599 }