Author: drobiazko Date: Tue Jul 14 22:25:19 2009 New Revision: 794095 URL: http://svn.apache.org/viewvc?rev=794095&view=rev Log: TAP5-226: Add annotation @SessionAttribute to map a field to a specific session attribute
Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java (with props) tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java (with props) tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java (with props) tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java (with props) tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.tml tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.tml Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/IntegrationTests.java tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java Tue Jul 14 22:25:19 2009 @@ -0,0 +1,38 @@ +// Copyright 2009 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; + +/** + * Used to map a property of a page or component to value stored in session. + * + * @since 5.2.0.0 + */ +...@target(FIELD) +...@documented +...@retention(RUNTIME) +public @interface SessionAttribute +{ + + /** + * Name of a Session-Attribute. + */ + String value() default ""; +} \ No newline at end of file Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/annotations/SessionAttribute.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java Tue Jul 14 22:25:19 2009 @@ -0,0 +1,116 @@ +// Copyright 2009 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.util.List; + +import javassist.Modifier; + +import org.apache.tapestry5.annotations.SessionAttribute; +import org.apache.tapestry5.ioc.ObjectLocator; +import org.apache.tapestry5.model.MutableComponentModel; +import org.apache.tapestry5.services.ClassTransformation; +import org.apache.tapestry5.services.ComponentClassTransformWorker; +import org.apache.tapestry5.services.Request; +import org.apache.tapestry5.services.Session; +import org.apache.tapestry5.services.TransformMethodSignature; + + +/** + * Looks for the {...@link SessionAttribute} annotation and converts read and write access on such + * fields into calls to the {...@link Session#getAttribute(String)} and + * {...@link Session#setAttribute(String, Object)}. + * + */ +public class SessionAttributeWorker implements ComponentClassTransformWorker +{ + + private ObjectLocator objectLocator; + + public SessionAttributeWorker(ObjectLocator objectLocator) + { + super(); + this.objectLocator = objectLocator; + } + + public void transform(ClassTransformation transformation, MutableComponentModel model) + { + List<String> names = transformation.findFieldsWithAnnotation(SessionAttribute.class); + + for (String fieldName : names) + { + SessionAttribute annotation = transformation.getFieldAnnotation( + fieldName, + SessionAttribute.class); + + String sessionKey = annotation.value(); + + if ("".equals(sessionKey)) + { + sessionKey = fieldName; + } + + String fieldType = transformation.getFieldType(fieldName); + + Request request = objectLocator.getService(Request.class); + + String requestField = transformation.addInjectedField( + Request.class, + "_request", + request); + + replaceReadAccess(transformation, fieldName, fieldType, sessionKey, requestField); + replaceWriteAccess(transformation, fieldName, fieldType, sessionKey, requestField); + } + } + + private void replaceReadAccess(ClassTransformation transformation, String fieldName, + String fieldType, String sessionKey, String requestField) + { + String readMethodName = transformation.newMemberName("read", fieldName); + + TransformMethodSignature readMethodSignature = new TransformMethodSignature( + Modifier.PRIVATE, fieldType, readMethodName, null, null); + + String body = String.format( + "return (%s) %s.getSession(true).getAttribute(\"%s\");", + fieldType, + requestField, + sessionKey); + + transformation.addMethod(readMethodSignature, body); + transformation.replaceReadAccess(fieldName, readMethodName); + } + + private void replaceWriteAccess(ClassTransformation transformation, String fieldName, + String fieldType, String sessionKey, String requestField) + { + String writeMethodName = transformation.newMemberName("write", fieldName); + + TransformMethodSignature writeSignature = new TransformMethodSignature(Modifier.PRIVATE, + "void", writeMethodName, new String[] + { fieldType }, null); + + String body = String.format( + "%s.getSession(true).setAttribute(\"%s\", $1);", + requestField, + sessionKey); + + transformation.addMethod(writeSignature, body); + transformation.replaceWriteAccess(fieldName, writeMethodName); + + } + +} Propchange: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java ------------------------------------------------------------------------------ svn:eol-style = native 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=794095&r1=794094&r2=794095&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 Jul 14 22:25:19 2009 @@ -531,6 +531,8 @@ configuration.add("UnclaimedField", new UnclaimedFieldWorker(), "after:*"); configuration.add("PageActivationContext", new PageActivationContextWorker(), "before:OnEvent"); + + configuration.add("SessionAttribute", new SessionAttributeWorker(locator)); } /** 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=794095&r1=794094&r2=794095&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 Jul 14 22:25:19 2009 @@ -2966,4 +2966,18 @@ assertText("divwithid","Div Content"); } + + @Test + public void session_attribute() + { + start("SessionAttribute Demo"); + + assertTextPresent("Foo"); + assertTextPresent("Bar"); + + clickAndWait("link=Read SessionAttribute"); + + assertTextPresent("read Foo"); + assertTextPresent("read Bar"); + } } \ No newline at end of file Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java?rev=794095&r1=794094&r2=794095&view=diff ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java (original) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/Index.java Tue Jul 14 22:25:19 2009 @@ -355,7 +355,11 @@ new Item("RenderClientIdDemo", "RenderClientId Mixin", - "Force render of client-side id of a client element via the RenderClientId mixin") + "Force render of client-side id of a client element via the RenderClientId mixin"), + + new Item("SessionAttributeDemo", + "SessionAttribute Demo", + "Annotation to map a field to a specific session attribute") ); Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java Tue Jul 14 22:25:19 2009 @@ -0,0 +1,32 @@ +// Copyright 2009 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; + +import org.apache.tapestry5.annotations.Property; +import org.apache.tapestry5.annotations.SessionAttribute; +import org.apache.tapestry5.integration.app1.data.Track; + +public class ReadSessionAttribute +{ + + @SessionAttribute + @Property + private Track favoriteTrack; + + @SessionAttribute("track_in_session") + @Property + private Track anotherTrack; + +} Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java Tue Jul 14 22:25:19 2009 @@ -0,0 +1,49 @@ +// Copyright 2009 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; + +import org.apache.tapestry5.annotations.SessionAttribute; +import org.apache.tapestry5.integration.app1.data.Track; +import org.apache.tapestry5.ioc.annotations.Inject; +import org.apache.tapestry5.services.Request; + +public class SessionAttributeDemo +{ + @Inject + private Request request; + + @SessionAttribute + private Track favoriteTrack; + + @SessionAttribute("track_in_session") + private Track anotherTrack; + + void onActivate() + { + favoriteTrack = new Track(); + favoriteTrack.setTitle("Foo"); + + anotherTrack = new Track(); + anotherTrack.setTitle("Bar"); + } + + public Track getFavoriteTrack(){ + return (Track) request.getSession(true).getAttribute("favoriteTrack"); + } + + public Track getAnotherTrack(){ + return (Track) request.getSession(true).getAttribute("track_in_session"); + } +} Propchange: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.tml?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.tml (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/ReadSessionAttribute.tml Tue Jul 14 22:25:19 2009 @@ -0,0 +1,7 @@ +<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> + <h1>Read SessionAttribute Demo</h1> + + <p>read ${favoriteTrack.title}</p> + <p>read ${anotherTrack.title}</p> + +</html> Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.tml URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.tml?rev=794095&view=auto ============================================================================== --- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.tml (added) +++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/SessionAttributeDemo.tml Tue Jul 14 22:25:19 2009 @@ -0,0 +1,9 @@ +<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> + <h1>@SessionAttribute Demo</h1> + + <p>${favoriteTrack.title}</p> + <p>${anotherTrack.title}</p> + + <t:pagelink page="ReadSessionAttribute">Read SessionAttribute</t:pagelink> + +</html>