org.atmosphere.cpr
Class AtmosphereFramework

java.lang.Object
  extended by org.atmosphere.cpr.AtmosphereFramework
All Implemented Interfaces:
ServletContextProvider

public class AtmosphereFramework
extends Object
implements ServletContextProvider

The AtmosphereFramework acts as a dispatcher for AtmosphereHandler defined in META-INF/atmosphere.xml, or if atmosphere.xml is missing, all classes that implements AtmosphereHandler will be discovered and mapped using the class's name.

You can force the framework to use native API of the Web Server instead of the Servlet 3.0 Async API you are deploying on by adding

  <init-param>
      <param-name>org.atmosphere.useNative</param-name>
      <param-value>true</param-value>
  </init-param>
 
You can force this framework to use one Thread per connection instead of native API of the Web Server you are deploying on by adding
  <init-param>
      <param-name>org.atmosphere.useBlocking</param-name>
      <param-value>true</param-value>
  </init-param>
 
You can also define Broadcasterby adding:
  <init-param>
      <param-name>org.atmosphere.cpr.broadcasterClass</param-name>
      <param-value>class-name</param-value>
  </init-param>
 
You can also for Atmosphere to use OutputStream for all write operations.
  <init-param>
      <param-name>org.atmosphere.useStream</param-name>
      <param-value>true</param-value>
  </init-param>
 
You can also configure BroadcasterCache that persist message when Browser is disconnected.
  <init-param>
      <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name>
      <param-value>class-name</param-value>
  </init-param>
 
You can also configure Atmosphere to use http session or not
  <init-param>
      <param-name>org.atmosphere.cpr.sessionSupport</param-name>
      <param-value>false</param-value>
  </init-param>
 
You can also configure BroadcastFilter that will be applied at all newly created Broadcaster
  <init-param>
      <param-name>org.atmosphere.cpr.broadcastFilterClasses</param-name>
      <param-value>BroadcastFilter class name separated by coma</param-value>
  </init-param>
 
All the property available are defined in ApplicationConfig The Atmosphere Framework can also be used as a Servlet Filter (AtmosphereFilter).

If you are planning to use JSP, Servlet or JSF, you can instead use the MeteorServlet, which allow the use of Meteor inside those components.

Author:
Jeanfrancois Arcand

Nested Class Summary
static class AtmosphereFramework.Action
          Simple class/struck that hold the current state.
static class AtmosphereFramework.AtmosphereHandlerWrapper
           
 
Field Summary
protected  AsyncSupport asyncSupport
           
protected  String atmosphereDotXmlPath
           
protected  Map<String,AtmosphereFramework.AtmosphereHandlerWrapper> atmosphereHandlers
           
protected  boolean autoDetectHandlers
           
protected static String broadcasterCacheClassName
           
protected  String broadcasterClassName
           
protected  BroadcasterFactory broadcasterFactory
           
protected  String broadcasterFactoryClassName
           
protected  List<String> broadcasterFilters
           
protected  String broadcasterLifeCyclePolicy
           
protected  ConcurrentLinkedQueue<String> broadcasterTypes
           
protected  AtmosphereConfig config
           
static String DEFAULT_ATMOSPHERE_CONFIG_PATH
           
protected  String handlersPath
           
protected  HashMap<String,String> initParams
           
protected  boolean isBroadcasterSpecified
           
protected  AtomicBoolean isCometSupportConfigured
           
protected  boolean isCometSupportSpecified
           
protected  boolean isFilter
           
protected  boolean isSessionSupportSpecified
           
protected static org.slf4j.Logger logger
           
protected  ArrayList<String> possibleComponentsCandidate
           
protected  javax.servlet.ServletConfig servletConfig
           
protected  boolean useBlockingImplementation
           
protected  boolean useNativeImplementation
           
protected  boolean useStreamForFlushingComments
           
protected  boolean webSocketEnabled
           
protected  WebSocketProtocol webSocketProtocol
           
protected  String webSocketProtocolClassName
           
 
Constructor Summary
AtmosphereFramework()
          Create an AtmosphereFramework.
AtmosphereFramework(boolean isFilter, boolean autoDetectHandlers)
          Create an AtmosphereFramework.
AtmosphereFramework(javax.servlet.ServletConfig sc)
          Create an AtmosphereFramework and initialize it via init(javax.servlet.ServletConfig)
 
Method Summary
 AtmosphereFramework addAtmosphereHandler(String mapping, AtmosphereHandler h)
          Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.
 AtmosphereFramework addAtmosphereHandler(String mapping, AtmosphereHandler h, Broadcaster broadcaster)
          Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.
 AtmosphereFramework addAtmosphereHandler(String mapping, AtmosphereHandler h, String broadcasterId)
          Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.
 AtmosphereFramework addBroadcasterType(String broadcasterTypeString)
          Add a new Broadcaster class name AtmosphereServlet can use when initializing requests, and when atmosphere.xml broadcaster element is unspecified.
 AtmosphereFramework addInitParameter(String name, String value)
          Add init-param like if they were defined in web.xml
 void autoDetectAtmosphereHandlers(javax.servlet.ServletContext servletContext, URLClassLoader classloader)
          Auto detect instance of AtmosphereHandler in case META-INF/atmosphere.xml is missing.
protected  void autoDetectContainer()
          Auto detect the underlying Servlet Container we are running on.
protected  void autoDetectWebSocketHandler(javax.servlet.ServletContext servletContext, URLClassLoader classloader)
          Auto detect instance of WebSocketHandler in case META-INF/atmosphere.xml is missing.
 List<String> broadcasterFilters()
           
protected  void configureBroadcaster(javax.servlet.ServletContext sc)
           
protected  Map<String,String> configureQueryStringAsRequest(AtmosphereRequest request)
           
protected  void configureWebDotXmlAtmosphereHandler(javax.servlet.ServletConfig sc)
           
protected  AsyncSupportResolver createAsyncSupportResolver()
          Returns an instance of AsyncSupportResolver AsyncSupportResolver
 AtmosphereFramework destroy()
           
protected  boolean detectSupportedFramework(javax.servlet.ServletConfig sc)
          Auto-detect Jersey when no atmosphere.xml file are specified.
 AtmosphereFramework.Action doCometSupport(AtmosphereRequest req, AtmosphereResponse res)
          Invoke the proprietary AsyncSupport
protected  void doInitParams(javax.servlet.ServletConfig sc)
          Read init param from web.xml and apply them.
protected  void doInitParamsForWebSocket(javax.servlet.ServletConfig sc)
           
 AsyncSupport getAsyncSupport()
          Return the current AsyncSupport
 AtmosphereConfig getAtmosphereConfig()
          Return a configured instance of AtmosphereConfig
 String getAtmosphereDotXmlPath()
           
 Map<String,AtmosphereFramework.AtmosphereHandlerWrapper> getAtmosphereHandlers()
           
 String getBroadcasterCacheClassName()
          Return the BroadcasterCache class name.
 BroadcasterFactory getBroadcasterFactory()
          Get the BroadcasterFactory which is used by Atmosphere to construct Broadcaster
 AsyncSupport getCometSupport()
          Return the current AsyncSupport
 String getDefaultBroadcasterClassName()
          Return the default Broadcaster class name.
 String getHandlersPath()
           
 javax.servlet.ServletConfig getServletConfig()
           
 javax.servlet.ServletContext getServletContext()
           
 WebSocketProtocol getWebSocketProtocol()
           
 String getWebSocketProtocolClassName()
           
 AtmosphereFramework init(javax.servlet.ServletConfig sc)
          Load the AtmosphereHandler associated with this AtmosphereServlet.
protected  void initWebSocketProtocol()
           
protected  boolean isIECandidate(AtmosphereRequest request)
           
 boolean isUseBlockingImplementation()
           
 boolean isUseNativeImplementation()
           
 boolean isUseStreamForFlushingComments()
          true if Atmosphere uses AtmosphereResponse.getOutputStream() by default for write operation.
protected  void loadAtmosphereDotXml(InputStream stream, URLClassLoader c)
          Load AtmosphereHandler defined under META-INF/atmosphere.xml
 void loadAtmosphereHandlersFromPath(URLClassLoader classloader, String realPath)
           
 void loadConfiguration(javax.servlet.ServletConfig sc)
           
protected  void loadWebSocketFromPath(URLClassLoader classloader, String realPath)
           
protected  String lookupDefaultBroadcasterType()
           
protected  void readSystemProperties()
           
 AtmosphereFramework removeAllAtmosphereHandler()
          Remove all AtmosphereHandler
 AtmosphereFramework removeAllInitParams()
          Remove all init parameters.
 AtmosphereFramework removeAtmosphereHandler(String mapping)
          Remove an AtmosphereHandler
protected  void sessionSupport(boolean sessionSupport)
           
 AtmosphereFramework setAsyncSupport(AsyncSupport asyncSupport)
          Set the AsyncSupport implementation.
 AtmosphereFramework setAtmosphereDotXmlPath(String atmosphereDotXmlPath)
           
 void setBroadcasterCacheClassName(String broadcasterCacheClassName)
          Set the BroadcasterCache class name.
 AtmosphereFramework setBroadcasterFactory(BroadcasterFactory broadcasterFactory)
          Set the BroadcasterFactory which is used by Atmosphere to construct Broadcaster
 AtmosphereFramework setCometSupport(AsyncSupport asyncSupport)
           
 AtmosphereFramework setDefaultBroadcasterClassName(String bccn)
          Set the default Broadcaster class name
 AtmosphereFramework setHandlersPath(String handlersPath)
           
 AtmosphereFramework setUseBlockingImplementation(boolean useBlockingImplementation)
           
 AtmosphereFramework setUseNativeImplementation(boolean useNativeImplementation)
           
 AtmosphereFramework setUseStreamForFlushingComments(boolean useStreamForFlushingComments)
          Set to true so Atmosphere uses AtmosphereResponse.getOutputStream() by default for write operation.
 AtmosphereFramework setWebSocketProtocolClassName(String webSocketProtocolClassName)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ATMOSPHERE_CONFIG_PATH

public static final String DEFAULT_ATMOSPHERE_CONFIG_PATH
See Also:
Constant Field Values

logger

protected static final org.slf4j.Logger logger

broadcasterFilters

protected final List<String> broadcasterFilters

possibleComponentsCandidate

protected final ArrayList<String> possibleComponentsCandidate

initParams

protected final HashMap<String,String> initParams

config

protected final AtmosphereConfig config

isCometSupportConfigured

protected final AtomicBoolean isCometSupportConfigured

isFilter

protected final boolean isFilter

atmosphereHandlers

protected final Map<String,AtmosphereFramework.AtmosphereHandlerWrapper> atmosphereHandlers

broadcasterTypes

protected final ConcurrentLinkedQueue<String> broadcasterTypes

useNativeImplementation

protected boolean useNativeImplementation

useBlockingImplementation

protected boolean useBlockingImplementation

useStreamForFlushingComments

protected boolean useStreamForFlushingComments

asyncSupport

protected AsyncSupport asyncSupport

broadcasterClassName

protected String broadcasterClassName

isCometSupportSpecified

protected boolean isCometSupportSpecified

isBroadcasterSpecified

protected boolean isBroadcasterSpecified

isSessionSupportSpecified

protected boolean isSessionSupportSpecified

broadcasterFactory

protected BroadcasterFactory broadcasterFactory

broadcasterFactoryClassName

protected String broadcasterFactoryClassName

broadcasterCacheClassName

protected static String broadcasterCacheClassName

webSocketEnabled

protected boolean webSocketEnabled

broadcasterLifeCyclePolicy

protected String broadcasterLifeCyclePolicy

webSocketProtocolClassName

protected String webSocketProtocolClassName

webSocketProtocol

protected WebSocketProtocol webSocketProtocol

handlersPath

protected String handlersPath

servletConfig

protected javax.servlet.ServletConfig servletConfig

autoDetectHandlers

protected boolean autoDetectHandlers

atmosphereDotXmlPath

protected String atmosphereDotXmlPath
Constructor Detail

AtmosphereFramework

public AtmosphereFramework()
Create an AtmosphereFramework.


AtmosphereFramework

public AtmosphereFramework(javax.servlet.ServletConfig sc)
                    throws javax.servlet.ServletException
Create an AtmosphereFramework and initialize it via init(javax.servlet.ServletConfig)

Throws:
javax.servlet.ServletException

AtmosphereFramework

public AtmosphereFramework(boolean isFilter,
                           boolean autoDetectHandlers)
Create an AtmosphereFramework.

Parameters:
isFilter - true if this instance is used as an AtmosphereFilter
Method Detail

getServletContext

public javax.servlet.ServletContext getServletContext()
Specified by:
getServletContext in interface ServletContextProvider

getServletConfig

public javax.servlet.ServletConfig getServletConfig()

broadcasterFilters

public List<String> broadcasterFilters()

getAtmosphereConfig

public AtmosphereConfig getAtmosphereConfig()
Return a configured instance of AtmosphereConfig

Returns:
a configured instance of AtmosphereConfig

addAtmosphereHandler

public AtmosphereFramework addAtmosphereHandler(String mapping,
                                                AtmosphereHandler h)
Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.

Parameters:
mapping - The servlet mapping (servlet path)
h - implementation of an AtmosphereHandler

addAtmosphereHandler

public AtmosphereFramework addAtmosphereHandler(String mapping,
                                                AtmosphereHandler h,
                                                String broadcasterId)
Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.

Parameters:
mapping - The servlet mapping (servlet path)
h - implementation of an AtmosphereHandler
broadcasterId - The Broadcaster.getID() value.

addAtmosphereHandler

public AtmosphereFramework addAtmosphereHandler(String mapping,
                                                AtmosphereHandler h,
                                                Broadcaster broadcaster)
Add an AtmosphereHandler serviced by the Servlet This API is exposed to allow embedding an Atmosphere application.

Parameters:
mapping - The servlet mapping (servlet path)
h - implementation of an AtmosphereHandler
broadcaster - The Broadcaster associated with AtmosphereHandler.

removeAtmosphereHandler

public AtmosphereFramework removeAtmosphereHandler(String mapping)
Remove an AtmosphereHandler

Parameters:
mapping - the mapping used when invoking addAtmosphereHandler(String, AtmosphereHandler);
Returns:
true if removed

removeAllAtmosphereHandler

public AtmosphereFramework removeAllAtmosphereHandler()
Remove all AtmosphereHandler


removeAllInitParams

public AtmosphereFramework removeAllInitParams()
Remove all init parameters.


addInitParameter

public AtmosphereFramework addInitParameter(String name,
                                            String value)
Add init-param like if they were defined in web.xml

Parameters:
name - The name
value - The value

readSystemProperties

protected void readSystemProperties()

init

public AtmosphereFramework init(javax.servlet.ServletConfig sc)
                         throws javax.servlet.ServletException
Load the AtmosphereHandler associated with this AtmosphereServlet.

Parameters:
sc - the ServletContext
Throws:
javax.servlet.ServletException

configureWebDotXmlAtmosphereHandler

protected void configureWebDotXmlAtmosphereHandler(javax.servlet.ServletConfig sc)

configureBroadcaster

protected void configureBroadcaster(javax.servlet.ServletContext sc)
                             throws ClassNotFoundException,
                                    InstantiationException,
                                    IllegalAccessException
Throws:
ClassNotFoundException
InstantiationException
IllegalAccessException

doInitParamsForWebSocket

protected void doInitParamsForWebSocket(javax.servlet.ServletConfig sc)

doInitParams

protected void doInitParams(javax.servlet.ServletConfig sc)
Read init param from web.xml and apply them.

Parameters:
sc - ServletConfig

loadConfiguration

public void loadConfiguration(javax.servlet.ServletConfig sc)
                       throws javax.servlet.ServletException
Throws:
javax.servlet.ServletException

detectSupportedFramework

protected boolean detectSupportedFramework(javax.servlet.ServletConfig sc)
                                    throws ClassNotFoundException,
                                           IllegalAccessException,
                                           InstantiationException,
                                           NoSuchMethodException,
                                           InvocationTargetException
Auto-detect Jersey when no atmosphere.xml file are specified.

Parameters:
sc - ServletConfig
Returns:
true if Jersey classes are detected
Throws:
ClassNotFoundException
IllegalAccessException
InstantiationException
NoSuchMethodException
InvocationTargetException

lookupDefaultBroadcasterType

protected String lookupDefaultBroadcasterType()

sessionSupport

protected void sessionSupport(boolean sessionSupport)

initWebSocketProtocol

protected void initWebSocketProtocol()

destroy

public AtmosphereFramework destroy()

loadAtmosphereDotXml

protected void loadAtmosphereDotXml(InputStream stream,
                                    URLClassLoader c)
                             throws IOException,
                                    javax.servlet.ServletException
Load AtmosphereHandler defined under META-INF/atmosphere.xml

Parameters:
stream - The input stream we read from.
c - The classloader
Throws:
IOException
javax.servlet.ServletException

setAsyncSupport

public AtmosphereFramework setAsyncSupport(AsyncSupport asyncSupport)
Set the AsyncSupport implementation. Make sure you don't set an implementation that only works on some Container. See BlockingIOCometSupport for an example.

Parameters:
asyncSupport -

setCometSupport

public AtmosphereFramework setCometSupport(AsyncSupport asyncSupport)
Parameters:
asyncSupport -
Returns:

getAsyncSupport

public AsyncSupport getAsyncSupport()
Return the current AsyncSupport

Returns:
the current AsyncSupport

getCometSupport

public AsyncSupport getCometSupport()
Return the current AsyncSupport

Returns:
the current AsyncSupport

createAsyncSupportResolver

protected AsyncSupportResolver createAsyncSupportResolver()
Returns an instance of AsyncSupportResolver AsyncSupportResolver

Returns:
CometSupportResolver

autoDetectContainer

protected void autoDetectContainer()
Auto detect the underlying Servlet Container we are running on.


autoDetectAtmosphereHandlers

public void autoDetectAtmosphereHandlers(javax.servlet.ServletContext servletContext,
                                         URLClassLoader classloader)
                                  throws MalformedURLException,
                                         URISyntaxException
Auto detect instance of AtmosphereHandler in case META-INF/atmosphere.xml is missing.

Parameters:
servletContext - ServletContext
classloader - URLClassLoader to load the class.
Throws:
MalformedURLException
URISyntaxException

loadAtmosphereHandlersFromPath

public void loadAtmosphereHandlersFromPath(URLClassLoader classloader,
                                           String realPath)

autoDetectWebSocketHandler

protected void autoDetectWebSocketHandler(javax.servlet.ServletContext servletContext,
                                          URLClassLoader classloader)
                                   throws MalformedURLException,
                                          URISyntaxException
Auto detect instance of WebSocketHandler in case META-INF/atmosphere.xml is missing.

Parameters:
servletContext - ServletContext
classloader - URLClassLoader to load the class.
Throws:
MalformedURLException
URISyntaxException

loadWebSocketFromPath

protected void loadWebSocketFromPath(URLClassLoader classloader,
                                     String realPath)

doCometSupport

public AtmosphereFramework.Action doCometSupport(AtmosphereRequest req,
                                                 AtmosphereResponse res)
                                          throws IOException,
                                                 javax.servlet.ServletException
Invoke the proprietary AsyncSupport

Parameters:
req -
res -
Returns:
an AtmosphereFramework.Action
Throws:
IOException
javax.servlet.ServletException

getDefaultBroadcasterClassName

public String getDefaultBroadcasterClassName()
Return the default Broadcaster class name.

Returns:
the broadcasterClassName

setDefaultBroadcasterClassName

public AtmosphereFramework setDefaultBroadcasterClassName(String bccn)
Set the default Broadcaster class name

Parameters:
bccn - the broadcasterClassName to set

isUseStreamForFlushingComments

public boolean isUseStreamForFlushingComments()
true if Atmosphere uses AtmosphereResponse.getOutputStream() by default for write operation.

Returns:
the useStreamForFlushingComments

setUseStreamForFlushingComments

public AtmosphereFramework setUseStreamForFlushingComments(boolean useStreamForFlushingComments)
Set to true so Atmosphere uses AtmosphereResponse.getOutputStream() by default for write operation. Default is false.

Parameters:
useStreamForFlushingComments - the useStreamForFlushingComments to set

getBroadcasterFactory

public BroadcasterFactory getBroadcasterFactory()
Get the BroadcasterFactory which is used by Atmosphere to construct Broadcaster

Returns:
BroadcasterFactory

setBroadcasterFactory

public AtmosphereFramework setBroadcasterFactory(BroadcasterFactory broadcasterFactory)
                                          throws IllegalAccessException,
                                                 InstantiationException,
                                                 ClassNotFoundException
Set the BroadcasterFactory which is used by Atmosphere to construct Broadcaster

Returns:
BroadcasterFactory
Throws:
IllegalAccessException
InstantiationException
ClassNotFoundException

getBroadcasterCacheClassName

public String getBroadcasterCacheClassName()
Return the BroadcasterCache class name.

Returns:
the BroadcasterCache class name.

setBroadcasterCacheClassName

public void setBroadcasterCacheClassName(String broadcasterCacheClassName)
                                  throws IllegalAccessException,
                                         InstantiationException,
                                         ClassNotFoundException
Set the BroadcasterCache class name.

Parameters:
broadcasterCacheClassName -
Throws:
IllegalAccessException
InstantiationException
ClassNotFoundException

addBroadcasterType

public AtmosphereFramework addBroadcasterType(String broadcasterTypeString)
Add a new Broadcaster class name AtmosphereServlet can use when initializing requests, and when atmosphere.xml broadcaster element is unspecified.

Parameters:
broadcasterTypeString -

getWebSocketProtocolClassName

public String getWebSocketProtocolClassName()

setWebSocketProtocolClassName

public AtmosphereFramework setWebSocketProtocolClassName(String webSocketProtocolClassName)

getAtmosphereHandlers

public Map<String,AtmosphereFramework.AtmosphereHandlerWrapper> getAtmosphereHandlers()

configureQueryStringAsRequest

protected Map<String,String> configureQueryStringAsRequest(AtmosphereRequest request)

isIECandidate

protected boolean isIECandidate(AtmosphereRequest request)

getWebSocketProtocol

public WebSocketProtocol getWebSocketProtocol()

isUseNativeImplementation

public boolean isUseNativeImplementation()

setUseNativeImplementation

public AtmosphereFramework setUseNativeImplementation(boolean useNativeImplementation)

isUseBlockingImplementation

public boolean isUseBlockingImplementation()

setUseBlockingImplementation

public AtmosphereFramework setUseBlockingImplementation(boolean useBlockingImplementation)

getAtmosphereDotXmlPath

public String getAtmosphereDotXmlPath()

setAtmosphereDotXmlPath

public AtmosphereFramework setAtmosphereDotXmlPath(String atmosphereDotXmlPath)

getHandlersPath

public String getHandlersPath()

setHandlersPath

public AtmosphereFramework setHandlersPath(String handlersPath)


Copyright © 2012. All Rights Reserved.