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: */
017:
018: package org.apache.catalina.core;
019:
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022:
023: import org.apache.catalina.Lifecycle;
024: import org.apache.catalina.LifecycleEvent;
025: import org.apache.catalina.LifecycleListener;
026: import org.apache.catalina.util.StringManager;
027: import org.apache.juli.logging.Log;
028: import org.apache.juli.logging.LogFactory;
029: import org.apache.tomcat.jni.Library;
030:
031: /**
032: * Implementation of <code>LifecycleListener</code> that will init and
033: * and destroy APR.
034: *
035: * @author Remy Maucherat
036: * @author Filip Hanik
037: * @version $Revision: 534930 $ $Date: 2007-05-03 18:43:35 +0200 (jeu., 03 mai 2007) $
038: * @since 4.1
039: */
040:
041: public class AprLifecycleListener implements LifecycleListener {
042:
043: private static Log log = LogFactory
044: .getLog(AprLifecycleListener.class);
045:
046: /**
047: * The string manager for this package.
048: */
049: protected StringManager sm = StringManager
050: .getManager(Constants.Package);
051:
052: // ---------------------------------------------- Constants
053:
054: protected static final int TCN_REQUIRED_MAJOR = 1;
055: protected static final int TCN_REQUIRED_MINOR = 1;
056: protected static final int TCN_REQUIRED_PATCH = 8;
057: protected static final int TCN_RECOMMENDED_PV = 10;
058:
059: // ---------------------------------------------- Properties
060: protected static String SSLEngine = "on"; //default on
061: protected static boolean sslInitialized = false;
062: protected static boolean aprInitialized = false;
063:
064: // ---------------------------------------------- LifecycleListener Methods
065:
066: /**
067: * Primary entry point for startup and shutdown events.
068: *
069: * @param event The event that has occurred
070: */
071: public void lifecycleEvent(LifecycleEvent event) {
072:
073: if (Lifecycle.INIT_EVENT.equals(event.getType())) {
074: aprInitialized = init();
075: if (aprInitialized) {
076: try {
077: initializeSSL();
078: } catch (Throwable t) {
079: if (!log.isDebugEnabled()) {
080: log.info(sm.getString("aprListener.sslInit"));
081: } else {
082: log.debug(sm.getString("aprListener.sslInit"));
083: }
084: }
085: }
086: } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
087: if (!aprInitialized) {
088: return;
089: }
090: try {
091: terminateAPR();
092: } catch (Throwable t) {
093: if (!log.isDebugEnabled()) {
094: log.info(sm.getString("aprListener.aprDestroy"));
095: } else {
096: log
097: .debug(
098: sm
099: .getString("aprListener.aprDestroy"),
100: t);
101: }
102: }
103: }
104:
105: }
106:
107: private static synchronized void terminateAPR()
108: throws ClassNotFoundException, NoSuchMethodException,
109: IllegalAccessException, InvocationTargetException {
110: String methodName = "terminate";
111: Method method = Class.forName("org.apache.tomcat.jni.Library")
112: .getMethod(methodName, (Class[]) null);
113: method.invoke(null, (Object[]) null);
114: }
115:
116: private boolean init() {
117: int major = 0;
118: int minor = 0;
119: int patch = 0;
120: if (aprInitialized) {
121: return true;
122: }
123: try {
124: String methodName = "initialize";
125: Class paramTypes[] = new Class[1];
126: paramTypes[0] = String.class;
127: Object paramValues[] = new Object[1];
128: paramValues[0] = null;
129: Class clazz = Class
130: .forName("org.apache.tomcat.jni.Library");
131: Method method = clazz.getMethod(methodName, paramTypes);
132: method.invoke(null, paramValues);
133: major = clazz.getField("TCN_MAJOR_VERSION").getInt(null);
134: minor = clazz.getField("TCN_MINOR_VERSION").getInt(null);
135: patch = clazz.getField("TCN_PATCH_VERSION").getInt(null);
136: } catch (Throwable t) {
137: if (!log.isDebugEnabled()) {
138: log.info(sm.getString("aprListener.aprInit", System
139: .getProperty("java.library.path")));
140: } else {
141: log.debug(sm.getString("aprListener.aprInit", System
142: .getProperty("java.library.path")), t);
143: }
144: return false;
145: }
146: if ((major != TCN_REQUIRED_MAJOR)
147: || (minor != TCN_REQUIRED_MINOR)
148: || (patch < TCN_REQUIRED_PATCH)) {
149: log.error(sm.getString("aprListener.tcnInvalid", major
150: + "." + minor + "." + patch, TCN_REQUIRED_MAJOR
151: + "." + TCN_REQUIRED_MINOR + "."
152: + TCN_REQUIRED_PATCH));
153: try {
154: // Terminate the APR in case the version
155: // is below required.
156: terminateAPR();
157: } catch (Throwable t) {
158: // Ignore
159: }
160: return false;
161: }
162: if (patch < TCN_RECOMMENDED_PV) {
163: if (!log.isDebugEnabled()) {
164: log.info(sm.getString("aprListener.tcnVersion", major
165: + "." + minor + "." + patch, TCN_REQUIRED_MAJOR
166: + "." + TCN_REQUIRED_MINOR + "."
167: + TCN_RECOMMENDED_PV));
168: } else {
169: log.debug(sm.getString("aprListener.tcnVersion", major
170: + "." + minor + "." + patch, TCN_REQUIRED_MAJOR
171: + "." + TCN_REQUIRED_MINOR + "."
172: + TCN_RECOMMENDED_PV));
173: }
174: }
175: if (!log.isDebugEnabled()) {
176: log.info(sm.getString("aprListener.tcnValid", major + "."
177: + minor + "." + patch));
178: } else {
179: log.debug(sm.getString("aprListener.tcnValid", major + "."
180: + minor + "." + patch));
181: }
182: // Log APR flags
183: log.info(sm
184: .getString("aprListener.flags", Library.APR_HAVE_IPV6,
185: Library.APR_HAS_SENDFILE,
186: Library.APR_HAS_SO_ACCEPTFILTER,
187: Library.APR_HAS_RANDOM));
188: return true;
189: }
190:
191: private static synchronized void initializeSSL()
192: throws ClassNotFoundException, NoSuchMethodException,
193: IllegalAccessException, InvocationTargetException {
194:
195: if ("off".equalsIgnoreCase(SSLEngine)) {
196: return;
197: }
198: if (sslInitialized) {
199: //only once per VM
200: return;
201: }
202: String methodName = "initialize";
203: Class paramTypes[] = new Class[1];
204: paramTypes[0] = String.class;
205: Object paramValues[] = new Object[1];
206: paramValues[0] = "on".equalsIgnoreCase(SSLEngine) ? null
207: : SSLEngine;
208: Class clazz = Class.forName("org.apache.tomcat.jni.SSL");
209: Method method = clazz.getMethod(methodName, paramTypes);
210: method.invoke(null, paramValues);
211: sslInitialized = true;
212: }
213:
214: public String getSSLEngine() {
215: return SSLEngine;
216: }
217:
218: public void setSSLEngine(String SSLEngine) {
219: this.SSLEngine = SSLEngine;
220: }
221:
222: }
|