Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.tutorial.services » [javadoc | source]
    1   // Copyright 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.tutorial.services;
   16   
   17   import org.apache.tapestry5.SymbolConstants;
   18   import org.apache.tapestry5.ioc.MappedConfiguration;
   19   import org.apache.tapestry5.ioc.OrderedConfiguration;
   20   import org.apache.tapestry5.ioc.annotations.Local;
   21   import org.apache.tapestry5.services.Request;
   22   import org.apache.tapestry5.services.RequestFilter;
   23   import org.apache.tapestry5.services.RequestHandler;
   24   import org.apache.tapestry5.services.Response;
   25   import org.slf4j.Logger;
   26   
   27   import java.io.IOException;
   28   
   29   /**
   30    * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to configure and extend
   31    * Tapestry, or to place your own services.
   32    */
   33   public class AppModule
   34   {
   35       public static void contributeApplicationDefaults(
   36               MappedConfiguration<String, String> configuration)
   37       {
   38           // Contributions to ApplicationDefaults will override any contributions to
   39           // FactoryDefaults (with the same key). Here we're restricting the supported
   40           // locales to just "en" (English). As you add localised message catalogs and other assets,
   41           // you can extend this list of locales (it's a comma seperated series of locale names;
   42           // the first locale name is the default when there's no reasonable match).
   43   
   44           configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
   45           configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
   46       }
   47   
   48       /**
   49        * This is a service definition, the service will be named TimingFilter. The interface, RequestFilter, is used
   50        * within the RequestHandler service pipeline, which is built from the RequestHandler service configuration.
   51        * Tapestry IoC is responsible for passing in an appropriate Log instance. Requests for static resources are handled
   52        * at a higher level, so this filter will only be invoked for Tapestry related requests.
   53        */
   54       public RequestFilter buildTimingFilter(final Logger logger)
   55       {
   56           return new RequestFilter()
   57           {
   58               public boolean service(Request request, Response response, RequestHandler handler)
   59                       throws IOException
   60               {
   61                   long startTime = System.currentTimeMillis();
   62   
   63                   try
   64                   {
   65                       // The reponsibility of a filter is to invoke the corresponding method
   66                       // in the handler. When you chain multiple filters together, each filter
   67                       // received a handler that is a bridge to the next filter.
   68   
   69                       return handler.service(request, response);
   70                   }
   71                   finally
   72                   {
   73                       long elapsed = System.currentTimeMillis() - startTime;
   74   
   75                       logger.info(String.format("Request time: %d ms", elapsed));
   76                   }
   77               }
   78           };
   79       }
   80   
   81       /**
   82        * This is a contribution to the RequestHandler service configuration. This is how we extend Tapestry using the
   83        * timing filter. A common use for this kind of filter is transaction management or security.
   84        */
   85       public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
   86   
   87                                            @Local
   88                                            RequestFilter filter)
   89       {
   90           // Each contribution to an ordered configuration has a name, When necessary, you may
   91           // set constraints to precisely control the invocation order of the contributed filter
   92           // within the pipeline.
   93   
   94           configuration.add("Timing", filter);
   95       }
   96   }

Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.tutorial.services » [javadoc | source]