KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > php > LifecycleListener


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.web.php;
24
25 import java.lang.reflect.Method JavaDoc;
26 import org.apache.catalina.Lifecycle;
27 import org.apache.catalina.LifecycleEvent;
28 import org.apache.catalina.util.StringManager;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * Implementation of <code>LifecycleListener</code> that will init and
34  * and destroy PHP.
35  *
36  * @author Mladen Turk
37  * @version $Revision: 4354 $ $Date: 2006-05-22 19:53:20 +0200 (lun., 22 mai 2006) $
38  * @since 1.0
39  */

40
41 public class LifecycleListener
42     implements org.apache.catalina.LifecycleListener {
43
44     private static Log log = LogFactory.getLog(LifecycleListener.class);
45
46     /**
47      * The string manager for this package.
48      */

49     protected StringManager sm =
50         StringManager.getManager(Constants.Package);
51
52     // -------------------------------------------------------------- Constants
53

54
55     protected static final int REQUIRED_MAJOR = 5;
56     protected static final int REQUIRED_MINOR = 1;
57     protected static final int REQUIRED_PATCH = 0;
58
59
60     // ---------------------------------------------- LifecycleListener Methods
61

62
63     /**
64      * Primary entry point for startup and shutdown events.
65      *
66      * @param event The event that has occurred
67      */

68     public void lifecycleEvent(LifecycleEvent event) {
69
70         if (Lifecycle.INIT_EVENT.equals(event.getType())) {
71             int major = 0;
72             int minor = 0;
73             int patch = 0;
74             try {
75                 String JavaDoc methodName = "initialize";
76                 Class JavaDoc paramTypes[] = new Class JavaDoc[1];
77                 paramTypes[0] = String JavaDoc.class;
78                 Object JavaDoc paramValues[] = new Object JavaDoc[1];
79                 paramValues[0] = null;
80                 Class JavaDoc clazz = Class.forName("org.apache.catalina.servlets.php.Library");
81                 Method JavaDoc method = clazz.getMethod(methodName, paramTypes);
82                 // TODO: Use sm to obtain optional library name.
83
method.invoke(null, paramValues);
84                 major = clazz.getField("PHP_MAJOR_VERSION").getInt(null);
85                 minor = clazz.getField("PHP_MINOR_VERSION").getInt(null);
86                 patch = clazz.getField("PHP_PATCH_VERSION").getInt(null);
87             } catch (Throwable JavaDoc t) {
88                 if (!log.isDebugEnabled()) {
89                     log.info(sm.getString("listener.initialize",
90                              System.getProperty("java.library.path")));
91                 }
92                 else {
93                     log.debug(sm.getString("listener.initialize",
94                               System.getProperty("java.library.path")), t);
95                 }
96                 return;
97             }
98             // Check if the PHP Native module matches required version.
99
if ((major != REQUIRED_MAJOR) ||
100                 (minor != REQUIRED_MINOR) ||
101                 (patch < REQUIRED_PATCH)) {
102                 log.error(sm.getString("listener.invalid", major + "."
103                           + minor + "." + patch, REQUIRED_MAJOR + "."
104                           + REQUIRED_MINOR + "."
105                           + REQUIRED_PATCH));
106             }
107         }
108         else if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
109             try {
110                 String JavaDoc methodName = "terminate";
111                 Method JavaDoc method = Class.forName("org.apache.catalina.servlets.php.Library")
112                     .getMethod(methodName, (Class JavaDoc [])null);
113                 method.invoke(null, (Object JavaDoc []) null);
114             }
115             catch (Throwable JavaDoc t) {
116                 if (!log.isDebugEnabled()) {
117                     log.info(sm.getString("listener.terminate"));
118                 }
119                 else {
120                     log.debug(sm.getString("listener.terminate"), t);
121                 }
122             }
123         }
124     }
125 }
126
Popular Tags