You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by fr...@apache.org on 2007/02/08 16:27:21 UTC

svn commit: r504930 - in /tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support: ./ src/main/java/org/apache/tapestry/internal/model/ src/main/java/org/apache/tapestry/internal/services/ src/main/java/org/apache/tapestry/internal/structure/...

Author: freemant
Date: Thu Feb  8 07:27:20 2007
New Revision: 504930

URL: http://svn.apache.org/viewvc?view=rev&rev=504930
Log:
Look up the configureXXX() methods only once when creating the component instantiator for the container, so that it doesn't have to be done every time a component is configured.

Added:
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java   (with props)
Modified:
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/.project
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/model/MutableEmbeddedComponentModelImpl.java
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/services/ComponentClassTransformerImpl.java
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentModel.java
    tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/MutableEmbeddedComponentModel.java

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/.project
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/.project?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/.project (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/.project Thu Feb  8 07:27:20 2007
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <projectDescription>
-	<name>tapestry-core</name>
+	<name>tapestry-core-kt</name>
 	<comment></comment>
 	<projects>
 	</projects>

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/model/MutableEmbeddedComponentModelImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/model/MutableEmbeddedComponentModelImpl.java?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/model/MutableEmbeddedComponentModelImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/model/MutableEmbeddedComponentModelImpl.java Thu Feb  8 07:27:20 2007
@@ -22,6 +22,7 @@
 
 import org.apache.tapestry.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry.ioc.internal.util.InternalUtils;
+import org.apache.tapestry.model.EmbeddedComponentConfigurator;
 import org.apache.tapestry.model.MutableEmbeddedComponentModel;
 
 public class MutableEmbeddedComponentModelImpl implements MutableEmbeddedComponentModel
@@ -37,8 +38,20 @@
     private Map<String, String> _parameters;
 
     /** List of mixin class names. */
-    private List<String> _mixinClassNames;
-
+    private List<String> _mixinClassNames;
+    
+    private EmbeddedComponentConfigurator _configurator;
+
+    public EmbeddedComponentConfigurator getConfigurator()
+    {
+        return _configurator;
+    }
+
+    public void setConfigurator(EmbeddedComponentConfigurator configurator)
+    {
+        _configurator = configurator;
+    }
+
     public MutableEmbeddedComponentModelImpl(String id, String componentType,
             String componentClassName, String declaredClass)
     {

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/services/ComponentClassTransformerImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/services/ComponentClassTransformerImpl.java?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/services/ComponentClassTransformerImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/services/ComponentClassTransformerImpl.java Thu Feb  8 07:27:20 2007
@@ -12,128 +12,186 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.apache.tapestry.internal.services;
-
+package org.apache.tapestry.internal.services;
+
 import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newThreadSafeMap;
-
-import java.util.Map;
-
-import javassist.CtClass;
-import javassist.NotFoundException;
-
-import org.apache.commons.logging.Log;
-import org.apache.tapestry.annotations.ComponentClass;
-import org.apache.tapestry.events.InvalidationListener;
-import org.apache.tapestry.internal.model.MutableComponentModelImpl;
-import org.apache.tapestry.ioc.LogSource;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import javassist.CtClass;
+import javassist.NotFoundException;
+
+import org.apache.commons.logging.Log;
+import org.apache.tapestry.annotations.ComponentClass;
+import org.apache.tapestry.events.InvalidationListener;
+import org.apache.tapestry.internal.model.MutableComponentModelImpl;
+import org.apache.tapestry.ioc.LogSource;
 import org.apache.tapestry.ioc.Resource;
 import org.apache.tapestry.ioc.internal.util.ClasspathResource;
-import org.apache.tapestry.model.ComponentModel;
-import org.apache.tapestry.model.MutableComponentModel;
-import org.apache.tapestry.services.ComponentClassTransformWorker;
-
-/**
- * Implementation of {@link org.apache.tapestry.internal.services.ComponentClassTransformer}.
- */
-public class ComponentClassTransformerImpl implements ComponentClassTransformer,
-        InvalidationListener
-{
-    /** Map from class name to class transformation. */
-    private final Map<String, InternalClassTransformation> _nameToClassTransformation = newThreadSafeMap();
-
-    private final Map<String, ComponentModel> _nameToComponentModel = newThreadSafeMap();
-
-    private final ComponentClassTransformWorker _workerChain;
-
-    private final LogSource _logSource;
-
-    /**
-     * @param workerChain
-     *            the ordered list of class transform works as a chain of command instance
-     */
-    public ComponentClassTransformerImpl(ComponentClassTransformWorker workerChain,
-            LogSource logSource)
-    {
-        _workerChain = workerChain;
-        _logSource = logSource;
-    }
-
-    /**
-     * Clears the cache of {@link InternalClassTransformation} instances whenever the class loader
-     * is invalidated.
-     */
-    public void objectWasInvalidated()
-    {
-        _nameToClassTransformation.clear();
-        _nameToComponentModel.clear();
-    }
-
-    public void transformComponentClass(CtClass ctClass, ClassLoader classLoader)
-    {
-        String classname = ctClass.getName();
-
-        String parentClassname;
-
-        try
-        {
-            parentClassname = ctClass.getSuperclass().getName();
-        }
-        catch (NotFoundException ex)
-        {
-            throw new RuntimeException(ex);
-        }
-
-        Log log = _logSource.getLog(classname);
-
-        // If the parent class is in a controlled package (and has the ComponentClass annotation),
-        // it will already have been loaded and transformed (that is driven by the
-        // ComponentInstantiatorSource).
-
-        InternalClassTransformation parentTransformation = _nameToClassTransformation
-                .get(parentClassname);
-
-        // TODO: Check that the name is not already in the map. But I think that can't happen,
-        // because the classloader itself is synchronized.
-
-        Resource baseResource = new ClasspathResource(classname.replace(".", "/") + ".class");
-
-        ComponentModel parentModel = _nameToComponentModel.get(parentClassname);
-
-        MutableComponentModel model = new MutableComponentModelImpl(classname, log, baseResource,
-                parentModel);
-
-        InternalClassTransformation transformation = parentTransformation == null ? new InternalClassTransformationImpl(
-                ctClass, classLoader, log, model)
-                : new InternalClassTransformationImpl(ctClass, parentTransformation, classLoader,
-                        log, model);
-
-        // Not all classes in the packages are components. That's not just sloppy coding by
-        // application developers, it also represents inner classes (including anonymous inner
-        // classes).
-
-        if (transformation.getAnnotation(ComponentClass.class) == null)
-            return;
-
-        _workerChain.transform(transformation, model);
-
-        transformation.finish();
-
-        if (log.isDebugEnabled())
-            log.debug("Finished class transformation: " + transformation);
-
-        _nameToClassTransformation.put(classname, transformation);
-        _nameToComponentModel.put(classname, model);
-    }
-
-    public Instantiator createInstantiator(Class componentClass)
-    {
-        String className = componentClass.getName();
-
-        InternalClassTransformation ct = _nameToClassTransformation.get(className);
-
-        if (ct == null)
-            throw new RuntimeException(ServicesMessages.classNotTransformed(className));
-
-        return ct.createInstantiator(componentClass);
-    }
-}
+import org.apache.tapestry.ioc.internal.util.InternalUtils;
+import org.apache.tapestry.model.ComponentModel;
+import org.apache.tapestry.model.EmbeddedComponentConfigurator;
+import org.apache.tapestry.model.MutableComponentModel;
+import org.apache.tapestry.model.MutableEmbeddedComponentModel;
+import org.apache.tapestry.services.ComponentClassTransformWorker;
+
+/**
+ * Implementation of {@link org.apache.tapestry.internal.services.ComponentClassTransformer}.
+ */
+public class ComponentClassTransformerImpl implements ComponentClassTransformer,
+        InvalidationListener
+{
+    /** Map from class name to class transformation. */
+    private final Map<String, InternalClassTransformation> _nameToClassTransformation = newThreadSafeMap();
+
+    private final Map<String, ComponentModel> _nameToComponentModel = newThreadSafeMap();
+
+    private final ComponentClassTransformWorker _workerChain;
+
+    private final LogSource _logSource;
+
+    /**
+     * @param workerChain
+     *            the ordered list of class transform works as a chain of command instance
+     */
+    public ComponentClassTransformerImpl(ComponentClassTransformWorker workerChain,
+            LogSource logSource)
+    {
+        _workerChain = workerChain;
+        _logSource = logSource;
+    }
+
+    /**
+     * Clears the cache of {@link InternalClassTransformation} instances whenever the class loader
+     * is invalidated.
+     */
+    public void objectWasInvalidated()
+    {
+        _nameToClassTransformation.clear();
+        _nameToComponentModel.clear();
+    }
+
+    public void transformComponentClass(CtClass ctClass, ClassLoader classLoader)
+    {
+        String classname = ctClass.getName();
+
+        String parentClassname;
+
+        try
+        {
+            parentClassname = ctClass.getSuperclass().getName();
+        }
+        catch (NotFoundException ex)
+        {
+            throw new RuntimeException(ex);
+        }
+
+        Log log = _logSource.getLog(classname);
+
+        // If the parent class is in a controlled package (and has the ComponentClass annotation),
+        // it will already have been loaded and transformed (that is driven by the
+        // ComponentInstantiatorSource).
+
+        InternalClassTransformation parentTransformation = _nameToClassTransformation
+                .get(parentClassname);
+
+        // TODO: Check that the name is not already in the map. But I think that can't happen,
+        // because the classloader itself is synchronized.
+
+        Resource baseResource = new ClasspathResource(classname.replace(".", "/") + ".class");
+
+        ComponentModel parentModel = _nameToComponentModel.get(parentClassname);
+
+        MutableComponentModel model = new MutableComponentModelImpl(classname, log, baseResource,
+                parentModel);
+
+        InternalClassTransformation transformation = parentTransformation == null ? new InternalClassTransformationImpl(
+                ctClass, classLoader, log, model)
+                : new InternalClassTransformationImpl(ctClass, parentTransformation, classLoader,
+                        log, model);
+
+        // Not all classes in the packages are components. That's not just sloppy coding by
+        // application developers, it also represents inner classes (including anonymous inner
+        // classes).
+
+        if (transformation.getAnnotation(ComponentClass.class) == null)
+            return;
+
+        _workerChain.transform(transformation, model);
+
+        transformation.finish();
+
+        if (log.isDebugEnabled())
+            log.debug("Finished class transformation: " + transformation);
+
+        _nameToClassTransformation.put(classname, transformation);
+        _nameToComponentModel.put(classname, model);
+    }
+
+    public Instantiator createInstantiator(Class componentClass)
+    {
+        String className = componentClass.getName();
+
+        InternalClassTransformation ct = _nameToClassTransformation.get(className);
+
+        if (ct == null)
+            throw new RuntimeException(ServicesMessages.classNotTransformed(className));
+
+        setEmbeddedComponentConfigurators(componentClass);
+
+        return ct.createInstantiator(componentClass);
+    }
+
+    /**
+     * Creates an {@link EmbeddedComponentConfigurator} for each component embedded in the given
+     * container. For each such component, look up a method configure<em>Id</em>() in the
+     * container class. If so, create an anonymous EmbeddedComponentConfigurator class to call that
+     * method and store it into the corresponding
+     * {@link org.apache.tapestry.model.EmbeddedComponentModel}. This way the method is looked up
+     * only once now instead of everything the component is configured.
+     * 
+     * @param containerClass
+     */
+    private void setEmbeddedComponentConfigurators(Class containerClass)
+    {
+        ComponentModel model = _nameToComponentModel.get(containerClass.getName());
+        for (String id : model.getEmbeddedComponentIds())
+        {
+            MutableEmbeddedComponentModel embeddedComponentModel = (MutableEmbeddedComponentModel) model
+                    .getEmbeddedComponentModel(id);
+            embeddedComponentModel.setConfigurator(getConfigurator(containerClass, id));
+        }
+
+    }
+
+    private EmbeddedComponentConfigurator getConfigurator(Class containerClass, String id)
+    {
+        String methodName = "configure" + InternalUtils.capitalize(id);
+        try
+        {
+            final Method method = containerClass.getDeclaredMethod(methodName, new Class[0]);
+            return new EmbeddedComponentConfigurator()
+            {
+
+                public void execute(Object container)
+                {
+                    try
+                    {
+                        method.invoke(container, new Object[0]);
+                    }
+                    catch (Exception ex)
+                    {
+                        throw new RuntimeException(ex);
+                    }
+
+                }
+
+            };
+        }
+        catch (NoSuchMethodException ex)
+        {
+            return null;
+        }
+    }
+}

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/internal/structure/ComponentPageElementImpl.java Thu Feb  8 07:27:20 2007
@@ -18,7 +18,6 @@
 import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
 import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
 
-import java.lang.reflect.Method;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -45,6 +44,8 @@
 import org.apache.tapestry.ioc.internal.util.TapestryException;
 import org.apache.tapestry.ioc.services.TypeCoercer;
 import org.apache.tapestry.model.ComponentModel;
+import org.apache.tapestry.model.EmbeddedComponentConfigurator;
+import org.apache.tapestry.model.EmbeddedComponentModel;
 import org.apache.tapestry.model.ParameterModel;
 import org.apache.tapestry.runtime.Component;
 import org.apache.tapestry.runtime.ComponentEvent;
@@ -989,27 +990,21 @@
 
     /**
      * Call the configure&lt;component-id&gt;() method in the container (if any) to configure this
-     * component.
+     * component. This object was stored there by
+     * {@link org.apache.tapestry.internal.services.ComponentClassTransformerImpl#setEmbeddedComponentConfigurators(Class)}.
      */
     private void configure()
     {
         if (_container == null)
             return;
-        Object container = _container.getComponent();
-        try
-        {
-            Method method = container.getClass().getMethod(
-                    "configure" + InternalUtils.capitalize(_id));
-            method.invoke(container);
-        }
-        catch (NoSuchMethodException ex)
-        {
-
-        }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ex);
-        }
+        EmbeddedComponentModel embeddedModel = _container.getComponentResources()
+                .getComponentModel().getEmbeddedComponentModel(_id);
+        if (embeddedModel == null)
+            return;
+        EmbeddedComponentConfigurator configurator = embeddedModel.getConfigurator();
+        if (configurator == null)
+            return;
+        configurator.execute(_container.getComponent());
 
     }
 

Added: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java?view=auto&rev=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java (added)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java Thu Feb  8 07:27:20 2007
@@ -0,0 +1,27 @@
+// Copyright 2007 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
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.model;
+
+/**
+ * An object that configures a particular component in a container.
+ */
+public interface EmbeddedComponentConfigurator
+{
+    /**
+     * Configures the component that is embedded in the given container.
+     */
+    void execute(Object container);
+
+}

Propchange: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentConfigurator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentModel.java?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentModel.java (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/EmbeddedComponentModel.java Thu Feb  8 07:27:20 2007
@@ -12,40 +12,45 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package org.apache.tapestry.model;
-
-import java.util.List;
-
-import org.apache.tapestry.annotations.Component;
-
-/**
- * The model for a component embedded within another component, as defined by the
- * {@link org.apache.tapestry.annotations.Component} annotation.
- */
-public interface EmbeddedComponentModel
-{
-    /** A unique id for the embedded component. */
-    String getId();
-
-    /** The type of the component, which may be blank. */
-    String getComponentType();
-
-    /**
-     * The class name of the component, as derived from the field to which the {@link Component}
-     * annotation is applied. This value is only used when the componentType property is blank.
-     */
-    String getComponentClassName();
-
-    /** A sorted list of the names of all bound parameters. */
-    List<String> getParameterNames();
-
-    /** The value for each parameter, which will be interpreted as a binding expression. */
-    String getParameterValue(String parameterName);
-
-    /**
-     * Returns the fully qualified class names of all mixins added to this component, sorted
-     * alphabetically.
-     */
-    List<String> getMixinClassNames();
-
-}
+package org.apache.tapestry.model;
+
+import java.util.List;
+
+import org.apache.tapestry.annotations.Component;
+
+/**
+ * The model for a component embedded within another component, as defined by the
+ * {@link org.apache.tapestry.annotations.Component} annotation.
+ */
+public interface EmbeddedComponentModel
+{
+    /** A unique id for the embedded component. */
+    String getId();
+
+    /** The type of the component, which may be blank. */
+    String getComponentType();
+
+    /**
+     * The class name of the component, as derived from the field to which the {@link Component}
+     * annotation is applied. This value is only used when the componentType property is blank.
+     */
+    String getComponentClassName();
+
+    /** A sorted list of the names of all bound parameters. */
+    List<String> getParameterNames();
+
+    /** The value for each parameter, which will be interpreted as a binding expression. */
+    String getParameterValue(String parameterName);
+
+    /**
+     * Returns the fully qualified class names of all mixins added to this component, sorted
+     * alphabetically.
+     */
+    List<String> getMixinClassNames();
+
+    /**
+     * Returns the {@link EmbeddedComponentConfigurator} that can configure this component.
+     */
+    EmbeddedComponentConfigurator getConfigurator();
+
+}

Modified: tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/MutableEmbeddedComponentModel.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/MutableEmbeddedComponentModel.java?view=diff&rev=504930&r1=504929&r2=504930
==============================================================================
--- tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/MutableEmbeddedComponentModel.java (original)
+++ tapestry/tapestry5/tapestry-core/branches/kt-20070205-IDE-support/src/main/java/org/apache/tapestry/model/MutableEmbeddedComponentModel.java Thu Feb  8 07:27:20 2007
@@ -23,5 +23,8 @@
     void addParameter(String name, String value);
 
     /** Adds a mixin to the component in terms of its fully qualified class name. */
-    void addMixin(String mixinClassName);
+    void addMixin(String mixinClassName);
+    
+    void setConfigurator(EmbeddedComponentConfigurator configurator);
+
 }