[seam-commits] Seam SVN: r8479 - in trunk: src/wicket/org/jboss/seam/wicket and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Jul 17 14:29:30 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-07-17 14:29:30 -0400 (Thu, 17 Jul 2008)
New Revision: 8479

Added:
   trunk/src/wicket/org/jboss/seam/wicket/SeamPropertyModel.java
Modified:
   trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/FormInputBorder.java
   trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Main.java
   trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Register.java
   trunk/src/wicket/org/jboss/seam/wicket/ModelValidator.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java
Log:
Some fixes for the interceptor model and a PropertyModel which supports injection

Modified: trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/FormInputBorder.java
===================================================================
--- trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/FormInputBorder.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/FormInputBorder.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -6,9 +6,11 @@
 import org.apache.wicket.markup.html.border.Border;
 import org.apache.wicket.markup.html.form.FormComponent;
 import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
+import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.model.PropertyModel;
 import org.jboss.seam.wicket.ModelValidator;
+import org.jboss.seam.wicket.SeamPropertyModel;
 
 /**
  * Wicket allows you to build powerful custom components easily.
@@ -36,11 +38,26 @@
     * @param component The component to wrap
     * @param model The model to attach the component to
     */
+   public FormInputBorder(String id, String label, FormComponent component, PropertyModel model, boolean ajaxValidate)
+   {
+      this(id, label, component, model, ajaxValidate, model.getTarget().getClass(), model.getPropertyExpression());
+   }
+   
+   public FormInputBorder(String id, String label, FormComponent component, SeamPropertyModel model, boolean ajaxValidate)
+   {
+      this(id, label, component, model, ajaxValidate, model.getTarget().getClass(), model.getPropertyExpression());
+   }
+   
    public FormInputBorder(String id, String label, FormComponent component, PropertyModel model)
    {
-      this(id, label, component, model, true);
+      this(id, label, component, model, true, model.getTarget().getClass(), model.getPropertyExpression());
    }
    
+   public FormInputBorder(String id, String label, FormComponent component, SeamPropertyModel model)
+   {
+      this(id, label, component, model, true, model.getTarget().getClass(), model.getPropertyExpression());
+   }
+   
    /**
     * Create a new form input border which validates
     * @param id Id of border component on page
@@ -49,7 +66,7 @@
     * @param model The model to attach the component to
     * @param ajaxValidate Whether to use ajax validation
     */
-   public FormInputBorder(String id, String label, FormComponent component, PropertyModel model, boolean ajaxValidate)
+   public FormInputBorder(String id, String label, FormComponent component, IModel model, boolean ajaxValidate, Class modelClass, String propertyExpression)
    {
       super(id);
       component.setLabel(new Model(label));
@@ -63,11 +80,10 @@
       }
       Label labelComponent = new Label("label", label);
       add(labelComponent);
-      add(component, model);
+      add(component, model, modelClass, propertyExpression);
       feedbackPanel = new ComponentFeedbackPanel("message", component);
       
       add(feedbackPanel);
-      component.add(new ModelValidator(model));
       
       if (ajaxValidate)
       {
@@ -100,7 +116,12 @@
    
    public FormInputBorder add(FormComponent component, PropertyModel model)
    {
-      component.add(new ModelValidator(model));
+      return add(component, model, model.getTarget().getClass(), model.getPropertyExpression());
+   }
+   
+   public FormInputBorder add(FormComponent component, IModel model, Class modelClass, String expression)
+   {
+      component.add(new ModelValidator(modelClass, expression));
       component.setModel(model);
       add(component);
       return this;

Modified: trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Main.java
===================================================================
--- trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Main.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Main.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -36,6 +36,7 @@
 import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
 import org.apache.wicket.markup.repeater.Item;
 import org.apache.wicket.markup.repeater.data.DataView;
+import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.PropertyModel;
 import org.jboss.seam.annotations.In;
 import org.jboss.seam.annotations.security.Restrict;
@@ -44,6 +45,7 @@
 import org.jboss.seam.example.wicket.action.Hotel;
 import org.jboss.seam.example.wicket.action.HotelSearching;
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.wicket.SeamPropertyModel;
 
 @Restrict
 public class Main extends WebPage
@@ -227,7 +229,16 @@
       public HotelSearchForm(String id)
       {
          super(id);
-         add(new TextField("searchString", new PropertyModel(hotelSearch, "searchString")));
+         add(new TextField("searchString", new SeamPropertyModel("searchString")
+         {
+            
+            @Override
+            public Object getTarget()
+            {
+               return hotelSearch;
+            }
+         
+         }));
          List<Integer> pageSizes = Arrays.asList(new Integer[] { 5, 10, 20 });
          add(new DropDownChoice("pageSize", new PropertyModel(this, "pageSize"), pageSizes));
          add(new IndicatingAjaxButton("submit", this)

Modified: trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Register.java
===================================================================
--- trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Register.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/examples/wicket/src/web/org/jboss/seam/example/wicket/Register.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -8,9 +8,11 @@
 import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
 import org.apache.wicket.markup.html.link.PageLink;
 import org.apache.wicket.markup.html.panel.ComponentFeedbackPanel;
+import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.PropertyModel;
 import org.jboss.seam.annotations.In;
 import org.jboss.seam.example.wicket.action.User;
+import org.jboss.seam.wicket.SeamPropertyModel;
 
 public class Register extends WebPage
 {
@@ -40,12 +42,42 @@
          add(new PageLink("cancel", Home.class));
          username = new TextField("username");
          username.setRequired(true);
-         add(new FormInputBorder("usernameDecorate", "Username", username, new PropertyModel(user, "username")));
-         add(new FormInputBorder("nameDecorate", "Real Name", new TextField("name").setRequired(true), new PropertyModel(user, "name")));
+         add(new FormInputBorder("usernameDecorate", "Username", username, new SeamPropertyModel("username")
+         {
+            
+            @Override
+            public Object getTarget()
+            {
+               return user;
+            }
+            
+         }));
+         add(new FormInputBorder("nameDecorate", "Real Name", new TextField("name").setRequired(true), new SeamPropertyModel("name")
+         {
+            @Override
+            public Object getTarget()
+            {
+               return user;
+            }
+         }));
          FormComponent password = new PasswordTextField("password").setRequired(true);
          FormComponent verify = new PasswordTextField("verify").setRequired(true);
-         add(new FormInputBorder("passwordDecorate", "Password", password , new PropertyModel(user, "password")));
-         add(new FormInputBorder("verifyDecorate", "Verify Password", verify, new PropertyModel(register, "verify")));
+         add(new FormInputBorder("passwordDecorate", "Password", password , new SeamPropertyModel("password")
+         {
+            @Override
+            public Object getTarget()
+            {
+               return user;
+            }
+         }));
+         add(new FormInputBorder("verifyDecorate", "Verify Password", verify, new SeamPropertyModel("verify")
+         {
+            @Override
+            public Object getTarget()
+            {
+               return register;
+            }
+         }));
          add(new EqualPasswordInputValidator(password, verify));
       }
       

Modified: trunk/src/wicket/org/jboss/seam/wicket/ModelValidator.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ModelValidator.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/src/wicket/org/jboss/seam/wicket/ModelValidator.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -1,5 +1,6 @@
 package org.jboss.seam.wicket;
 
+import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.PropertyModel;
 import org.apache.wicket.validation.IValidatable;
 import org.apache.wicket.validation.IValidationError;
@@ -38,10 +39,10 @@
     */
    public ModelValidator(PropertyModel propertyModel)
    {
-      this.clazz = propertyModel.getTarget().getClass();
-      this.property = propertyModel.getPropertyExpression();
+      this(propertyModel.getTarget().getClass(), propertyModel.getPropertyExpression());
    }
    
+   
    /**
     * Do the validation, normally called by Wicket
     */

Added: trunk/src/wicket/org/jboss/seam/wicket/SeamPropertyModel.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/SeamPropertyModel.java	                        (rev 0)
+++ trunk/src/wicket/org/jboss/seam/wicket/SeamPropertyModel.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -0,0 +1,48 @@
+package org.jboss.seam.wicket;
+
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.PropertyModel;
+
+public abstract class SeamPropertyModel implements IModel
+{
+   
+   private String expression;
+   private PropertyModel model;
+   
+   public SeamPropertyModel(String expression)
+   {
+      this.expression = expression;
+   }
+
+   public abstract Object getTarget();
+   
+   public Object getObject()
+   {
+      return getModel().getObject();
+   }
+   
+   public void setObject(Object object)
+   {
+      getModel().setObject(object);
+   }
+   
+   private PropertyModel getModel()
+   {
+      if (model == null)
+      {
+         model = new PropertyModel(getTarget(), expression);
+      }
+      return model;
+   }
+   
+   public void detach()
+   {
+      model = null;
+   }
+   
+   public String getPropertyExpression()
+   {
+      return getModel().getPropertyExpression();
+   }
+
+}


Property changes on: trunk/src/wicket/org/jboss/seam/wicket/SeamPropertyModel.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -123,64 +123,67 @@
    {
       log.debug("Instrumenting " + className);
       CtClass implementation = classPool.get(className);
-      CtClass handlerClass = classPool.get(WicketHandler.class.getName());
-      
-      CtField handlerField = new CtField(handlerClass, "handler", implementation);
-      Initializer handlerInitializer = Initializer.byCall(handlerClass, "create");
-      implementation.addField(handlerField, handlerInitializer);
-      
-      CtClass instrumentedComponent = classPool.get(InstrumentedComponent.class.getName());
-      implementation.addInterface(instrumentedComponent);
-      CtMethod getHandlerMethod = CtNewMethod.getter("getHandler", handlerField);
-      CtMethod getEnclosingInstance = CtNewMethod.make("public " + InstrumentedComponent.class.getName() +" getEnclosingInstance() { return " + WicketHandler.class.getName() + ".getEnclosingInstance(this); }", implementation);
-      implementation.addMethod(getEnclosingInstance);
-      implementation.addMethod(getHandlerMethod);
-      
-      for (CtMethod method : implementation.getDeclaredMethods())
+      if (isInstrumentable(implementation))
       {
-         if (!Modifier.isStatic(method.getModifiers()))
+         CtClass handlerClass = classPool.get(WicketHandler.class.getName());
+         
+         CtField handlerField = new CtField(handlerClass, "handler", implementation);
+         Initializer handlerInitializer = Initializer.byCall(handlerClass, "create");
+         implementation.addField(handlerField, handlerInitializer);
+         
+         CtClass instrumentedComponent = classPool.get(InstrumentedComponent.class.getName());
+         implementation.addInterface(instrumentedComponent);
+         CtMethod getHandlerMethod = CtNewMethod.getter("getHandler", handlerField);
+         CtMethod getEnclosingInstance = CtNewMethod.make("public " + InstrumentedComponent.class.getName() +" getEnclosingInstance() { return " + WicketHandler.class.getName() + ".getEnclosingInstance(this, 10); }", implementation);
+         implementation.addMethod(getEnclosingInstance);
+         implementation.addMethod(getHandlerMethod);
+         
+         for (CtMethod method : implementation.getDeclaredMethods())
          {
-            String methodName = method.getName();
-            if (!("getHandler".equals(method.getName()) || "getEnclosingInstance".equals(method.getName()) ))
+            if (!Modifier.isStatic(method.getModifiers()))
             {
-               String methodSignature = "";
-               for (int i = 0; i < method.getParameterTypes().length; i++)
+               String methodName = method.getName();
+               if (!("getHandler".equals(method.getName()) || "getEnclosingInstance".equals(method.getName())))
                {
-                  if (i > 0)
+                  String methodSignature = "";
+                  for (int i = 0; i < method.getParameterTypes().length; i++)
                   {
-                     methodSignature += ",";
+                     if (i > 0)
+                     {
+                        methodSignature += ",";
+                     }
+                     methodSignature += method.getParameterTypes()[i].getName() + ".class";
                   }
-                  methodSignature += method.getParameterTypes()[i].getName() + ".class";
+                  String methodCall = "this.getClass().getDeclaredMethod(\""+ methodName + "\", methodParameters)";
+                  String methodParameters;
+                  if (methodSignature.length() > 0)
+                  {
+                     methodParameters = "Class[] methodParameters = {" + methodSignature + "};";
+                  }
+                  else
+                  {
+                     methodParameters = "Class[] methodParameters = new Class[0];";
+                  }
+                  log.trace("Method call: " + methodCall);
+                  
+                  method.insertBefore(methodParameters + "handler.beforeInvoke(this, " + methodCall + ");");
+                  method.insertBefore("handler.setCallInProgress(true);");
+                  method.insertAfter(methodParameters + "handler.afterInvoke(this, " + methodCall + ");");
+                  method.insertAfter("handler.setCallInProgress(false);", true);
+                  log.trace("instrumented method " + method.getName());
                }
-               String methodCall = "this.getClass().getDeclaredMethod(\""+ methodName + "\", methodParameters)";
-               String methodParameters;
-               if (methodSignature.length() > 0)
-               {
-                  methodParameters = "Class[] methodParameters = {" + methodSignature + "};";
-               }
-               else
-               {
-                  methodParameters = "Class[] methodParameters = new Class[0];";
-               }
-               log.trace("Method call: " + methodCall);
-               
-               method.insertBefore(methodParameters + "handler.beforeInvoke(this, " + methodCall + ");");
-               method.insertBefore("handler.setCallInProgress(true);");
-               method.insertAfter(methodParameters + "handler.afterInvoke(this, " + methodCall + ");");
-               method.insertAfter("handler.setCallInProgress(false);", true);
-               log.trace("instrumented method " + method.getName());
             }
          }
-      }
-      for (CtConstructor constructor : implementation.getConstructors())
-      {
-         if (constructor.isConstructor())
+         for (CtConstructor constructor : implementation.getConstructors())
          {
-            constructor.insertBeforeBody("handler.beforeInvoke(this);");
-            constructor.insertBeforeBody("handler.setCallInProgress(true);");
-            constructor.insertAfter("handler.afterInvoke(this);");
-            constructor.insertAfter("handler.setCallInProgress(false);");
-            log.trace("instrumented constructor " + constructor.getName());
+            if (constructor.isConstructor())
+            {
+               constructor.insertBeforeBody("handler.beforeInvoke(this);");
+               constructor.insertBeforeBody("handler.setCallInProgress(true);");
+               constructor.insertAfter("handler.afterInvoke(this);");
+               constructor.insertAfter("handler.setCallInProgress(false);");
+               log.trace("instrumented constructor " + constructor.getName());
+            }
          }
       }
       classes.add(implementation.getName());
@@ -191,4 +194,10 @@
       return classLoader;
    }
 
+   private static boolean isInstrumentable(CtClass clazz)
+   {
+      int modifiers = clazz.getModifiers();
+      return !(Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers) || Modifier.isEnum(modifiers));
+   }
+   
 }

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java	2008-07-17 17:52:41 UTC (rev 8478)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java	2008-07-17 18:29:30 UTC (rev 8479)
@@ -27,6 +27,7 @@
    private Class<?> type;
    private transient WicketComponent component;
    private boolean callInProgress;
+   private int reentrant = 0;
    
    private WicketComponent getComponent()
    {
@@ -70,22 +71,34 @@
    
    private void beforeInvoke(InvocationContext invocationContext)
    {
-      for (RootInterceptor interceptor : getInterceptors())
+      if (reentrant ==0)
       {
-         interceptor.beforeInvoke(invocationContext);
+         for (RootInterceptor interceptor : getInterceptors())
+         {
+            interceptor.beforeInvoke(invocationContext);
+         }
       }
+      reentrant++;
    }
    
    private void afterInvoke(InvocationContext invocationContext)
    {
-      for (RootInterceptor interceptor : getInterceptors())
+      reentrant--;
+      if (reentrant == 0)
       {
-         interceptor.afterInvoke(invocationContext);
+         for (RootInterceptor interceptor : getInterceptors())
+         {
+            interceptor.afterInvoke(invocationContext);
+         }
       }
    }
  
    public boolean isCallInProgress()
    {
+      if (callInProgress == false)
+      {
+         reentrant = 0;
+      }
       return callInProgress;
    }
    
@@ -94,14 +107,14 @@
       this.callInProgress = callInProgress;
    }
    
-   public static InstrumentedComponent getEnclosingInstance(Object bean)
+   public static InstrumentedComponent getEnclosingInstance(Object bean, int level)
    {
       Class enclosingType = bean.getClass().getEnclosingClass();
       if (enclosingType != null)
       {
          try 
          {
-            java.lang.reflect.Field enclosingField = bean.getClass().getDeclaredField("this$0");
+            java.lang.reflect.Field enclosingField = bean.getClass().getDeclaredField("this$" + level);
             enclosingField.setAccessible(true);
             Object enclosingInstance = enclosingField.get(bean);
             if (enclosingInstance instanceof InstrumentedComponent)
@@ -109,7 +122,17 @@
                return (InstrumentedComponent) enclosingInstance;
             }
          }
-         catch (Exception e) {}
+         catch (Exception e) 
+         {
+            if (level == 0)
+            {
+               return null;
+            }
+            else
+            {
+               return getEnclosingInstance(bean, level -1);
+            }
+         }
       }
       return null;
    }




More information about the seam-commits mailing list