KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > SessionPurgeUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * SessionPurgeUtil.java
26  *
27  * Created on March 10, 2003, 3:28 PM
28  */

29
30 package com.sun.enterprise.web;
31
32 import java.util.logging.*;
33 import com.sun.logging.*;
34 import java.util.Hashtable JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import org.apache.catalina.Context;
38 import org.apache.catalina.Container;
39 import org.apache.catalina.Engine;
40 import org.apache.catalina.Manager;
41 import org.apache.catalina.core.StandardContext;
42 import org.apache.catalina.session.SessionPurgeCapable;
43 import java.sql.Connection JavaDoc;
44
45
46 /**
47  *
48  * @author lwhite
49  */

50 public class SessionPurgeUtil {
51
52     /**
53      * The embedded Catalina object.
54      */

55     protected EmbeddedWebContainer _embedded = null;
56     private WebContainer webContainer = null;
57     private static Logger _logger;
58     static
59     {
60             _logger=LogDomains.getLogger(LogDomains.WEB_LOGGER);
61     }
62     
63     /** Creates a new instance of SessionPurgeUtil */
64     public SessionPurgeUtil() {
65     }
66     
67
68     /** Creates a new instance of SessionPurgeUtil */
69     /*
70     public SessionPurgeUtil(Hashtable instances) {
71         _instances = instances;
72     }
73      */

74     
75     /** Creates a new instance of SessionPurgeUtil */
76     public SessionPurgeUtil(EmbeddedWebContainer embedded) {
77         _embedded = embedded;
78     }
79     
80      public String JavaDoc getApplicationId(Context ctx) {
81         com.sun.enterprise.web.WebModule wm =
82             (com.sun.enterprise.web.WebModule)ctx;
83         return wm.getID();
84     }
85      
86      public String JavaDoc getApplicationName(Context ctx) {
87         return ctx.getName();
88     }
89      
90     public String JavaDoc getJ2EEApplicationName(Context ctx) {
91         return ((StandardContext)ctx).getJ2EEApplication();
92     }
93     
94     public void purgeSessionsForContext(Context ctx) {
95         Manager mgr = ctx.getManager();
96         _logger.finest("SessionPurgeUtil: mgr = " + mgr);
97         _logger.finest("SessionPurgeUtil: mgr SessionPurgeCapable: " + (mgr instanceof SessionPurgeCapable));
98         if(mgr instanceof SessionPurgeCapable) {
99             ((SessionPurgeCapable)mgr).clearSessions();
100             ((SessionPurgeCapable)mgr).clearStore();
101         }
102     }
103              
104     public void purgeSessionsForApp(String JavaDoc appName) {
105         _logger.finest("IN SessionPurgeUtil:purgeSessionsForApp" + appName);
106         try {
107             Engine[] engines = _embedded.getEngines();
108             
109             for(int h=0; h<engines.length; h++) {
110                 Container engine = (Container) engines[h];
111                 Container[] hosts = engine.findChildren();
112                 for(int i=0; i<hosts.length; i++) {
113                     Container nextHost = hosts[i];
114                     Container [] webModules = nextHost.findChildren();
115                     for (int j=0; j<webModules.length; j++) {
116                         Container nextWebModule = webModules[j];
117                         Context ctx = (Context)nextWebModule;
118                         //this code gets managers
119
//String webAppName = this.getApplicationId(ctx);
120
String JavaDoc webAppName = this.getApplicationName(ctx);
121                         Manager nextManager = nextWebModule.getManager();
122                         _logger.finest("webAppName = " + webAppName + ", appName = " + appName);
123                         if(webAppName.equals(appName)) {
124                             _logger.finest("found our manager:" + nextManager.getClass().getName());
125                             //call the clearSessions and clearStore
126
/*
127                             if(nextManager instanceof PersistentManagerBase) {
128                                 ((PersistentManagerBase)nextManager).clearSessions();
129                                 ((PersistentManagerBase)nextManager).clearStore();
130                             }
131                              */

132                             if(nextManager instanceof SessionPurgeCapable) {
133                                 ((SessionPurgeCapable)nextManager).clearSessions();
134                                 ((SessionPurgeCapable)nextManager).clearStore();
135                             }
136                         }
137                     }
138                 }
139             }
140         } catch (Throwable JavaDoc th) {
141             _logger.log(Level.SEVERE, "Exception thrown", th);
142         }
143                 
144     }
145     
146     public void closeCachedConnectionForApp(String JavaDoc appName) {
147         _logger.finest("IN SessionPurgeUtil:closeCachedConnectionForApp" + appName);
148
149         try {
150             Engine[] engines = _embedded.getEngines();
151             
152             for(int h=0; h<engines.length; h++) {
153                 Container engine = (Container) engines[h];
154                 Container[] hosts = engine.findChildren();
155                 for(int i=0; i<hosts.length; i++) {
156                     Container nextHost = hosts[i];
157                     Container [] webModules = nextHost.findChildren();
158                     for (int j=0; j<webModules.length; j++) {
159                         Container nextWebModule = webModules[j];
160                         Context ctx = (Context)nextWebModule;
161                         //this code gets managers
162
//String webAppName = this.getApplicationId(ctx);
163
String JavaDoc webAppName = this.getApplicationName(ctx);
164                         Manager nextManager = nextWebModule.getManager();
165                         _logger.finest("webAppName = " + webAppName + ", appName = " + appName);
166                         if(webAppName.equals(appName)) {
167                             _logger.finest("found our manager:" + nextManager.getClass().getName());
168                             //close the cached connection of the dedicated store instance
169
if(nextManager instanceof ShutdownCleanupCapable) {
170                                 ShutdownCleanupCapable nextOne = (ShutdownCleanupCapable) nextManager;
171                                 nextOne.doCloseCachedConnection();
172                             }
173                         }
174                     }
175                 }
176             }
177         } catch (Throwable JavaDoc th) {
178             _logger.log(Level.SEVERE, "Exception thrown", th);
179         }
180                 
181     }
182     
183 }
184
Popular Tags