You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2006/10/16 21:47:10 UTC

svn commit: r464641 - in /incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket: Application.java IDestroyer.java IInitializer.java Initializer.java protocol/http/WebApplication.java protocol/http/WicketServlet.java util/lang/Objects.java

Author: ehillenius
Date: Mon Oct 16 12:47:08 2006
New Revision: 464641

URL: http://svn.apache.org/viewvc?view=rev&rev=464641
Log:
Implemented optional interface for initializers: IDestroyer that may be implemented by initializers to clear up stuff when the application is shut down.
Implemented destroy for JMX so that it deregisters the registrations it is responsible for
Tweaked logging, and introduced logStarted that may be used to print a customized 'application started' message

Added:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IDestroyer.java
Modified:
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Application.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IInitializer.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Initializer.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebApplication.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketServlet.java
    incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Application.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Application.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Application.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Application.java Mon Oct 16 12:47:08 2006
@@ -22,8 +22,11 @@
 import java.io.InputStream;
 import java.io.Serializable;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -55,7 +58,7 @@
 import wicket.settings.Settings;
 import wicket.util.file.IResourceFinder;
 import wicket.util.lang.Classes;
-import wicket.util.string.Strings;
+import wicket.util.lang.Objects;
 import wicket.util.time.Duration;
 
 /**
@@ -217,6 +220,9 @@
 	/** Record what the configuration is, so that we can query for it later. */
 	private String configurationType;
 
+	/** list of initializers. */
+	private List initializers = new ArrayList();
+
 	/** Markup cache for this application */
 	private final MarkupCache markupCache;
 
@@ -349,7 +355,6 @@
 		// should counter act each other for all properties.
 		if (DEVELOPMENT.equalsIgnoreCase(configurationType))
 		{
-			log.info("You are in DEVELOPMENT mode");
 			getResourceSettings().setResourcePollFrequency(Duration.ONE_SECOND);
 			getDebugSettings().setComponentUseCheck(true);
 			getDebugSettings().setSerializeSessionAttributes(true);
@@ -626,10 +631,10 @@
 	 */
 	public final void initializeComponents()
 	{
-		// Load any wicket components we can find
+		// Load any wicket properties files we can find
 		try
 		{
-			// Load components used by all applications
+			// Load properties files used by all libraries
 			final Enumeration resources = getClass().getClassLoader().getResources(
 					"wicket.properties");
 			while (resources.hasMoreElements())
@@ -641,7 +646,7 @@
 					final Properties properties = new Properties();
 					in = url.openStream();
 					properties.load(in);
-					initializeComponents(properties);
+					load(properties);
 				}
 				finally
 				{
@@ -656,6 +661,9 @@
 		{
 			throw new WicketRuntimeException("Unable to load initializers file", e);
 		}
+
+		// now call any initializers we read
+		callInitializers();
 	}
 
 	/**
@@ -736,7 +744,7 @@
 	 */
 	protected void destroy()
 	{
-
+		callDestroyers();
 	}
 
 	/**
@@ -793,6 +801,22 @@
 	}
 
 	/**
+	 * Log that this application is started.
+	 */
+	protected void logStarted()
+	{
+		String version = getFrameworkSettings().getVersion();
+		StringBuffer b = new StringBuffer();
+		b.append("[").append(getName()).append("] Started Wicket ");
+		if (!"n/a".equals(version))
+		{
+			b.append("version ").append(version).append(" ");
+		}
+		b.append("in ").append(getConfigurationType()).append(" mode");
+		log.info(b.toString());
+	}
+
+	/**
 	 * Creates a new session facade. Is called once per application, and is
 	 * typically not something clients reimplement.
 	 * 
@@ -817,42 +841,57 @@
 	}
 
 	/**
-	 * Instantiate initializer with the given class name.
+	 * Construct and add initializer from the provided class name.
 	 * 
 	 * @param className
-	 *            The name of the initializer class
 	 */
-	private final void initialize(final String className)
+	private final void addInitializer(String className)
 	{
-		if (!Strings.isEmpty(className))
+		IInitializer initializer = (IInitializer)Objects.newInstance(className);
+		if (initializer != null)
 		{
-			try
-			{
-				Class c = getApplicationSettings().getClassResolver().resolveClass(className);
-				((IInitializer)c.newInstance()).init(this);
-			}
-			catch (ClassCastException e)
-			{
-				throw new WicketRuntimeException("Unable to initialize " + className, e);
-			}
-			catch (InstantiationException e)
-			{
-				throw new WicketRuntimeException("Unable to initialize " + className, e);
-			}
-			catch (IllegalAccessException e)
+			initializers.add(initializer);
+		}
+	}
+
+	/**
+	 * @param properties
+	 *            Properties map with names of any library destroyers in it
+	 */
+	private final void callDestroyers()
+	{
+		for (Iterator i = initializers.iterator(); i.hasNext();)
+		{
+			IInitializer initializer = (IInitializer)i.next();
+			if (initializer instanceof IDestroyer)
 			{
-				throw new WicketRuntimeException("Unable to initialize " + className, e);
+				log.info("[" + getName() + "] destroy: " + initializer);
+				((IDestroyer)initializer).destroy(this);
 			}
 		}
 	}
 
 	/**
 	 * @param properties
+	 *            Properties map with names of any library destroyers in it
+	 */
+	private final void callInitializers()
+	{
+		for (Iterator i = initializers.iterator(); i.hasNext();)
+		{
+			IInitializer initializer = (IInitializer)i.next();
+			log.info("[" + getName() + "] init: " + initializer);
+			initializer.init(this);
+		}
+	}
+
+	/**
+	 * @param properties
 	 *            Properties map with names of any library initializers in it
 	 */
-	private final void initializeComponents(final Properties properties)
+	private final void load(final Properties properties)
 	{
-		initialize(properties.getProperty("initializer"));
-		initialize(properties.getProperty(getName() + "-initializer"));
+		addInitializer(properties.getProperty("initializer"));
+		addInitializer(properties.getProperty(getName() + "-initializer"));
 	}
 }

Added: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IDestroyer.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IDestroyer.java?view=auto&rev=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IDestroyer.java (added)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IDestroyer.java Mon Oct 16 12:47:08 2006
@@ -0,0 +1,42 @@
+/*
+ * $Id: IInitializer.java 4597 2006-02-21 22:08:11 +0000 (Tue, 21 Feb 2006)
+ * jdonnerstag $ $Revision: 460927 $ $Date: 2006-02-21 22:08:11 +0000 (Tue, 21 Feb
+ * 2006) $
+ * 
+ * ==============================================================================
+ * Licensed 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 wicket;
+
+/**
+ * Optional interface for {@link IInitializer initializers} that can clean up
+ * stuff initializers created. Initializers simple have to implement this
+ * interface and do their thing in {@link #destroy(Application)}.
+ * <p>
+ * Destroyers can be used to cleanup code when the application unloads. It only
+ * guarantees a best effort of cleaning up. Typically, for web applications,
+ * this is called when the Wicket servlet/ filter is unloaded by the container,
+ * which may depend on the container's implementation and things like the time
+ * out period it uses and whether all threads of the web app were cleared.
+ * </p>
+ * 
+ * @author Eelco Hillenius
+ */
+public interface IDestroyer
+{
+	/**
+	 * @param application
+	 *            The application loading the component
+	 */
+	void destroy(Application application);
+}

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IInitializer.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IInitializer.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IInitializer.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/IInitializer.java Mon Oct 16 12:47:08 2006
@@ -33,6 +33,17 @@
  * You don't have to pre-register {@link PackageResource package resources}, as
  * they can be initialized lazily.
  * </p>
+ * <p>
+ * Initializers can be configured by having a wicket.properties file in the
+ * class path root, with property 'initializer=${initializer class name}'. You
+ * can have one such properties per jar file, but the initializer that property
+ * denotes can delegate to other initializers of that library.
+ * </p>
+ * <p>
+ * If an initializer also implement {@link IDestroyer}, the instance will be
+ * kept for destroying, so that it may clean up whatever it did when
+ * initializing.
+ * </p>
  * 
  * @author Jonathan Locke
  */

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Initializer.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Initializer.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Initializer.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Initializer.java Mon Oct 16 12:47:08 2006
@@ -46,4 +46,12 @@
 		IRedirectListener.INTERFACE.register();
 		IResourceListener.INTERFACE.register();
 	}
+	
+	/**
+	 * @see java.lang.Object#toString()
+	 */
+	public String toString()
+	{
+		return "Wicket core library initializer";
+	}
 }

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebApplication.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebApplication.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebApplication.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebApplication.java Mon Oct 16 12:47:08 2006
@@ -71,11 +71,11 @@
  * init() method. For example:
  * 
  * <pre>
- *           public void init()
- *           {
- *               String webXMLParameter = getWicketServlet().getInitParameter(&quot;myWebXMLParameter&quot;);
- *               URL schedulersConfig = getWicketServlet().getServletContext().getResource(&quot;/WEB-INF/schedulers.xml&quot;);
- *               ...
+ *             public void init()
+ *             {
+ *                 String webXMLParameter = getWicketServlet().getInitParameter(&quot;myWebXMLParameter&quot;);
+ *                 URL schedulersConfig = getWicketServlet().getServletContext().getResource(&quot;/WEB-INF/schedulers.xml&quot;);
+ *                 ...
  * </pre>
  * 
  * @see WicketServlet
@@ -537,6 +537,14 @@
 		{
 			configure(Application.DEVELOPMENT, wicketServlet.getInitParameter("sourceFolder"));
 		}
+	}
+
+	/**
+	 * @see wicket.Application#logStarted()
+	 */
+	protected void logStarted()
+	{
+		super.logStarted();
 	}
 
 	/**

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketServlet.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketServlet.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketServlet.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WicketServlet.java Mon Oct 16 12:47:08 2006
@@ -48,15 +48,15 @@
  * one application server to another, but should look something like this:
  * 
  * <pre>
- *                   &lt;servlet&gt;
- *                       &lt;servlet-name&gt;MyApplication&lt;/servlet-name&gt;
- *                       &lt;servlet-class&gt;wicket.protocol.http.WicketServlet&lt;/servlet-class&gt;
- *                       &lt;init-param&gt;
- *                           &lt;param-name&gt;applicationClassName&lt;/param-name&gt;
- *                           &lt;param-value&gt;com.whoever.MyApplication&lt;/param-value&gt;
- *                       &lt;/init-param&gt;
- *                       &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
- *                   &lt;/servlet&gt;
+ *                     &lt;servlet&gt;
+ *                         &lt;servlet-name&gt;MyApplication&lt;/servlet-name&gt;
+ *                         &lt;servlet-class&gt;wicket.protocol.http.WicketServlet&lt;/servlet-class&gt;
+ *                         &lt;init-param&gt;
+ *                             &lt;param-name&gt;applicationClassName&lt;/param-name&gt;
+ *                             &lt;param-value&gt;com.whoever.MyApplication&lt;/param-value&gt;
+ *                         &lt;/init-param&gt;
+ *                         &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
+ *                     &lt;/servlet&gt;
  * </pre>
  * 
  * Note that the applicationClassName parameter you specify must be the fully
@@ -68,10 +68,10 @@
  * looks like:
  * 
  * <pre>
- *                   &lt;init-param&gt;
- *                     &lt;param-name&gt;applicationFactoryClassName&lt;/param-name&gt;
- *                       &lt;param-value&gt;teachscape.platform.web.wicket.SpringApplicationFactory&lt;/param-value&gt;
- *                   &lt;/init-param&gt;
+ *                     &lt;init-param&gt;
+ *                       &lt;param-name&gt;applicationFactoryClassName&lt;/param-name&gt;
+ *                         &lt;param-value&gt;teachscape.platform.web.wicket.SpringApplicationFactory&lt;/param-value&gt;
+ *                     &lt;/init-param&gt;
  * </pre>
  * 
  * and it has to satisfy interface
@@ -88,11 +88,11 @@
  * init() method of {@link javax.servlet.GenericServlet}. For example:
  * 
  * <pre>
- *                   public void init() throws ServletException
- *                   {
- *                       ServletConfig config = getServletConfig();
- *                       String webXMLParameter = config.getInitParameter(&quot;myWebXMLParameter&quot;);
- *                       ...
+ *                     public void init() throws ServletException
+ *                     {
+ *                         ServletConfig config = getServletConfig();
+ *                         String webXMLParameter = config.getInitParameter(&quot;myWebXMLParameter&quot;);
+ *                         ...
  * </pre>
  * 
  * </p>
@@ -165,7 +165,7 @@
 				throw new WicketRuntimeException(ex.getMessage());
 			}
 		}
-		
+
 		// Create a new webrequest
 		final WebRequest request = webApplication.newWebRequest(servletRequest);
 
@@ -192,7 +192,7 @@
 
 		// First, set the webapplication for this thread
 		Application.set(webApplication);
-		
+
 		// Get session for request
 		final WebSession session = webApplication.getSession(request);
 
@@ -206,8 +206,10 @@
 		try
 		{
 			// Create a new request cycle
-			// FIXME post 1.2 Instead of doing this, we should get a request cycle factory
-			// from the application settings and use that. That way we are a step
+			// FIXME post 1.2 Instead of doing this, we should get a request
+			// cycle factory
+			// from the application settings and use that. That way we are a
+			// step
 			// closer to a session-less operation of Wicket.
 			RequestCycle cycle = session.newRequestCycle(request, response);
 
@@ -265,7 +267,7 @@
 	 */
 	public void init()
 	{
-		if (this.webApplication == null) 
+		if (this.webApplication == null)
 		{
 			IWebApplicationFactory factory = getApplicationFactory();
 
@@ -292,15 +294,18 @@
 			// Call internal init method of web application for default
 			// initialisation
 			this.webApplication.internalInit();
-			
+
 			// Call init method of web application
 			this.webApplication.init();
-			
+
 			// We initialize components here rather than in the constructor or
 			// in the internal init, because in the init method class aliases
 			// can be added, that would be used in installing resources in the
 			// component.
 			this.webApplication.initializeComponents();
+
+			// Give the application the option to log that it is started
+			this.webApplication.logStarted();
 		}
 		finally
 		{

Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java?view=diff&rev=464641&r1=464640&r2=464641
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/util/lang/Objects.java Mon Oct 16 12:47:08 2006
@@ -39,6 +39,7 @@
 import wicket.application.IClassResolver;
 import wicket.settings.IApplicationSettings;
 import wicket.util.io.ByteCountingOutputStream;
+import wicket.util.string.Strings;
 
 /**
  * Object utilities.
@@ -930,6 +931,40 @@
 			return ((Character)value).charValue();
 		}
 		return Long.parseLong(stringValue(value, true));
+	}
+
+	/**
+	 * Creates a new instance using the current application's class resolver.
+	 * Returns null if className is null.
+	 * 
+	 * @param className
+	 *            The full class name
+	 * @return The new object instance
+	 */
+	public static Object newInstance(final String className)
+	{
+		if (!Strings.isEmpty(className))
+		{
+			try
+			{
+				Class c = Application.get().getApplicationSettings().getClassResolver()
+						.resolveClass(className);
+				return c.newInstance();
+			}
+			catch (ClassCastException e)
+			{
+				throw new WicketRuntimeException("Unable to create " + className, e);
+			}
+			catch (InstantiationException e)
+			{
+				throw new WicketRuntimeException("Unable to create " + className, e);
+			}
+			catch (IllegalAccessException e)
+			{
+				throw new WicketRuntimeException("Unable to create " + className, e);
+			}
+		}
+		return null;
 	}
 
 	/**