Author: mgrigorov
Date: Thu Oct 27 15:08:35 2011
New Revision: 1189798

URL: http://svn.apache.org/viewvc?rev=1189798&view=rev
Log:
WICKET-4149 Modify wicket-ioc modules to take advantage of the new 
IBehaviorInstantiationListener


Added:
    
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestBehavior.java
Modified:
    
wicket/trunk/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
    
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
    
wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/SpringComponentInjector.java
    
wicket/trunk/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java

Modified: 
wicket/trunk/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java?rev=1189798&r1=1189797&r2=1189798&view=diff
==============================================================================
--- 
wicket/trunk/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
 (original)
+++ 
wicket/trunk/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
 Thu Oct 27 15:08:35 2011
@@ -18,7 +18,9 @@ package org.apache.wicket.guice;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
+import org.apache.wicket.IBehaviorInstantiationListener;
 import org.apache.wicket.application.IComponentInstantiationListener;
+import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.injection.IFieldValueFactory;
 
 import com.google.inject.Guice;
@@ -44,7 +46,8 @@ import com.google.inject.Stage;
  */
 public class GuiceComponentInjector extends 
org.apache.wicket.injection.Injector
        implements
-               IComponentInstantiationListener
+               IComponentInstantiationListener,
+               IBehaviorInstantiationListener
 {
        private final IFieldValueFactory fieldValueFactory;
 
@@ -102,6 +105,7 @@ public class GuiceComponentInjector exte
        {
                app.setMetaData(GuiceInjectorHolder.INJECTOR_KEY, new 
GuiceInjectorHolder(injector));
                fieldValueFactory = new GuiceFieldValueFactory(wrapInProxies);
+               app.getBehaviorInstantiationListeners().add(this);
                bind(app);
        }
 
@@ -121,4 +125,9 @@ public class GuiceComponentInjector exte
        {
                inject(component);
        }
+
+       public void onInstantiation(Behavior behavior)
+       {
+               inject(behavior);
+       }
 }

Added: 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestBehavior.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestBehavior.java?rev=1189798&view=auto
==============================================================================
--- 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestBehavior.java
 (added)
+++ 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestBehavior.java
 Thu Oct 27 15:08:35 2011
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.wicket.guice;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.behavior.Behavior;
+import org.junit.Assert;
+
+import com.google.inject.Inject;
+
+/**
+ * A behavior that will be use injected services
+ * 
+ * https://issues.apache.org/jira/browse/WICKET-4149
+ */
+public class TestBehavior extends Behavior
+{
+       private static final long serialVersionUID = 1L;
+
+       @Inject
+       @Blue
+       private ITestService injectedFieldBlue;
+
+       @Override
+       public void bind(Component component)
+       {
+               super.bind(component);
+
+               Assert.assertNotNull(injectedFieldBlue);
+               Assert.assertEquals("blue", injectedFieldBlue.getString());
+       }
+
+}

Modified: 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java?rev=1189798&r1=1189797&r2=1189798&view=diff
==============================================================================
--- 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
 (original)
+++ 
wicket/trunk/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
 Thu Oct 27 15:08:35 2011
@@ -62,6 +62,7 @@ public class TestComponent extends Compo
        {
                super(id);
                noComponent = new TestNoComponent();
+               add(new TestBehavior());
        }
 
        /**

Modified: 
wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/SpringComponentInjector.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/SpringComponentInjector.java?rev=1189798&r1=1189797&r2=1189798&view=diff
==============================================================================
--- 
wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/SpringComponentInjector.java
 (original)
+++ 
wicket/trunk/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/SpringComponentInjector.java
 Thu Oct 27 15:08:35 2011
@@ -20,14 +20,17 @@ import javax.servlet.ServletContext;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
+import org.apache.wicket.IBehaviorInstantiationListener;
 import org.apache.wicket.MetaDataKey;
 import org.apache.wicket.Session;
 import org.apache.wicket.application.IComponentInstantiationListener;
+import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.injection.IFieldValueFactory;
 import org.apache.wicket.injection.Injector;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.protocol.http.WebApplication;
 import org.apache.wicket.spring.ISpringContextLocator;
+import org.apache.wicket.util.lang.Args;
 import org.springframework.context.ApplicationContext;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 
@@ -44,7 +47,10 @@ import org.springframework.web.context.s
  * @author <a href="mailto:[email protected]";>Justin Lee</a>
  * 
  */
-public class SpringComponentInjector extends Injector implements 
IComponentInstantiationListener
+public class SpringComponentInjector extends Injector
+       implements
+               IComponentInstantiationListener,
+               IBehaviorInstantiationListener
 {
        private final IFieldValueFactory fieldValueFactory;
 
@@ -101,19 +107,14 @@ public class SpringComponentInjector ext
        public SpringComponentInjector(final WebApplication webapp, final 
ApplicationContext ctx,
                final boolean wrapInProxies)
        {
-               if (webapp == null)
-               {
-                       throw new IllegalArgumentException("Argument [[webapp]] 
cannot be null");
-               }
+               Args.notNull(webapp, "webapp");
 
-               if (ctx == null)
-               {
-                       throw new IllegalArgumentException("Argument [[ctx]] 
cannot be null");
-               }
+               Args.notNull(ctx, "ctx");
 
                // store context in application's metadata ...
                webapp.setMetaData(CONTEXT_KEY, ctx);
                fieldValueFactory = new AnnotProxyFieldValueFactory(new 
ContextLocator(), wrapInProxies);
+               webapp.getBehaviorInstantiationListeners().add(this);
                bind(webapp);
        }
 
@@ -131,6 +132,11 @@ public class SpringComponentInjector ext
        }
 
 
+       public void onInstantiation(Behavior behavior)
+       {
+               inject(behavior);
+       }
+
        /**
         * A context locator that locates the context in application's 
metadata. This locator also keeps
         * a transient cache of the lookup.
@@ -155,5 +161,4 @@ public class SpringComponentInjector ext
 
        }
 
-
 }

Modified: 
wicket/trunk/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java?rev=1189798&r1=1189797&r2=1189798&view=diff
==============================================================================
--- 
wicket/trunk/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
 (original)
+++ 
wicket/trunk/wicket-spring/src/test/java/org/apache/wicket/spring/injection/annot/SpringBeanTest.java
 Thu Oct 27 15:08:35 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.wicket.spring.injection.annot;
 
+import org.apache.wicket.Page;
+import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.proxy.ILazyInitProxy;
 import org.apache.wicket.spring.Bean;
 import org.apache.wicket.spring.SpringBeanLocator;
@@ -125,6 +127,20 @@ public class SpringBeanTest extends Asse
 
                assertTrue(locator.getBeanName().equals("mrBean"));
        }
+
+       /**
+        * https://issues.apache.org/jira/browse/WICKET-4149
+        */
+       @Test
+       public void beanInjectedInBehavior()
+       {
+               ctx.putBean("mrBean", new Bean());
+
+               // with no name specified we get IllegalStateException
+               Page page = tester.startPage(new 
AnnotatedFieldInBehaviorPage());
+               TestBehavior behavior = (TestBehavior)page.getBehaviorById(0);
+               assertNotNull(behavior.getBean());
+       }
 }
 
 class AnnotatedBeanRequired extends DummyHomePage
@@ -159,3 +175,32 @@ class AnnotatedBeanNotRequiredDifferentN
                return bean;
        }
 }
+
+/**
+ * A behavior which will be automatically processed for @SpringBean annotation
+ */
+class TestBehavior extends Behavior
+{
+       private static final long serialVersionUID = 1L;
+
+       @SpringBean()
+       private Bean bean;
+
+       public Bean getBean()
+       {
+               return bean;
+       }
+}
+
+/**
+ * A test page with a behavior which will be processed for @SpringBean 
annotations
+ */
+class AnnotatedFieldInBehaviorPage extends DummyHomePage
+{
+       private static final long serialVersionUID = 1L;
+
+       public AnnotatedFieldInBehaviorPage()
+       {
+               add(new TestBehavior());
+       }
+}
\ No newline at end of file


Reply via email to