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 2010/06/25 02:06:08 UTC

svn commit: r957764 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/internal/services/ main/java/org/apache/tapestry5/services/ test/java/org/apache/tapestry5/internal/services/

Author: hlship
Date: Fri Jun 25 00:06:08 2010
New Revision: 957764

URL: http://svn.apache.org/viewvc?rev=957764&view=rev
Log:
TAP5-1190: Add convienience for obtaining the active page for the request as a Component (from the public ComponentSource service)

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentSource.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentSourceImplTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentSourceImpl.java?rev=957764&r1=957763&r2=957764&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentSourceImpl.java Fri Jun 25 00:06:08 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2010 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,
@@ -21,6 +21,7 @@ import org.apache.tapestry5.ioc.internal
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.services.ComponentClassResolver;
 import org.apache.tapestry5.services.ComponentSource;
+import org.apache.tapestry5.services.RequestGlobals;
 
 public class ComponentSourceImpl implements ComponentSource
 {
@@ -28,10 +29,13 @@ public class ComponentSourceImpl impleme
 
     private final ComponentClassResolver resolver;
 
-    public ComponentSourceImpl(RequestPageCache pageCache, ComponentClassResolver resolver)
+    private final RequestGlobals globals;
+
+    public ComponentSourceImpl(RequestPageCache pageCache, ComponentClassResolver resolver, RequestGlobals globals)
     {
         this.pageCache = pageCache;
         this.resolver = resolver;
+        this.globals = globals;
     }
 
     public Component getComponent(String completeId)
@@ -61,7 +65,6 @@ public class ComponentSourceImpl impleme
             nestedId = nestedId.substring(0, dollarx);
         }
 
-
         ComponentPageElement element = page.getComponentElementByNestedId(nestedId);
 
         if (mixinId == null)
@@ -89,4 +92,15 @@ public class ComponentSourceImpl impleme
 
         return getPage(pageName);
     }
+
+    public Component getActivePage()
+    {
+        String pageName = globals.getActivePageName();
+
+        if (pageName == null)
+            throw new RuntimeException("The identity of the active page for this request has not yet been established.");
+
+        return getPage(pageName);
+    }
+
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentSource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentSource.java?rev=957764&r1=957763&r2=957764&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentSource.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/ComponentSource.java Fri Jun 25 00:06:08 2010
@@ -4,7 +4,7 @@
 // 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,
@@ -14,6 +14,7 @@
 
 package org.apache.tapestry5.services;
 
+import org.apache.tapestry5.EventConstants;
 import org.apache.tapestry5.runtime.Component;
 
 /**
@@ -27,10 +28,12 @@ public interface ComponentSource
      * Gets a component by its {@linkplain org.apache.tapestry5.ComponentResourcesCommon#getCompleteId() complete id}.
      * If the component id is for a mixin, then the mixin attached to the component will be returned. A mixin's complete
      * id is its container's complete id, suffixed with "$" and the mixin's id (its simple class name).
-     *
-     * @param completeId complete component id (case insensitive)
+     * 
+     * @param completeId
+     *            complete component id (case insensitive)
      * @return the component
-     * @throws IllegalArgumentException if the component can not be found
+     * @throws IllegalArgumentException
+     *             if the component can not be found
      * @see org.apache.tapestry5.ComponentResourcesCommon#getCompleteId()
      */
     Component getComponent(String completeId);
@@ -38,19 +41,35 @@ public interface ComponentSource
     /**
      * Returns the page identified by its logical page name. A logical page name is the short form of a page name often
      * emebedded into URLs.
-     *
-     * @param pageName the logical page name
+     * 
+     * @param pageName
+     *            the logical page name
      * @return the corresponding page's root component
-     * @throws IllegalArgumentException if the page can not be found
+     * @throws IllegalArgumentException
+     *             if the page can not be found
      */
     Component getPage(String pageName);
 
     /**
-     * A convienience for obtaining a page instance via a class instance.  This is provided so as to be refactoring
-     * safe.  The pageClass is simply converted to a class name and this is used to locate a page instance.
-     *
-     * @param pageClass used to locate the page instance
+     * A convienience for obtaining a page instance via a class instance. This is provided so as to be refactoring
+     * safe. The pageClass is simply converted to a class name and this is used to locate a page instance.
+     * 
+     * @param pageClass
+     *            used to locate the page instance
      * @return the page instance
      */
     Component getPage(Class pageClass);
+
+    /**
+     * Returns the <em>active page</em>, as defined by {@link RequestGlobals#getActivePageName()}. This is the primary
+     * page for handling the current request, the page which will be {@linkplain EventConstants#ACTIVATE activated} for
+     * the request.
+     * The identity of the active page is not known until the correct {@link Dispatcher} determines this.
+     * 
+     * @return the active page
+     * @throws RuntimeException
+     *             if the active page is not yet known
+     * @since 5.2.0
+     */
+    Component getActivePage();
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentSourceImplTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentSourceImplTest.java?rev=957764&r1=957763&r2=957764&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentSourceImplTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/ComponentSourceImplTest.java Fri Jun 25 00:06:08 2010
@@ -1,10 +1,10 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2010 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,
@@ -20,6 +20,7 @@ import org.apache.tapestry5.internal.tes
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.services.ComponentClassResolver;
 import org.apache.tapestry5.services.ComponentSource;
+import org.apache.tapestry5.services.RequestGlobals;
 import org.testng.annotations.Test;
 
 public class ComponentSourceImplTest extends InternalBaseTestCase
@@ -42,7 +43,7 @@ public class ComponentSourceImplTest ext
 
         replay();
 
-        ComponentSource source = new ComponentSourceImpl(cache, resolver);
+        ComponentSource source = new ComponentSourceImpl(cache, resolver, null);
 
         assertSame(source.getComponent(PAGE_NAME), component);
 
@@ -66,7 +67,7 @@ public class ComponentSourceImplTest ext
 
         replay();
 
-        ComponentSource source = new ComponentSourceImpl(cache, resolver);
+        ComponentSource source = new ComponentSourceImpl(cache, resolver, null);
 
         assertSame(source.getComponent(PAGE_NAME + ":" + NESTED_ELEMENT_ID), component);
 
@@ -86,7 +87,7 @@ public class ComponentSourceImplTest ext
 
         replay();
 
-        ComponentSource source = new ComponentSourceImpl(cache, resolver);
+        ComponentSource source = new ComponentSourceImpl(cache, resolver, null);
 
         assertSame(source.getPage(PAGE_NAME), component);
 
@@ -108,10 +109,59 @@ public class ComponentSourceImplTest ext
 
         replay();
 
-        ComponentSource source = new ComponentSourceImpl(cache, resolver);
+        ComponentSource source = new ComponentSourceImpl(cache, resolver, null);
 
         assertSame(source.getPage(ComponentSourceImplTest.class), component);
 
         verify();
     }
+
+    @Test
+    public void get_active_page()
+    {
+        RequestPageCache cache = mockRequestPageCache();
+        Page page = mockPage();
+        Component component = mockComponent();
+        ComponentClassResolver resolver = mockComponentClassResolver();
+        RequestGlobals globals = mockRequestGlobals();
+        String pageName = "Active";
+
+        expect(globals.getActivePageName()).andReturn(pageName);
+
+        train_get(cache, pageName, page);
+        train_getRootComponent(page, component);
+
+        replay();
+
+        ComponentSource source = new ComponentSourceImpl(cache, resolver, globals);
+
+        assertSame(source.getActivePage(), component);
+
+        verify();
+    }
+
+    @Test
+    public void get_active_page_before_known()
+    {
+        RequestGlobals globals = mockRequestGlobals();
+
+        expect(globals.getActivePageName()).andReturn(null);
+
+        replay();
+
+        ComponentSource source = new ComponentSourceImpl(null, null, globals);
+
+        try
+        {
+            source.getActivePage();
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertMessageContains(ex, "active page", "not yet been established");
+        }
+
+        verify();
+
+    }
 }