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.DataInputStream;
019: import java.io.File;
020: import java.net.URL;
021: import java.util.Properties;
022:
023: import javax.naming.Context;
024:
025: /**
026: * The Client test suite needs the following environment variables
027: * to be set before it can be run.
028: *
029: * <code>test.home</code>
030: * <code>server.classpath</code>
031: *
032: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
033: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
034: */
035: public class RiTestServer implements TestServer {
036:
037: protected Process server;
038: protected boolean startServerProcess;
039: protected String configFile;
040: protected String serverClassName = " org.apache.openejb.ri.server.Server ";
041: protected String classPath;
042: protected DataInputStream in;
043: protected DataInputStream err;
044: protected String testHomePath;
045: protected File testHome;
046:
047: /**
048: * The environment variable <code>test.home</code> sould be set
049: * to the base directory where the test suite is located.
050: */
051: public static final String TEST_HOME = "test.home";
052: public static final String SERVER_CLASSPATH = "server.classpath";
053: public static final String SERVER_CONFIG = "test.server.config";
054: public static final String START_SERVER_PROCESS = "test.start.server.process";
055: public static final String BAD_ENVIRONMENT_ERROR = "The following environment variables must be set before running the test suite:\n";
056:
057: static {
058: System.setProperty("noBanner", "true");
059: }
060:
061: public RiTestServer() {
062: }
063:
064: public void init(Properties props) {
065: try {
066: /* [DMB] Temporary fix */
067: try {
068: System.setSecurityManager(new TestSecurityManager());
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: /* [DMB] Temporary fix */
073:
074: String tmp = props
075: .getProperty(START_SERVER_PROCESS, "true").trim();
076: startServerProcess = "true".equalsIgnoreCase(tmp);
077:
078: /* If we will not be starting process for the
079: * server than we don't need to read in the other
080: * properties
081: */
082: if (!startServerProcess)
083: return;
084:
085: testHomePath = props.getProperty(TEST_HOME);
086: classPath = props.getProperty(SERVER_CLASSPATH);
087: configFile = props.getProperty(SERVER_CONFIG);
088:
089: checkEnvironment();
090:
091: testHome = new File(testHomePath);
092: testHome = testHome.getAbsoluteFile();
093: testHomePath = testHome.getAbsolutePath();
094:
095: prepareServerClasspath();
096: } catch (Exception e) {
097: e.printStackTrace();
098: System.exit(-1);
099: }
100: }
101:
102: public void destroy() {
103:
104: }
105:
106: /**
107: * Starts and Ri Server with the configuration file from
108: * the properties used to create this RiTestServer.
109: *
110: * @param confFileName
111: */
112: public void start() {
113:
114: if (!startServerProcess)
115: return;
116:
117: String command = "java -classpath " + classPath + " "
118: + serverClassName + " " + configFile;
119: try {
120: server = Runtime.getRuntime().exec(command);
121: in = new DataInputStream(server.getInputStream());
122: err = new DataInputStream(server.getErrorStream());
123: while (true) {
124: try {
125: String line = in.readLine();
126: System.out.println(line);
127: if (line == null
128: || "[RI Server] Ready!".equals(line))
129: break;
130:
131: } catch (Exception e) {
132: break;
133: }
134: }
135:
136: Thread t = new Thread(new Runnable() {
137: public void run() {
138: while (true) {
139: try {
140: String line = in.readLine();
141: if (line == null)
142: break;
143: System.out.println(line);
144: } catch (Exception e) {
145: break;
146: }
147: }
148:
149: }
150: });
151: t.start();
152: Thread t2 = new Thread(new Runnable() {
153: public void run() {
154: while (true) {
155: try {
156: String line = err.readLine();
157: if (line == null)
158: break;
159: // System.out.println(line);
160: } catch (Exception e) {
161: break;
162: }
163: }
164:
165: }
166: });
167: t2.start();
168: } catch (Exception e) {
169: e.printStackTrace();
170: }
171: }
172:
173: public void stop() {
174: if (!startServerProcess)
175: return;
176:
177: if (server != null)
178: server.destroy();
179: server = null;
180: try {
181: in.close();
182: err.close();
183: } catch (Exception e) {
184: }
185: }
186:
187: public Properties getContextEnvironment() {
188: Properties properties = new Properties();
189: properties.put(Context.INITIAL_CONTEXT_FACTORY,
190: "org.apache.openejb.ri.server.RiInitCtxFactory");
191:
192: try {
193: properties.put(Context.PROVIDER_URL, new URL("http",
194: "127.0.0.1", 1098, ""));
195: } catch (Exception e) {
196: }
197:
198: //properties.put(Context.SECURITY_PRINCIPAL, "STATEFUL_TEST_CLIENT");
199: //properties.put(Context.SECURITY_CREDENTIALS, "STATEFUL_TEST_CLIENT");
200:
201: return properties;
202: }
203:
204: //==========================================
205: // Methods supporting this implementation
206: // of the TestServer interface
207: //
208: private String getConfFilePath(String confFileName) {
209: String str = getConfFile(confFileName).getAbsolutePath();
210: return str;
211: }
212:
213: private File getConfFile(String confFileName) {
214: return new File(testHome, confFileName);
215: }
216:
217: private void checkEnvironment() {
218:
219: if (testHomePath == null || classPath == null
220: || configFile == null) {
221: String error = BAD_ENVIRONMENT_ERROR;
222: error += (testHomePath == null) ? TEST_HOME + "\n" : "";
223: error += (classPath == null) ? SERVER_CLASSPATH + "\n" : "";
224: error += (configFile == null) ? SERVER_CONFIG + "\n" : "";
225: throw new RuntimeException(error);
226: }
227: }
228:
229: private void prepareServerClasspath() {
230: char PS = File.pathSeparatorChar;
231: char FS = File.separatorChar;
232:
233: String javaTools = System.getProperty("java.home") + FS + "lib"
234: + FS + "tools.jar";
235: classPath = classPath.replace('/', FS);
236: classPath = classPath.replace(':', PS);
237: classPath += PS + javaTools;
238: }
239: //
240: // Methods supporting this implementation
241: // of the TestServer interface
242: //==========================================
243:
244: }
|