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.loader;
017:
018: import java.lang.reflect.Method;
019: import java.lang.reflect.InvocationTargetException;
020: import java.util.Properties;
021: import java.io.File;
022:
023: public class OpenEJBInstance {
024: private final Method init;
025: private final Method isInitialized;
026:
027: public OpenEJBInstance() throws Exception {
028: Class<?> openejb = loadOpenEJBClass();
029: this .init = openejb.getMethod("init", Properties.class);
030: this .isInitialized = openejb.getMethod("isInitialized");
031: }
032:
033: public void init(Properties props) throws Exception {
034: try {
035: init.invoke(null, props);
036: } catch (InvocationTargetException e) {
037: if (e.getCause() instanceof Exception) {
038: throw (Exception) e.getCause();
039: }
040: throw (Error) e.getCause();
041: } catch (Exception e) {
042: throw new RuntimeException("OpenEJB.init: ", e);
043: }
044: }
045:
046: public boolean isInitialized() {
047: try {
048: return (Boolean) isInitialized.invoke(null);
049: } catch (InvocationTargetException e) {
050: throw new RuntimeException("OpenEJB.isInitialized: ", e
051: .getCause());
052: } catch (Exception e) {
053: throw new RuntimeException("OpenEJB.isInitialized: ", e);
054: }
055: }
056:
057: private Class<?> loadOpenEJBClass() throws Exception {
058: ClassPath classPath = SystemInstance.get().getClassPath();
059: ClassLoader classLoader = classPath.getClassLoader();
060: try {
061: return classLoader.loadClass("org.apache.openejb.OpenEJB");
062: } catch (Exception e) {
063: try {
064: checkOpenEjbHome(SystemInstance.get().getHome()
065: .getDirectory());
066: FileUtils home = SystemInstance.get().getHome();
067: classPath.addJarsToPath(home.getDirectory("lib"));
068: } catch (Exception e2) {
069: throw new Exception(
070: "Could not load OpenEJB libraries. Exception: "
071: + e2.getClass().getName() + " "
072: + e2.getMessage());
073: }
074: try {
075: return classLoader
076: .loadClass("org.apache.openejb.OpenEJB");
077: } catch (Exception e2) {
078: throw new Exception(
079: "Could not load OpenEJB class after embedding libraries. Exception: "
080: + e2.getClass().getName() + " "
081: + e2.getMessage());
082: }
083: }
084: }
085:
086: String NO_HOME = "The openejb.home is not set.";
087:
088: String BAD_HOME = "Invalid openejb.home: ";
089:
090: String NOT_THERE = "The path specified does not exist.";
091:
092: String NOT_DIRECTORY = "The path specified is not a directory.";
093:
094: String NO_DIST = "The path specified is not correct, it does not contain a 'dist' directory.";
095:
096: String NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries.";
097:
098: String INSTRUCTIONS = "Please edit the web.xml of the openejb_loader webapp and set the openejb.home init-param to the full path where OpenEJB is installed.";
099:
100: private void checkOpenEjbHome(File openejbHome) throws Exception {
101: try {
102:
103: String homePath = openejbHome.getAbsolutePath();
104:
105: if (!openejbHome.exists())
106: handleError(BAD_HOME + homePath, NOT_THERE,
107: INSTRUCTIONS);
108:
109: if (!openejbHome.isDirectory())
110: handleError(BAD_HOME + homePath, NOT_DIRECTORY,
111: INSTRUCTIONS);
112:
113: File openejbHomeLibs = new File(openejbHome, "lib");
114: if (!openejbHomeLibs.exists())
115: handleError(BAD_HOME + homePath, NO_DIST, INSTRUCTIONS);
116:
117: String[] libs = openejbHomeLibs.list();
118: boolean found = false;
119: for (int i = 0; i < libs.length && !found; i++) {
120: found = (libs[i].startsWith("openejb-") && libs[i]
121: .endsWith(".jar"));
122: }
123: if (!found)
124: handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS);
125:
126: } catch (Exception e) {
127: e.printStackTrace();
128: }
129: }
130:
131: private void handleError(String m1, String m2, String m3)
132: throws Exception {
133: System.err
134: .println("--[PLEASE FIX]-------------------------------------");
135: System.err.println(m1);
136: System.err.println(m2);
137: System.err.println(m3);
138: System.err
139: .println("---------------------------------------------------");
140: throw new Exception(m1 + " " + m2 + " " + m3);
141: }
142: }
|