1 /** 2 * This file is part of the equanda project. 3 * 4 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/ 7 * 8 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 9 * ANY KIND, either express or implied. See the License for the specific language governing rights and 10 * limitations under the License. 11 * 12 * Alternatively, the contents of this file may be used under the terms of 13 * either the GNU General Public License Version 2 or later (the "GPL"), or 14 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 15 * in which case the provisions of the GPL or the LGPL are applicable instead 16 * of those above. If you wish to allow use of your version of this file only 17 * under the terms of either the GPL or the LGPL, and not to allow others to 18 * use your version of this file under the terms of the MPL, indicate your 19 * decision by deleting the provisions above and replace them with the notice 20 * and other provisions required by the GPL or the LGPL. If you do not delete 21 * the provisions above, a recipient may use your version of this file under 22 * the terms of any one of the MPL, the GPL or the LGPL. 23 */ 24 25 package org.equanda.test.gui.services; 26 27 import javassist.runtime.Desc; 28 import org.apache.tapestry5.Translator; 29 import org.apache.tapestry5.ioc.Configuration; 30 import org.apache.tapestry5.ioc.MappedConfiguration; 31 import org.apache.tapestry5.ioc.OrderedConfiguration; 32 import org.apache.tapestry5.ioc.ServiceBinder; 33 import org.apache.tapestry5.ioc.annotations.InjectService; 34 import org.apache.tapestry5.ioc.services.Coercion; 35 import org.apache.tapestry5.ioc.services.CoercionTuple; 36 import org.apache.tapestry5.services.Request; 37 import org.apache.tapestry5.services.RequestFilter; 38 import org.apache.tapestry5.services.RequestHandler; 39 import org.apache.tapestry5.services.Response; 40 import org.slf4j.Logger; 41 42 import java.io.IOException; 43 44 /** 45 * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to configure and extend 46 * Tapestry, or to place your own service definitions. 47 */ 48 public class AppModule 49 { 50 /** 51 * Bind extra services. 52 * 53 * @param binder object to bind services to 54 */ 55 public static void bind( ServiceBinder binder ) 56 { 57 // binder.bind(MyServiceInterface.class, MyServiceImpl.class); 58 59 // Make bind() calls on the binder object to define most IoC services. 60 // Use service builder methods (example below) when the implementation 61 // is provided inline, or requires more initialization than simply 62 // invoking the constructor. 63 64 // assure that the ContextClassLoader is used by javassist (seems to not be the default) 65 System.out.println( "Desc.useContextClassLoader = " + Desc.useContextClassLoader ); 66 javassist.runtime.Desc.useContextClassLoader = true; 67 } 68 69 /** 70 * Contribute application defaults 71 * 72 * @param configuration configuration to add to 73 */ 74 public static void contributeApplicationDefaults( 75 MappedConfiguration<String, String> configuration ) 76 { 77 // Contributions to ApplicationDefaults will override any contributions to 78 // FactoryDefaults (with the same key). Here we're restricting the supported 79 // locales to just "en" (English). As you add localised message catalogs and other assets, 80 // you can extend this list of locales (it's a comma seperated series of locale names; 81 // the first locale name is the default when there's no reasonable match). 82 83 configuration.add( "tapestry.supported-locales", "en,nl" ); 84 } 85 86 87 /** 88 * This is a service definition, the service will be named "TimingFilter". The interface, RequestFilter, is used 89 * within the RequestHandler service pipeline, which is built from the RequestHandler service configuration. 90 * Tapestry IoC is responsible for passing in an appropriate Log instance. Requests for static resources are handled 91 * at a higher level, so this filter will only be invoked for Tapestry related requests. 92 * <p/> 93 * <p/> 94 * Service builder methods are useful when the implementation is inline as an inner class (as here) or require some 95 * other kind of special initialization. In most cases, use the static bind() method instead. 96 * <p/> 97 * <p/> 98 * If this method was named "build", then the service id would be taken from the service interface and would be 99 * "RequestFilter". Since Tapestry already defines a service named "RequestFilter" we use an explicit service id 100 * that we can reference inside the contribution method. 101 * 102 * @param log log 103 * @return request filter 104 */ 105 public RequestFilter buildTimingFilter( final Logger log ) 106 { 107 return new RequestFilter() 108 { 109 public boolean service( Request request, Response response, RequestHandler handler ) 110 throws IOException 111 { 112 long startTime = System.currentTimeMillis(); 113 114 try 115 { 116 // The reponsibility of a filter is to invoke the corresponding method 117 // in the handler. When you chain multiple filters together, each filter 118 // received a handler that is a bridge to the next filter. 119 120 return handler.service( request, response ); 121 } 122 finally 123 { 124 long elapsed = System.currentTimeMillis() - startTime; 125 126 log.info( String.format( "Request time: %d ms", elapsed ) ); 127 } 128 } 129 }; 130 } 131 132 /** 133 * This is a contribution to the RequestHandler service configuration. This is how we extend Tapestry using the 134 * timing filter. A common use for this kind of filter is transaction management or security. 135 * 136 * @param configuration configuration to add to 137 * @param filter filter info 138 */ 139 public void contributeRequestHandler( OrderedConfiguration<RequestFilter> configuration, 140 @InjectService( "TimingFilter" ) 141 RequestFilter filter ) 142 { 143 // Each contribution to an ordered configuration has a name, When necessary, you may 144 // set constraints to precisely control the invocation order of the contributed filter 145 // within the pipeline. 146 147 configuration.add( "Timing", filter ); 148 } 149 }