KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > sessionContainerAdapter > JmxContainerAdapterSessionManager


1
2
3 package com.lutris.appserver.server.sessionContainerAdapter;
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import javax.management.Attribute JavaDoc;
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.ObjectName JavaDoc;
14
15 import com.lutris.appserver.server.Application;
16 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
17 import com.lutris.appserver.server.session.Session;
18 import com.lutris.appserver.server.session.SessionException;
19
20
21
22 /**
23  * <p>Description: </p>
24  * an implementation of ContainerAdapterSessionManager specific to the Tomcat Servlet container.
25  * It uses JMX to obtain some data form the Tomcat's session manager, which
26  * really manages the sessions
27  * @version 1.0
28  */

29 public class JmxContainerAdapterSessionManager extends ContainerAdapterSessionManager {
30   
31   /**
32    * object name of the MBean which represents the Tomcat's session manager
33    */

34   private ObjectName JavaDoc objectName;
35
36   private boolean saveOnRestart = false;
37     
38   private int maxSessions = -1;
39   
40   private boolean initialized = false;
41   
42   private Application application;
43   
44    
45   /**
46    *
47    * @param application application that uses this session manager
48    * @param config application's config object
49    * @param logger ignored
50    * @throws SessionException
51    */

52   public JmxContainerAdapterSessionManager (com.lutris.appserver.server.Application application,
53       com.lutris.util.Config config, com.lutris.logging.LogChannel logger) throws com.lutris.appserver.server.session.SessionException
54   {
55     super(application, config, logger);
56     this.application = application;
57         try {
58             if (config.containsKey("SessionHome.SaveOnRestart")) {
59                 saveOnRestart = config.getBoolean("SessionHome.SaveOnRestart",false);
60             }
61          } catch (com.lutris.util.ConfigException e) {
62               throw new com.lutris.appserver.server.session.SessionException(e);
63          }
64          try{
65               if(config.containsKey("SessionHome.MaxSessions"))
66                 {
67                 maxSessions = config.getInt("SessionHome.MaxSessions");
68                 }
69          } catch (com.lutris.util.ConfigException e) {
70                   throw new com.lutris.appserver.server.session.SessionException(e);
71          }
72   }
73
74   /**
75      * Creates a new session
76      *
77      * @return Seession
78      * @throws SessionException
79      */

80   public Session createSession (HttpPresentationComms comms) throws com.lutris.appserver.server.session.SessionException {
81    
82     if (!initialized)
83         initialize();
84     return super.createSession(comms);
85   }
86
87   /**
88    * Returns teh session object corresponding to the HttpPresentationComms
89    * @param parm1 ignored
90    * @param sessionId ignored
91    * @param comms HttpPresentationComms object that contains HttpServletRequest from which the session is extracted
92    * @return the Session object
93    * @throws SessionException
94    */

95   public Session getSession (Thread JavaDoc parm1, String JavaDoc sessionId, HttpPresentationComms comms) throws com.lutris.appserver.server.session.SessionException {
96     
97     if (!initialized)
98         initialize();
99     return super.getSession(parm1,sessionId, comms);
100   }
101
102   
103   
104   /**
105    * Finds the MBeanServer
106    * @return javax.management.MBeanServer
107    * @throws SessionException
108    */

109   private MBeanServer JavaDoc findMBeanServer () throws com.lutris.appserver.server.session.SessionException {
110     MBeanServer JavaDoc mBeanServer;
111     try {
112       java.util.ArrayList JavaDoc server = MBeanServerFactory.findMBeanServer(null);
113       if (server == null) {
114         throw new com.lutris.appserver.server.session.SessionException("MBeansServer not found");
115       }
116       else {
117         mBeanServer = (MBeanServer JavaDoc)server.get(0);
118       }
119     } catch (Exception JavaDoc e) {
120       throw new com.lutris.appserver.server.session.SessionException(e);
121     }
122     return mBeanServer;
123   }
124
125  
126     /**
127      * Delete existing session
128      *
129      * @return
130      * @throws SessionException
131      */

132     public void deleteSession(Session SessionId)
133             throws com.lutris.appserver.server.session.SessionException {
134         if (!initialized)
135             initialize();
136
137          expireSession(SessionId.getSessionKey());
138     }
139
140     /**
141      * Delete existing session
142      *
143      * @return
144      * @throws SessionException
145      */

146     public void deleteSession(String JavaDoc SessionId)
147             throws com.lutris.appserver.server.session.SessionException {
148         if (!initialized)
149             initialize();
150         
151          expireSession(SessionId);
152     }
153
154       /**
155        * Returns true if the session identified by sessionId exists
156        * @param sessionId the identification of the session
157        * @return
158        * @throws SessionException
159        */

160       public boolean sessionExists (String JavaDoc sessionId) throws com.lutris.appserver.server.session.SessionException {
161         //if (contextPath == null) {
162
// return true;
163
//}
164
if (!initialized)
165             initialize();
166         
167         Enumeration JavaDoc e = getSessionKeys();
168         while (e.hasMoreElements()) {
169           String JavaDoc currentE = (String JavaDoc)e.nextElement();
170           if (currentE.equals(sessionId)) {
171             return true;
172           }
173         }
174         return false;
175       }
176       /**
177          * Get all of the active sessions Keys.
178          *
179          * @return An enumeration of the active sessions Keys.
180          * @exception SessionException
181          * If the sessions cannot be retrieved.
182          */

183   public Enumeration JavaDoc getSessionKeys () throws com.lutris.appserver.server.session.SessionException {
184     
185     if (!initialized)
186         initialize();
187     try {
188       MBeanServer JavaDoc mBeanServer = findMBeanServer();
189       String JavaDoc SKeys = (String JavaDoc)mBeanServer.invoke(objectName, "listSessionIds",
190           null, null);
191       return new StringTokenizer JavaDoc(SKeys);
192     } catch (Exception JavaDoc e) {
193       throw new com.lutris.appserver.server.session.SessionException(e);
194     }
195   }
196
197   /**
198    * The last time when this session has been accessed
199    * @param SessionId session key
200    * @return
201    * @throws SessionException
202    */

203   public String JavaDoc getLastAccessTime (String JavaDoc SessionId) throws com.lutris.appserver.server.session.SessionException {
204     
205     if (!initialized)
206         initialize();
207     try {
208       Object JavaDoc[] param = new Object JavaDoc[1];
209       param[0] = SessionId;
210       String JavaDoc[] signature = new String JavaDoc[1];
211       signature[0] = "java.lang.String";
212       MBeanServer JavaDoc mBeanServer = findMBeanServer();
213       String JavaDoc time = (String JavaDoc)mBeanServer.invoke(objectName, "getLastAccessedTime",
214           param, signature);
215       return time;
216     } catch (Exception JavaDoc e) {
217       throw new com.lutris.appserver.server.session.SessionException(e);
218     }
219   }
220
221   /**
222    * Expires the session identified by the SessionId
223    * @param SessionId - session key
224    * @throws SessionException
225    */

226   public void expireSession (String JavaDoc SessionId) throws com.lutris.appserver.server.session.SessionException {
227     try {
228       Object JavaDoc[] param = new Object JavaDoc[1];
229       param[0] = SessionId;
230       String JavaDoc[] signature = new String JavaDoc[1];
231       signature[0] = "java.lang.String";
232       MBeanServer JavaDoc mBeanServer = findMBeanServer();
233       mBeanServer.invoke(objectName, "expireSession", param, signature);
234       return;
235     } catch (Exception JavaDoc e) {
236       throw new com.lutris.appserver.server.session.SessionException(e);
237     }
238   }
239
240   
241   /**
242    * Find the named MBean Attribute
243    * @param attributeName - the name of the attribute
244    * @return attribute object
245    * @throws Exception
246    */

247   private Object JavaDoc findMBeanAttribute (String JavaDoc attributeName) throws Exception JavaDoc {
248     MBeanServer JavaDoc mBeanServer = findMBeanServer();
249     return mBeanServer.getAttribute(objectName, attributeName);
250   }
251
252   /**
253      * Gets the maximum number of concurent sessions that existed at any time
254      *
255      * @return The number of sessions, or -1.
256      */

257   public int maxSessionCount () {
258     try {
259         if (!initialized)
260             initialize();
261       return ((Integer JavaDoc)findMBeanAttribute("maxActive")).intValue();
262     } catch (Exception JavaDoc e) {
263       return -1;
264     }
265   }
266
267   /**
268    *
269    * @return the number of expired sessions (not including the sessions forced to expire),
270    * or -1 if it can not be determined
271    * @throws SessionException
272    */

273   public int expiredSessionCount () throws com.lutris.appserver.server.session.SessionException {
274     if (!initialized)
275         initialize();
276     try {
277       return ((Integer JavaDoc)findMBeanAttribute("expiredSessions")).intValue();
278     } catch (Exception JavaDoc e) {
279       return -1;
280     }
281   }
282
283   /**
284      * Gets the number of currently active sessions.
285      *
286      * @return The number of currently active sessions.
287      * @exception SessionException
288      *
289      */

290   public int activeSessionCount () throws com.lutris.appserver.server.session.SessionException {
291     if (!initialized)
292         initialize();
293     try {
294       return ((Integer JavaDoc)findMBeanAttribute("activeSessions")).intValue();
295     } catch (Exception JavaDoc e) {
296       return -1;
297     }
298   }
299
300   /**
301      * Reset the maximum session count. See <CODE>maxSessionCount()</CODE>.
302      * The highwater mark should be reset to the current number of sessions.
303      *
304      * @exception SessionException
305      * if the max session count cannot be reset.
306      */

307
308   public void resetMaxSessionCount () throws com.lutris.appserver.server.session.SessionException {
309     if (!initialized)
310         initialize();
311     try {
312     // Object[] param = new Object[1];
313
// param[0] = new Integer(0);
314
// String[] signature = new String[1];
315
// signature[0] = "int";
316
// MBeanServer mBeanServer = findMBeanServer();
317
// mBeanServer.invoke(objectName, "setMaxActive", param, signature);
318
int current = activeSessionCount();
319         MBeanServer JavaDoc mBeanServer = findMBeanServer();
320         Attribute JavaDoc atr = new Attribute JavaDoc("maxActive",new Integer JavaDoc(current));
321         mBeanServer.setAttribute(objectName, atr);
322         
323         return;
324     } catch (Exception JavaDoc e) {
325       throw new com.lutris.appserver.server.session.SessionException(e.getMessage());
326     }
327   }
328    
329     public void initialize()
330     throws com.lutris.appserver.server.session.SessionException {
331
332     try {
333
334         MBeanServer JavaDoc mBeanServer = findMBeanServer();
335         Object JavaDoc current = application.getClass().getClassLoader();
336
337         ObjectName JavaDoc oname = new ObjectName JavaDoc("*:j2eeType=WebModule,*");
338
339         Set JavaDoc moduls = mBeanServer.queryNames(oname, null);
340         Object JavaDoc[] o = (Object JavaDoc[]) moduls.toArray();
341         
342         for (int j = 0; j < o.length; j++) {
343
344     
345                 
346         Object JavaDoc context = (Object JavaDoc) mBeanServer.invoke((ObjectName JavaDoc) o[j], "findMappingObject",new Object JavaDoc[]{},new String JavaDoc[]{});
347                 
348         Method JavaDoc getLoader = context.getClass().getMethod("getLoader", new Class JavaDoc[] {});
349         Object JavaDoc webAppLoader = getLoader.invoke(context,new Object JavaDoc[]{});
350                 
351                 Method JavaDoc getClassLoader = null;
352                     getClassLoader = webAppLoader.getClass().getMethod(
353                             "getClassLoader", new Class JavaDoc[] {});
354                     Object JavaDoc webAppClassLoader = getClassLoader.invoke(
355                             webAppLoader, new Object JavaDoc[] {});
356
357                 
358
359             if (webAppClassLoader.equals(current))
360              {
361             
362             String JavaDoc contextPath = (String JavaDoc) mBeanServer.getAttribute((ObjectName JavaDoc) o[j], "path");//sa
363
String JavaDoc domain = (String JavaDoc) mBeanServer.getAttribute((ObjectName JavaDoc) o[j], "engineName");//bez slesha
364
String JavaDoc temp = (String JavaDoc)((ObjectName JavaDoc) o[j]).getKeyProperty("name");
365                 
366             String JavaDoc host = temp.substring(0,temp.indexOf(contextPath));
367             host = host.replaceAll("/","");
368
369             objectName = new ObjectName JavaDoc(domain+":type=Manager,path="+contextPath+",host="+host);
370                 
371             if (!saveOnRestart) {
372                     Attribute JavaDoc atr = new Attribute JavaDoc("pathname",null);
373                     mBeanServer.setAttribute(objectName,atr);
374                 }
375                 
376                 if (maxSessions>0) {
377                      Attribute JavaDoc atr = new Attribute JavaDoc("maxActiveSessions",new Integer JavaDoc(maxSessions));
378                      mBeanServer.setAttribute(objectName,atr);
379                 }
380             initialized = true;
381             return;
382             }
383         }
384
385     } catch (Exception JavaDoc e) {
386        throw new SessionException("Problem in initialization"
387             + e.getMessage());
388     }
389
390   }
391     /**
392      *
393      */

394     public void shutdown() {
395         super.shutdown();
396         initialized = false;
397         application = null;
398         objectName = null;
399         
400     }
401 }
402
403
404
405
Popular Tags