KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ConnectionShutdownUtil.java
26  *
27  * Created on February 27, 2003, 4:45 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.Valve;
42 import org.apache.catalina.core.ContainerBase;
43 import java.sql.Connection JavaDoc;
44
45 import com.sun.enterprise.resource.ResourceSpec;
46 import com.sun.enterprise.Switch;
47
48 /**
49  *
50  * @author lwhite
51  */

52 public class ConnectionShutdownUtil {
53     
54     /**
55      * The embedded Catalina object.
56      */

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

75     
76     /** Creates a new instance of ConnectionShutdownUtil */
77     public ConnectionShutdownUtil(EmbeddedWebContainer embedded) {
78         _embedded = embedded;
79     }
80     
81     public String JavaDoc getApplicationId(Context ctx) {
82         com.sun.enterprise.web.WebModule wm =
83             (com.sun.enterprise.web.WebModule)ctx;
84         return wm.getID();
85     }
86      
87     public String JavaDoc getApplicationName(Context ctx) {
88         return ctx.getName();
89     }
90         
91     public ArrayList JavaDoc runGetShutdownCapables() {
92         _logger.finest("IN ConnectionShutdownUtil:runGetShutdownCapables");
93         //System.out.println("IN ConnectionShutdownUtil:runGetShutdownCapables");
94
//each element of this list will contain
95
//a list of connections
96
ArrayList JavaDoc shutdownCapablesList = new ArrayList JavaDoc();
97
98         try {
99             Engine[] engines = _embedded.getEngines();
100             
101             for(int h=0; h<engines.length; h++) {
102                 Container engine = (Container) engines[h];
103                 Container[] hosts = engine.findChildren();
104                 for(int i=0; i<hosts.length; i++) {
105                     Container nextHost = hosts[i];
106                     Container [] webModules = nextHost.findChildren();
107                     for (int j=0; j<webModules.length; j++) {
108                         Container nextWebModule = webModules[j];
109                         Context ctx = (Context)nextWebModule;
110                         //this code gets managers
111
String JavaDoc webAppName = this.getApplicationId(ctx);
112                         Manager nextManager = nextWebModule.getManager();
113                         if(nextManager instanceof ShutdownCleanupCapable) {
114                             _logger.finest("found a shutdown capable manager:" + nextManager.getClass().getName());
115                             //System.out.println("found a shutdown capable manager:" + nextManager.getClass().getName());
116
shutdownCapablesList.add(nextManager);
117                         }
118                     }
119                     _logger.finest("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
120                     //System.out.println("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
121
//now get any shutdown capable valves installed in virtual hosts
122
Valve[] valves = ((ContainerBase)nextHost).getValves();
123                     for(int k=0; k<valves.length; k++) {
124                         Valve nextValve = valves[k];
125                         _logger.finest("VALVE = " + nextValve.getClass().getName());
126                         //System.out.println("VALVE = " + nextValve.getClass().getName());
127
if(nextValve instanceof ShutdownCleanupCapable) {
128                             _logger.finest("found a shutdown capable valve");
129                             //System.out.println("found a shutdown capable valve");
130
shutdownCapablesList.add(nextValve);
131                         }
132                     }
133                 }
134             }
135         } catch (Throwable JavaDoc th) {
136             _logger.log(Level.SEVERE, "Exception thrown", th);
137         }
138         _logger.finest("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
139         //System.out.println("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
140
return shutdownCapablesList;
141                 
142     }
143     
144     public ArrayList JavaDoc runGetShutdownCapablesForAppName(String JavaDoc appName) {
145         //FIXME this is not returning valves
146
_logger.finest("IN ConnectionShutdownUtil:runGetShutdownCapablesForAppName");
147         //System.out.println("IN ConnectionShutdownUtil:runGetShutdownCapablesForAppName");
148
//each element of this list will contain
149
//a list of connections
150
ArrayList JavaDoc shutdownCapablesList = new ArrayList JavaDoc();
151         try {
152             Engine[] engines = _embedded.getEngines();
153             
154             for(int h=0; h<engines.length; h++) {
155                 Container engine = (Container) engines[h];
156                 Container[] hosts = engine.findChildren();
157                 for(int i=0; i<hosts.length; i++) {
158                     Container nextHost = hosts[i];
159                     Container [] webModules = nextHost.findChildren();
160                     for (int j=0; j<webModules.length; j++) {
161                         Container nextWebModule = webModules[j];
162                         Context ctx = (Context)nextWebModule;
163                         String JavaDoc webAppName = this.getApplicationName(ctx);
164                         //this code gets managers
165
//String webAppName = this.getApplicationId(ctx);
166
Manager nextManager = nextWebModule.getManager();
167                         if(nextManager instanceof ShutdownCleanupCapable) {
168                             if(webAppName.equals(appName)) {
169                                 _logger.finest("found a shutdown capable manager:" + nextManager.getClass().getName());
170                                 //System.out.println("found a shutdown capable manager:" + nextManager.getClass().getName());
171
shutdownCapablesList.add(nextManager);
172                             }
173                         }
174                     }
175                     //System.out.println("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
176

177                 }
178             }
179         } catch (Throwable JavaDoc th) {
180             _logger.log(Level.SEVERE, "Exception thrown", th);
181         }
182         _logger.finest("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
183         //System.out.println("1) shutdownCapablesList Size = " + shutdownCapablesList.size());
184
return shutdownCapablesList;
185                 
186     }
187     
188     public void runShutdownCleanupFromShutdownCleanupCapableList(ArrayList JavaDoc shutdownCapablesList) {
189         for(int i=0; i<shutdownCapablesList.size(); i++) {
190             ShutdownCleanupCapable nextOne = (ShutdownCleanupCapable)shutdownCapablesList.get(i);
191             nextOne.doShutdownCleanup();
192         }
193     }
194     
195     public void runCloseAllConnections() {
196         ArrayList JavaDoc list = this.runGetShutdownCapables();
197         this.runShutdownCleanupFromShutdownCleanupCapableList(list);
198     }
199     
200     public void runCloseCachedConnectionForApp(String JavaDoc appName) {
201         ArrayList JavaDoc list = this.runGetShutdownCapablesForAppName(appName);
202         this.runCloseCachedConnectionFromShutdownCleanupCapableList(list, appName);
203     }
204     
205     public void runCloseCachedConnectionFromShutdownCleanupCapableList(ArrayList JavaDoc shutdownCapablesList, String JavaDoc appName) {
206         for(int i=0; i<shutdownCapablesList.size(); i++) {
207             ShutdownCleanupCapable nextOne = (ShutdownCleanupCapable)shutdownCapablesList.get(i);
208             nextOne.doCloseCachedConnection();
209         }
210     }
211
212     public void clearoutJDBCPool() {
213         //this will clear out the HADB connection pool
214
String JavaDoc hadbPoolname = this.getHadbJdbcConnectionPoolNameFromConfig();
215         if(hadbPoolname == null) {
216             return;
217         }
218         ResourceSpec resourceSpec = new ResourceSpec(hadbPoolname,
219             ResourceSpec.JNDI_NAME);
220         resourceSpec.setConnectionPoolName(hadbPoolname);
221         if(resourceSpec == null) {
222             return;
223         }
224         Switch.getSwitch().getPoolManager().emptyResourcePool(resourceSpec);
225         resourceSpec = new ResourceSpec(hadbPoolname, ResourceSpec.JDBC_URL);
226         resourceSpec.setConnectionPoolName(hadbPoolname);
227         if(resourceSpec == null) {
228             return;
229         }
230         Switch.getSwitch().getPoolManager().emptyResourcePool(resourceSpec);
231     }
232     
233     private String JavaDoc getHadbJdbcConnectionPoolNameFromConfig() {
234         ServerConfigLookup configLookup = new ServerConfigLookup();
235         return configLookup.getHadbJdbcConnectionPoolNameFromConfig();
236     }
237     
238 }
239
240
241
Popular Tags