net.rim.pushsdk.commons
Class Application

java.lang.Object
  extended by net.rim.pushsdk.commons.Application
All Implemented Interfaces:
org.springframework.context.ApplicationContextAware

public class Application
extends Object
implements org.springframework.context.ApplicationContextAware

Class that maintains a static reference to the ApplicationContext.

The ApplicationContext is used to get references to the beans managed by Spring for this SDK. It is recommend you use one of the *BeanLocator classes instead of using Application directly.

This class implements the org.springframework.context.ApplicationContextAware interface; therefore, if an ApplicationContext is available, it will be automatically injected into this class and used to serve bean lookups. If no ApplicationContext is set, then this class will by default attempt to load an ApplicationContext from the context XML files in the classpath using this pattern:

 classpath*:pushsdk-*-context.xml
 

In case of multiple context config locations, later bean definitions will override ones defined in earlier loaded files. This can be leveraged to deliberately override certain bean definitions via an extra XML file that appears later in the classpath.

Important design note: It is mandatory that an application have one, and only one, ApplicationContext set to prevent memory leaks. If your application using the SDK is a Spring enabled application, use your own method of getting the SDK beans from your own ApplicationContext (i.e. inject the SDK beans into your own application's beans using dependency injection provided by Spring) rather than using this class; similarly do not use the *BeanLocators either for the same reason.

This class is useful for standalone applications that themselves do not use the Spring framework. This class will statically instantiate an application context in order to provide an entry point to any of the beans used by the SDK, taking the context definition files from the class path.

If your application is running in a servlet container, it is highly recommend you initialize your ApplicationContext using org.springframework.web.context.ContextLoaderListener via your web.xml. To enable this add the following to your web.xml:

 <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
and

 <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value> 
         classpath:/pushsdk-push-app-context.xml 
         classpath:/pushsdk-commons-context.xml 
         classpath:/pushsdk-push-context.xml 
         ...etc...
       </param-value>
  </context-param>
 

Example usage:

 PushService pushService = (PushService) Application.getContext().getBean("pushService");
 

Author:
mhogan

Field Summary
static String DEFAULT_CONFIG_LOCATIONS
          Look up path for context XML files if not ApplicationContext was injected into this class: classpath*:pushsdk-*-context.xml.
 
Constructor Summary
Application()
           
 
Method Summary
static org.springframework.context.ApplicationContext getContext()
          Returns the application context.
static org.springframework.context.ApplicationContext getContext(boolean createFromClasspathIfNotExist)
          Returns the ApplicationContext.
 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
          ApplicationContextAware interface implementation; e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONFIG_LOCATIONS

public static String DEFAULT_CONFIG_LOCATIONS
Look up path for context XML files if not ApplicationContext was injected into this class: classpath*:pushsdk-*-context.xml.

Constructor Detail

Application

public Application()
Method Detail

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
ApplicationContextAware interface implementation; e.g. called by the servlet container to set the context if org.springframework.web.context.ContextLoaderListener is used.

Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

getContext

public static org.springframework.context.ApplicationContext getContext()
Returns the application context. See getContext(boolean) javadoc for details. This method will delegate to that method passing 'true' as a parameter.

Returns:
the application context. May be null.

getContext

public static org.springframework.context.ApplicationContext getContext(boolean createFromClasspathIfNotExist)
Returns the ApplicationContext. The ApplicationContext is typically injected by Spring since this class implements ApplicationContextAware (e.g. running in a J2EE web server you can use org.springframework.web.context.ContextLoaderListener via your web.xml). If the ApplicationContext is not injected, then this method may attempt to construct an ApplicationContext by loading the context XML file(s) from the classpath using the pattern as defined by DEFAULT_CONFIG_LOCATIONS depending on the value of the createFromClasspathIfNotExist parameter.

Parameters:
createFromClasspathIfNotExist - true if this class should create the ApplicationContext if it has not been injected by Spring by trying to load the context XML files by looking in the classpath; false if this class should not attempt to create an ApplicationContext if one was not automatically injected by Spring from implementing the ApplicationContextAware interface
Returns:
the application context. May be null.


Copyright © 2011 Research In Motion. All Rights Reserved.