You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jv...@apache.org on 2001/06/14 17:08:48 UTC

cvs commit: jakarta-turbine/src/java/org/apache/turbine Turbine.java

jvanzyl     01/06/14 08:08:48

  Modified:    src/java/org/apache/turbine Turbine.java
  Log:
  - changes to Turbine.java that now allow it to control the setup
    of the logging and resource usage. there's also some facade
    goodies at the bottom of the class that can be moved out
    or made permanent.
  
  Revision  Changes    Path
  1.59      +218 -72   jakarta-turbine/src/java/org/apache/turbine/Turbine.java
  
  Index: Turbine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine/src/java/org/apache/turbine/Turbine.java,v
  retrieving revision 1.58
  retrieving revision 1.59
  diff -u -r1.58 -r1.59
  --- Turbine.java	2001/05/23 15:42:15	1.58
  +++ Turbine.java	2001/06/14 15:08:47	1.59
  @@ -54,37 +54,32 @@
    * <http://www.apache.org/>.
    */
   
  -// Java Core Classes
   import java.io.IOException;
   import java.io.PrintWriter;
  +import java.io.FileInputStream;
   import java.util.Enumeration;
  -
  -// Java Servlet Classes
  +import java.util.Properties;
   import javax.servlet.ServletConfig;
  +import javax.servlet.ServletContext;
   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;
  -
  -// Turbine Modules
   import org.apache.turbine.modules.ActionLoader;
   import org.apache.turbine.modules.PageLoader;
   import org.apache.turbine.modules.actions.sessionvalidator.SessionValidator;
  -
  -// Turbine Utility Classes
   import org.apache.turbine.util.DynamicURI;
   import org.apache.turbine.util.Log;
   import org.apache.turbine.util.RunData;
   import org.apache.turbine.util.RunDataFactory;
   import org.apache.turbine.util.StringUtils;
   import org.apache.turbine.util.security.AccessControlList;
  -
  -//Turbine Services
   import org.apache.turbine.services.TurbineServices;
  -import org.apache.turbine.services.resources.TurbineResources;
  -import org.apache.turbine.services.logging.LoggingService;
   import org.apache.turbine.services.template.TurbineTemplate;
   
  +import org.apache.log4j.Category;
  +import org.apache.velocity.runtime.configuration.Configuration;
  +
   /**
    * Turbine is the main servlet for the entire system. It is <code>final</code>
    * because you should <i>not</i> ever need to subclass this servlet.  If you
  @@ -116,9 +111,11 @@
    * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
    * @author <a href="mailto:krzewski@e-point.pl">Rafal Krzewski</a>
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
  - * @version $Id: Turbine.java,v 1.58 2001/05/23 15:42:15 mikeh Exp $
  + * @version $Id: Turbine.java,v 1.59 2001/06/14 15:08:47 jvanzyl Exp $
    */
  -public class Turbine extends HttpServlet
  +public class Turbine 
  +    extends HttpServlet 
  +    implements TurbineConstants
   {
       /**
        * Name of path info parameter used to indicate the redirected stage of
  @@ -149,6 +146,8 @@
        */
       private static boolean firstDoGet = true;
   
  +    private static Configuration configuration;
  +
       /**
        * This init method will load the default resources from a
        * properties file.
  @@ -165,7 +164,7 @@
           {
               if(!firstInit)
               {
  -                log ("Double initializaton of Turbine was attempted!");
  +                Log.info ("Double initializaton of Turbine was attempted!");
                   return;
               }
               // executing init will trigger some static initializers, so we have
  @@ -174,24 +173,80 @@
   
               try
               {
  -                // Initalize TurbineServices and init bootstrap services
  -                TurbineServices services =
  +                // We want to save the ServletConfig and
  +                // ServletContext so that we can share these objects
  +                // with parts of Turbine that may need access and 
  +                // have no direct contact with RunData. Right now
  +                // the ServletService is providing this information,
  +                // but that service could disappear and client code
  +                // could use the methods now provided in the Turbine
  +                // class.
  +                setTurbineServletConfig(getServletConfig());
  +                setTurbineServletContext(getServletContext());
  +            
  +                // Set the applicationRoot for this webapp.
  +                setApplicationRoot(getServletContext().getRealPath("/"));
  +                
  +                // Get the instance of the service manager
  +                TurbineServices serviceManager =
                       (TurbineServices) TurbineServices.getInstance();
  -
  -                // Initialize essential services (Resources & Logging)
  -                services.initPrimaryServices(config);
  -
  -                // Initialize other services that require early init
  -                services.initServices(config, false);
  +                
  +                // This is for our log4j setup. We are embedding
  +                // the log4j configuration in the TRP but log4j
  +                // must be initialized with a Properties object.
  +                // We load it here so that we can add a "webapp"
  +                // property that will be used in the form of
  +                // ${webapp} in log4j properties. We need this
  +                // so that the log files can be placed in
  +                // the webapp space.
  +                Properties p = new Properties();
  +                
  +                p.load(new FileInputStream(
  +                    getApplicationRoot() + DEFAULT_TURBINE_RESOURCES));
  +                
  +                p.setProperty("webapp", getApplicationRoot());
  +                
  +                // We are still using our Log facade but we
  +                // should move toward using log4j correctly.
  +                Log.setProperties(p);
  +                Log.init();
  +                
  +                // Set the service managers application root. In our
  +                // case it is the webapp context.
  +                serviceManager.setApplicationRoot(getApplicationRoot());
  +                
  +                // This should eventually be a Configuration
  +                // interface so that service and app configuration
  +                // can be stored anywhere.
  +                configuration = new Configuration(
  +                    getApplicationRoot() + DEFAULT_TURBINE_RESOURCES);
  +                
  +                serviceManager.setConfiguration(configuration);
  +                
  +                // We are using the 'default' category for logging
  +                // when a category isn't specified. The 'default'
  +                // category must be setup in the TRP.
  +                serviceManager.setCategory(Category.getInstance("default"));
  +                
  +                // The TurbineResourceService needs to access the
  +                // whole configuration file because it really has
  +                // no configuration of its own.
  +                serviceManager.setServiceObject("configuration", configuration);
  +                
  +                // Initialize the service manager. Services
  +                // that have its 'earlyInit' property set to
  +                // a value of 'true' will be started when
  +                // the service manager is initialized.
  +                serviceManager.init();
               }
               catch ( Exception e )
               {
                   // save the exception to complain loudly later :-)
                   initFailure = e;
  -                log ("Turbine: init() failed: " + StringUtils.stackTrace(e));
  +                Log.info ("Turbine: init() failed: " + StringUtils.stackTrace(e));
                   return;
               }
  -            log ("Turbine: init() Ready to Rumble!");
  +            Log.info ("Turbine: init() Ready to Rumble!");
           }
       }
   
  @@ -209,10 +264,13 @@
               {
                   if (firstDoGet)
                   {
  -                    log("Turbine: Starting HTTP initialization of services");
  -                    TurbineServices.getInstance().initServices(data);
  -                    log("Turbine: Completed HTTP initialization of services");
  -
  +                    // All we want to do here is save some servlet
  +                    // information so that services and processes
  +                    // that don't have direct access to a RunData
  +                    // object can still know something about
  +                    // the servlet environment.
  +                    saveServletInfo(data);
  +                    
                       // Mark that we're done.
                       firstDoGet = false;
                  }
  @@ -230,7 +288,7 @@
           TurbineServices.getInstance().shutdownServices();
           System.gc();
   
  -        log("Turbine: Done shutting down!");
  +        Log.info("Turbine: Done shutting down!");
       }
   
       /**
  @@ -241,8 +299,7 @@
        * @exception IOException a servlet exception.
        * @exception ServletException a servlet exception.
        */
  -    public final void doGet (HttpServletRequest req,
  -                       HttpServletResponse res)
  +    public final void doGet (HttpServletRequest req, HttpServletResponse res)
           throws IOException,
                  ServletException
       {
  @@ -258,9 +315,8 @@
   
               // Get general RunData here...
               // Perform turbine specific initialization below.
  -            data = RunDataFactory.getRunData( req, res,
  -                                              getServletConfig() );
  -
  +            data = RunDataFactory.getRunData(req, res, getServletConfig());
  +            
               // If this is the first invocation, perform some
               // initialization.  Certain services need RunData to initialize
               // themselves.
  @@ -268,7 +324,7 @@
   
               // Get the instance of the Session Validator.
               SessionValidator sessionValidator = (SessionValidator)ActionLoader
  -                .getInstance().getInstance(TurbineResources.getString(
  +                .getInstance().getInstance(configuration.getString(
                       "action.sessionvalidator"));
   
               // if this is the redirected stage of the initial request, 
  @@ -281,8 +337,7 @@
                   if (data.getSession().isNew())
                   {
                       String message = "Infinite redirect detected...";
  -                    log(message);
  -                    Log.error(message);
  +                    Log.info(message);
                       throw new Exception(message);
                   }
                   else
  @@ -340,7 +395,7 @@
                       // as the session is new take this opportunity to 
                       // set the session timeout if specified in TR.properties
                       int timeout = 
  -                        TurbineResources.getInt("session.timeout", -1);
  +                        configuration.getInt("session.timeout", -1);
                       
                       if (timeout != -1)
                       {
  @@ -362,9 +417,9 @@
               // mandate its page selection policy for non-logged in users
               // after the logout has taken place.
               if ( data.hasAction()
  -                    && data.getAction().equalsIgnoreCase(TurbineResources
  +                    && data.getAction().equalsIgnoreCase(configuration
                               .getString("action.login"))
  -                    || data.getAction().equalsIgnoreCase(TurbineResources
  +                    || data.getAction().equalsIgnoreCase(configuration
                               .getString("action.logout")))
               {
                   // If a User is logging in, we should refresh the
  @@ -378,7 +433,7 @@
                   // associated with the previous User.  Currently the
                   // only keys stored in the session are "turbine.user"
                   // and "turbine.acl".
  -                if (data.getAction().equalsIgnoreCase(TurbineResources
  +                if (data.getAction().equalsIgnoreCase(configuration
                           .getString("action.login")))
                   {
                       String[] names = data.getSession().getValueNames();
  @@ -403,7 +458,7 @@
               // TurbineResources.properties...screen.homepage; or, you
               // can specify your own SessionValidator action.
               ActionLoader.getInstance().exec(
  -                data,TurbineResources.getString("action.sessionvalidator") );
  +                data,configuration.getString("action.sessionvalidator") );
   
               // Put the Access Control List into the RunData object, so
               // it is easily available to modules.  It is also placed
  @@ -411,7 +466,7 @@
               // out the ACL to force it to be rebuilt based on more
               // information.
               ActionLoader.getInstance().exec(
  -                data,TurbineResources.getString("action.accesscontroller"));
  +                data,configuration.getString("action.accesscontroller"));
   
               // Start the execution phase. DefaultPage will execute the
               // appropriate action as well as get the Layout from the
  @@ -427,20 +482,20 @@
               
               String defaultPage = TurbineTemplate.getDefaultPageName(data);
               
  +            Log.debug("Default Page Module: " + defaultPage);
  +            
               if (defaultPage == null)
               {
  -                /*
  -                 * In this case none of the template services are running.
  -                 * The application may be using ECS for views, or a
  -                 * decendent of RawScreen is trying to produce output.
  -                 * If there is a 'page.default' property in the TR.props
  -                 * then use that, otherwise return DefaultPage which will
  -                 * handle ECS view scenerios and RawScreen scenerios. The
  -                 * app developer can still specify the 'page.default'
  -                 * if they wish but the DefaultPage should work in
  -                 * most cases.
  -                 */
  -                defaultPage = TurbineResources.getString(
  +                // In this case none of the template services are running.
  +                // The application may be using ECS for views, or a
  +                // decendent of RawScreen is trying to produce output.
  +                // If there is a 'page.default' property in the TR.props
  +                // then use that, otherwise return DefaultPage which will
  +                // handle ECS view scenerios and RawScreen scenerios. The
  +                // app developer can still specify the 'page.default'
  +                // if they wish but the DefaultPage should work in
  +                // most cases.
  +                defaultPage = configuration.getString(
                       "page.default", "DefaultPage");
               }
               
  @@ -467,10 +522,8 @@
                   if ( data.isPageSet() && data.isOutSet() == false )
                   {
                       // Modules can override these.
  -                    data.getResponse()
  -                        .setLocale( data.getLocale() );
  -                    data.getResponse()
  -                        .setContentType( data.getContentType() );
  +                    data.getResponse().setLocale(data.getLocale());
  +                    data.getResponse().setContentType(data.getContentType());
   
                       // Handle the case where a module may want to send
                       // a redirect.
  @@ -530,7 +583,7 @@
        * @exception ServletException a servlet exception.
        */
       public final void doPost (HttpServletRequest req,
  -                        HttpServletResponse res)
  +                              HttpServletResponse res)
           throws IOException,
                  ServletException
       {
  @@ -561,8 +614,8 @@
        * @param e The exception to report.
        */
       private final void handleException(RunData data,
  -                                 HttpServletResponse res,
  -                                 Throwable t)
  +                                       HttpServletResponse res,
  +                                       Throwable t)
       {
           // make sure that the stack trace makes it the log
           Log.error("Turbine.handleException: "+t.getMessage());
  @@ -576,22 +629,22 @@
               data.setStackTrace(StringUtils.stackTrace(t),t);
   
               // setup the screen
  -            data.setScreen(TurbineResources
  -                           .getString("screen.error"));
  +            data.setScreen(
  +                configuration.getString("screen.error"));
   
               // do more screen setup for template execution if needed
               if (data.getTemplateInfo() != null)
  -                data.getTemplateInfo()
  -                    .setScreenTemplate(TurbineResources
  -                                       .getString("template.error"));
  +            {
  +                data.getTemplateInfo().setScreenTemplate(
  +                    configuration.getString("template.error"));
  +            }                                       
   
               // Make sure to not execute an action.
               data.setAction ("");
   
               PageLoader.getInstance()
  -                .exec(data,
  -                      TurbineResources.getString("page.default", 
  -                      "DefaultPage"));
  +                .exec(data, configuration.getString(
  +                    "page.default", "DefaultPage"));
   
               data.getResponse().setContentType( data.getContentType() );
               data.getResponse().setStatus ( data.getStatusCode() );
  @@ -617,8 +670,8 @@
               }
               catch (IOException ignored) {}
   
  -            log ( data.getStackTrace() );
  -            org.apache.turbine.util.Log.error ( e.getMessage(), e );
  +            Log.info ( data.getStackTrace() );
  +            Log.error ( e.getMessage(), e );
           }
           // Attempt to do *something* at this point...
           catch ( Throwable reallyScrewedNow )
  @@ -644,4 +697,97 @@
                       reallyScrewedNow.getMessage(), reallyScrewedNow );
           }
       }
  +
  +    // These can probably go into a helper class that
  +    // the main Turbine class could use. It would probably
  +    // be wise to build up an contract that is accessible
  +    // via the Turbine class so that we can try to develop
  +    // a single point of access for our users. We can
  +    // slowly start closing off some of our exposed
  +    // internals.
  +    //
  +    // This is information that is collected from
  +    // the first request. This information is used by
  +    // services that previously needed to be initialized
  +    // with a RunData object. These values are in no
  +    // way meant to be used on a per request basis.
  +    //
  +    // There are only a couple of services that needed
  +    // RunData for initialization but these values are
  +    // very useful to processes which have no direct
  +    // access to a RunData object. A scheduled job,
  +    // for example, might want to process something
  +    // and place it within the webapp space. Having
  +    // these values stored here makes this possible.
  +
  +    private static String serverName;
  +    private static String serverPort;
  +    private static String serverScheme;
  +    private static String scriptName;
  +    private static String applicationRoot;
  +    private static ServletConfig servletConfig;
  +    private static ServletContext servletContext;
  +
  +    public void saveServletInfo(RunData data)
  +    {
  +        serverName = data.getRequest().getServerName();
  +        serverPort = new Integer(data.getRequest().getServerPort()).toString();
  +        serverScheme = data.getRequest().getScheme();
  +        scriptName = applicationRoot + data.getRequest().getServletPath();
  +    }
  +
  +    public void setApplicationRoot(String applicationRoot)
  +    {
  +        this.applicationRoot = applicationRoot;
  +    }        
  +
  +    public static String getApplicationRoot()
  +    {
  +        return applicationRoot;
  +    }
  +
  +    public static String getServerName()
  +    {
  +        return serverName;
  +    }
  +
  +    public static String getServerPort()
  +    {
  +        return serverPort;
  +    }
  +
  +    public static String getServerScheme()
  +    {
  +        return serverScheme;
  +    }
  +    
  +    public static String getScriptName()
  +    {
  +        return scriptName;
  +    }
  +
  +    public static void setTurbineServletConfig(ServletConfig s)
  +    {
  +        servletConfig = s;
  +    }        
  +
  +    public static ServletConfig getTurbineServletConfig()
  +    {
  +        return servletConfig;
  +    }
  +    
  +    public static void setTurbineServletContext(ServletContext s)
  +    {
  +        servletContext = s;
  +    }        
  +    
  +    public static ServletContext getTurbineServletContext()
  +    {
  +        return servletContext;
  +    }        
  +
  +    public static Configuration getConfiguration()
  +    {
  +        return configuration;
  +    }        
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org