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.server;
017:
018: import org.apache.openejb.util.Connect;
019:
020: import java.io.File;
021:
022: import java.io.InputStream;
023:
024: import java.io.OutputStream;
025:
026: import java.lang.reflect.Method;
027:
028: import java.util.ArrayList;
029:
030: import java.util.Iterator;
031:
032: import java.util.Map;
033:
034: import java.util.Set;
035:
036: public class Start {
037:
038: public static void main(String[] args) {
039:
040: // System.exit(new Start().start()?0:1);
041:
042: new Start().start();
043:
044: }
045:
046: public boolean start() {
047:
048: if (!connect()) {
049:
050: forkServerProcess();
051:
052: return Connect.connect(10, "localhost", 4201);
053:
054: } else {
055:
056: System.out.println(":: server already started ::");
057:
058: return true;
059:
060: }
061:
062: }
063:
064: private void forkServerProcess() {
065:
066: try {
067:
068: ArrayList cmd = new ArrayList();
069:
070: String s = java.io.File.separator;
071:
072: String java = System.getProperty("java.home") + s + "bin"
073: + s + "java";
074:
075: cmd.add(java);
076:
077: addSystemProperties(cmd);
078:
079: cmd.add("-classpath");
080:
081: cmd.add(getClasspath());
082:
083: cmd.add("org.apache.openejb.server.Main");
084:
085: String[] command = (String[]) cmd.toArray(new String[0]);
086:
087: Runtime runtime = Runtime.getRuntime();
088:
089: Process server = runtime.exec(command);
090:
091: InputStream out = server.getInputStream();
092:
093: Thread serverOut = new Thread(new Pipe(out, System.out));
094:
095: serverOut.setDaemon(true);
096:
097: serverOut.start();
098:
099: InputStream err = server.getErrorStream();
100:
101: Thread serverErr = new Thread(new Pipe(err, System.err));
102:
103: serverErr.setDaemon(true);
104:
105: serverErr.start();
106:
107: } catch (Exception e) {
108:
109: throw new RuntimeException("Cannot start the server.");
110:
111: }
112:
113: }
114:
115: private void addSystemProperties(ArrayList cmd) {
116:
117: Set set = System.getProperties().entrySet();
118:
119: for (Iterator iter = set.iterator(); iter.hasNext();) {
120:
121: Map.Entry entry = (Map.Entry) iter.next();
122:
123: String key = (String) entry.getKey();
124:
125: String value = (String) entry.getValue();
126:
127: if (key.matches("^-X.*")) {
128:
129: cmd.add(key + value);
130:
131: } else if (!key
132: .matches("^(java|javax|os|sun|user|file|awt|line|path)\\..*")) {
133:
134: cmd.add("-D" + key + "=" + value);
135:
136: }
137:
138: }
139:
140: }
141:
142: private String getClasspath() {
143:
144: String classpath = System.getProperty("java.class.path");
145:
146: ClassLoader cl = Thread.currentThread().getContextClassLoader();
147:
148: String antLoader = "org.apache.tools.ant.AntClassLoader";
149:
150: if (cl.getClass().getName().equals(antLoader)) {
151:
152: try {
153:
154: Class ant = cl.getClass();
155:
156: Method getClasspath = ant.getMethod("getClasspath",
157: new Class[0]);
158:
159: classpath += File.pathSeparator
160: + getClasspath.invoke(cl, new Object[0]);
161:
162: } catch (Exception e) {
163:
164: e.printStackTrace();
165:
166: }
167:
168: }
169:
170: return classpath;
171:
172: }
173:
174: public static boolean connect() {
175:
176: return Connect.connect(1, "localhost", 4201);
177:
178: }
179:
180: private static final class Pipe implements Runnable {
181:
182: private final InputStream is;
183:
184: private final OutputStream out;
185:
186: private Pipe(InputStream is, OutputStream out) {
187:
188: super ();
189:
190: this .is = is;
191:
192: this .out = out;
193:
194: }
195:
196: public void run() {
197:
198: try {
199:
200: int i = is.read();
201:
202: out.write(i);
203:
204: while (i != -1) {
205:
206: i = is.read();
207:
208: out.write(i);
209:
210: }
211:
212: } catch (Exception e) {
213:
214: e.printStackTrace();
215:
216: }
217:
218: }
219:
220: }
221:
222: }
|