001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb;
017:
018: import java.util.Date;
019: import java.util.Properties;
020:
021: import javax.transaction.TransactionManager;
022:
023: import org.apache.openejb.loader.SystemInstance;
024: import org.apache.openejb.spi.ApplicationServer;
025: import org.apache.openejb.spi.Assembler;
026: import org.apache.openejb.spi.ContainerSystem;
027: import org.apache.openejb.spi.SecurityService;
028: import org.apache.openejb.util.LogCategory;
029: import org.apache.openejb.util.Logger;
030: import org.apache.openejb.util.Messages;
031: import org.apache.openejb.util.OpenEjbVersion;
032: import org.apache.openejb.util.SafeToolkit;
033:
034: /**
035: * @version $Rev: 607045 $ $Date: 2007-12-27 04:10:23 -0800 $
036: */
037: public final class OpenEJB {
038:
039: private static Instance instance;
040:
041: public static ApplicationServer getApplicationServer() {
042: return SystemInstance.get().getComponent(
043: ApplicationServer.class);
044: }
045:
046: public static TransactionManager getTransactionManager() {
047: return SystemInstance.get().getComponent(
048: TransactionManager.class);
049: }
050:
051: public static class Instance {
052: private static Messages messages = new Messages(
053: "org.apache.openejb.util.resources");
054: private final Throwable initialized;
055:
056: /**
057: * 1 usage
058: * org.apache.openejb.core.ivm.naming.InitContextFactory
059: */
060: public Instance(Properties props) throws OpenEJBException {
061: this (props, null);
062: }
063:
064: /**
065: * 2 usages
066: */
067: public Instance(Properties initProps,
068: ApplicationServer appServer) throws OpenEJBException {
069: initialized = new Exception("Initialized at " + new Date())
070: .fillInStackTrace();
071:
072: Logger logger = Logger.getInstance(
073: LogCategory.OPENEJB_STARTUP,
074: "org.apache.openejb.util.resources");
075:
076: try {
077: SystemInstance.init(initProps);
078: } catch (Exception e) {
079: throw new OpenEJBException(e);
080: }
081: SystemInstance system = SystemInstance.get();
082:
083: SafeToolkit toolkit = SafeToolkit.getToolkit("OpenEJB");
084:
085: if (appServer == null) {
086: ApplicationServer defaultServer = (ApplicationServer) toolkit
087: .newInstance("org.apache.openejb.core.ServerFederation");
088: appServer = defaultServer;
089: }
090: system.setComponent(ApplicationServer.class, appServer);
091:
092: /*
093: * Output startup message
094: */
095: OpenEjbVersion versionInfo = OpenEjbVersion.get();
096:
097: if (initProps.getProperty("openejb.nobanner") == null) {
098: System.out.println("Apache OpenEJB "
099: + versionInfo.getVersion() + " build: "
100: + versionInfo.getDate() + "-"
101: + versionInfo.getTime());
102: System.out.println("" + versionInfo.getUrl());
103: }
104:
105: Logger logger2 = Logger.getInstance(LogCategory.OPENEJB,
106: "org.apache.openejb.util.resources");
107: logger2.info("startup.banner", versionInfo.getUrl(),
108: new Date(), versionInfo.getCopyright(), versionInfo
109: .getVersion(), versionInfo.getDate(),
110: versionInfo.getTime());
111:
112: logger.info("openejb.home = "
113: + SystemInstance.get().getHome().getDirectory()
114: .getAbsolutePath());
115: logger.info("openejb.base = "
116: + SystemInstance.get().getBase().getDirectory()
117: .getAbsolutePath());
118:
119: Properties props = new Properties(SystemInstance.get()
120: .getProperties());
121:
122: if (initProps == null) {
123: logger.debug("startup.noInitializationProperties");
124: } else {
125: props.putAll(initProps);
126: }
127:
128: /* Uses the EnvProps.ASSEMBLER property to obtain the Assembler impl.
129: Default is org.apache.openejb.assembler.classic.Assembler */
130: String className = props.getProperty(EnvProps.ASSEMBLER);
131: if (className == null) {
132: className = props
133: .getProperty("openejb.assembler",
134: "org.apache.openejb.assembler.classic.Assembler");
135: } else {
136: logger.warning("startup.deprecatedPropertyName",
137: EnvProps.ASSEMBLER);
138: }
139:
140: logger.debug("startup.instantiatingAssemblerClass",
141: className);
142: Assembler assembler = null;
143:
144: try {
145: assembler = (Assembler) toolkit.newInstance(className);
146: } catch (OpenEJBException oe) {
147: logger.fatal("startup.assemblerCannotBeInstantiated",
148: oe);
149: throw oe;
150: } catch (Throwable t) {
151: String msg = messages
152: .message("startup.openejbEncounteredUnexpectedError");
153: logger.fatal(msg, t);
154: throw new OpenEJBException(msg, t);
155: }
156:
157: SystemInstance.get().setComponent(Assembler.class,
158: assembler);
159:
160: try {
161: assembler.init(props);
162: } catch (OpenEJBException oe) {
163: logger.fatal("startup.assemblerFailedToInitialize", oe);
164: throw oe;
165: } catch (Throwable t) {
166: String msg = messages
167: .message("startup.assemblerEncounteredUnexpectedError");
168: logger.fatal(msg, t);
169: throw new OpenEJBException(msg, t);
170: }
171:
172: try {
173: assembler.build();
174: } catch (OpenEJBException oe) {
175: logger.fatal("startup.assemblerFailedToBuild", oe);
176: throw oe;
177: } catch (Throwable t) {
178: String msg = messages
179: .message("startup.assemblerEncounterUnexpectedBuildError");
180: logger.fatal(msg, t);
181: throw new OpenEJBException(msg, t);
182: }
183:
184: ContainerSystem containerSystem = assembler
185: .getContainerSystem();
186:
187: if (containerSystem == null) {
188: String msg = messages
189: .message("startup.assemblerReturnedNullContainer");
190: logger.fatal(msg);
191: throw new OpenEJBException(msg);
192: }
193:
194: system.setComponent(ContainerSystem.class, containerSystem);
195:
196: if (logger.isDebugEnabled()) {
197: logger.debug("startup.debugContainers", containerSystem
198: .containers().length);
199:
200: if (containerSystem.containers().length > 0) {
201: Container[] c = containerSystem.containers();
202: logger.debug("startup.debugContainersType");
203: for (int i = 0; i < c.length; i++) {
204: String entry = " ";
205: switch (c[i].getContainerType()) {
206: case BMP_ENTITY:
207: entry += "BMP ENTITY ";
208: break;
209: case CMP_ENTITY:
210: entry += "CMP ENTITY ";
211: break;
212: case STATEFUL:
213: entry += "STATEFUL ";
214: break;
215: case STATELESS:
216: entry += "STATELESS ";
217: break;
218: case MESSAGE_DRIVEN:
219: entry += "MESSAGE ";
220: break;
221: }
222: entry += c[i].getContainerID();
223: logger.debug("startup.debugEntry", entry);
224: }
225: }
226:
227: logger.debug("startup.debugDeployments",
228: containerSystem.deployments().length);
229: if (containerSystem.deployments().length > 0) {
230: logger.debug("startup.debugDeploymentsType");
231: DeploymentInfo[] d = containerSystem.deployments();
232: for (int i = 0; i < d.length; i++) {
233: String entry = " ";
234: switch (d[i].getComponentType()) {
235: case BMP_ENTITY:
236: entry += "BMP_ENTITY ";
237: break;
238: case CMP_ENTITY:
239: entry += "CMP_ENTITY ";
240: break;
241: case STATEFUL:
242: entry += "STATEFUL ";
243: break;
244: case STATELESS:
245: entry += "STATELESS ";
246: break;
247: case MESSAGE_DRIVEN:
248: entry += "MESSAGE ";
249: break;
250: }
251: entry += d[i].getDeploymentID();
252: logger.debug("startup.debugEntry", entry);
253: }
254: }
255: }
256:
257: SecurityService securityService = assembler
258: .getSecurityService();
259: if (securityService == null) {
260: String msg = messages
261: .message("startup.assemblerReturnedNullSecurityService");
262: logger.fatal(msg);
263: throw new OpenEJBException(msg);
264: } else {
265: logger.debug("startup.securityService", securityService
266: .getClass().getName());
267: }
268: system.setComponent(SecurityService.class, securityService);
269:
270: TransactionManager transactionManager = assembler
271: .getTransactionManager();
272: if (transactionManager == null) {
273: String msg = messages
274: .message("startup.assemblerReturnedNullTransactionManager");
275: logger.fatal(msg);
276: throw new OpenEJBException(msg);
277: } else {
278: logger.debug("startup.transactionManager",
279: transactionManager.getClass().getName());
280: }
281: SystemInstance.get().setComponent(TransactionManager.class,
282: transactionManager);
283:
284: logger.info("startup.ready");
285:
286: String loader = initProps.getProperty("openejb.loader");
287: String nobanner = initProps.getProperty("openejb.nobanner");
288: if (nobanner == null
289: && (loader == null || (loader != null && loader
290: .startsWith("tomcat")))) {
291: System.out.println(messages.message("startup.ready"));
292: }
293: }
294:
295: public Throwable getInitialized() {
296: return initialized;
297: }
298: }
299:
300: public static void destroy() {
301: instance = null;
302: SystemInstance.get().removeComponent(ContainerSystem.class);
303: }
304:
305: /**
306: * 1 usage
307: * org.apache.openejb.core.ivm.naming.InitContextFactory
308: */
309: public static void init(Properties props) throws OpenEJBException {
310: init(props, null);
311: }
312:
313: private static Messages messages = new Messages(
314: "org.apache.openejb.util.resources");
315: private static Logger logger = Logger.getInstance(
316: LogCategory.OPENEJB_STARTUP,
317: "org.apache.openejb.util.resources");
318:
319: /**
320: * 2 usages
321: */
322: public static void init(Properties initProps,
323: ApplicationServer appServer) throws OpenEJBException {
324: if (isInitialized()) {
325: if (instance != null) {
326: String msg = messages
327: .message("startup.alreadyInitialized");
328: logger.error(msg, instance.initialized);
329: throw new OpenEJBException(msg, instance.initialized);
330: } else {
331: String msg = messages
332: .message("startup.alreadyInitialized");
333: logger.error(msg);
334: throw new OpenEJBException(msg);
335: }
336: } else {
337: instance = new Instance(initProps, appServer);
338: }
339: }
340:
341: /**
342: * 1 usages
343: */
344: public static boolean isInitialized() {
345: return instance != null
346: || SystemInstance.get().getComponent(
347: ContainerSystem.class) != null;
348: }
349: }
|