1 2 3 package com.lutris.appserver.server.sessionContainerAdapter; 4 5 import java.lang.reflect.Method ; 6 import java.util.Enumeration ; 7 import java.util.Set ; 8 import java.util.StringTokenizer ; 9 10 import javax.management.Attribute ; 11 import javax.management.MBeanServer ; 12 import javax.management.MBeanServerFactory ; 13 import javax.management.ObjectName ; 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 29 public class JmxContainerAdapterSessionManager extends ContainerAdapterSessionManager { 30 31 34 private ObjectName 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 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 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 95 public Session getSession (Thread parm1, String 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 109 private MBeanServer findMBeanServer () throws com.lutris.appserver.server.session.SessionException { 110 MBeanServer mBeanServer; 111 try { 112 java.util.ArrayList 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 )server.get(0); 118 } 119 } catch (Exception e) { 120 throw new com.lutris.appserver.server.session.SessionException(e); 121 } 122 return mBeanServer; 123 } 124 125 126 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 146 public void deleteSession(String SessionId) 147 throws com.lutris.appserver.server.session.SessionException { 148 if (!initialized) 149 initialize(); 150 151 expireSession(SessionId); 152 } 153 154 160 public boolean sessionExists (String sessionId) throws com.lutris.appserver.server.session.SessionException { 161 if (!initialized) 165 initialize(); 166 167 Enumeration e = getSessionKeys(); 168 while (e.hasMoreElements()) { 169 String currentE = (String )e.nextElement(); 170 if (currentE.equals(sessionId)) { 171 return true; 172 } 173 } 174 return false; 175 } 176 183 public Enumeration getSessionKeys () throws com.lutris.appserver.server.session.SessionException { 184 185 if (!initialized) 186 initialize(); 187 try { 188 MBeanServer mBeanServer = findMBeanServer(); 189 String SKeys = (String )mBeanServer.invoke(objectName, "listSessionIds", 190 null, null); 191 return new StringTokenizer (SKeys); 192 } catch (Exception e) { 193 throw new com.lutris.appserver.server.session.SessionException(e); 194 } 195 } 196 197 203 public String getLastAccessTime (String SessionId) throws com.lutris.appserver.server.session.SessionException { 204 205 if (!initialized) 206 initialize(); 207 try { 208 Object [] param = new Object [1]; 209 param[0] = SessionId; 210 String [] signature = new String [1]; 211 signature[0] = "java.lang.String"; 212 MBeanServer mBeanServer = findMBeanServer(); 213 String time = (String )mBeanServer.invoke(objectName, "getLastAccessedTime", 214 param, signature); 215 return time; 216 } catch (Exception e) { 217 throw new com.lutris.appserver.server.session.SessionException(e); 218 } 219 } 220 221 226 public void expireSession (String SessionId) throws com.lutris.appserver.server.session.SessionException { 227 try { 228 Object [] param = new Object [1]; 229 param[0] = SessionId; 230 String [] signature = new String [1]; 231 signature[0] = "java.lang.String"; 232 MBeanServer mBeanServer = findMBeanServer(); 233 mBeanServer.invoke(objectName, "expireSession", param, signature); 234 return; 235 } catch (Exception e) { 236 throw new com.lutris.appserver.server.session.SessionException(e); 237 } 238 } 239 240 241 247 private Object findMBeanAttribute (String attributeName) throws Exception { 248 MBeanServer mBeanServer = findMBeanServer(); 249 return mBeanServer.getAttribute(objectName, attributeName); 250 } 251 252 257 public int maxSessionCount () { 258 try { 259 if (!initialized) 260 initialize(); 261 return ((Integer )findMBeanAttribute("maxActive")).intValue(); 262 } catch (Exception e) { 263 return -1; 264 } 265 } 266 267 273 public int expiredSessionCount () throws com.lutris.appserver.server.session.SessionException { 274 if (!initialized) 275 initialize(); 276 try { 277 return ((Integer )findMBeanAttribute("expiredSessions")).intValue(); 278 } catch (Exception e) { 279 return -1; 280 } 281 } 282 283 290 public int activeSessionCount () throws com.lutris.appserver.server.session.SessionException { 291 if (!initialized) 292 initialize(); 293 try { 294 return ((Integer )findMBeanAttribute("activeSessions")).intValue(); 295 } catch (Exception e) { 296 return -1; 297 } 298 } 299 300 307 308 public void resetMaxSessionCount () throws com.lutris.appserver.server.session.SessionException { 309 if (!initialized) 310 initialize(); 311 try { 312 int current = activeSessionCount(); 319 MBeanServer mBeanServer = findMBeanServer(); 320 Attribute atr = new Attribute ("maxActive",new Integer (current)); 321 mBeanServer.setAttribute(objectName, atr); 322 323 return; 324 } catch (Exception 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 mBeanServer = findMBeanServer(); 335 Object current = application.getClass().getClassLoader(); 336 337 ObjectName oname = new ObjectName ("*:j2eeType=WebModule,*"); 338 339 Set moduls = mBeanServer.queryNames(oname, null); 340 Object [] o = (Object []) moduls.toArray(); 341 342 for (int j = 0; j < o.length; j++) { 343 344 345 346 Object context = (Object ) mBeanServer.invoke((ObjectName ) o[j], "findMappingObject",new Object []{},new String []{}); 347 348 Method getLoader = context.getClass().getMethod("getLoader", new Class [] {}); 349 Object webAppLoader = getLoader.invoke(context,new Object []{}); 350 351 Method getClassLoader = null; 352 getClassLoader = webAppLoader.getClass().getMethod( 353 "getClassLoader", new Class [] {}); 354 Object webAppClassLoader = getClassLoader.invoke( 355 webAppLoader, new Object [] {}); 356 357 358 359 if (webAppClassLoader.equals(current)) 360 { 361 362 String contextPath = (String ) mBeanServer.getAttribute((ObjectName ) o[j], "path"); String domain = (String ) mBeanServer.getAttribute((ObjectName ) o[j], "engineName"); String temp = (String )((ObjectName ) o[j]).getKeyProperty("name"); 365 366 String host = temp.substring(0,temp.indexOf(contextPath)); 367 host = host.replaceAll("/",""); 368 369 objectName = new ObjectName (domain+":type=Manager,path="+contextPath+",host="+host); 370 371 if (!saveOnRestart) { 372 Attribute atr = new Attribute ("pathname",null); 373 mBeanServer.setAttribute(objectName,atr); 374 } 375 376 if (maxSessions>0) { 377 Attribute atr = new Attribute ("maxActiveSessions",new Integer (maxSessions)); 378 mBeanServer.setAttribute(objectName,atr); 379 } 380 initialized = true; 381 return; 382 } 383 } 384 385 } catch (Exception e) { 386 throw new SessionException("Problem in initialization" 387 + e.getMessage()); 388 } 389 390 } 391 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 |