KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > AprLifecycleListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.core;
19
20
21 import java.lang.reflect.Method JavaDoc;
22 import org.apache.catalina.Lifecycle;
23 import org.apache.catalina.LifecycleEvent;
24 import org.apache.catalina.LifecycleListener;
25 import org.apache.catalina.util.StringManager;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30
31
32
33 /**
34  * Implementation of <code>LifecycleListener</code> that will init and
35  * and destroy APR.
36  *
37  * @author Remy Maucherat
38  * @author Filip Hanik
39  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
40  * @since 4.1
41  */

42
43 public class AprLifecycleListener
44     implements LifecycleListener {
45
46     private static Log log = LogFactory.getLog(AprLifecycleListener.class);
47
48     /**
49      * The string manager for this package.
50      */

51     protected StringManager sm =
52         StringManager.getManager(Constants.Package);
53
54     
55     // -------------------------------------------------------------- Constants
56

57
58     protected static final int REQUIRED_MAJOR = 1;
59     protected static final int REQUIRED_MINOR = 1;
60     protected static final int REQUIRED_PATCH = 3;
61     protected static final int RECOMMENDED_PV = 7;
62
63
64     // ---------------------------------------------- Properties
65
protected static String JavaDoc SSLEngine = "on"; //default on
66
protected static boolean sslInitialized = false;
67     
68     // ---------------------------------------------- LifecycleListener Methods
69

70     /**
71      * Primary entry point for startup and shutdown events.
72      *
73      * @param event The event that has occurred
74      */

75     public void lifecycleEvent(LifecycleEvent event) {
76
77         if (Lifecycle.INIT_EVENT.equals(event.getType())) {
78             int major = 0;
79             int minor = 0;
80             int patch = 0;
81             try {
82                 String JavaDoc methodName = "initialize";
83                 Class JavaDoc paramTypes[] = new Class JavaDoc[1];
84                 paramTypes[0] = String JavaDoc.class;
85                 Object JavaDoc paramValues[] = new Object JavaDoc[1];
86                 paramValues[0] = null;
87                 Class JavaDoc clazz = Class.forName("org.apache.tomcat.jni.Library");
88                 Method JavaDoc method = clazz.getMethod(methodName, paramTypes);
89                 method.invoke(null, paramValues);
90                 major = clazz.getField("TCN_MAJOR_VERSION").getInt(null);
91                 minor = clazz.getField("TCN_MINOR_VERSION").getInt(null);
92                 patch = clazz.getField("TCN_PATCH_VERSION").getInt(null);
93             } catch (Throwable JavaDoc t) {
94                 if (!log.isDebugEnabled()) {
95                     log.info(sm.getString("aprListener.aprInit",
96                             System.getProperty("java.library.path")));
97                 } else {
98                     log.debug(sm.getString("aprListener.aprInit",
99                             System.getProperty("java.library.path")), t);
100                 }
101                 return;
102             }
103             if ((major != REQUIRED_MAJOR) || (minor != REQUIRED_MINOR)
104                     || (patch < REQUIRED_PATCH)) {
105                 log.error(sm.getString("aprListener.tcnInvalid", major + "."
106                         + minor + "." + patch, REQUIRED_MAJOR + "."
107                         + REQUIRED_MINOR + "." + REQUIRED_PATCH));
108             }
109             if (patch < RECOMMENDED_PV) {
110                 if (!log.isDebugEnabled()) {
111                     log.info(sm.getString("aprListener.tcnVersion", major + "."
112                             + minor + "." + patch, REQUIRED_MAJOR + "."
113                             + REQUIRED_MINOR + "." + RECOMMENDED_PV));
114                 } else {
115                     log.debug(sm.getString("aprListener.tcnVersion", major + "."
116                             + minor + "." + patch, REQUIRED_MAJOR + "."
117                             + REQUIRED_MINOR + "." + RECOMMENDED_PV));
118                 }
119             }
120             try {
121                 initializeSSL();
122             }catch ( Throwable JavaDoc t ) {
123                 log.error(sm.getString("aprListener.sslInit",t.getMessage()),t);
124             }
125         } else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
126             try {
127                 String JavaDoc methodName = "terminate";
128                 Method JavaDoc method = Class.forName("org.apache.tomcat.jni.Library")
129                     .getMethod(methodName, (Class JavaDoc [])null);
130                 method.invoke(null, (Object JavaDoc []) null);
131             } catch (Throwable JavaDoc t) {
132                 if (!log.isDebugEnabled()) {
133                     log.info(sm.getString("aprListener.aprDestroy"));
134                 } else {
135                     log.debug(sm.getString("aprListener.aprDestroy"), t);
136                 }
137             }
138         }
139
140     }
141     
142     public static synchronized void initializeSSL()
143         throws ClassNotFoundException JavaDoc,NoSuchMethodException JavaDoc,
144                IllegalAccessException JavaDoc,InvocationTargetException JavaDoc{
145         
146         if ("off".equalsIgnoreCase(SSLEngine) ) return;
147         if ( sslInitialized ) return; //only once per VM
148
String JavaDoc methodName = "initialize";
149         Class JavaDoc paramTypes[] = new Class JavaDoc[1];
150         paramTypes[0] = String JavaDoc.class;
151         Object JavaDoc paramValues[] = new Object JavaDoc[1];
152         paramValues[0] = "on".equalsIgnoreCase(SSLEngine)?null:SSLEngine;
153         Class JavaDoc clazz = Class.forName("org.apache.tomcat.jni.SSL");
154         Method JavaDoc method = clazz.getMethod(methodName, paramTypes);
155         method.invoke(null, paramValues);
156         sslInitialized = true;
157
158     }
159
160
161 }
162
Popular Tags