You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by dr...@apache.org on 2008/06/10 23:31:25 UTC
svn commit: r666333 - in /tapestry/tapestry5/trunk:
tapestry-core/src/main/java/org/apache/tapestry5/annotations/
tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/
tapestry-core/src/main/java/org/apache/tapestry5/services/ tapestry-c...
Author: drobiazko
Date: Tue Jun 10 14:31:25 2008
New Revision: 666333
URL: http://svn.apache.org/viewvc?rev=666333&view=rev
Log:
TAPESTRY-2235: Annotation for managing a property as the page activation context
Added:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/PageActivationContext.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/music/MusicDetails2.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/music/MusicDetails2.java
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Path.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Service.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/transform/TransformStrings.properties
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Music.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/userguide.apt
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/PageActivationContext.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/PageActivationContext.java?rev=666333&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/PageActivationContext.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/PageActivationContext.java Tue Jun 10 14:31:25 2008
@@ -0,0 +1,37 @@
+// Copyright 2008 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.annotations;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+/**
+ * Annotation for a field for which the page activation context handlers (onActivate and onPassivate) should be created.
+ * In order to use this annotation you must contribute a {@link ValueEncoder} for the class of the annotated property.
+ */
+@Target(FIELD)
+@Documented
+@Retention(RUNTIME)
+public @interface PageActivationContext
+{
+ /** Whether to create an activate event handler. */
+ boolean activate() default true;
+
+ /** Whether to create a passivate event handler */
+ boolean passivate() default true;
+}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Path.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Path.java?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Path.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Path.java Tue Jun 10 14:31:25 2008
@@ -14,8 +14,6 @@
package org.apache.tapestry5.annotations;
-import org.apache.tapestry5.Asset;
-import org.apache.tapestry5.ioc.annotations.Inject;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Service.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Service.java?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Service.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/Service.java Tue Jun 10 14:31:25 2008
@@ -14,7 +14,6 @@
package org.apache.tapestry5.annotations;
-import org.apache.tapestry5.ioc.annotations.Inject;
import java.lang.annotation.Documented;
import static java.lang.annotation.ElementType.FIELD;
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java?rev=666333&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/PageActivationContextWorker.java Tue Jun 10 14:31:25 2008
@@ -0,0 +1,65 @@
+// Copyright 2008 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.transform;
+
+import java.lang.reflect.Modifier;
+import java.util.List;
+
+import org.apache.tapestry5.annotations.PageActivationContext;
+import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.services.ClassTransformation;
+import org.apache.tapestry5.services.ComponentClassTransformWorker;
+import org.apache.tapestry5.services.TransformMethodSignature;
+
+/**
+ * Provides the page activation context handlers.
+ *
+ * @see org.apache.tapestry5.annotations.PageActivationContext
+ */
+public class PageActivationContextWorker implements ComponentClassTransformWorker
+{
+ public void transform(ClassTransformation transformation, MutableComponentModel model)
+ {
+ List<String> fields = transformation.findFieldsWithAnnotation(PageActivationContext.class);
+
+ if(fields.size()>1)
+ throw new RuntimeException(TransformMessages.illegalNumberOfPageActivationContextHandlers(fields));
+
+ for (String fieldName : fields)
+ {
+ PageActivationContext annotation = transformation.getFieldAnnotation(fieldName, PageActivationContext.class);
+
+ String fieldType = transformation.getFieldType(fieldName);
+
+ if (annotation.activate())
+ {
+ TransformMethodSignature activate
+ = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL, "void",
+ "onActivate",
+ new String[] { fieldType }, null);
+ transformation.addTransformedMethod(activate, fieldName + " = $1;");
+ }
+
+ if (annotation.passivate())
+ {
+ TransformMethodSignature passivate
+ = new TransformMethodSignature(Modifier.PROTECTED | Modifier.FINAL, "java.lang.Object", "onPassivate",
+ null, null);
+ transformation.addTransformedMethod(passivate, "return "+fieldName + ";");
+ }
+ }
+
+ }
+}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/TransformMessages.java Tue Jun 10 14:31:25 2008
@@ -14,7 +14,10 @@
package org.apache.tapestry5.internal.transform;
+import java.util.List;
+
import org.apache.tapestry5.ioc.Messages;
+import org.apache.tapestry5.ioc.internal.util.InternalUtils;
import org.apache.tapestry5.ioc.internal.util.MessagesImpl;
import org.apache.tapestry5.runtime.Component;
import org.apache.tapestry5.services.TransformMethodSignature;
@@ -43,4 +46,9 @@
{
return MESSAGES.format("cached-no-parameters", method);
}
+
+ static String illegalNumberOfPageActivationContextHandlers(List<String> fields)
+ {
+ return MESSAGES.format("illegal-number-of-page-activation-context-handlers", InternalUtils.joinSorted(fields));
+ }
}
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=666333&r1=666332&r2=666333&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 Tue Jun 10 14:31:25 2008
@@ -380,6 +380,8 @@
// be converted to clear out at the end of the request.
configuration.add("UnclaimedField", new UnclaimedFieldWorker(), "after:*");
+
+ configuration.add("PageActivationContext", new PageActivationContextWorker(), "before:*");
}
/**
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/transform/TransformStrings.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/transform/TransformStrings.properties?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/transform/TransformStrings.properties (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/transform/TransformStrings.properties Tue Jun 10 14:31:25 2008
@@ -16,3 +16,4 @@
component-not-assignable-to-field=Component %s is not assignable to field %s (of type %s).
cached-no-return-value=@Cached may only be used with methods that return values: %s
cached-no-parameters=@Cached cannot be used with methods that accept parameters: %s
+illegal-number-of-page-activation-context-handlers=Illegal number of fields annotated with @PageActivationContext: %s. Only one field is allowed.
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Music.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Music.tml?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Music.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Music.tml Tue Jun 10 14:31:25 2008
@@ -2,13 +2,16 @@
<h1>Music Library</h1>
- <t:grid source="tracks" row="track" remove="genre,artist,playcount">
+ <t:grid source="tracks" row="track" remove="genre,artist,playcount" add="titleCopy">
<t:parameter name="titleCell">
<t:pagelink page="music/details" context="track">${track.title}</t:pagelink>
</t:parameter>
<t:parameter name="ratingcell">
<t:outputRating rating="track.rating"/>
</t:parameter>
+ <t:parameter name="titleCopyCell">
+ <t:pagelink page="music/details2" context="track">${track.title} (Copy)</t:pagelink>
+ </t:parameter>
</t:grid>
</html>
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/music/MusicDetails2.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/music/MusicDetails2.tml?rev=666333&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/music/MusicDetails2.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/music/MusicDetails2.tml Tue Jun 10 14:31:25 2008
@@ -0,0 +1,11 @@
+<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+
+ <h1>Track Details</h1>
+
+ <t:beandisplay object="track"/>
+
+ <p>
+ <t:pagelink page="music">Back to music library</t:pagelink>
+ </p>
+
+</html>
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java Tue Jun 10 14:31:25 2008
@@ -1823,6 +1823,25 @@
assertText("activePageName", "music/Details");
}
+
+ /**
+ * TAPESTRY-2235
+ */
+ @Test
+ public void generated_activation_context_handlers()
+ {
+ start("Music Page", "69");
+
+ assertText("activePageName", "Music");
+
+ clickAndWait("link=Wake Me Up (Copy)");
+
+ assertText("activePageName", "music/Details2");
+
+ assertText("//div[@class='t-beandisplay-value title']", "Wake Me Up");
+
+ assertText("//div[@class='t-beandisplay-value artist']", "Norah Jones");
+ }
/**
* TAPESTRY-1869
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/music/MusicDetails2.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/music/MusicDetails2.java?rev=666333&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/music/MusicDetails2.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/music/MusicDetails2.java Tue Jun 10 14:31:25 2008
@@ -0,0 +1,27 @@
+// Copyright 2008 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.integration.app1.pages.music;
+
+
+import org.apache.tapestry5.annotations.PageActivationContext;
+import org.apache.tapestry5.annotations.Property;
+import org.apache.tapestry5.integration.app1.data.Track;
+
+public class MusicDetails2
+{
+ @Property
+ @PageActivationContext
+ private Track track;
+}
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java?rev=666333&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/transform/PageActivationContextWorkerTest.java Tue Jun 10 14:31:25 2008
@@ -0,0 +1,137 @@
+// Copyright 2008 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.transform;
+
+import java.lang.reflect.Modifier;
+
+import org.apache.tapestry5.annotations.PageActivationContext;
+import org.apache.tapestry5.integration.app1.data.Track;
+import org.apache.tapestry5.model.MutableComponentModel;
+import org.apache.tapestry5.services.ClassTransformation;
+import org.apache.tapestry5.services.ComponentClassTransformWorker;
+import org.apache.tapestry5.services.TransformMethodSignature;
+import org.apache.tapestry5.test.TapestryTestCase;
+import org.testng.annotations.Test;
+
+public class PageActivationContextWorkerTest extends TapestryTestCase {
+
+ private static final String CLASS_NAME = Track.class.getName();
+
+ @Test
+ public void activate_dafault_passivate_false() {
+ ClassTransformation ct = mockClassTransformation();
+ MutableComponentModel model = mockMutableComponentModel();
+ PageActivationContext annotation = newMock(PageActivationContext.class);
+ ComponentClassTransformWorker worker = new PageActivationContextWorker();
+
+ train_findFieldsWithAnnotation(ct, PageActivationContext.class,
+ "myfield");
+ train_getFieldAnnotation(ct, "myfield", PageActivationContext.class,
+ annotation);
+ train_getFieldType(ct, "myfield", CLASS_NAME);
+ expect(annotation.activate()).andReturn(true);
+
+ TransformMethodSignature sig = new TransformMethodSignature(
+ Modifier.PROTECTED | Modifier.FINAL, "void", "onActivate",
+ new String[] { CLASS_NAME }, null);
+
+ ct.addTransformedMethod(sig, "myfield = $1;");
+
+ expect(annotation.passivate()).andReturn(false);
+
+ replay();
+
+ worker.transform(ct, model);
+
+ verify();
+ }
+
+ @Test
+ public void activate_false_passivate_default() {
+ ClassTransformation ct = mockClassTransformation();
+ MutableComponentModel model = mockMutableComponentModel();
+ PageActivationContext annotation = newMock(PageActivationContext.class);
+ ComponentClassTransformWorker worker = new PageActivationContextWorker();
+
+ train_findFieldsWithAnnotation(ct, PageActivationContext.class,
+ "myfield");
+ train_getFieldAnnotation(ct, "myfield", PageActivationContext.class,
+ annotation);
+ train_getFieldType(ct, "myfield", CLASS_NAME);
+ expect(annotation.activate()).andReturn(false);
+
+ expect(annotation.passivate()).andReturn(true);
+
+ TransformMethodSignature sig = new TransformMethodSignature(
+ Modifier.PROTECTED | Modifier.FINAL, "java.lang.Object",
+ "onPassivate", null, null);
+
+ ct.addTransformedMethod(sig, "return myfield;");
+
+ replay();
+
+ worker.transform(ct, model);
+
+ verify();
+ }
+
+ @Test
+ public void activate_false_passivate_false() {
+ ClassTransformation ct = mockClassTransformation();
+ MutableComponentModel model = mockMutableComponentModel();
+ PageActivationContext annotation = newMock(PageActivationContext.class);
+ ComponentClassTransformWorker worker = new PageActivationContextWorker();
+
+ train_findFieldsWithAnnotation(ct, PageActivationContext.class,
+ "myfield");
+ train_getFieldAnnotation(ct, "myfield", PageActivationContext.class,
+ annotation);
+ train_getFieldType(ct, "myfield", CLASS_NAME);
+ expect(annotation.activate()).andReturn(false);
+
+ expect(annotation.passivate()).andReturn(false);
+
+ replay();
+
+ worker.transform(ct, model);
+
+ verify();
+ }
+
+ @Test
+ public void illegal_number_of_page_activation_context_handlers()
+ {
+ ClassTransformation ct = mockClassTransformation();
+ MutableComponentModel model = mockMutableComponentModel();
+ ComponentClassTransformWorker worker = new PageActivationContextWorker();
+
+ train_findFieldsWithAnnotation(ct, PageActivationContext.class,
+ "myfield", "myfield2");
+
+ replay();
+
+ try
+ {
+ worker.transform(ct, model);
+ fail("did not throw");
+ }catch(RuntimeException e)
+ {
+ e.printStackTrace();
+ }
+
+ verify();
+ }
+
+
+}
Modified: tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/userguide.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/userguide.apt?rev=666333&r1=666332&r2=666333&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/userguide.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-hibernate/src/site/apt/userguide.apt Tue Jun 10 14:31:25 2008
@@ -36,6 +36,23 @@
Accessing the page as <</viewperson/152>> would load the Person entity with id 152 and use that as the page context.
+Using @PageActivationContext
+
+ If you prefer to use annotations, you may let Tapestry generate the page activation context handlers for you.
+ Relying on an existing ValueEncoder for the corresponding property you can use the @PageActivationContext annotation.
+ The disadvantage is that you can't access the handlers in a unit test.
+
+
++----+
+public class ViewPerson
+{
+ @Property
+ @PageActivationContext
+ private Person person;
+
+}
++----+
+
Using @Persist with entities
If you wish to persist an entity in the session, you may use the "entity" persistence strategy: