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 2009/10/27 07:45:19 UTC

svn commit: r830070 - in /wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection: Injector.java web/InjectorHolder.java

Author: ivaynberg
Date: Tue Oct 27 06:45:11 2009
New Revision: 830070

URL: http://svn.apache.org/viewvc?rev=830070&view=rev
Log:
WICKET-625, WICKET-2080

Modified:
    wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/Injector.java
    wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/web/InjectorHolder.java

Modified: wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/Injector.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/Injector.java?rev=830070&r1=830069&r2=830070&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/Injector.java (original)
+++ wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/Injector.java Tue Oct 27 06:45:11 2009
@@ -18,7 +18,10 @@
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.wicket.Component;
@@ -39,10 +42,7 @@
 {
 	private static Injector instance = new Injector();
 
-	// FIXME: WICKET-625 - Wicket doesn't clean up properly when hot-deploying;
-	// hangs onto Class references.
-	// We need some way to clean out this hashmap when we're done.
-	private final ConcurrentHashMap<Class< ? >, Field[]> classToFields = new ConcurrentHashMap<Class< ? >, Field[]>();
+	private final Map<ClassLoader, ConcurrentHashMap<String, Field[]>> cache = Collections.synchronizedMap(new WeakHashMap<ClassLoader, ConcurrentHashMap<String, Field[]>>());
 
 	/**
 	 * @return static instance of ProxyInjector
@@ -63,7 +63,7 @@
 	 *            class to be tested for being a boundary class
 	 * @return true if the class is a boundary class, false otherwise
 	 */
-	protected boolean isBoundaryClass(Class< ? > clazz)
+	protected boolean isBoundaryClass(Class<?> clazz)
 	{
 		if (clazz.equals(WebPage.class) || clazz.equals(Page.class) || clazz.equals(Panel.class) ||
 			clazz.equals(MarkupContainer.class) || clazz.equals(Component.class))
@@ -83,12 +83,25 @@
 	 */
 	public Object inject(Object object, IFieldValueFactory factory)
 	{
-		Class< ? > clazz = object.getClass();
-		Field[] fields = classToFields.get(clazz);
+		final Class<?> clazz = object.getClass();
+
+		Field[] fields = null;
+
+		// try cache
+		ConcurrentHashMap<String, Field[]> container = cache.get(clazz.getClassLoader());
+		if (container != null)
+		{
+			fields = container.get(clazz.getName());
+		}
+
 		if (fields == null)
 		{
 			fields = findFields(clazz, factory);
-			classToFields.put(clazz, fields);
+
+			// write to cache
+			container = new ConcurrentHashMap<String, Field[]>();
+			container.put(clazz.getName(), fields);
+			cache.put(clazz.getClassLoader(), container);
 		}
 
 		for (int i = 0; i < fields.length; i++)
@@ -135,7 +148,7 @@
 	 * @param factory
 	 * @return an array of fields that can be injected using the given field value factory
 	 */
-	private Field[] findFields(Class< ? > clazz, IFieldValueFactory factory)
+	private Field[] findFields(Class<?> clazz, IFieldValueFactory factory)
 	{
 		List<Field> matched = new ArrayList<Field>();
 

Modified: wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/web/InjectorHolder.java
URL: http://svn.apache.org/viewvc/wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/web/InjectorHolder.java?rev=830070&r1=830069&r2=830070&view=diff
==============================================================================
--- wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/web/InjectorHolder.java (original)
+++ wicket/branches/wicket-1.4.x/wicket-ioc/src/main/java/org/apache/wicket/injection/web/InjectorHolder.java Tue Oct 27 06:45:11 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.wicket.injection.web;
 
+import org.apache.wicket.Application;
+import org.apache.wicket.MetaDataKey;
 import org.apache.wicket.injection.ConfigurableInjector;
 
 /**
@@ -39,16 +41,19 @@
  * InjectorHolder.setInjector(new MockSpringInjector());
  * 
  * //from this point on InjectableWebPage and InjectablePanel
- * //will be injected using the MockSpringInjector 
+ * //will be injected using the MockSpringInjector
  * </pre>
  * 
  * @author Igor Vaynberg (ivaynberg)
  * 
- * TODO shouldn't we move this class to org.apache.wicket.injection ?
+ *         TODO shouldn't we move this class to org.apache.wicket.injection ?
  */
 public class InjectorHolder
 {
-	private static ConfigurableInjector injector = null;
+	private static final MetaDataKey<ConfigurableInjector> INJECTOR_KEY = new MetaDataKey<ConfigurableInjector>()
+	{
+		private static final long serialVersionUID = 1L;
+	};
 
 	/**
 	 * Gets an injector
@@ -59,12 +64,13 @@
 	 */
 	public static ConfigurableInjector getInjector()
 	{
+		ConfigurableInjector injector = Application.get().getMetaData(INJECTOR_KEY);
 		if (injector == null)
 		{
 			throw new IllegalStateException("InjectorHolder has not been assigned an injector. "
-					+ "Use InjectorHolder.setInjector() to assign an injector. "
-					+ "In most cases this should be done once inside "
-					+ "SpringWebApplication subclass's init() method.");
+				+ "Use InjectorHolder.setInjector() to assign an injector. "
+				+ "In most cases this should be done once inside "
+				+ "SpringWebApplication subclass's init() method.");
 		}
 		return injector;
 	}
@@ -79,7 +85,8 @@
 	 */
 	public static void setInjector(ConfigurableInjector newInjector)
 	{
-		injector = newInjector;
+		Application application = Application.get();
+		application.setMetaData(INJECTOR_KEY, newInjector);
 	}
 
 }