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.util.Properties;
021:
022: import org.apache.openejb.loader.Embedder;
023: import org.apache.openejb.loader.SystemInstance;
024:
025: /**
026: * This class should only be loadded and used via reflection from TomcatEmbedder.
027: */
028: class TomcatHook {
029: @SuppressWarnings({"UnusedDeclaration"})
030: private static void hook(Properties properties) {
031: // verify properties
032: if (properties == null)
033: throw new NullPointerException("properties is null");
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: if (SystemInstance.isInitialized()) {
046: return;
047: }
048:
049: properties.setProperty("openejb.loader", "tomcat-system");
050:
051: String catalinaHome = System.getProperty("catalina.home");
052: properties.setProperty("openejb.home", catalinaHome);
053: System.setProperty("openejb.home", catalinaHome);
054:
055: String catalinaBase = System.getProperty("catalina.base");
056: properties.setProperty("openejb.base", catalinaBase);
057: System.setProperty("openejb.base", catalinaBase);
058:
059: System.setProperty("openejb.war", openejbWar.getAbsolutePath());
060: File libDir = new File(openejbWar, "lib");
061: String libPath = libDir.getAbsolutePath();
062: properties.setProperty("openejb.libs", libPath);
063:
064: // System.setProperty("tomcat.version", "x.y.z.w");
065: // System.setProperty("tomcat.built", "mmm dd yyyy hh:mm:ss");
066: try {
067: Properties tomcatServerInfo = new Properties();
068: ClassLoader classLoader = TomcatHook.class.getClassLoader();
069: tomcatServerInfo
070: .load(classLoader
071: .getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"));
072:
073: String serverNumber = tomcatServerInfo
074: .getProperty("server.number");
075: if (serverNumber == null) {
076: // Tomcat5 only has server.info
077: String serverInfo = tomcatServerInfo
078: .getProperty("server.info");
079: if (serverInfo != null) {
080: int slash = serverInfo.indexOf('/');
081: serverNumber = serverInfo.substring(slash + 1);
082: }
083: }
084: if (serverNumber != null) {
085: System.setProperty("tomcat.version", serverNumber);
086: }
087:
088: String serverBuilt = tomcatServerInfo
089: .getProperty("server.built");
090: if (serverBuilt != null) {
091: System.setProperty("tomcat.built", serverBuilt);
092: }
093: } catch (Throwable e) {
094: }
095:
096: try {
097: // create the loader
098: SystemInstance.init(properties);
099: Embedder embedder = new Embedder(
100: "org.apache.openejb.tomcat.catalina.TomcatLoader");
101: embedder.init(properties);
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: }
106: }
|