    001    // Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
    002    //
    003    // Licensed under the Apache License, Version 2.0 (the "License");
    004    // you may not use this file except in compliance with the License.
    005    // You may obtain a copy of the License at
    006    //
    007    // http://www.apache.org/licenses/LICENSE-2.0
    008    //
    009    // Unless required by applicable law or agreed to in writing, software
    010    // distributed under the License is distributed on an "AS IS" BASIS,
    011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012    // See the License for the specific language governing permissions and
    013    // limitations under the License.
    014
    015    package org.apache.tapestry5.ioc.internal.services;
    016
    017    import org.apache.tapestry5.ioc.ObjectCreator;
    018    import org.apache.tapestry5.ioc.ServiceLifecycle;
    019    import org.apache.tapestry5.ioc.ServiceResources;
    020    import org.apache.tapestry5.ioc.services.Builtin;
    021    import org.apache.tapestry5.ioc.services.PerthreadManager;
    022    import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
    023
    024    /**
    025     * Allows a service to exist "per thread" (in each thread). Creates a proxy that delegates to a per-thread instance.
    026     * <p/>
    027     * This scheme ensures that, although the service builder method will be invoked many times over the life of the
    028     * application, the service decoration process occurs only once. The final calling chain is: Service Proxy --&gt;
    029     * Interceptor(s) (from Decorators) --&gt; Advise Proxy (from Advisiors) --&gt; PerThread Proxy --&gt; (per thread)
    030     * instance.
    031     */
    032    @SuppressWarnings("all")
    033    public class PerThreadServiceLifecycle implements ServiceLifecycle
    034    {
    035        private final PerthreadManager perthreadManager;
    036
    037        private final PlasticProxyFactory proxyFactory;
    038
    039        public PerThreadServiceLifecycle(@Builtin
    040        PerthreadManager perthreadManager,
    041
    042        @Builtin
    043        PlasticProxyFactory proxyFactory)
    044        {
    045            this.perthreadManager = perthreadManager;
    046            this.proxyFactory = proxyFactory;
    047        }
    048
    049        /**
    050         * Returns false; this lifecycle represents a service that will be created many times (by each thread).
    051         */
    052        public boolean isSingleton()
    053        {
    054            return false;
    055        }
    056
    057        public Object createService(ServiceResources resources, ObjectCreator creator)
    058        {
    059            ObjectCreator perThreadCreator = new PerThreadServiceCreator(perthreadManager, creator);
    060
    061            Class serviceInterface = resources.getServiceInterface();
    062
    063            return proxyFactory.createProxy(serviceInterface, perThreadCreator, String.format("<PerThread Proxy for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
    064        }
    065    }