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/05/28 20:42:29 UTC

svn commit: r1128736 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/pageload/ main/java/org/apache/tapestry5/services/ main/java/org/apache/tapestry5/services/pageload/ test/groovy/org/apache/tapestry5/interna...

Author: hlship
Date: Sat May 28 18:42:29 2011
New Revision: 1128736

URL: http://svn.apache.org/viewvc?rev=1128736&view=rev
Log:
Start of skinning support

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentRequestSelectorAnalyzer.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentRequestSelectorAnalyzer.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceLocator.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceSelector.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageLoadModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/ComponentResourceSelectorTests.groovy
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentRequestSelectorAnalyzer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentRequestSelectorAnalyzer.java?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentRequestSelectorAnalyzer.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/pageload/DefaultComponentRequestSelectorAnalyzer.java Sat May 28 18:42:29 2011
@@ -0,0 +1,28 @@
+// Copyright 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
+//
+// 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.tapestry5.internal.pageload;
+
+import java.util.Locale;
+
+import org.apache.tapestry5.services.pageload.ComponentRequestSelectorAnalyzer;
+import org.apache.tapestry5.services.pageload.ComponentResourceSelector;
+
+public class DefaultComponentRequestSelectorAnalyzer implements ComponentRequestSelectorAnalyzer
+{
+    public ComponentResourceSelector buildSelector(Locale locale)
+    {
+        return new ComponentResourceSelector(locale);
+    }
+}

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java?rev=1128736&r1=1128735&r2=1128736&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java Sat May 28 18:42:29 2011
@@ -237,6 +237,7 @@ import org.apache.tapestry5.services.mes
 import org.apache.tapestry5.services.meta.FixedExtractor;
 import org.apache.tapestry5.services.meta.MetaDataExtractor;
 import org.apache.tapestry5.services.meta.MetaWorker;
+import org.apache.tapestry5.services.pageload.PageLoadModule;
 import org.apache.tapestry5.services.templates.ComponentTemplateLocator;
 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
 import org.apache.tapestry5.util.StringToEnumCoercion;
@@ -256,7 +257,7 @@ import org.slf4j.Logger;
  */
 @Marker(Core.class)
 @SubModule(
-{ InternalModule.class, AssetsModule.class })
+{ InternalModule.class, AssetsModule.class, PageLoadModule.class })
 public final class TapestryModule
 {
     private final PipelineBuilder pipelineBuilder;

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentRequestSelectorAnalyzer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentRequestSelectorAnalyzer.java?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentRequestSelectorAnalyzer.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentRequestSelectorAnalyzer.java Sat May 28 18:42:29 2011
@@ -0,0 +1,22 @@
+package org.apache.tapestry5.services.pageload;
+
+import java.util.Locale;
+
+/**
+ * Determines the {@link ComponentResourceSelector} for the current request. This is often based on cookies, query
+ * parameters, or other details available in the {@link Request}. The default implementation simply wraps the
+ * provided Locale as a ComponentResourceSelector. A custom implementation may
+ * {@linkplain ComponentResourceSelector#withAxis(Class, Object) add additional axes} to the selector.
+ * 
+ * @since 5.3.0
+ */
+public interface ComponentRequestSelectorAnalyzer
+{
+    /**
+     * Constructors a selector given the locale.
+     * 
+     * @param locale
+     * @return
+     */
+    ComponentResourceSelector buildSelector(Locale locale);
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceLocator.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceLocator.java?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceLocator.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceLocator.java Sat May 28 18:42:29 2011
@@ -0,0 +1,6 @@
+package org.apache.tapestry5.services.pageload;
+
+public interface ComponentResourceLocator
+{
+
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceSelector.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceSelector.java?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceSelector.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/ComponentResourceSelector.java Sat May 28 18:42:29 2011
@@ -0,0 +1,108 @@
+// Copyright 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
+//
+// 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.tapestry5.services.pageload;
+
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+
+/**
+ * Encapsulates the information that is used when selecting a template or message catalog associated with a component.
+ * The selector is created using information from the incoming request (principally, the {@link Locale}, which can be
+ * considered the primary axis), but with additional, application-specific axes, which provides a way to skin an
+ * application (when used in concert with a custom {@link ComponentResourceLocator} implementation.
+ * 
+ * @since 5.3.0
+ */
+public class ComponentResourceSelector
+{
+    private final Locale locale;
+
+    private final Map<Class, Object> axis;
+
+    public ComponentResourceSelector(Locale locale)
+    {
+        this(locale, Collections.<Class, Object> emptyMap());
+    }
+
+    private ComponentResourceSelector(Locale locale, Map<Class, Object> axis)
+    {
+        assert locale != null;
+
+        this.locale = locale;
+        this.axis = axis;
+    }
+
+    /**
+     * Returns a <em>new</em> selector with the given axis data. It is not allowed to redefine an existing axis type.
+     * Typically, the axis type is an enum type. Axis values are expected to be immutable, and to implement
+     * {@code equals()} and {@code hashCode()}.
+     * 
+     * @param axisName
+     *            non-blank axis key
+     * @param axisValue
+     *            non-null axis value
+     * @return
+     */
+    public <T> ComponentResourceSelector withAxis(Class<T> axisType, T axisValue)
+    {
+        assert axisType != null;
+        assert axisValue != null;
+
+        if (axis.containsKey(axisType))
+            throw new IllegalArgumentException(String.format("Axis type %s is already specified as %s.",
+                    axisType.getName(), axis.get(axisType)));
+
+        Map<Class, Object> updated = CollectionFactory.newMap(axis);
+        updated.put(axisType, axisValue);
+
+        return new ComponentResourceSelector(locale, updated);
+    }
+
+    /**
+     * Returns a previously stored axis value, or null if no axis value of the specified type has been stored.
+     * 
+     * @param <T>
+     * @param axisType
+     * @return value or null
+     */
+    public <T> T getAxis(Class<T> axisType)
+    {
+        return axisType.cast(axis.get(axisType));
+    }
+
+    /** Returns true if the object is another selector with the same locale and set of axis. */
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (obj == this)
+            return true;
+
+        if (!(obj instanceof ComponentResourceSelector))
+            return false;
+
+        ComponentResourceSelector other = (ComponentResourceSelector) obj;
+
+        return locale.equals(other.locale) && axis.equals(other.axis);
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return 37 * locale.hashCode() + axis.hashCode();
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageLoadModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageLoadModule.java?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageLoadModule.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/pageload/PageLoadModule.java Sat May 28 18:42:29 2011
@@ -0,0 +1,18 @@
+package org.apache.tapestry5.services.pageload;
+
+import org.apache.tapestry5.internal.pageload.DefaultComponentRequestSelectorAnalyzer;
+import org.apache.tapestry5.ioc.ServiceBinder;
+import org.apache.tapestry5.ioc.annotations.Marker;
+import org.apache.tapestry5.services.Core;
+
+/**
+ * @since 5.3.0
+ */
+@Marker(Core.class)
+public class PageLoadModule
+{
+    public static void bind(ServiceBinder binder)
+    {
+        binder.bind(ComponentRequestSelectorAnalyzer.class, DefaultComponentRequestSelectorAnalyzer.class);
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/ComponentResourceSelectorTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/ComponentResourceSelectorTests.groovy?rev=1128736&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/ComponentResourceSelectorTests.groovy (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/internal/pageload/ComponentResourceSelectorTests.groovy Sat May 28 18:42:29 2011
@@ -0,0 +1,65 @@
+// Copyright 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
+//
+// 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.tapestry5.internal.pageload
+
+import org.apache.tapestry5.ioc.annotations.AnnotationUseContext
+import org.apache.tapestry5.services.pageload.ComponentResourceSelector
+import org.testng.Assert
+import org.testng.annotations.Test
+
+class ComponentResourceSelectorTests extends Assert
+{
+    ComponentResourceSelector english = new ComponentResourceSelector(Locale.ENGLISH)
+    ComponentResourceSelector french = new ComponentResourceSelector(Locale.FRENCH)
+
+    @Test
+    void mismatch_if_locales_not_same() {
+        assert english != french
+}
+
+    @Test
+    void equals_this_is_true() {
+        assert  english == english
+}
+
+    @Test
+    void equals_null_is_false() {
+        assert english != null
+}
+
+    @Test
+    void with_axis_returns_new_instance() {
+        def withAxis = english.withAxis(AnnotationUseContext.class, AnnotationUseContext.SERVICE)
+
+        assert ! english.is(withAxis)
+
+        assert english.getAxis(AnnotationUseContext.class) == null
+
+        assert withAxis.getAxis(AnnotationUseContext.class).is(AnnotationUseContext.SERVICE)
+}
+
+    @Test
+    void cant_add_same_axis_type_again() {
+
+        def withAxis = english.withAxis(AnnotationUseContext.class, AnnotationUseContext.SERVICE)
+
+        try {
+            withAxis.withAxis(AnnotationUseContext.class, AnnotationUseContext.COMPONENT)
+}
+        catch (IllegalArgumentException ex) {
+            assert ex.message == "Axis type org.apache.tapestry5.ioc.annotations.AnnotationUseContext is already specified as SERVICE."
+        }
+    }
+}