KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > frontends > PhoenixServlet


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.frontends;
9
10 import java.io.File JavaDoc;
11 import javax.servlet.ServletContext JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServlet JavaDoc;
14 import org.apache.avalon.excalibur.i18n.ResourceManager;
15 import org.apache.avalon.excalibur.i18n.Resources;
16 import org.apache.avalon.framework.CascadingRuntimeException;
17 import org.apache.avalon.framework.ExceptionUtil;
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
20 import org.apache.avalon.framework.container.ContainerUtil;
21 import org.apache.avalon.framework.logger.AvalonFormatter;
22 import org.apache.avalon.framework.logger.LogKitLogger;
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.phoenix.components.embeddor.SingleAppEmbeddor;
26 import org.apache.avalon.phoenix.interfaces.Embeddor;
27 import org.apache.log.Hierarchy;
28 import org.apache.log.LogTarget;
29 import org.apache.log.Priority;
30 import org.apache.log.output.io.FileTarget;
31
32 /**
33  * WARNING: DO NOT USE THIS SERVLET FOR PRODUCTION SERVICE. THIS IS EXPERIMENTAL.
34  * Servlet frontends for SingleAppEmbeddor.
35  *
36  * @author <a HREF="mailto:colus@apache.org">Eung-ju Park</a>
37  * @deprecated
38  */

39 public class PhoenixServlet
40     extends HttpServlet JavaDoc
41     implements Runnable JavaDoc
42 {
43     private static final Resources REZ =
44         ResourceManager.getPackageResources( PhoenixServlet.class );
45
46     private static final String JavaDoc DEFAULT_LOG_FILE = "/logs/phoenix.log";
47
48     private static final String JavaDoc DEFAULT_FORMAT =
49         "%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} [%8.8{category}] (%{context}): "
50         + "%{message}\n%{throwable}";
51
52     private Parameters m_parameters;
53
54     private SingleAppEmbeddor m_embeddor;
55
56     private String JavaDoc getInitParameter( final String JavaDoc name,
57                                      final String JavaDoc defaultValue )
58     {
59         final String JavaDoc value = getInitParameter( name );
60         if( null == value )
61         {
62             return defaultValue;
63         }
64         else
65         {
66             return value;
67         }
68     }
69
70     public void init()
71         throws ServletException JavaDoc
72     {
73         super.init();
74
75         final ServletContext JavaDoc context = getServletContext();
76         final String JavaDoc phoenixHome = getInitParameter( "phoenix.home", "/WEB-INF" );
77         final String JavaDoc logDestination = getInitParameter( "log-destination" );
78         final String JavaDoc logPriority = getInitParameter( "log-priority" );
79         final String JavaDoc appName = getInitParameter( "application-name", "default" );
80         final String JavaDoc appLoc =
81             getInitParameter( "application-location", phoenixHome + "/" + appName );
82         final String JavaDoc configFile =
83             getInitParameter( "config-file", phoenixHome + "/conf/kernel.xml" );
84
85         m_parameters = new Parameters();
86         m_parameters.setParameter( "phoenix.home", context.getRealPath( phoenixHome ) );
87         m_parameters.setParameter( "log-destination", context.getRealPath( logDestination ) );
88         m_parameters.setParameter( "log-priority", logPriority );
89         m_parameters.setParameter( "application-name", appName );
90         m_parameters.setParameter( "application-location", context.getRealPath( appLoc ) );
91
92         try
93         {
94             final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
95             final Configuration kernelConf =
96                 builder.buildFromFile( context.getRealPath( configFile ) );
97             final Configuration embeddorConf = kernelConf.getChild( "embeddor" );
98             final String JavaDoc embeddorClassname = embeddorConf.getAttribute( "class" );
99
100             m_embeddor = (SingleAppEmbeddor)Class.forName( embeddorClassname ).newInstance();
101
102             ContainerUtil.enableLogging( m_embeddor, createLogger( m_parameters ) );
103             ContainerUtil.parameterize( m_embeddor, m_parameters );
104             ContainerUtil.configure( m_embeddor, embeddorConf );
105             ContainerUtil.initialize( m_embeddor );
106
107             final Thread JavaDoc thread = new Thread JavaDoc( this, "Phoenix-Monitor" );
108             thread.start();
109         }
110         catch( final Throwable JavaDoc throwable )
111         {
112             log( REZ.getString( "main.exception.header" ) );
113             log( "---------------------------------------------------------" );
114             log( ExceptionUtil.printStackTrace( throwable ) );
115             log( "---------------------------------------------------------" );
116             log( REZ.getString( "main.exception.footer" ) );
117             throw new ServletException JavaDoc( throwable );
118         }
119
120         getServletContext().setAttribute( Embeddor.ROLE, m_embeddor );
121     }
122
123     public void run()
124     {
125         try
126         {
127             m_embeddor.execute();
128         }
129         catch( final Throwable JavaDoc throwable )
130         {
131             log( REZ.getString( "main.exception.header" ) );
132             log( "---------------------------------------------------------" );
133             log( ExceptionUtil.printStackTrace( throwable ) );
134             log( "---------------------------------------------------------" );
135             log( REZ.getString( "main.exception.footer" ) );
136
137             final String JavaDoc message = REZ.getString( "servlet.error.execute" );
138             throw new CascadingRuntimeException( message, throwable );
139         }
140     }
141
142     public void destroy()
143     {
144         getServletContext().removeAttribute( Embeddor.ROLE );
145
146         try
147         {
148             ContainerUtil.shutdown( m_embeddor );
149         }
150         catch( final Throwable JavaDoc throwable )
151         {
152             log( REZ.getString( "main.exception.header" ) );
153             log( "---------------------------------------------------------" );
154             log( ExceptionUtil.printStackTrace( throwable ) );
155             log( "---------------------------------------------------------" );
156             log( REZ.getString( "main.exception.footer" ) );
157         }
158         m_embeddor = null;
159         m_parameters = null;
160     }
161
162     private Logger createLogger( final Parameters parameters )
163         throws Exception JavaDoc
164     {
165         final String JavaDoc phoenixHome = parameters.getParameter( "phoenix.home" );
166         final String JavaDoc logDestination =
167             parameters.getParameter( "log-destination", phoenixHome + DEFAULT_LOG_FILE );
168         final String JavaDoc logPriority =
169             parameters.getParameter( "log-priority", "INFO" );
170         final AvalonFormatter formatter = new AvalonFormatter( DEFAULT_FORMAT );
171         final File JavaDoc file = new File JavaDoc( logDestination );
172         final FileTarget logTarget = new FileTarget( file, false, formatter );
173
174         //Create an anonymous hierarchy so no other
175
//components can get access to logging hierarchy
176
final Hierarchy hierarchy = new Hierarchy();
177         final org.apache.log.Logger logger = hierarchy.getLoggerFor( "Phoenix" );
178         logger.setLogTargets( new LogTarget[]{logTarget} );
179         logger.setPriority( Priority.getPriorityForName( logPriority ) );
180         logger.info( "Logger started" );
181         return new LogKitLogger( logger );
182     }
183 }
184
Popular Tags