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.cli;
017:
018: import org.apache.openejb.loader.SystemClassPath;
019:
020: import java.io.File;
021: import java.net.URI;
022: import java.net.URL;
023: import java.net.URLDecoder;
024:
025: /**
026: * @version $Rev: 635146 $ $Date: 2008-03-08 18:54:16 -0800 $
027: */
028: public class Bootstrap {
029:
030: private final static String OPENEJB_VERSION_PROPERTIES_FILE_NAME = "openejb-version.properties";
031: private final static String OPENEJB_HOME_PROPERTY_NAME = "openejb.home";
032: private final static String OPENEJB_BASE_PROPERTY_NAME = "openejb.base";
033: private final static String OPENEJB_CLI_MAIN_CLASS_NAME = "org.apache.openejb.cli.MainImpl";
034:
035: private static void setupHome(String[] args) {
036: for (String arg : args) {
037: if (arg.startsWith("-D" + OPENEJB_HOME_PROPERTY_NAME)) {
038: addProperty(arg);
039: } else if (arg
040: .startsWith("-D" + OPENEJB_BASE_PROPERTY_NAME)) {
041: addProperty(arg);
042: }
043: }
044:
045: String homeProperty = System
046: .getProperty(OPENEJB_HOME_PROPERTY_NAME);
047: if (homeProperty != null) {
048: if (new File(homeProperty).exists()) {
049: return;
050: }
051: }
052:
053: try {
054: URL classURL = Thread.currentThread()
055: .getContextClassLoader().getResource(
056: OPENEJB_VERSION_PROPERTIES_FILE_NAME);
057:
058: if (classURL != null) {
059: String propsString = classURL.getFile();
060:
061: propsString = propsString.substring(0, propsString
062: .indexOf("!"));
063:
064: URI uri = new URI(propsString);
065:
066: File jarFile = new File(uri.getSchemeSpecificPart());
067:
068: if (jarFile.getName().indexOf("openejb-core") > -1) {
069: File lib = jarFile.getParentFile();
070: File home = lib.getParentFile().getCanonicalFile();
071:
072: System.setProperty(OPENEJB_HOME_PROPERTY_NAME, home
073: .getAbsolutePath());
074: }
075: }
076: } catch (Exception e) {
077: System.err.println("Error setting "
078: + OPENEJB_HOME_PROPERTY_NAME + " property: "
079: + e.getClass() + ": " + e.getMessage());
080: }
081: }
082:
083: private static void addProperty(String arg) {
084: String prop = arg.substring(arg.indexOf("-D") + 2, arg
085: .indexOf("="));
086: String val = arg.substring(arg.indexOf("=") + 1);
087:
088: System.setProperty(prop, val);
089: }
090:
091: private static void setupClasspath() {
092: try {
093: File lib = new File(System
094: .getProperty(OPENEJB_HOME_PROPERTY_NAME)
095: + File.separator + "lib");
096: SystemClassPath systemCP = new SystemClassPath();
097: systemCP.addJarsToPath(lib);
098: } catch (Exception e) {
099: System.err.println("Error setting up the classpath: "
100: + e.getClass() + ": " + e.getMessage());
101: }
102: }
103:
104: /**
105: * Read commands from BASE_PATH (using XBean's ResourceFinder) and execute the one specified on the command line
106: */
107: public static void main(String[] args) throws Exception {
108: setupHome(args);
109: setupClasspath();
110:
111: Class<?> clazz = Bootstrap.class.getClassLoader().loadClass(
112: OPENEJB_CLI_MAIN_CLASS_NAME);
113: Main main = (Main) clazz.newInstance();
114: try {
115: main.main(args);
116: } catch (SystemExitException e) {
117: System.exit(e.getExitCode());
118: }
119: }
120:
121: }
|