Author: hlship
Date: Fri Feb 19 18:54:16 2010
New Revision: 911929
URL: http://svn.apache.org/viewvc?rev=911929&view=rev
Log:
Rewrite SessionAttributeWorker to use a FieldValueConduit
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/services/TapestryModule.java
Modified:
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=911929&r1=911928&r2=911929&view=diff
==============================================================================
---
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java
(original)
+++
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/SessionAttributeWorker.java
Fri Feb 19 18:54:16 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,91 +14,75 @@
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.ioc.services.FieldValueConduit;
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;
+import org.apache.tapestry5.services.TransformField;
/**
* 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)}.
+ * fields into calls to the {...@link Session#getAttribute(String)} and
{...@link Session#setAttribute(String, Object)}.
*/
public class SessionAttributeWorker implements ComponentClassTransformWorker
{
- private final ObjectLocator objectLocator;
+ private final Request request;
- public SessionAttributeWorker(ObjectLocator objectLocator)
+ private class SessionKeyConduit implements FieldValueConduit
{
- this.objectLocator = objectLocator;
- }
+ private final String key;
- public void transform(ClassTransformation transformation,
MutableComponentModel model)
- {
- List<String> names =
transformation.findFieldsWithAnnotation(SessionAttribute.class);
-
- for (String fieldName : names)
+ public SessionKeyConduit(String key)
{
- SessionAttribute annotation =
transformation.getFieldAnnotation(fieldName,
- SessionAttribute.class);
-
- String sessionKey = annotation.value();
-
- if ("".equals(sessionKey))
- {
- sessionKey = fieldName;
- }
-
- String fieldType = transformation.getFieldType(fieldName);
+ this.key = key;
+ }
- Request request = objectLocator.getService(Request.class);
+ private Session getSession()
+ {
+ return request.getSession(true);
+ }
- String requestField =
transformation.addInjectedField(Request.class, "_request",
- request);
+ public Object get()
+ {
+ // TODO: caching, and not creating the session unnecessarily
+ return getSession().getAttribute(key);
+ }
- replaceReadAccess(transformation, fieldName, fieldType,
sessionKey, requestField);
- replaceWriteAccess(transformation, fieldName, fieldType,
sessionKey, requestField);
+ public void set(Object newValue)
+ {
+ getSession().setAttribute(key, newValue);
}
}
- private void replaceReadAccess(ClassTransformation transformation, String
fieldName,
- String fieldType, String sessionKey, String requestField)
+ public SessionAttributeWorker(Request request)
{
- 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);
+ this.request = request;
}
- private void replaceWriteAccess(ClassTransformation transformation, String
fieldName,
- String fieldType, String sessionKey, String requestField)
+ public void transform(ClassTransformation transformation,
MutableComponentModel model)
{
- String writeMethodName = transformation.newMemberName("write",
fieldName);
+ for (TransformField field :
transformation.matchFieldsWithAnnotation(SessionAttribute.class))
+ {
+ convertFieldToSessionAccess(field);
+ }
+ }
- TransformMethodSignature writeSignature = new
TransformMethodSignature(Modifier.PRIVATE,
- "void", writeMethodName, new String[]
- { fieldType }, null);
+ private void convertFieldToSessionAccess(TransformField field)
+ {
+ SessionAttribute annotation =
field.getAnnotation(SessionAttribute.class);
- String body = String.format("%s.getSession(true).setAttribute(\"%s\",
$1);", requestField,
- sessionKey);
+ field.claim(annotation);
- transformation.addMethod(writeSignature, body);
- transformation.replaceWriteAccess(fieldName, writeMethodName);
+ String key = determineSessionKey(field, annotation.value());
+ field.replaceAccess(new SessionKeyConduit(key));
}
+ private String determineSessionKey(TransformField field, String value)
+ {
+ return value.equals("") ? field.getName() : value;
+ }
}
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=911929&r1=911928&r2=911929&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
Fri Feb 19 18:54:16 2010
@@ -582,7 +582,7 @@
add(configuration, PageDetached.class,
TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE, "pageDetached");
configuration.add("Retain", new RetainWorker());
- configuration.addInstance("Persist", PersistWorker.class);
+ configuration.addInstance("Persist", PersistWorker.class);
configuration.addInstance("IncludeStylesheet",
IncludeStylesheetWorker.class, "after:SetupRender");
configuration
@@ -611,7 +611,7 @@
configuration.add("PageActivationContext", new
PageActivationContextWorker(), "before:OnEvent");
- configuration.add("SessionAttribute", new
SessionAttributeWorker(locator));
+ configuration.addInstance("SessionAttribute",
SessionAttributeWorker.class);
}
/**