OW2 Consortium telosys

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13 lguerin 1
package org.objectweb.telosys.common;
2
 
3
import java.util.Properties;
4
 
5
import javax.servlet.ServletContext;
6
import javax.servlet.ServletContextEvent;
7
import javax.servlet.ServletContextListener;
8
 
9
import org.objectweb.telosys.dal.tools.ConnectionPoolThread;
10
import org.objectweb.telosys.screen.env.ScreenApplication;
11
import org.objectweb.telosys.screen.env.ScreenApplicationManager;
12
import org.objectweb.telosys.util.StrUtil;
13
 
14
/**
15
 *
16
 * @author Laurent GUERIN
17
 *
18
 */
19
public abstract class TelosysContextListener implements ServletContextListener {
20
 
21
    private static final String SEPARATOR1 = "===========================================================================";
22
 
23
    private static final String SEPARATOR2 = "---------------------------------------------------------------------------";
24
 
25
    private static final TelosysClassLogger log = new TelosysClassLogger(TelosysContextListener.class);
26
 
27
    private ConnectionPoolThread _connectionPoolThread = null ;
28
 
29
    /**
30
     * Constructs a ServletContextListener dedicated to a Telosys web application
31
     */
32
    public TelosysContextListener()
33
    {
34
        super();
35
        log.info("Context listener instance created.");
36
    }
37
 
38
	/**
39
	 * First initialization step : Telosys initialization
40
	 * @param servletContext
41
	 * @param screenApplication
42
	 */
43
	public abstract void initTelosys(ServletContext servletContext, ScreenApplication screenApplication);
44
 
45
	/**
46
	 * Second initialization step : Application initialization
47
	 * @param servletContext
48
	 * @param screenApplication
49
	 */
50
	public abstract void initApplication(ServletContext servletContext, ScreenApplication screenApplication);
51
 
52
 
53
	/* (non-Javadoc)
54
	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
55
	 */
56
	public void contextInitialized(ServletContextEvent event)
57
	{
58
        log.trace("context initialized");
59
        ServletContext servletContext = event.getServletContext();
60
 
61
        info(SEPARATOR1);
62
        info("Application start-up ...");
63
        info(SEPARATOR1);
64
 
65
        //--- STEP 1 : TELOSYS STARTUP
66
        infoStep(1);
67
        Telosys.startup(servletContext);
68
        printContextInfo(servletContext);
69
        info(SEPARATOR2);
70
 
71
        printTelosysInfo();
72
        info(SEPARATOR2);
73
 
74
 
75
        //--- STEP 2 : CHECK SCREEN APPLICATION OBJECT
76
        infoStep(2);
77
//        ScreenApplication screenApplication = ScreenApplicationManager.openScreenApplication ( servletContext );
78
//        if ( screenApplication != null )
79
//        {
80
//            info("ScreenApplication instance created.");
81
//        }
82
//        else
83
//        {
84
//            error("Cannot open ScreenApplication !" );
85
//        }
86
        ScreenApplication screenApplication = ScreenApplicationManager.getScreenApplication();
87
        if ( screenApplication != null )
88
        {
89
            info("ScreenApplication ready.");
90
        }
91
        else
92
        {
93
            error("Cannot get ScreenApplication !" );
94
        }
95
 
96
        //--- STEP 3 : DELEGATE TELOSYS INITIALIZATION
97
        infoStep(3);
98
        info("Telosys initialization ...");
99
        try {
100
            initTelosys(servletContext, screenApplication);
101
        } catch (Exception e)
102
        {
103
            //error("Exception in initialization ! ");
104
            error("Exception = " + e.toString() + " - Message = " + e.getMessage());
105
            e.printStackTrace();
106
        } catch (Throwable t)
107
        {
108
            //error("Throwable exception in initialization ! ");
109
            error("Throwable exception = " + t.toString() + " - Message = " + t.getMessage());
110
            t.printStackTrace();
111
        }
112
        info(SEPARATOR2);
113
 
114
        printTelosysConfig();
115
        info(SEPARATOR2);
116
 
117
        //--- STEP 4 : DELEGATE APPLICATION INITIALIZATION
118
        infoStep(4);
119
        info("Application initialization ...");
120
        try {
121
            initApplication(servletContext, screenApplication);
122
        } catch (Exception e)
123
        {
124
            //error("Exception in initialization ! ");
125
            error("Exception = " + e.toString() + " - Message = " + e.getMessage());
126
            e.printStackTrace();
127
        } catch (Throwable t)
128
        {
129
            //error("Throwable exception in initialization ! ");
130
            error("Throwable exception = " + t.toString() + " - Message = " + t.getMessage());
131
            t.printStackTrace();
132
        }
133
 
134
        //--- STEP 5 : CONNECTION POOL THREAD MANAGEMENT
135
        infoStep(5);
136
        startConnectionPoolThread();
137
 
138
        info(SEPARATOR1);
139
        info("End of application start-up.");
140
        info(SEPARATOR1);
141
	}
142
 
143
 
144
	/* (non-Javadoc)
145
	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
146
	 */
147
	public void contextDestroyed(ServletContextEvent event)
148
	{
149
        info(SEPARATOR1);
150
        stopConnectionPoolThread() ;
151
        info(SEPARATOR1);
152
        // TODO : Stop ConnectionPoolThread
153
        info("Context destroyed.");
154
        info(SEPARATOR1);
155
	}
156
 
157
    //-----------------------------------------------------------------------------
158
    /**
159
     * Print the step message
160
     * @param step
161
     */
162
    public void infoStep(int step)
163
    {
164
        info("========== STEP " + step + " ...");
165
    }
166
 
167
    //-----------------------------------------------------------------------------
168
    /**
169
     * Print a standard info message, using the active loggers
170
     * @param s
171
     */
172
    public void info(String s)
173
    {
174
        Telosys.info(this.getClass().getName(), s);
175
    }
176
 
177
    //-----------------------------------------------------------------------------
178
    /**
179
     * Print a standard error message, using the active loggers
180
     * @param sMsg
181
     */
182
    public void error(String sMsg)
183
    {
184
        Telosys.error(this.getClass().getName(), sMsg);
185
    }
186
 
187
    //-----------------------------------------------------------------------------
188
//    private void printSeparator()
189
//    {
190
//        info(SEPARATOR);
191
//    }
192
    //-----------------------------------------------------------------------------
193
    private void printContextInfo(ServletContext context)
194
    {
195
	    if (context != null)
196
	    {
197
	        info("ServletContext : " );
198
	        info(" . Server Info     : " + context.getServerInfo());
199
	        info(" . Version         : " + context.getMajorVersion() + "." + context.getMinorVersion());
200
	        info(" . Context Name    : " + context.getServletContextName());
201
	        info(" . Real Path       : " + context.getRealPath(""));
202
	    }
203
	    else
204
	    {
205
	        //info("Context not found ! ");
206
	        error("ServletContext is null !");
207
	    }
208
    }
209
    //-----------------------------------------------------------------------------
210
    private void printTelosysInfo()
211
    {
212
        info("TELOSYS Version : " + Telosys.getVersion() );
213
    }
214
    //-----------------------------------------------------------------------------
215
    private void printTelosysConfig()
216
    {
217
        info("Telosys configuration");
218
        info(" . Web App Context Name    : " + Telosys.getWebAppName() );
219
        info(" . Web App Root Directory  : " + Telosys.getWebAppRootDir() );
220
        //info(" . Configuration directory = " + Telosys.getConfigDirectory());
221
        //info(" . transform('./myfile.txt') = " + Telosys.transformFileName("./myfile.txt"));
222
        //info(" . transform('$ROOT/myfile.txt') = " + Telosys.transformFileName("$ROOT/myfile.txt"));
223
        //info(" . Properties file name    = " + Telosys.getPropertiesFile());
224
        info(" . SAX parser class        : " + Telosys.getSAXParserClassName() );
225
        info(" . Authentication required : " + Telosys.isAuthenticationRequired() ) ;
226
        info(" . Factory                 : " + ( Telosys.getFactory() != null ? "ready" : "not set !" ) ) ;
227
    }
228
 
229
    //-----------------------------------------------------------------------------
230
    /**
231
     * Starts the Connection Pool Thread if necessary <br>
232
     * ( if there's a property set in telosys.properties )
233
     *
234
     * @since v 1.0.2
235
     */
236
    private void startConnectionPoolThread()
237
	{
238
        info("Start ConnectionPoolThread management ...");
239
        int iSleepDuration = -1 ;
240
        Properties prop = Telosys.getProperties();
241
        if ( prop != null )
242
        {
243
	        String sSleepDuration = prop.getProperty("ConnectionPoolThread.sleepDuration");
244
	        if ( sSleepDuration != null )
245
	        {
246
	            iSleepDuration = StrUtil.getInt(sSleepDuration, -1);
247
	        }
248
	        if ( iSleepDuration > 0 )
249
	        {
250
	            if ( iSleepDuration < 600 ) // Less than 10 minutes is not acceptable
251
	            {
252
		            info("Duration property increased to 600 seconds ( original value = " + iSleepDuration + " )");
253
	                iSleepDuration = 600 ;
254
	            }
255
		        try
256
		        {
257
		            ConnectionPoolThread thread = new ConnectionPoolThread(iSleepDuration);
258
		            thread.start();
259
		            info("Thread " + thread.toString() + " started : sleep duration = " + iSleepDuration );
260
		            _connectionPoolThread = thread ;
261
		        } catch (Throwable t)
262
		        {
263
		            error("Exception : " + t.toString() + " - Message = " + t.getMessage());
264
		            t.printStackTrace();
265
		        }
266
	        }
267
	        else
268
	        {
269
	            info("No property for ConnectionPoolThread => thread not started.");
270
	        }
271
        }
272
        else
273
        {
274
	        info("Telosys properties not loaded => thread not started.");
275
	    }
276
	}
277
 
278
    private void stopConnectionPoolThread()
279
	{
280
        if ( _connectionPoolThread != null )
281
        {
282
            info("Stop ConnectionPoolThread management ...");
283
            _connectionPoolThread.interrupt();
284
        }
285
        else
286
        {
287
            info("No ConnectionPoolThread to stop.");
288
        }
289
	}
290
 
291
}