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.tomcat.loader;
018:
019: import java.io.File;
020: import java.lang.reflect.Method;
021: import java.net.URL;
022: import java.net.URI;
023: import java.net.URLDecoder;
024: import java.util.Properties;
025:
026: public class TomcatEmbedder {
027: public static void embed(Properties properties,
028: ClassLoader catalinaCl) {
029: if (catalinaCl == null)
030: throw new NullPointerException("catalinaCl is null");
031: if (properties == null)
032: throw new NullPointerException("properties is null");
033:
034: if (!properties.containsKey("openejb.war")) {
035: throw new IllegalArgumentException(
036: "properties must contain the openejb.war property");
037: }
038: File openejbWar = new File(properties
039: .getProperty("openejb.war"));
040: if (!openejbWar.isDirectory()) {
041: throw new IllegalArgumentException(
042: "openejb.war is not a directory: " + openejbWar);
043: }
044:
045: ClassLoader oldCl = Thread.currentThread()
046: .getContextClassLoader();
047: Thread.currentThread().setContextClassLoader(catalinaCl);
048: try {
049: // WebappClassLoader childCl = new WebappClassLoader(catalinaCl)
050: Class<?> webappClClass = catalinaCl
051: .loadClass("org.apache.catalina.loader.WebappClassLoader");
052: ClassLoader childCl = (ClassLoader) webappClClass
053: .getConstructor(ClassLoader.class).newInstance(
054: catalinaCl);
055:
056: // childCl.addRepository(openejb-tomcat-loader.jar)
057: File this Jar = getThisJar();
058: webappClClass.getMethod("addRepository", String.class)
059: .invoke(childCl, this Jar.toURI().toString());
060:
061: // childCl.addRepository(openejb-loader.jar)
062: File jarFile = findOpenEJBJar(openejbWar, "openejb-loader");
063: webappClClass.getMethod("addRepository", String.class)
064: .invoke(childCl, jarFile.toURI().toString());
065:
066: // childCl.start()
067: webappClClass.getMethod("start").invoke(childCl);
068:
069: // TomcatHook.hook()
070: Class<?> tomcatUtilClass = childCl
071: .loadClass("org.apache.openejb.tomcat.loader.TomcatHook");
072: Method hookMethod = tomcatUtilClass.getDeclaredMethod(
073: "hook", Properties.class);
074: hookMethod.setAccessible(true);
075: hookMethod.invoke(null, properties);
076: } catch (Throwable e) {
077: e.printStackTrace();
078: } finally {
079: Thread.currentThread().setContextClassLoader(oldCl);
080: }
081: }
082:
083: private static File getThisJar() {
084: return jarLocation(TomcatEmbedder.class);
085: }
086:
087: private static File jarLocation(Class clazz) {
088: try {
089: String classFileName = clazz.getName().replace(".", "/")
090: + ".class";
091:
092: URL classURL = clazz.getClassLoader().getResource(
093: classFileName);
094:
095: URI uri = null;
096: String url = classURL.toExternalForm();
097: if (url.contains(" ")) {
098: url = url.replaceAll(" ", "%20");
099: }
100: uri = new URI(url);
101:
102: if (uri.getPath() == null) {
103: uri = new URI(uri.getRawSchemeSpecificPart());
104: }
105:
106: String path = uri.getPath();
107: if (path.contains("!")) {
108: path = path.substring(0, path.indexOf('!'));
109: } else {
110: path = path.substring(0, path.length()
111: - classFileName.length());
112: }
113:
114: return new File(URLDecoder.decode(path));
115: } catch (Exception e) {
116: throw new IllegalStateException(e);
117: }
118: }
119:
120: private static File findOpenEJBJar(File openejbWar,
121: String namePrefix) {
122: File openEJBLibDir = new File(openejbWar, "lib");
123: if (openEJBLibDir == null)
124: return null;
125:
126: File openejbLoaderJar = null;
127: for (File file : openEJBLibDir.listFiles()) {
128: if (file.getName().startsWith(namePrefix + "-")
129: && file.getName().endsWith(".jar")) {
130: return file;
131: }
132: }
133:
134: return openejbLoaderJar;
135: }
136: }
|