001    package org.tynamo.jpa.sample.services;
002    
003    import org.tynamo.jpa.JPASymbols;
004    import org.tynamo.jpa.JPATransactionAdvisor;
005    import org.apache.tapestry5.SymbolConstants;
006    import org.apache.tapestry5.ioc.MappedConfiguration;
007    import org.apache.tapestry5.ioc.MethodAdviceReceiver;
008    import org.apache.tapestry5.ioc.ServiceBinder;
009    import org.apache.tapestry5.ioc.annotations.Match;
010    import org.apache.tapestry5.services.Request;
011    import org.apache.tapestry5.services.RequestFilter;
012    import org.apache.tapestry5.services.RequestHandler;
013    import org.apache.tapestry5.services.Response;
014    import org.slf4j.Logger;
015    
016    import java.io.IOException;
017    
018    /**
019     * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to
020     * configure and extend Tapestry, or to place your own service definitions.
021     */
022    public class AppModule {
023            public static void bind(ServiceBinder binder) {
024                    binder.bind(TestService.class, TestServiceImpl.class);
025                    // binder.bind(MyServiceInterface.class, MyServiceImpl.class);
026    
027                    // Make bind() calls on the binder object to define most IoC services.
028                    // Use service builder methods (example below) when the implementation
029                    // is provided inline, or requires more initialization than simply
030                    // invoking the constructor.
031            }
032    
033    
034            public static void contributeApplicationDefaults(
035                            MappedConfiguration<String, String> configuration) {
036                    // Contributions to ApplicationDefaults will override any contributions to
037                    // FactoryDefaults (with the same key). Here we're restricting the supported
038                    // locales to just "en" (English). As you add localised message catalogs and other assets,
039                    // you can extend this list of locales (it's a comma separated series of locale names;
040                    // the first locale name is the default when there's no reasonable match).
041    
042                    configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
043    
044                    // The factory default is true but during the early stages of an application
045                    // overriding to false is a good idea. In addition, this is often overridden
046                    // on the command line as -Dtapestry.production-mode=false
047                    configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
048    
049                    // The application version number is incorprated into URLs for some
050                    // assets. Web browsers will cache assets because of the far future expires
051                    // header. If existing assets are changed, the version number should also
052                    // change, to force the browser to download new versions.
053                    configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0-SNAPSHOT");
054    
055                    configuration.add(JPASymbols.PERSISTENCE_UNIT, "testPU");
056            }
057    
058    
059            /**
060             * This is a service definition, the service will be named "TimingFilter". The interface,
061             * RequestFilter, is used within the RequestHandler service pipeline, which is built from the
062             * RequestHandler service configuration. Tapestry IoC is responsible for passing in an
063             * appropriate Logger instance. Requests for static resources are handled at a higher level, so
064             * this filter will only be invoked for Tapestry related requests.
065             * <p/>
066             * <p/>
067             * Service builder methods are useful when the implementation is inline as an inner class
068             * (as here) or require some other kind of special initialization. In most cases,
069             * use the static bind() method instead.
070             * <p/>
071             * <p/>
072             * If this method was named "build", then the service id would be taken from the
073             * service interface and would be "RequestFilter".  Since Tapestry already defines
074             * a service named "RequestFilter" we use an explicit service id that we can reference
075             * inside the contribution method.
076             */
077            public RequestFilter buildTimingFilter(final Logger log) {
078                    return new RequestFilter() {
079                            public boolean service(Request request, Response response, RequestHandler handler)
080                                            throws IOException {
081                                    long startTime = System.currentTimeMillis();
082    
083                                    try {
084                                            // The responsibility of a filter is to invoke the corresponding method
085                                            // in the handler. When you chain multiple filters together, each filter
086                                            // received a handler that is a bridge to the next filter.
087    
088                                            return handler.service(request, response);
089                                    }
090                                    finally {
091                                            long elapsed = System.currentTimeMillis() - startTime;
092    
093                                            log.info(String.format("Request time: %d ms", elapsed));
094                                    }
095                            }
096                    };
097            }
098    
099            @Match("*Service")
100            public static void adviseTransactions(JPATransactionAdvisor advisor, MethodAdviceReceiver receiver) {
101                    advisor.addTransactionCommitAdvice(receiver);
102            }
103    }