You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2010/09/27 20:18:59 UTC
svn commit: r1001852 - in /wicket/branches/wicket-1.4.x/wicket/src:
main/java/org/apache/wicket/ main/java/org/apache/wicket/application/
test/java/org/apache/wicket/
Author: ivaynberg
Date: Mon Sep 27 18:18:59 2010
New Revision: 1001852
URL: http://svn.apache.org/viewvc?rev=1001852&view=rev
Log:
Issue: WICKET-3082
Added:
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java (with props)
Modified:
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Component.java
wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java?rev=1001852&r1=1001851&r2=1001852&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Application.java Mon Sep 27 18:18:59 2010
@@ -27,7 +27,9 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
+import org.apache.wicket.application.IComponentInitializationListener;
import org.apache.wicket.application.IComponentInstantiationListener;
import org.apache.wicket.application.IComponentOnAfterRenderListener;
import org.apache.wicket.application.IComponentOnBeforeRenderListener;
@@ -235,6 +237,9 @@ public abstract class Application
/** list of {@link IComponentInstantiationListener}s. */
private IComponentInstantiationListener[] componentInstantiationListeners = new IComponentInstantiationListener[0];
+ /** list of {@link IComponentInitializationListener}s. */
+ private CopyOnWriteArrayList<IComponentInitializationListener> componentInitializationListeners = new CopyOnWriteArrayList<IComponentInitializationListener>();
+
/** The converter locator instance. */
private IConverterLocator converterLocator;
@@ -330,6 +335,49 @@ public abstract class Application
}
/**
+ * Adds a component initialization listener. This method should typically only be called during
+ * application startup; it is not thread safe.
+ * <p>
+ * Each added listener will be notified after Component's {@link Component#onInitialize()}
+ * method has been executed.
+ * </p>
+ * <p>
+ * Note: wicket does not guarantee the execution order of added listeners
+ *
+ * @param listener
+ * the listener to add
+ */
+ public final void addComponentInitializationListener(
+ final IComponentInitializationListener listener)
+ {
+ if (listener == null)
+ {
+ throw new IllegalArgumentException("argument listener may not be null");
+ }
+
+ if (componentInitializationListeners.contains(listener))
+ {
+ return;
+ }
+ componentInitializationListeners.add(listener);
+ }
+
+ /**
+ * Fires registered {@link IComponentInitializationListener}s on the component
+ *
+ * @param component
+ *
+ * @see #addComponentInitializationListener(IComponentInitializationListener)
+ */
+ public final void fireComponentInitializationListeners(Component component)
+ {
+ for (IComponentInitializationListener listener : componentInitializationListeners)
+ {
+ listener.onInitialize(component);
+ }
+ }
+
+ /**
* Configures application settings to good defaults.
*/
public final void configure()
Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Component.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Component.java?rev=1001852&r1=1001851&r2=1001852&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Component.java (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/Component.java Mon Sep 27 18:18:59 2010
@@ -4056,6 +4056,8 @@ public abstract class Component implemen
" has not called super.onInitializer() in the override of onInitialize() method");
}
setFlag(FLAG_INITIALIZE_SUPER_CALL_VERIFIED, false);
+
+ getApplication().fireComponentInitializationListeners(this);
}
}
Modified: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/MarkupContainer.java?rev=1001852&r1=1001851&r2=1001852&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/MarkupContainer.java (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/MarkupContainer.java Mon Sep 27 18:18:59 2010
@@ -971,12 +971,12 @@ public abstract class MarkupContainer ex
if (page != null)
{
- component.initialize();
+ page.componentAdded(component);
}
if (page != null)
{
- page.componentAdded(component);
+ component.initialize();
}
// if the PREPARED_FOR_RENDER flag is set, we have already called
Added: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java?rev=1001852&view=auto
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java (added)
+++ wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java Mon Sep 27 18:18:59 2010
@@ -0,0 +1,38 @@
+/*
+ * 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.application;
+
+import org.apache.wicket.Component;
+
+/**
+ * Listener interface that receives messages when components are initialized (after
+ * Component#onInitialize method has been executed).
+ *
+ * <strong>Implementations must be thread safe</strong>
+ *
+ * @author Igor Vaynberg
+ */
+public interface IComponentInitializationListener
+{
+ /**
+ * Called for every component after its Component#onInitialize method has been executed.
+ *
+ * @param component
+ * the component that is being instantiated.
+ */
+ void onInitialize(Component component);
+}
Propchange: wicket/branches/wicket-1.4.x/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java?rev=1001852&r1=1001851&r2=1001852&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java (original)
+++ wicket/branches/wicket-1.4.x/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java Mon Sep 27 18:18:59 2010
@@ -16,6 +16,10 @@
*/
package org.apache.wicket;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.wicket.application.IComponentInitializationListener;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -126,6 +130,56 @@ public class ComponentInitializationTest
assertTrue(illegalState);
}
+ public void testInitListeners()
+ {
+ TestInitListener listener1 = new TestInitListener();
+ TestInitListener listener2 = new TestInitListener();
+ tester.getApplication().addComponentInitializationListener(listener1);
+ tester.getApplication().addComponentInitializationListener(listener2);
+
+ WebPage page = new WebPage()
+ {
+ };
+ TestComponent t1 = new TestComponent("t1");
+ TestComponent t2 = new TestComponent("t2");
+
+ t1.add(t2);
+ page.add(t1);
+
+ assertTrue(listener1.getComponents().contains(page));
+ assertTrue(listener1.getComponents().contains(t1));
+ assertTrue(listener1.getComponents().contains(t2));
+ assertTrue(listener2.getComponents().contains(page));
+ assertTrue(listener2.getComponents().contains(t1));
+ assertTrue(listener2.getComponents().contains(t2));
+ }
+
+ public void testInitializationOrder()
+ {
+ TestInitListener listener1 = new TestInitListener();
+ tester.getApplication().addComponentInitializationListener(listener1);
+
+ WebPage page = new WebPage()
+ {
+ };
+ TestComponent t1 = new TestComponent("t1");
+ TestComponent t2 = new TestComponent("t2");
+ TestComponent t3 = new TestComponent("t3");
+ TestComponent t4 = new TestComponent("t4");
+
+ t1.add(t2);
+ page.add(t1);
+ t1.add(t3);
+ t3.add(t4);
+
+ assertTrue(page == listener1.getComponents().get(0));
+ assertTrue(t1 == listener1.getComponents().get(1));
+ assertTrue(t2 == listener1.getComponents().get(2));
+ assertTrue(t3 == listener1.getComponents().get(3));
+ assertTrue(t4 == listener1.getComponents().get(4));
+ }
+
+
public static class TestPage extends WebPage implements IMarkupResourceStreamProvider
{
private int count = 0;
@@ -205,4 +259,22 @@ public class ComponentInitializationTest
// missing super call
}
}
+
+ private static class TestInitListener implements IComponentInitializationListener
+ {
+ private List<Component> components = new ArrayList<Component>();
+
+ public void onInitialize(Component component)
+ {
+ System.out.println(component);
+ components.add(component);
+ }
+
+ public List<Component> getComponents()
+ {
+ return components;
+ }
+
+
+ }
}