Home » openejb-3.1.2-src » org.apache » openejb » server » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.server;
   18   
   19   import org.apache.openejb.util.Connect;
   20   
   21   import java.io.File;
   22   
   23   import java.io.InputStream;
   24   
   25   import java.io.OutputStream;
   26   
   27   import java.lang.reflect.Method;
   28   
   29   import java.util.ArrayList;
   30   
   31   import java.util.Iterator;
   32   
   33   import java.util.Map;
   34   
   35   import java.util.Set;
   36   
   37   public class Start {
   38   
   39       public static void main(String[] args) {
   40   
   41   //        System.exit(new Start().start()?0:1);
   42   
   43           new Start().start();
   44   
   45       }
   46   
   47       public boolean start() {
   48   
   49           if (!connect()) {
   50   
   51               forkServerProcess();
   52   
   53               return Connect.connect(10, "localhost", 4201);
   54   
   55           } else {
   56   
   57               System.out.println(":: server already started ::");
   58   
   59               return true;
   60   
   61           }
   62   
   63       }
   64   
   65       private void forkServerProcess() {
   66   
   67           try {
   68   
   69               ArrayList cmd = new ArrayList();
   70   
   71               String s = java.io.File.separator;
   72   
   73               String java = System.getProperty("java.home") + s + "bin" + s + "java";
   74   
   75               cmd.add(java);
   76   
   77               addSystemProperties(cmd);
   78   
   79               cmd.add("-classpath");
   80   
   81               cmd.add(getClasspath());
   82   
   83               cmd.add("org.apache.openejb.server.Main");
   84   
   85               String[] command = (String[]) cmd.toArray(new String[0]);
   86   
   87               Runtime runtime = Runtime.getRuntime();
   88   
   89               Process server = runtime.exec(command);
   90   
   91               InputStream out = server.getInputStream();
   92   
   93               Thread serverOut = new Thread(new Pipe(out, System.out));
   94   
   95               serverOut.setDaemon(true);
   96   
   97               serverOut.start();
   98   
   99               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.matches("^(java|javax|os|sun|user|file|awt|line|path)\\..*")) {
  132   
  133                   cmd.add("-D" + key + "=" + value);
  134   
  135               }
  136   
  137           }
  138   
  139       }
  140   
  141       private String getClasspath() {
  142   
  143           String classpath = System.getProperty("java.class.path");
  144   
  145           ClassLoader cl = Thread.currentThread().getContextClassLoader();
  146   
  147           String antLoader = "org.apache.tools.ant.AntClassLoader";
  148   
  149           if (cl.getClass().getName().equals(antLoader)) {
  150   
  151               try {
  152   
  153                   Class ant = cl.getClass();
  154   
  155                   Method getClasspath = ant.getMethod("getClasspath", new Class[0]);
  156   
  157                   classpath += File.pathSeparator + getClasspath.invoke(cl, new Object[0]);
  158   
  159               } catch (Exception e) {
  160   
  161                   e.printStackTrace();
  162   
  163               }
  164   
  165           }
  166   
  167           return classpath;
  168   
  169       }
  170   
  171       public static boolean connect() {
  172   
  173           return Connect.connect(1, "localhost", 4201);
  174   
  175       }
  176   
  177       private static final class Pipe implements Runnable {
  178   
  179           private final InputStream is;
  180   
  181           private final OutputStream out;
  182   
  183           private Pipe(InputStream is, OutputStream out) {
  184   
  185               super();
  186   
  187               this.is = is;
  188   
  189               this.out = out;
  190   
  191           }
  192   
  193           public void run() {
  194   
  195               try {
  196   
  197                   int i = is.read();
  198   
  199                   out.write(i);
  200   
  201                   while (i != -1) {
  202   
  203                       i = is.read();
  204   
  205                       out.write(i);
  206   
  207                   }
  208   
  209               } catch (Exception e) {
  210   
  211                   e.printStackTrace();
  212   
  213               }
  214   
  215           }
  216   
  217       }
  218   
  219   }

Home » openejb-3.1.2-src » org.apache » openejb » server » [javadoc | source]