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 java.io.File;
   20   import java.io.IOException;
   21   import java.util.Properties;
   22   import java.util.Map;
   23   import java.util.Set;
   24   import java.util.Collections;
   25   
   26   import org.apache.openejb.loader.SystemInstance;
   27   import org.apache.openejb.util.OpenEjbVersion;
   28   import org.apache.openejb.util.Messages;
   29   import org.apache.openejb.cli.SystemExitException;
   30   import org.apache.commons.cli.CommandLineParser;
   31   import org.apache.commons.cli.PosixParser;
   32   import org.apache.commons.cli.Options;
   33   import org.apache.commons.cli.CommandLine;
   34   import org.apache.commons.cli.ParseException;
   35   import org.apache.commons.cli.HelpFormatter;
   36   import org.apache.commons.cli.Option;
   37   import org.apache.commons.cli.OptionBuilder;
   38   import org.apache.xbean.finder.ResourceFinder;
   39   
   40   /**
   41    * Assemble OpenEJB instance and boot it up
   42    * 
   43    * @version $Rev: 777557 $ $Date: 2009-05-22 08:00:34 -0700 (Fri, 22 May 2009) $
   44    */
   45   public class Main {
   46   
   47       private static Messages messages = new Messages(Main.class);
   48   
   49       public static void main(String args[]) throws SystemExitException {
   50           CommandLineParser parser = new PosixParser();
   51   
   52           // create the Options
   53           Options options = new Options();
   54           options.addOption(option("v", "version", "cmd.start.opt.version"));
   55           options.addOption(option("h", "help", "cmd.start.opt.help"));
   56           options.addOption(option(null, "conf", "file", "cmd.start.opt.conf"));
   57           options.addOption(option(null, "local-copy", "boolean", "cmd.start.opt.localCopy"));
   58           options.addOption(option(null, "examples", "cmd.start.opt.examples"));
   59   
   60           ResourceFinder finder = new ResourceFinder("META-INF/");
   61   
   62           Set<String> services = null;
   63           try {
   64               Map<String, Properties> serviceEntries = finder.mapAvailableProperties(ServerService.class.getName());
   65               services = serviceEntries.keySet();
   66               for (String service : services) {
   67                   options.addOption(option(null, service+"-port", "int", "cmd.start.opt.port", service));
   68                   options.addOption(option(null, service+"-bind", "host", "cmd.start.opt.bind", service));
   69               }
   70           } catch (IOException e) {
   71               services = Collections.EMPTY_SET;
   72           }
   73   
   74           CommandLine line = null;
   75           try {
   76               // parse the command line arguments
   77               line = parser.parse(options, args);
   78           } catch (ParseException exp) {
   79               help(options);
   80               throw new SystemExitException(-1);
   81           }
   82   
   83           if (line.hasOption("help")) {
   84               help(options);
   85               return;
   86           } else if (line.hasOption("version")) {
   87               OpenEjbVersion.get().print(System.out);
   88               return;
   89           } else if (line.hasOption("examples")) {
   90               try {
   91                   String text = finder.findString("org.apache.openejb.cli/start.examples");
   92                   System.out.println(text);
   93                   return;
   94               } catch (IOException e) {
   95                   System.err.println("Unable to print examples:");
   96                   e.printStackTrace();
   97                   throw new SystemExitException(-2);
   98               }
   99           }
  100   
  101           Properties props = SystemInstance.get().getProperties();
  102   
  103           if (line.hasOption("conf")) {
  104               props.setProperty("openejb.configuration", line.getOptionValue("conf"));
  105           } else if (line.hasOption("local-copy")) {
  106               String value = line.getOptionValue("local-copy");
  107               if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
  108                   props.setProperty("openejb.localcopy", "false");
  109               } else {
  110                   props.setProperty("openejb.localcopy", "true");
  111               }
  112           }
  113   
  114           for (String service : services) {
  115               String[] opts = {"port", "bind"};
  116               for (String opt : opts) {
  117                   String option = service + "-" + opt;
  118                   if (line.hasOption(option)){
  119                       props.setProperty(service+"."+opt, line.getOptionValue(option));
  120                   }
  121               }
  122           }
  123   
  124           try {
  125               SystemInstance system = SystemInstance.get();
  126               File libs = system.getHome().getDirectory("lib");
  127               system.getClassPath().addJarsToPath(libs);
  128               initServer();
  129           } catch (DontStartServerException ignored) {
  130           } catch (Exception e) {
  131               e.printStackTrace();
  132           }
  133       }
  134   
  135       private static void help(Options options) {
  136           HelpFormatter formatter = new HelpFormatter();
  137           formatter.printHelp("start [options]", "\n"+i18n("cmd.start.description"), options, "\n");
  138       }
  139   
  140       private static Option option(String shortOpt, String longOpt, String description) {
  141           return OptionBuilder.withLongOpt(longOpt).withDescription(i18n(description)).create(shortOpt);
  142       }
  143   
  144       private static Option option(String shortOpt, String longOpt, String argName, String description, Object... details) {
  145           return OptionBuilder.withLongOpt(longOpt).withArgName(argName).hasArg().withDescription(i18n(description,details)).create(shortOpt);
  146       }
  147   
  148       private static String i18n(String key, Object... details) {
  149           return messages.format(key, details);
  150       }
  151   
  152   
  153       private static void initServer() throws Exception {
  154           Server server = new Server();
  155           server.init(SystemInstance.get().getProperties());
  156           server.start();
  157       }
  158   }
  159   
  160   class DontStartServerException extends Exception {
  161       private static final long serialVersionUID = 1L;
  162   }

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