You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/04/21 00:45:33 UTC

svn commit: r1095543 - /tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java

Author: hlship
Date: Wed Apr 20 22:45:33 2011
New Revision: 1095543

URL: http://svn.apache.org/viewvc?rev=1095543&view=rev
Log:
TAP5-853: Re-implement perThreadServiceLifecycle to use PlasticProxyFactory rather than ClassFactory

Modified:
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java?rev=1095543&r1=1095542&r2=1095543&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/services/PerThreadServiceLifecycle.java Wed Apr 20 22:45:33 2011
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008, 2009, 2010 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
 // You may obtain a copy of the License at
 //
-//     http://www.apache.org/licenses/LICENSE-2.0
+// http://www.apache.org/licenses/LICENSE-2.0
 //
 // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,39 +17,33 @@ package org.apache.tapestry5.ioc.interna
 import org.apache.tapestry5.ioc.ObjectCreator;
 import org.apache.tapestry5.ioc.ServiceLifecycle;
 import org.apache.tapestry5.ioc.ServiceResources;
-import org.apache.tapestry5.ioc.services.*;
-
-import static java.lang.String.format;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Modifier;
+import org.apache.tapestry5.ioc.services.Builtin;
+import org.apache.tapestry5.ioc.services.PerthreadManager;
+import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
 
 /**
- * Allows a service to exist "per thread" (in each thread). This involves an inner proxy, which caches an object derived
- * from a {@link org.apache.tapestry5.ioc.ObjectCreator} as a key in the {@link org.apache.tapestry5.ioc.services.PerthreadManager}.
- * Method invocations are delegated to the per-thread service instance.
+ * Allows a service to exist "per thread" (in each thread). Creates a proxy that delegates to a per-thread instance.
  * <p/>
  * This scheme ensures that, although the service builder method will be invoked many times over the life of the
  * application, the service decoration process occurs only once. The final calling chain is: Service Proxy --&gt;
- * Decorator(s) --&gt; PerThread Proxy --&gt; (per thread) instance.
+ * Interceptor(s) (from Decorators) --&gt; Advise Proxy (from Advisiors) --&gt; PerThread Proxy --&gt; (per thread)
+ * instance.
  */
 @SuppressWarnings("all")
 public class PerThreadServiceLifecycle implements ServiceLifecycle
 {
-    private static final String PER_THREAD_METHOD_NAME = "_perThreadInstance";
-
     private final PerthreadManager perthreadManager;
 
-    private final ClassFactory classFactory;
+    private final PlasticProxyFactory proxyFactory;
 
     public PerThreadServiceLifecycle(@Builtin
     PerthreadManager perthreadManager,
 
-                                     @Builtin
-                                     ClassFactory classFactory)
+    @Builtin
+    PlasticProxyFactory proxyFactory)
     {
         this.perthreadManager = perthreadManager;
-        this.classFactory = classFactory;
+        this.proxyFactory = proxyFactory;
     }
 
     /**
@@ -62,53 +56,11 @@ public class PerThreadServiceLifecycle i
 
     public Object createService(ServiceResources resources, ObjectCreator creator)
     {
-        Class proxyClass = createProxyClass(resources);
-
         ObjectCreator perThreadCreator = new PerThreadServiceCreator(perthreadManager, creator);
 
-        try
-        {
-            Constructor ctor = proxyClass.getConstructors()[0];
-
-            return ctor.newInstance(perThreadCreator);
-        }
-        catch (InvocationTargetException ex)
-        {
-            throw new RuntimeException(ex.getCause());
-        }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ex);
-        }
-    }
-
-    private Class createProxyClass(ServiceResources resources)
-    {
         Class serviceInterface = resources.getServiceInterface();
 
-        ClassFab cf = classFactory.newClass(serviceInterface);
-
-        cf.addField("_creator", Modifier.PRIVATE | Modifier.FINAL, ObjectCreator.class);
-
-        // Constructor takes a ServiceCreator
-
-        cf.addConstructor(new Class[]
-                { ObjectCreator.class }, null, "_creator = $1;");
-
-        String body = format("return (%s) _creator.createObject();", serviceInterface.getName());
-
-        MethodSignature sig = new MethodSignature(serviceInterface, PER_THREAD_METHOD_NAME, null,
-                                                  null);
-
-        cf.addMethod(Modifier.PRIVATE, sig, body);
-
-        String toString = format(
-                "<PerThread Proxy for %s(%s)>",
-                resources.getServiceId(),
-                serviceInterface.getName());
-
-        cf.proxyMethodsToDelegate(serviceInterface, PER_THREAD_METHOD_NAME + "()", toString);
-
-        return cf.createClass();
+        return proxyFactory.createProxy(serviceInterface, perThreadCreator, resources.getImplementationClass(),
+                String.format("<PerThread Proxy for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
     }
 }