1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.wsrf.bootstrap;
36
37 import java.beans.Introspector;
38 import java.net.InetAddress;
39 import java.sql.Driver;
40 import java.sql.DriverManager;
41 import java.util.Enumeration;
42
43 import javax.servlet.ServletContext;
44 import javax.servlet.ServletContextEvent;
45 import javax.servlet.ServletContextListener;
46
47 import org.apache.axis2.context.ConfigurationContext;
48 import org.apache.log4j.Logger;
49 import org.apache.xml.resolver.tools.CatalogResolver;
50 import org.hsqldb.DatabaseManager;
51 import org.ogf.graap.wsag.server.persistence.EmfRegistry;
52
53
54
55
56
57 public class WSAG4JContextListener implements ServletContextListener
58 {
59
60
61
62
63 private static final int DB_WAITING_TIME = 2500;
64
65
66
67
68 private static final int MAX_DB_SHUTDOWN_RETRIES = 25;
69
70 private static final Logger LOG = Logger.getLogger( WSAG4JContextListener.class );
71
72
73
74
75
76
77 public void contextDestroyed( ServletContextEvent eContext )
78 {
79
80 BootstrapIsolationLayer isolationLayer =
81 (BootstrapIsolationLayer) eContext.getServletContext().getAttribute( "isolationLayer" );
82 LOG.info( "shutdown WSAG4J service" );
83
84 try
85 {
86 isolationLayer.shutdown();
87 isolationLayer = null;
88 }
89 catch ( Exception e )
90 {
91 String message = "WSAG4J shutdown process failed. Error: " + e.getMessage();
92 LOG.error( message );
93 }
94
95 BootstrapRegistry.finalizeBootstrapRegistry();
96
97
98
99
100
101
102
103 Thread runner = DatabaseManager.getTimer().getThread();
104 DatabaseManager.getTimer().shutdown();
105
106
107
108
109
110
111
112
113 int maxWaitingTime = MAX_DB_SHUTDOWN_RETRIES;
114
115 while ( runner.isAlive() )
116 {
117 if ( maxWaitingTime == 0 )
118 {
119 break;
120 }
121
122 maxWaitingTime--;
123
124 try
125 {
126 LOG.debug( "wait for HSQLDB to finish outstanding work..." );
127 synchronized ( this )
128 {
129 wait( DB_WAITING_TIME );
130 }
131 }
132 catch ( InterruptedException e )
133 {
134 break;
135 }
136 }
137
138
139
140
141 if ( runner.isAlive() )
142 {
143 LOG.warn( "HSQLDB failed to terminate normally. Send interupt." );
144 runner.interrupt();
145 }
146 else
147 {
148 LOG.info( "HSQLDB shutdown complete." );
149 }
150
151
152
153
154 LOG.debug( "cleanup database drivers..." );
155 Introspector.flushCaches();
156 for ( Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements(); )
157 {
158 Driver driver = e.nextElement();
159 if ( driver.getClass().getClassLoader() == getClass().getClassLoader() )
160 {
161 try
162 {
163 LOG.debug( "deregister: " + driver.getClass().getName() );
164 DriverManager.deregisterDriver( driver );
165 }
166 catch ( Exception ex )
167 {
168 LOG.warn( "failed to deregister driver.", ex );
169 }
170 }
171 }
172
173 LOG.info( "shutdown of WSAG4J service completed" );
174 }
175
176
177
178
179
180
181 public void contextInitialized( ServletContextEvent contextEvent )
182 {
183 synchronized ( WSAG4JContextListener.class )
184 {
185
186
187
188
189
190 System.setProperty( "xmlbean.entityResolver", CatalogResolver.class.getName() );
191
192 try
193 {
194
195
196
197 ServletContext context = contextEvent.getServletContext();
198 String path = context.getRealPath( "/" );
199 String defaultGatewayURI = getDefaultGatewayURI( context );
200 String datapath = path + "/WEB-INF/wsag4j-data";
201
202 if ( !System.getProperties().containsKey( EmfRegistry.WSAG4J_DATAPATH ) )
203 {
204 System.getProperties().setProperty( EmfRegistry.WSAG4J_DATAPATH, datapath );
205 }
206
207
208
209
210 BootstrapIsolationLayer isolationLayer =
211 new BootstrapIsolationLayer( path, defaultGatewayURI );
212 isolationLayer.initialize();
213
214 contextEvent.getServletContext().setAttribute( "isolationLayer", isolationLayer );
215 }
216 catch ( Exception e )
217 {
218 String message = "WSAG4J initialization process failed. Error: " + e.getMessage();
219 LOG.error( message );
220 }
221
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 protected String getDefaultGatewayURI( ServletContext context ) throws Exception
237 {
238 String contextPath = context.getResource( "/" ).getPath();
239 contextPath = contextPath.substring( 0, contextPath.lastIndexOf( "/" ) );
240 contextPath = contextPath.substring( contextPath.lastIndexOf( "/" ) + 1 );
241
242 try
243 {
244 String host = InetAddress.getLocalHost().getHostAddress();
245 ConfigurationContext axisContext =
246 (ConfigurationContext) context.getAttribute( Bootstrap.CONFIGURATION_CONTEXT );
247
248
249
250
251 String port = (String) axisContext.getProperty( "RUNNING_PORT" );
252 port = ( port == null ) ? "8080" : port;
253
254 String generatedGatewayURL = "http://" + host + ":" + port + "/" + contextPath;
255
256 return generatedGatewayURL;
257 }
258 catch ( Exception e )
259 {
260 LOG.trace( "Could not generate gateway url. Use default." );
261 String generatedGatewayURL = "http://localhost:8080/" + contextPath;
262 return generatedGatewayURL;
263 }
264 }
265
266 }