1 8 package org.apache.avalon.phoenix.frontends; 9 10 import java.io.File ; 11 import javax.servlet.ServletContext ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServlet ; 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 39 public class PhoenixServlet 40 extends HttpServlet 41 implements Runnable 42 { 43 private static final Resources REZ = 44 ResourceManager.getPackageResources( PhoenixServlet.class ); 45 46 private static final String DEFAULT_LOG_FILE = "/logs/phoenix.log"; 47 48 private static final String 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 getInitParameter( final String name, 57 final String defaultValue ) 58 { 59 final String 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 72 { 73 super.init(); 74 75 final ServletContext context = getServletContext(); 76 final String phoenixHome = getInitParameter( "phoenix.home", "/WEB-INF" ); 77 final String logDestination = getInitParameter( "log-destination" ); 78 final String logPriority = getInitParameter( "log-priority" ); 79 final String appName = getInitParameter( "application-name", "default" ); 80 final String appLoc = 81 getInitParameter( "application-location", phoenixHome + "/" + appName ); 82 final String 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 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 thread = new Thread ( this, "Phoenix-Monitor" ); 108 thread.start(); 109 } 110 catch( final Throwable 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 ( 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 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 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 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 164 { 165 final String phoenixHome = parameters.getParameter( "phoenix.home" ); 166 final String logDestination = 167 parameters.getParameter( "log-destination", phoenixHome + DEFAULT_LOG_FILE ); 168 final String logPriority = 169 parameters.getParameter( "log-priority", "INFO" ); 170 final AvalonFormatter formatter = new AvalonFormatter( DEFAULT_FORMAT ); 171 final File file = new File ( logDestination ); 172 final FileTarget logTarget = new FileTarget( file, false, formatter ); 173 174 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 |