001: package com.lutris.appserver.server.sessionContainerAdapter;
002:
003: import java.lang.reflect.Method;
004: import java.util.Enumeration;
005: import java.util.Set;
006: import java.util.StringTokenizer;
007:
008: import javax.management.Attribute;
009: import javax.management.MBeanServer;
010: import javax.management.MBeanServerFactory;
011: import javax.management.ObjectName;
012:
013: import com.lutris.appserver.server.Application;
014: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
015: import com.lutris.appserver.server.session.Session;
016: import com.lutris.appserver.server.session.SessionException;
017:
018: /**
019: * <p>Description: </p>
020: * an implementation of ContainerAdapterSessionManager specific to the Tomcat Servlet container.
021: * It uses JMX to obtain some data form the Tomcat's session manager, which
022: * really manages the sessions
023: * @version 1.0
024: */
025: public class JmxContainerAdapterSessionManager extends
026: ContainerAdapterSessionManager {
027:
028: /**
029: * object name of the MBean which represents the Tomcat's session manager
030: */
031: private ObjectName objectName;
032:
033: private boolean saveOnRestart = false;
034:
035: private int maxSessions = -1;
036:
037: private boolean initialized = false;
038:
039: private Application application;
040:
041: /**
042: *
043: * @param application application that uses this session manager
044: * @param config application's config object
045: * @param logger ignored
046: * @throws SessionException
047: */
048: public JmxContainerAdapterSessionManager(
049: com.lutris.appserver.server.Application application,
050: com.lutris.util.Config config,
051: com.lutris.logging.LogChannel logger)
052: throws com.lutris.appserver.server.session.SessionException {
053: super (application, config, logger);
054: this .application = application;
055: try {
056: if (config.containsKey("SessionHome.SaveOnRestart")) {
057: saveOnRestart = config.getBoolean(
058: "SessionHome.SaveOnRestart", false);
059: }
060: } catch (com.lutris.util.ConfigException e) {
061: throw new com.lutris.appserver.server.session.SessionException(
062: e);
063: }
064: try {
065: if (config.containsKey("SessionHome.MaxSessions")) {
066: maxSessions = config.getInt("SessionHome.MaxSessions");
067: }
068: } catch (com.lutris.util.ConfigException e) {
069: throw new com.lutris.appserver.server.session.SessionException(
070: e);
071: }
072: }
073:
074: /**
075: * Creates a new session
076: *
077: * @return Seession
078: * @throws SessionException
079: */
080: public Session createSession(HttpPresentationComms comms)
081: throws com.lutris.appserver.server.session.SessionException {
082:
083: if (!initialized)
084: initialize();
085: return super .createSession(comms);
086: }
087:
088: /**
089: * Returns teh session object corresponding to the HttpPresentationComms
090: * @param parm1 ignored
091: * @param sessionId ignored
092: * @param comms HttpPresentationComms object that contains HttpServletRequest from which the session is extracted
093: * @return the Session object
094: * @throws SessionException
095: */
096: public Session getSession(Thread parm1, String sessionId,
097: HttpPresentationComms comms)
098: throws com.lutris.appserver.server.session.SessionException {
099:
100: if (!initialized)
101: initialize();
102: return super .getSession(parm1, sessionId, comms);
103: }
104:
105: /**
106: * Finds the MBeanServer
107: * @return javax.management.MBeanServer
108: * @throws SessionException
109: */
110: private MBeanServer findMBeanServer()
111: throws com.lutris.appserver.server.session.SessionException {
112: MBeanServer mBeanServer;
113: try {
114: java.util.ArrayList server = MBeanServerFactory
115: .findMBeanServer(null);
116: if (server == null) {
117: throw new com.lutris.appserver.server.session.SessionException(
118: "MBeansServer not found");
119: } else {
120: mBeanServer = (MBeanServer) server.get(0);
121: }
122: } catch (Exception e) {
123: throw new com.lutris.appserver.server.session.SessionException(
124: e);
125: }
126: return mBeanServer;
127: }
128:
129: /**
130: * Delete existing session
131: *
132: * @return
133: * @throws SessionException
134: */
135: public void deleteSession(Session SessionId)
136: throws com.lutris.appserver.server.session.SessionException {
137: if (!initialized)
138: initialize();
139:
140: expireSession(SessionId.getSessionKey());
141: }
142:
143: /**
144: * Delete existing session
145: *
146: * @return
147: * @throws SessionException
148: */
149: public void deleteSession(String SessionId)
150: throws com.lutris.appserver.server.session.SessionException {
151: if (!initialized)
152: initialize();
153:
154: expireSession(SessionId);
155: }
156:
157: /**
158: * Returns true if the session identified by sessionId exists
159: * @param sessionId the identification of the session
160: * @return
161: * @throws SessionException
162: */
163: public boolean sessionExists(String sessionId)
164: throws com.lutris.appserver.server.session.SessionException {
165: //if (contextPath == null) {
166: // return true;
167: //}
168: if (!initialized)
169: initialize();
170:
171: Enumeration e = getSessionKeys();
172: while (e.hasMoreElements()) {
173: String currentE = (String) e.nextElement();
174: if (currentE.equals(sessionId)) {
175: return true;
176: }
177: }
178: return false;
179: }
180:
181: /**
182: * Get all of the active sessions Keys.
183: *
184: * @return An enumeration of the active sessions Keys.
185: * @exception SessionException
186: * If the sessions cannot be retrieved.
187: */
188: public Enumeration getSessionKeys()
189: throws com.lutris.appserver.server.session.SessionException {
190:
191: if (!initialized)
192: initialize();
193: try {
194: MBeanServer mBeanServer = findMBeanServer();
195: String SKeys = (String) mBeanServer.invoke(objectName,
196: "listSessionIds", null, null);
197: return new StringTokenizer(SKeys);
198: } catch (Exception e) {
199: throw new com.lutris.appserver.server.session.SessionException(
200: e);
201: }
202: }
203:
204: /**
205: * The last time when this session has been accessed
206: * @param SessionId session key
207: * @return
208: * @throws SessionException
209: */
210: public String getLastAccessTime(String SessionId)
211: throws com.lutris.appserver.server.session.SessionException {
212:
213: if (!initialized)
214: initialize();
215: try {
216: Object[] param = new Object[1];
217: param[0] = SessionId;
218: String[] signature = new String[1];
219: signature[0] = "java.lang.String";
220: MBeanServer mBeanServer = findMBeanServer();
221: String time = (String) mBeanServer.invoke(objectName,
222: "getLastAccessedTime", param, signature);
223: return time;
224: } catch (Exception e) {
225: throw new com.lutris.appserver.server.session.SessionException(
226: e);
227: }
228: }
229:
230: /**
231: * Expires the session identified by the SessionId
232: * @param SessionId - session key
233: * @throws SessionException
234: */
235: public void expireSession(String SessionId)
236: throws com.lutris.appserver.server.session.SessionException {
237: try {
238: Object[] param = new Object[1];
239: param[0] = SessionId;
240: String[] signature = new String[1];
241: signature[0] = "java.lang.String";
242: MBeanServer mBeanServer = findMBeanServer();
243: mBeanServer.invoke(objectName, "expireSession", param,
244: signature);
245: return;
246: } catch (Exception e) {
247: throw new com.lutris.appserver.server.session.SessionException(
248: e);
249: }
250: }
251:
252: /**
253: * Find the named MBean Attribute
254: * @param attributeName - the name of the attribute
255: * @return attribute object
256: * @throws Exception
257: */
258: private Object findMBeanAttribute(String attributeName)
259: throws Exception {
260: MBeanServer mBeanServer = findMBeanServer();
261: return mBeanServer.getAttribute(objectName, attributeName);
262: }
263:
264: /**
265: * Gets the maximum number of concurent sessions that existed at any time
266: *
267: * @return The number of sessions, or -1.
268: */
269: public int maxSessionCount() {
270: try {
271: if (!initialized)
272: initialize();
273: return ((Integer) findMBeanAttribute("maxActive"))
274: .intValue();
275: } catch (Exception e) {
276: return -1;
277: }
278: }
279:
280: /**
281: *
282: * @return the number of expired sessions (not including the sessions forced to expire),
283: * or -1 if it can not be determined
284: * @throws SessionException
285: */
286: public int expiredSessionCount()
287: throws com.lutris.appserver.server.session.SessionException {
288: if (!initialized)
289: initialize();
290: try {
291: return ((Integer) findMBeanAttribute("expiredSessions"))
292: .intValue();
293: } catch (Exception e) {
294: return -1;
295: }
296: }
297:
298: /**
299: * Gets the number of currently active sessions.
300: *
301: * @return The number of currently active sessions.
302: * @exception SessionException
303: *
304: */
305: public int activeSessionCount()
306: throws com.lutris.appserver.server.session.SessionException {
307: if (!initialized)
308: initialize();
309: try {
310: return ((Integer) findMBeanAttribute("activeSessions"))
311: .intValue();
312: } catch (Exception e) {
313: return -1;
314: }
315: }
316:
317: /**
318: * Reset the maximum session count. See <CODE>maxSessionCount()</CODE>.
319: * The highwater mark should be reset to the current number of sessions.
320: *
321: * @exception SessionException
322: * if the max session count cannot be reset.
323: */
324:
325: public void resetMaxSessionCount()
326: throws com.lutris.appserver.server.session.SessionException {
327: if (!initialized)
328: initialize();
329: try {
330: // Object[] param = new Object[1];
331: // param[0] = new Integer(0);
332: // String[] signature = new String[1];
333: // signature[0] = "int";
334: // MBeanServer mBeanServer = findMBeanServer();
335: // mBeanServer.invoke(objectName, "setMaxActive", param, signature);
336: int current = activeSessionCount();
337: MBeanServer mBeanServer = findMBeanServer();
338: Attribute atr = new Attribute("maxActive", new Integer(
339: current));
340: mBeanServer.setAttribute(objectName, atr);
341:
342: return;
343: } catch (Exception e) {
344: throw new com.lutris.appserver.server.session.SessionException(
345: e.getMessage());
346: }
347: }
348:
349: public void initialize()
350: throws com.lutris.appserver.server.session.SessionException {
351:
352: try {
353:
354: MBeanServer mBeanServer = findMBeanServer();
355: Object current = application.getClass().getClassLoader();
356:
357: ObjectName oname = new ObjectName("*:j2eeType=WebModule,*");
358:
359: Set moduls = mBeanServer.queryNames(oname, null);
360: Object[] o = (Object[]) moduls.toArray();
361:
362: for (int j = 0; j < o.length; j++) {
363:
364: Object context = (Object) mBeanServer.invoke(
365: (ObjectName) o[j], "findMappingObject",
366: new Object[] {}, new String[] {});
367:
368: Method getLoader = context.getClass().getMethod(
369: "getLoader", new Class[] {});
370: Object webAppLoader = getLoader.invoke(context,
371: new Object[] {});
372:
373: Method getClassLoader = null;
374: getClassLoader = webAppLoader.getClass().getMethod(
375: "getClassLoader", new Class[] {});
376: Object webAppClassLoader = getClassLoader.invoke(
377: webAppLoader, new Object[] {});
378:
379: if (webAppClassLoader.equals(current)) {
380:
381: String contextPath = (String) mBeanServer
382: .getAttribute((ObjectName) o[j], "path");//sa
383: String domain = (String) mBeanServer.getAttribute(
384: (ObjectName) o[j], "engineName");//bez slesha
385: String temp = (String) ((ObjectName) o[j])
386: .getKeyProperty("name");
387:
388: String host = temp.substring(0, temp
389: .indexOf(contextPath));
390: host = host.replaceAll("/", "");
391:
392: objectName = new ObjectName(domain
393: + ":type=Manager,path=" + contextPath
394: + ",host=" + host);
395:
396: if (!saveOnRestart) {
397: Attribute atr = new Attribute("pathname", null);
398: mBeanServer.setAttribute(objectName, atr);
399: }
400:
401: if (maxSessions > 0) {
402: Attribute atr = new Attribute(
403: "maxActiveSessions", new Integer(
404: maxSessions));
405: mBeanServer.setAttribute(objectName, atr);
406: }
407: initialized = true;
408: return;
409: }
410: }
411:
412: } catch (Exception e) {
413: throw new SessionException("Problem in initialization"
414: + e.getMessage());
415: }
416:
417: }
418:
419: /**
420: *
421: */
422: public void shutdown() {
423: super .shutdown();
424: initialized = false;
425: application = null;
426: objectName = null;
427:
428: }
429: }
|