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.test;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.util.Properties;
021: import java.security.PrivilegedAction;
022: import java.security.AccessController;
023:
024: /**
025: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
026: *
027: * @version $Rev: 636618 $ $Date: 2008-03-12 20:20:58 -0700 $
028: */
029: public class TestManager {
030:
031: private static TestServer server;
032: private static TestDatabase database;
033: private static TestJms jms;
034: private static boolean warn = true;
035:
036: // TODO: Move it to a central place where all system properties are managed in a unified way
037: final static String TESTSUITE_PROPERTY_FILENAME = "openejb.testsuite.properties";
038: final static String TEST_SERVER_CLASSNAME = "openejb.test.server";
039: final static String TEST_DATABASE_CLASSNAME = "openejb.test.database";
040: final static String TEST_JMS_CLASSNAME = "openejb.test.jms";
041:
042: public static void init(String propertiesFileName) throws Exception {
043: Properties props = null;
044:
045: try {
046: props = new Properties(System.getProperties());
047: warn = props.getProperty("openejb.test.nowarn") == null;
048: } catch (SecurityException e) {
049: throw new IllegalArgumentException(
050: "Cannot access the system properties: "
051: + e.getClass().getName() + " "
052: + e.getMessage());
053: }
054:
055: if (propertiesFileName == null) {
056: try {
057: propertiesFileName = System
058: .getProperty(TESTSUITE_PROPERTY_FILENAME);
059: if (propertiesFileName != null) {
060: props.putAll(getProperties(propertiesFileName));
061: }
062:
063: } catch (SecurityException e) {
064: throw new IllegalArgumentException(
065: "Cannot access the system property \""
066: + TESTSUITE_PROPERTY_FILENAME + "\": "
067: + e.getClass().getName() + " "
068: + e.getMessage());
069: }
070: } else {
071: props.putAll(getProperties(propertiesFileName));
072: }
073: initServer(props);
074: initDatabase(props);
075: initJms(props);
076: }
077:
078: public static void start() throws Exception {
079: try {
080: if (server != null) {
081: server.start();
082: }
083: } catch (Exception e) {
084: if (warn)
085: System.out
086: .println("Cannot start the test server: "
087: + e.getClass().getName() + " "
088: + e.getMessage());
089: throw e;
090: }
091: try {
092: if (database != null) {
093: database.start();
094: }
095: } catch (Exception e) {
096: if (warn)
097: System.out
098: .println("Cannot start the test database: "
099: + e.getClass().getName() + " "
100: + e.getMessage());
101: throw e;
102: }
103: }
104:
105: public static void stop() throws Exception {
106: try {
107: if (database != null) {
108: database.stop();
109: }
110: } catch (Exception e) {
111: if (warn)
112: System.out
113: .println("Cannot stop the test database: "
114: + e.getClass().getName() + " "
115: + e.getMessage());
116: throw e;
117: }
118: try {
119: if (server != null) {
120: server.stop();
121: }
122: } catch (Exception e) {
123: if (warn)
124: System.out
125: .println("Cannot stop the test server 2: "
126: + e.getClass().getName() + " "
127: + e.getMessage());
128: throw e;
129: }
130: }
131:
132: private static Properties getProperties(String fileName)
133: throws Exception {
134: File file = new File(fileName);
135: file = file.getAbsoluteFile();
136: Properties props = (Properties) System.getProperties().clone();
137: props.load(new FileInputStream(file));
138: return props;
139: }
140:
141: private static ClassLoader getContextClassLoader() {
142: return AccessController
143: .doPrivileged(new PrivilegedAction<ClassLoader>() {
144: public ClassLoader run() {
145: return Thread.currentThread()
146: .getContextClassLoader();
147: }
148: });
149: }
150:
151: private static void initServer(Properties props) {
152: try {
153:
154: String className = props.getProperty(TEST_SERVER_CLASSNAME);
155: if (className == null) {
156: throw new IllegalArgumentException(
157: "Must specify a test server by setting its class name using the system property \""
158: + TEST_SERVER_CLASSNAME + "\"");
159: }
160: ClassLoader cl = getContextClassLoader();
161: Class<?> testServerClass = Class.forName(className, true,
162: cl);
163: server = (TestServer) testServerClass.newInstance();
164: server.init(props);
165: } catch (Exception e) {
166: if (warn)
167: e.printStackTrace();
168: if (warn)
169: System.out
170: .println("Cannot instantiate or initialize the test server: "
171: + e.getClass().getName()
172: + " "
173: + e.getMessage());
174: throw new RuntimeException(
175: "Cannot instantiate or initialize the test server: "
176: + e.getClass().getName() + " "
177: + e.getMessage(), e);
178: }
179: }
180:
181: private static void initDatabase(Properties props) {
182: try {
183: String className = props
184: .getProperty(TEST_DATABASE_CLASSNAME);
185: if (className == null)
186: throw new IllegalArgumentException(
187: "Must specify a test database by setting its class name using the system property \""
188: + TEST_DATABASE_CLASSNAME + "\"");
189: ClassLoader cl = getContextClassLoader();
190: Class<?> testDatabaseClass = Class.forName(className, true,
191: cl);
192: database = (TestDatabase) testDatabaseClass.newInstance();
193: database.init(props);
194: } catch (Exception e) {
195: if (warn)
196: System.out
197: .println("Cannot instantiate or initialize the test database: "
198: + e.getClass().getName()
199: + " "
200: + e.getMessage());
201: throw new RuntimeException(
202: "Cannot instantiate or initialize the test database: "
203: + e.getClass().getName() + " "
204: + e.getMessage(), e);
205: }
206: }
207:
208: private static void initJms(Properties props) {
209: try {
210: String className = props.getProperty(TEST_JMS_CLASSNAME);
211: if (className == null)
212: className = "org.apache.openejb.test.ActiveMqTestJms";
213: ClassLoader cl = getContextClassLoader();
214: Class<?> testJmsClass = Class.forName(className, true, cl);
215: jms = (TestJms) testJmsClass.newInstance();
216: jms.init(props);
217: } catch (Exception e) {
218: if (warn)
219: System.out
220: .println("Cannot instantiate or initialize the test jms: "
221: + e.getClass().getName()
222: + " "
223: + e.getMessage());
224: throw new RuntimeException(
225: "Cannot instantiate or initialize the test jms: "
226: + e.getClass().getName() + " "
227: + e.getMessage(), e);
228: }
229: }
230:
231: public static TestServer getServer() {
232: return server;
233: }
234:
235: public static TestDatabase getDatabase() {
236: return database;
237: }
238:
239: public static TestJms getJms() {
240: return jms;
241: }
242:
243: public static Properties getContextEnvironment() {
244: return server.getContextEnvironment();
245: }
246: }
|