KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > JvmRouteSessionIDBinderLifecycleListener


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

16 package org.apache.catalina.cluster.session;
17
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import javax.management.ObjectName JavaDoc;
22 import javax.management.modelmbean.ModelMBean JavaDoc;
23
24 import org.apache.catalina.Lifecycle;
25 import org.apache.catalina.LifecycleEvent;
26 import org.apache.catalina.LifecycleListener;
27 import org.apache.catalina.cluster.CatalinaCluster;
28 import org.apache.catalina.cluster.MessageListener;
29 import org.apache.catalina.core.StandardHost;
30 import org.apache.catalina.util.StringManager;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.commons.modeler.ManagedBean;
34 import org.apache.commons.modeler.Registry;
35
36 /**
37  * Register new JvmRouteSessionIDBinderListener to receive Session ID changes.
38  *
39  * add following at your server.xml Host section
40  *
41  * <pre>
42  * &lt;Host &gt;...
43  * &lt;Listener className=&quot;org.apache.catalina.cluster.session.JvmRouteSessionIDBinderLifecycleListener&quot; /&gt;
44  * &lt;Cluster ...&gt;
45  * &lt;/Host&gt;
46  * </pre>
47  *
48  * @author Peter Rossbach
49  */

50 public class JvmRouteSessionIDBinderLifecycleListener implements
51         LifecycleListener {
52     private static Log log = LogFactory
53             .getLog(JvmRouteSessionIDBinderLifecycleListener.class);
54
55     /**
56      * The descriptive information string for this implementation.
57      */

58     private static final String JavaDoc info = "org.apache.catalina.cluster.session.JvmRouteSessionIDBinderLifecycleListener/1.0";
59
60     /**
61      * The string resources for this package.
62      */

63     protected static final StringManager sm = StringManager
64             .getManager(Constants.Package);
65
66     private boolean enabled = true;
67
68     private MBeanServer JavaDoc mserver = null;
69
70     private Registry registry = null;
71
72     private MessageListener sessionMoverListener;
73
74     /*
75      * start and stop cluster
76      *
77      * @see org.apache.catalina.LifecycleListener#lifecycleEvent(org.apache.catalina.LifecycleEvent)
78      */

79     public void lifecycleEvent(LifecycleEvent event) {
80
81         if (enabled && event.getSource() instanceof StandardHost) {
82
83             if (Lifecycle.AFTER_START_EVENT.equals(event.getType())) {
84                 if (log.isDebugEnabled())
85                     log.debug(sm.getString("jvmRoute.listener.started"));
86                 startSessionIDListener((StandardHost) event.getSource());
87             } else if (Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) {
88                 if (log.isDebugEnabled())
89                     log.debug(sm.getString("jvmRoute.listener.stopped"));
90                 stopSessionIDListener((StandardHost) event.getSource());
91             }
92         }
93     }
94
95     /**
96      * stop sessionID binder at cluster
97      *
98      * @param host
99      * clustered host
100      */

101     protected void stopSessionIDListener(StandardHost host) {
102         if (sessionMoverListener != null) {
103             CatalinaCluster cluster = (CatalinaCluster) host.getCluster();
104             cluster.removeClusterListener(sessionMoverListener);
105             if (mserver != null) {
106                 try {
107                     ObjectName JavaDoc objectName = getObjectName(host);
108                     mserver.unregisterMBean(objectName);
109                 } catch (Exception JavaDoc e) {
110                     log.error(e);
111                 }
112             }
113         }
114     }
115
116     /**
117      * @param host
118      * @return
119      * @throws MalformedObjectNameException
120      */

121     protected ObjectName JavaDoc getObjectName(StandardHost host) throws MalformedObjectNameException JavaDoc {
122         ObjectName JavaDoc objectName = new ObjectName JavaDoc(
123                 host.getDomain()
124                         + ":type=Listener,name=JvmRouteSessionIDBinderListener,host=" + host.getName());
125         return objectName;
126     }
127
128     /**
129      * start sessionID mover at cluster
130      *
131      * @param host
132      * clustered host
133      */

134     protected void startSessionIDListener(StandardHost host) {
135         try {
136             ObjectName JavaDoc objectName = null;
137             getMBeanServer();
138             objectName = getObjectName(host);
139             if (mserver.isRegistered(objectName)) {
140                 if (log.isInfoEnabled())
141                     log.info(sm.getString("jvmRoute.run.already"));
142                 return;
143             }
144             sessionMoverListener = new JvmRouteSessionIDBinderListener();
145             mserver.registerMBean(getManagedBean(sessionMoverListener),
146                     objectName);
147             CatalinaCluster cluster = (CatalinaCluster) host.getCluster();
148             sessionMoverListener.setCluster(cluster);
149             ((JvmRouteSessionIDBinderListener) sessionMoverListener).start();
150         } catch (Exception JavaDoc ex) {
151             log.error(ex.getMessage(), ex);
152         }
153     }
154
155     protected MBeanServer JavaDoc getMBeanServer() throws Exception JavaDoc {
156         if (mserver == null) {
157             if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
158                 mserver = (MBeanServer JavaDoc) MBeanServerFactory
159                         .findMBeanServer(null).get(0);
160             } else {
161                 mserver = MBeanServerFactory.createMBeanServer();
162             }
163             registry = Registry.getRegistry(null, null);
164             registry.loadMetadata(this.getClass().getResourceAsStream(
165                     "mbeans-descriptors.xml"));
166         }
167         return (mserver);
168     }
169
170     /**
171      * Returns the ModelMBean
172      *
173      * @param object
174      * The Object to get the ModelMBean for
175      * @return The ModelMBean
176      * @throws Exception
177      * If an error occurs this constructors throws this exception
178      */

179     protected ModelMBean JavaDoc getManagedBean(Object JavaDoc object) throws Exception JavaDoc {
180         ModelMBean JavaDoc mbean = null;
181         if (registry != null) {
182             ManagedBean managedBean = registry.findManagedBean(object
183                     .getClass().getName());
184             mbean = managedBean.createMBean(object);
185         }
186         return mbean;
187     }
188
189     /**
190      * @return Returns the enabled.
191      */

192     public boolean isEnabled() {
193         return enabled;
194     }
195
196     /**
197      * @param enabled
198      * The enabled to set.
199      */

200     public void setEnabled(boolean enabled) {
201         this.enabled = enabled;
202     }
203
204     /**
205      * Return descriptive information about this Listener implementation and the
206      * corresponding version number, in the format
207      * <code>&lt;description&gt;/&lt;version&gt;</code>.
208      */

209     public String JavaDoc getInfo() {
210
211         return (info);
212
213     }
214 }
Popular Tags