Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * 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;
  
  import java.net.URL;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import org.slf4j.Logger;
Base class for all Wicket applications. To create a Wicket application, you generally should not directly subclass this class. Instead, you will want to subclass some subclass of Application, like WebApplication, which is appropriate for the protocol and markup type you are working with.

Application has the following interesting features / attributes:

  • Name - The Application's name, which is the same as its class name.
  • Home Page - The Application's home Page class. Subclasses must override getHomePage() to provide this property value.
  • Settings - Application settings are partitioned into sets of related settings using interfaces in the org.apache.wicket.settings package. These interfaces are returned by the following methods, which should be used to configure framework settings for your application: getApplicationSettings(), getDebugSettings(), getExceptionSettings(), getMarkupSettings(), getPageSettings(), getRequestCycleSettings(), getSecuritySettings and getSessionSettings(). These settings are configured by default through the constructor or internalInit methods. Default the application is configured for DEVELOPMENT. You can configure this globally to DEPLOYMENT or override specific settings by implementing the init() method.
  • Shared Resources - Resources added to an Application's SharedResources have application-wide scope and can be referenced using a logical scope and a name with the ResourceReference class. ResourceReferences can then be used by multiple components in the same application without additional overhead (beyond the ResourceReference instance held by each referee) and will yield a stable URL, permitting efficient browser caching of the resource (even if the resource is dynamically generated). Resources shared in this manner may also be localized. See ResourceReference for more details.
  • Custom Session Subclasses- In order to install your own Session subclass you must override ApplicationnewSession(org.apache.wicket.Request,org.apache.wicket.Response). For subclasses of org.apache.wicket.protocol.http.WebApplication you will want to subclass org.apache.wicket.protocol.http.WebSession.

Author(s):
Jonathan Locke
See also:
org.apache.wicket.protocol.http.WebApplication
 
 public abstract class Application
 {
Configuration constant for the 2 types
 
 	public static final String CONFIGURATION = "configuration";

Configuration type constant for getting the context path out of the web.xml
 
 	public static final String CONTEXTPATH = "contextpath";

Configuration type constant for deployment
 
 	public static final String DEPLOYMENT = "deployment";

Configuration type constant for development
 
 	public static final String DEVELOPMENT = "development";

Applications keyed on the getApplicationKey() so that they can be retrieved even without being in a request/ being set in the thread local (we need that e.g. for when we are in a destruction thread).
 
 	private static final Map applicationKeyToApplication = new HashMap(1);

Thread local holder of the application object.
 
 	private static final ThreadLocal current = new ThreadLocal();

Log.
 
 	private static final Logger log = LoggerFactory.getLogger(Application.class);

Checks if the Application threadlocal is set in this thread

Returns:
true if get() can return the instance of application, false otherwise
 
 	public static boolean exists()
 	{
 		return .get() != null;
 	}

Get Application for current thread.

Returns:
The current thread's Application
 
 	public static Application get()
 	{
 		final Application application = (Application).get();
 		if (application == null)
 		{
 			throw new WicketRuntimeException("There is no application attached to current thread " +
 				Thread.currentThread().getName());
 		}
 		return application;
 	}

Gets the Application based on the application key of that application. You typically never have to use this method unless you are working on an integration project.

Parameters:
applicationKey The unique key of the application within a certain context (e.g. a web application)
Returns:
The application
Throws:
java.lang.IllegalArgumentException When no application was found with the provided key
 
 	public static Application get(String applicationKey)
 	{
 		Application application = (Application).get(applicationKey);
 		return application;
 	}

Gets the keys of the currently registered Wicket applications for this web application. You typically never have to use this method unless you are working on an integration project.

Returns:
unmodifiable set with keys that correspond with getApplicationKey(). Never null, but possibly empty
 
 	public static Set/* <String> */getApplicationKeys()
 	{
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.

Parameters:
application The current application or null for this thread
 
 	public static void set(final Application application)
 	{
 		if (application == null)
 		{
 			throw new IllegalArgumentException("Argument application can not be null");
 		}
 		.set(application);
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.
 
 	public static void unset()
 	{
 		.set(null);
 	}

 
The converter locator instance.
 
list of initializers.
 
 	private final List initializers = new ArrayList();

Application level meta data.
 
 	private MetaDataEntry[] metaData;

Name of application subclass.
 
 	private final String name;

Request logger instance.
 
The session facade.
 
Settings for this application.
 
 	private Settings settings;

can the settings object be set/used.
 
 	private boolean settingsAccessible;

Shared resources for this application
 
 	private final SharedResources sharedResources;

Constructor. Use init() for any configuration of your application instead of overriding the constructor.
 
 	public Application()
 	{
 		// Create name from subclass
 		 = Classes.simpleName(getClass());
 
 		// Create shared resources repository
 
 		// Install default component instantiation listener that uses
 		// authorization strategy to check component instantiations.
 		{
 
 			public void onInstantiation(final Component component)
 			{
 				// If component instantiation is not authorized
 					component.getClass()))
 				{
 					// then call any unauthorized component instantiation
 					// listener
 				}
 			}
 		});
 	}

Adds a component instantiation listener. This method should typically only be called during application startup; it is not thread safe.

Note: wicket does not guarantee the execution order of added listeners

Parameters:
listener the listener to add
 
 	public final void addComponentInstantiationListener(
 	{
 		if (listener == null)
 		{
 			throw new IllegalArgumentException("argument listener may not be null");
 		}
 
 		// if an instance of this listener is already present ignore this call
 		for (int i = 0; i < .i++)
 		{
 			if (listener == [i])
 			{
 				return;
 			}
 		}
 
 		System.arraycopy(, 0, newListeners, 0,
 		newListeners[.] = listener;
 	}

Configures application settings to good defaults.
 
 	public final void configure()
 	{
 		final String configurationType = getConfigurationType();
 
 		// As long as this is public api the development and deployment mode
 		// should counter act each other for all properties.
 		if (.equalsIgnoreCase(configurationType))
 		{
 		}
 		else if (.equalsIgnoreCase(configurationType))
 		{
 		}
 		else
 		{
 			throw new IllegalArgumentException("Invalid configuration type: '" + configurationType +
 				"'.  Must be \"development\" or \"deployment\".");
 		}
 	}

Gets the unique key of this application within a given context (like a web application). NOT INTENDED FOR FRAMEWORK CLIENTS.

Returns:
The unique key of this application
 
 	public abstract String getApplicationKey();

Returns:
Application's application-wide settings
Since:
1.2
See also:
org.apache.wicket.settings.IApplicationSettings
 
 	{
 		return getSettings();
 	}

Gets the configuration mode to use for configuring the app, either DEVELOPMENT or DEPLOYMENT.

The configuration type. Must currently be either DEVELOPMENT or DEPLOYMENT. Currently, if the configuration type is DEVELOPMENT, resources are polled for changes, component usage is checked, wicket tags are not stripped from output and a detailed exception page is used. If the type is DEPLOYMENT, component usage is not checked, wicket tags are stripped from output and a non-detailed exception page is used to display errors.

Note that you should not run Wicket in DEVELOPMENT mode on production servers - the various debugging checks and resource polling is inefficient and may leak resources, particularly on webapp redeploy.

Returns:
configuration
Since:
1.2.3 (function existed as a property getter)
1.3.0 (abstract, used to configure things)
 
 	public abstract String getConfigurationType();

Returns:
The converter locator for this application
 
 	{
 	}

Returns:
Application's debug related settings
Since:
1.2
See also:
org.apache.wicket.settings.IDebugSettings
 
 	{
 		return getSettings();
 	}

Returns:
Application's exception handling settings
Since:
1.2
See also:
org.apache.wicket.settings.IExceptionSettings
 
 	{
 		return getSettings();
 	}

Returns:
Wicket framework settings
Since:
1.2
See also:
org.apache.wicket.settings.IFrameworkSettings
 
 	{
 		return getSettings();
 	}

Application subclasses must specify a home page class by implementing this abstract method.

Returns:
Home page class for this application
 
 	public abstract Class getHomePage();

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.

Deprecated:
please use org.apache.wicket.settings.IMarkupSettings.getMarkupCache() instead
Returns:
The markup cache associated with the application
 
 	public final IMarkupCache getMarkupCache()
 	{
 	}

Returns:
Application's markup related settings
Since:
1.2
See also:
org.apache.wicket.settings.IMarkupSettings
 
 	{
 		return getSettings();
 	}

Gets metadata for this application using the given key.

Parameters:
key The key for the data
Returns:
The metadata
See also:
MetaDataKey
 
 	public final Serializable getMetaData(final MetaDataKey key)
 	{
 		return (Serializable)key.get();
 	}

Gets the name of this application.

Returns:
The application name.
 
 	public final String getName()
 	{
 		return ;
 	}

Returns:
Application's page related settings
Since:
1.2
See also:
org.apache.wicket.settings.IPageSettings
 
 	{
 		return getSettings();
 	}

Returns:
Application's request cycle related settings
Since:
1.2
See also:
org.apache.wicket.settings.IDebugSettings
 
 	{
 		return getSettings();
 	}

Gets the org.apache.wicket.protocol.http.RequestLogger.

Returns:
The RequestLogger
 
 	public final IRequestLogger getRequestLogger()
 	{
 		{
 			if ( == null)
 			{
 			}
 		}
 		else
 		{
 			 = null;
 		}
 		return ;
 	}

Returns:
Application's resources related settings
Since:
1.3
See also:
org.apache.wicket.settings.IResourceSettings
 
 	{
 		return getSettings();
 	}

Returns:
Application's resources related settings
Since:
1.2
See also:
org.apache.wicket.settings.IResourceSettings
 
 	{
 		return getSettings();
 	}

Returns:
Application's security related settings
Since:
1.2
See also:
org.apache.wicket.settings.ISecuritySettings
 
 	{
 		return getSettings();
 	}

Returns:
Application's session related settings
Since:
1.2
See also:
org.apache.wicket.settings.ISessionSettings
 
 	{
 		return getSettings();
 	}

Gets the facade object for working getting/ storing session instances.

Returns:
The session facade
 
 	public final ISessionStore getSessionStore()
 	{
 		return ;
 	}

Gets the shared resources.

Returns:
The SharedResources for this application.
 
 	{
 		return ;
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL. Initializes wicket components.
 
 	public final void initializeComponents()
 	{
 		// Load any wicket properties files we can find
 		try
 		{
 			// Load properties files used by all libraries
 			final Enumeration resources = Thread.currentThread()
 				.getResources("wicket.properties");
 			while (resources.hasMoreElements())
 			{
 				InputStream in = null;
 				try
 				{
 					final URL url = (URL)resources.nextElement();
 					final Properties properties = new Properties();
 					in = url.openStream();
 					properties.load(in);
 					load(properties);
 				}
 				finally
 				{
 					if (in != null)
 					{
 						in.close();
 					}
 				}
 			}
 		}
 		catch (IOException e)
 		{
 			throw new WicketRuntimeException("Unable to load initializers file"e);
 		}
 
 		// now call any initializers we read
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL.

Parameters:
target
 
 	public void logEventTarget(IRequestTarget target)
 	{
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL.

Parameters:
requestTarget
 
 	public void logResponseTarget(IRequestTarget requestTarget)
 	{
 	}

Creates a new RequestCycle object. Override this method if you want to provide a custom request cycle.

Parameters:
request The request
response The response
Returns:
The request cycle
Since:
1.3
 
 	public abstract RequestCycle newRequestCycle(final Request requestfinal Response response);

FOR DEPRECATION ONLY.

Deprecated:
Applications wishing to provide custom request cycles should override method newRequestCycle(org.apache.wicket.Request,org.apache.wicket.Response)
Parameters:
application
request
response
Returns:
nothing
Throws:
java.lang.UnsupportedOperationException always
 
 	public final RequestCycle newRequestCycle(Application applicationRequest request,
 		Response response)
 	{
 	}

Creates a new session. Override this method if you want to provide a custom session.

Parameters:
request The request that will create this session.
response The response to initialize, for example with cookies. This is important to use cases involving unit testing because those use cases might want to be able to sign a user in automatically when the session is created.
Returns:
The session
Since:
1.3
 
 	public abstract Session newSession(Request requestResponse response);

Removes a component instantiation listener. This method should typicaly only be called during application startup; it is not thread safe.

Parameters:
listener the listener to remove
 
 	{
 		final int len = listeners.length;
 
 		if (listener != null && len > 0)
 		{
 			int pos = 0;
 
 			for (pos = 0; pos < lenpos++)
 			{
 				if (listener == listeners[pos])
 				{
 					break;
 				}
 			}
 
 			if (pos < len)
 			{
 				listeners[pos] = listeners[len - 1];
 				final IComponentInstantiationListener[] newListeners = new IComponentInstantiationListener[len - 1];
 				System.arraycopy(listeners, 0, newListeners, 0, newListeners.length);
 
 			}
 		}
 	}

Sets the metadata for this application using the given key. If the metadata object is not of the correct type for the metadata key, an IllegalArgumentException will be thrown. For information on creating MetaDataKeys, see MetaDataKey.

Parameters:
key The singleton key for the metadata
object The metadata object
Throws:
java.lang.IllegalArgumentException
See also:
MetaDataKey
 
 	// TODO: Replace the Serializable type with Object for next wicket version
 	public final synchronized void setMetaData(final MetaDataKey keyfinal Serializable object)
 	{
 		 = key.set(object);
 	}

Construct and add initializer from the provided class name.

Parameters:
className
 
 	private final void addInitializer(String className)
 	{
 		IInitializer initializer = (IInitializer)Objects.newInstance(className);
 		if (initializer != null)
 		{
 			.add(initializer);
 		}
 	}

Iterate initializers list, calling any IDestroyer instances found in it.
 
 	private final void callDestroyers()
 	{
 		for (Iterator i = .iterator(); i.hasNext();)
 		{
 			IInitializer initializer = (IInitializer)i.next();
 			if (initializer instanceof IDestroyer)
 			{
 				.info("[" + getName() + "] destroy: " + initializer);
 				((IDestroyer)initializer).destroy(this);
 			}
 		}
 	}

Iterate initializers list, calling any instances found in it.
 
 	private final void callInitializers()
 	{
 		for (Iterator i = .iterator(); i.hasNext();)
 		{
 			IInitializer initializer = (IInitializer)i.next();
 			.info("[" + getName() + "] init: " + initializer);
 			initializer.init(this);
 		}
 	}

This method is still here for backwards compatibility with 1.1 source code. The getXXXSettings() methods are now preferred. This method will be removed post 1.2 version.

 
 	private Settings getSettings()
 	{
 		{
 				"Use Application.init() method for configuring your application object");
 		}
 
 		if ( == null)
 		{
 			 = new Settings(this);
 		}
 		return ;
 	}

Parameters:
properties Properties map with names of any library initializers in it
 
 	private final void load(final Properties properties)
 	{
 		addInitializer(properties.getProperty("initializer"));
 		addInitializer(properties.getProperty(getName() + "-initializer"));
 	}

Called when wicket servlet is destroyed. Overrides do not have to call super.

Deprecated:
use onDestroy() instead
 
 	// TODO remove after deprecation release
 	protected final void destroy()
 	{
 	}

Called when wicket servlet is destroyed. Overrides do not have to call super.
 
 	protected void onDestroy()
 	{
 	}

Deprecated:
replaced by newRequestCycle(org.apache.wicket.Request,org.apache.wicket.Response)
Returns:
Request cycle factory for this kind of session.
 
 	// TODO remove after deprecation release
 	protected final Object getRequestCycleFactory()
 	{
 	}

Gets the factory for creating session instances.

Deprecated:
replaced by newSession(org.apache.wicket.Request,org.apache.wicket.Response)
Returns:
Factory for creating session instances
 
 	// TODO remove after deprecation release
 	protected final Object getSessionFactory()
 	{
 	}

Allows for initialization of the application by a subclass. Use this method for any application setup instead of the constructor.
 
 	protected void init()
 	{
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL IT.
 
 	protected void internalDestroy()
 	{
 		// Clear caches of Class keys so the classloader can be garbage
 		// collected (WICKET-625)
 		PropertyResolver.destroy(this);
 
 		Session.unset();
 		RequestContext.unset();
 		RequestCycle.set(null);
 	}

THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT OVERRIDE OR CALL. Internal initialization.
 
 	protected void internalInit()
 	{
 		IPageSettings pageSettings = getPageSettings();
 
 		// Install default component resolvers
 		pageSettings.addComponentResolver(new ParentResolver());
 		pageSettings.addComponentResolver(new FragmentResolver());
 
 		// Install button image resource factory
 
 		String applicationKey = getApplicationKey();
 		.put(applicationKeythis);
 
 	}

Creates and returns a new instance of IConverterLocator.

Returns:
A new IConverterLocator instance
 
 	{
 		return new ConverterLocator();
 	}

creates a new request logger when requests logging is enabled.

Returns:
The new request logger
 
 	{
 		return new RequestLogger();
 	}

Creates a new session facade. Is called once per application, and is typically not something clients reimplement.

Returns:
The session facade
 
 	protected abstract ISessionStore newSessionStore();

Notifies the registered component instantiation listeners of the construction of the provided component

Parameters:
component the component that is being instantiated
 
 	final void notifyComponentInstantiationListeners(final Component component)
 	{
 		for (int i = 0; i < leni++)
 		{
 		}
 	}
 
Adds an org.apache.wicket.application.IComponentOnBeforeRenderListener. This method should typically only be called during application startup; it is not thread safe.

Parameters:
listener
 
 	{
 		{
 		}
 
 		if (.contains(listener) == false)
 		{
 		}
 	}

	}

				listener.onBeforeRender(component);
Adds an org.apache.wicket.application.IComponentOnAfterRenderListener. This method should typically only be called during application startup; it is not thread safe.

Parameters:
listener
		if (.contains(listener) == false)
	}

	}

				listener.onAfterRender(component);
	private List renderHeadListeners = null;

Adds a listener that will be invoked for every header response

Parameters:
listener
	public final void addRenderHeadListener(IHeaderContributor listener)
		if ( == null)
	}

Parameters:
listener
		if ( != null)
	}

INTERNAL

Parameters:
response
		if ( != null)
				listener.renderHead(response);
New to GrepCode? Check out our FAQ X