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 java.io.File;
019: import java.io.IOException;
020: import java.util.Properties;
021: import java.util.Map;
022: import java.util.Set;
023: import java.util.Collections;
024:
025: import org.apache.openejb.loader.SystemInstance;
026: import org.apache.openejb.util.OpenEjbVersion;
027: import org.apache.openejb.util.Messages;
028: import org.apache.openejb.cli.SystemExitException;
029: import org.apache.commons.cli.CommandLineParser;
030: import org.apache.commons.cli.PosixParser;
031: import org.apache.commons.cli.Options;
032: import org.apache.commons.cli.CommandLine;
033: import org.apache.commons.cli.ParseException;
034: import org.apache.commons.cli.HelpFormatter;
035: import org.apache.commons.cli.Option;
036: import org.apache.commons.cli.OptionBuilder;
037: import org.apache.xbean.finder.ResourceFinder;
038:
039: /**
040: * Assemble OpenEJB instance and boot it up
041: *
042: * @version $Rev: 569496 $ $Date: 2007-08-24 12:58:18 -0700 $
043: */
044: public class Main {
045:
046: private static Messages messages = new Messages(Main.class);
047:
048: public static void main(String args[]) throws SystemExitException {
049: CommandLineParser parser = new PosixParser();
050:
051: // create the Options
052: Options options = new Options();
053: options.addOption(option("v", "version",
054: "cmd.start.opt.version"));
055: options.addOption(option("h", "help", "cmd.start.opt.help"));
056: options.addOption(option(null, "conf", "file",
057: "cmd.start.opt.conf"));
058: options.addOption(option(null, "local-copy", "boolean",
059: "cmd.start.opt.localCopy"));
060: options.addOption(option(null, "examples",
061: "cmd.start.opt.examples"));
062:
063: ResourceFinder finder = new ResourceFinder("META-INF/");
064:
065: Set<String> services = null;
066: try {
067: Map<String, Properties> serviceEntries = finder
068: .mapAvailableProperties(ServerService.class
069: .getName());
070: services = serviceEntries.keySet();
071: for (String service : services) {
072: options.addOption(option(null, service + "-port",
073: "int", "cmd.start.opt.port", service));
074: options.addOption(option(null, service + "-bind",
075: "host", "cmd.start.opt.bind", service));
076: }
077: } catch (IOException e) {
078: services = Collections.EMPTY_SET;
079: }
080:
081: CommandLine line = null;
082: try {
083: // parse the command line arguments
084: line = parser.parse(options, args);
085: } catch (ParseException exp) {
086: help(options);
087: throw new SystemExitException(-1);
088: }
089:
090: if (line.hasOption("help")) {
091: help(options);
092: return;
093: } else if (line.hasOption("version")) {
094: OpenEjbVersion.get().print(System.out);
095: return;
096: } else if (line.hasOption("examples")) {
097: try {
098: String text = finder
099: .findString("org.apache.openejb.cli/start.examples");
100: System.out.println(text);
101: return;
102: } catch (IOException e) {
103: System.err.println("Unable to print examples:");
104: e.printStackTrace();
105: throw new SystemExitException(-2);
106: }
107: }
108:
109: Properties props = SystemInstance.get().getProperties();
110:
111: if (line.hasOption("conf")) {
112: props.setProperty("openejb.configuration", line
113: .getOptionValue("conf"));
114: } else if (line.hasOption("local-copy")) {
115: String value = line.getOptionValue("local-copy");
116: if (value.equalsIgnoreCase("false")
117: || value.equalsIgnoreCase("no")
118: || value.equalsIgnoreCase("off")) {
119: props.setProperty("openejb.localcopy", "false");
120: } else {
121: props.setProperty("openejb.localcopy", "true");
122: }
123: }
124:
125: for (String service : services) {
126: String[] opts = { "port", "bind" };
127: for (String opt : opts) {
128: String option = service + "-" + opt;
129: if (line.hasOption(option)) {
130: props.setProperty(service + "." + opt, line
131: .getOptionValue(option));
132: }
133: }
134: }
135:
136: try {
137: SystemInstance system = SystemInstance.get();
138: File libs = system.getHome().getDirectory("lib");
139: system.getClassPath().addJarsToPath(libs);
140: initServer();
141: } catch (DontStartServerException ignored) {
142: } catch (Exception e) {
143: e.printStackTrace();
144: }
145: }
146:
147: private static void help(Options options) {
148: HelpFormatter formatter = new HelpFormatter();
149: formatter.printHelp("start [options]", "\n"
150: + i18n("cmd.start.description"), options, "\n");
151: }
152:
153: private static Option option(String shortOpt, String longOpt,
154: String description) {
155: return OptionBuilder.withLongOpt(longOpt).withDescription(
156: i18n(description)).create(shortOpt);
157: }
158:
159: private static Option option(String shortOpt, String longOpt,
160: String argName, String description, Object... details) {
161: return OptionBuilder.withLongOpt(longOpt).withArgName(argName)
162: .hasArg().withDescription(i18n(description, details))
163: .create(shortOpt);
164: }
165:
166: private static String i18n(String key, Object... details) {
167: return messages.format(key, details);
168: }
169:
170: private static void initServer() throws Exception {
171: Server server = new Server();
172: server.init(SystemInstance.get().getProperties());
173: server.start();
174: }
175: }
176:
177: class DontStartServerException extends Exception {
178: private static final long serialVersionUID = 1L;
179: }
|