001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.loader;
018:
019: import java.io.File;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022: import java.util.Properties;
023:
024: /**
025: * @version $Revision: 542619 $ $Date: 2007-05-29 11:43:31 -0700 $
026: */
027: public class Embedder {
028: private final String className;
029: private Class loaderClass;
030:
031: public Embedder(String className) {
032: this .className = className;
033: }
034:
035: public Class load() throws Exception {
036: if (loaderClass == null) {
037: ClassPath classPath = SystemInstance.get().getClassPath();
038: ClassLoader classLoader = classPath.getClassLoader();
039: try {
040: loaderClass = classLoader.loadClass(className);
041: } catch (Exception e) {
042: loaderClass = forcefulLoad(classPath, classLoader);
043: }
044: }
045: return loaderClass;
046: }
047:
048: public Object init(Properties properties) throws Exception {
049: Class loaderClass = load();
050:
051: try {
052: // get the init method
053: Method init = loaderClass.getMethod("init",
054: Properties.class);
055:
056: // create the instance
057: Object instance = loaderClass.newInstance();
058:
059: // invoke init method
060: Object value = init.invoke(instance, properties);
061: return value;
062: } catch (NoSuchMethodException e) {
063: throw (IllegalStateException) new IllegalStateException(
064: "Signatures for Loader are no longer correct.")
065: .initCause(e);
066: } catch (InvocationTargetException e) {
067: Throwable cause = e.getCause();
068: if (cause instanceof Error) {
069: throw (Error) cause;
070: } else {
071: throw (Exception) cause;
072: }
073: }
074: }
075:
076: private Class forcefulLoad(ClassPath classPath,
077: ClassLoader classLoader) throws Exception {
078: try {
079: File libsDir;
080:
081: String libsPath = SystemInstance.get().getProperty(
082: "openejb.libs");
083: if (libsPath != null) {
084: libsDir = new File(libsPath);
085: } else {
086: checkOpenEjbHome(SystemInstance.get().getHome()
087: .getDirectory());
088: FileUtils home = SystemInstance.get().getHome();
089: libsDir = home.getDirectory("lib");
090: }
091: classPath.addJarsToPath(libsDir);
092: } catch (Exception e2) {
093: throw new Exception(
094: "Could not load OpenEJB libraries. Exception: "
095: + e2.getClass().getName() + " "
096: + e2.getMessage());
097: }
098: try {
099: return classLoader.loadClass(className);
100: } catch (Exception e2) {
101: throw new Exception("Could not load class '" + className
102: + "' after embedding libraries. Exception: "
103: + e2.getClass().getName() + " " + e2.getMessage());
104: }
105: }
106:
107: private String NO_HOME = "The openejb.home is not set.";
108:
109: private String BAD_HOME = "Invalid openejb.home: ";
110:
111: private String NOT_THERE = "The path specified does not exist.";
112:
113: private String NOT_DIRECTORY = "The path specified is not a directory.";
114:
115: private String NO_LIBS = "The path specified is not correct, it does not contain any OpenEJB libraries.";
116:
117: // TODO: move this part back into the LoaderServlet
118: private 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.";
119:
120: private void checkOpenEjbHome(File openejbHome) throws Exception {
121: try {
122:
123: String homePath = openejbHome.getAbsolutePath();
124:
125: // The openejb.home must exist
126: if (!openejbHome.exists())
127: handleError(BAD_HOME + homePath, NOT_THERE,
128: INSTRUCTIONS);
129:
130: // The openejb.home must be a directory
131: if (!openejbHome.isDirectory())
132: handleError(BAD_HOME + homePath, NOT_DIRECTORY,
133: INSTRUCTIONS);
134:
135: // The openejb.home must contain a 'lib' directory
136: File openejbHomeLibs = new File(openejbHome, "lib");
137: if (!openejbHomeLibs.exists())
138: handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS);
139:
140: // The openejb.home there must be openejb*.jar files in the 'dist'
141: // directory
142: String[] libs = openejbHomeLibs.list();
143: boolean found = false;
144: for (int i = 0; i < libs.length && !found; i++) {
145: found = (libs[i].startsWith("openejb-") && libs[i]
146: .endsWith(".jar"));
147: }
148: if (!found)
149: handleError(BAD_HOME + homePath, NO_LIBS, INSTRUCTIONS);
150:
151: } catch (Exception e) {
152: e.printStackTrace();
153: }
154: }
155:
156: private void handleError(String m1, String m2, String m3)
157: throws Exception {
158: System.err
159: .println("--[PLEASE FIX]-------------------------------------");
160: System.err.println(m1);
161: System.err.println(m2);
162: System.err.println(m3);
163: System.err
164: .println("---------------------------------------------------");
165: throw new Exception(m1 + " " + m2 + " " + m3);
166: }
167:
168: private void handleError(String m1, String m2) throws Exception {
169: System.err
170: .println("--[PLEASE FIX]-------------------------------------");
171: System.err.println(m1);
172: System.err.println(m2);
173: System.err
174: .println("---------------------------------------------------");
175: throw new Exception(m1 + " " + m2);
176: }
177:
178: }
|