T - the web session key typepublic abstract class WebApplication<T>
extends org.tentackle.app.AbstractApplication
implements org.tentackle.session.SessionPoolProvider, org.tentackle.dbms.ConnectionManagerProvider
Web applications usually run in a container such as glassfish or in case of JRuby/Rails in a pack of mongrels or a single webrick. Because we cannot make any assumptions about the threading model (single thread/single instance/single jvm as with JRuby/Webrick or full-blown multithreaded as in glassfish or half-way in between like JRuby with mongrel_jcluster) the WebAbstractApplication provides a logical Db-pool on top of multiplexed physical database connections. The initial size of the logical Db pool is 2 on 1 physical connection. This will not waste resources in single thread per JVM setups (like Rails/Webrick) but will dynamically grow otherwise.
Web applications need some mapping between sessions and
SessionInfo-objects that in turn carry the user id
and a SecurityManager. For the time of a web-roundtrip
a logical Db is picked from the pool and associated with that
user info (i.e. "session"). Notice that there may be more than
one session per user (but each gets its own user info). That's
why user infos are mapped by a session key object and not by
the user id. Furthermore, depending on the container's session
model, it is not sure that the container's session carries
the user id at all.
| Constructor and Description |
|---|
WebApplication(String name)
Creates an instance of a web application.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addWebSession(T sessionKey,
org.tentackle.session.SessionInfo userInfo)
Adds a mapping between a web session key and a user session info.
This is usually done in the login controller. |
protected void |
configurePreferences() |
protected void |
configureSessionInfo(org.tentackle.session.SessionInfo sessionInfo)
Configures the session info.
|
org.tentackle.dbms.ConnectionManager |
createConnectionManager()
Creates the connection cache for the client sessions.
|
org.tentackle.session.SessionPool |
createSessionPool()
Creates the logical SessionPool.
The default implementation creates a DefaultDbPool with a 2 pre-opened Db, increment by 1, don't drop below 2. |
protected void |
doFinishStartup()
Finishes the startup.
The default implementation starts the modification thread, unless "--nomodthread" given, creates the connection cache, the Db pool,
and the DbServer instance. |
protected void |
doLogin()
Connects the server to the database backend.
|
protected void |
doStop(int exitValue)
Terminates the application server.
|
protected void |
doStop(int exitValue,
Exception ex)
Terminates the application server.
|
org.tentackle.dbms.ConnectionManager |
getConnectionManager()
Gets the connection manager.
|
static <T> WebApplication<T> |
getInstance(Class<? extends WebApplication<T>> clazz,
Properties props)
Gets the current web application instance or
starts a new instance if not already running.
|
org.tentackle.session.Session |
getSession(T sessionKey)
Gets a persistence session from the session pool by a web session key.
|
Collection<T> |
getSessionKeys(org.tentackle.session.SessionInfo sessionInfo)
Gets the web session keys for a user session info.
|
org.tentackle.session.SessionPool |
getSessionPool() |
static <T> WebApplication<T> |
getWebApplication()
Gets the current web application instance.
|
boolean |
isServer()
Returns whether the application is a server.
|
void |
putSession(org.tentackle.session.Session session)
Release a persistence session.
Should be invoked after sending/rendering the response to the web browser. |
void |
removeWebSession(T sessionKey)
Removes a mapping between a web session key and a user session info.
This is usually done in the logout controller. |
void |
setSessionInfo(org.tentackle.session.SessionInfo sessionInfo) |
void |
start(Properties props)
Starts the application.
|
void |
stop()
Gracefully terminates the web application server.
|
configureModificationTracker, configureSecurityManager, createDomainContext, createSession, createSessionInfo, deregisterJdbcDrivers, detectContainer, detectJNLP, doConfigureApplication, doInitialize, getCreationTime, getDomainContext, getName, getProperties, getProperty, getRunningApplication, getSession, getSessionInfo, getUser, getUser, isDeployedByJNLP, isRunningInContainer, logStackdump, register, setDomainContext, setName, setProperties, toString, unregister, updateSessionInfoAfterLoginpublic WebApplication(String name)
name - the application namepublic static <T> WebApplication<T> getWebApplication()
This is just a convenience method to AbstractApplication.getRunningApplication().
T - the web session key typepublic static <T> WebApplication<T> getInstance(Class<? extends WebApplication<T>> clazz, Properties props) throws org.tentackle.misc.ApplicationException
Web containers should use this method to make sure that only one instance is started per JVM/classloader-context.
If the application runs in a container (tomcat, glassfish, etc...) additional properties may be appended/overwritten to/in the given props-argument. In web.xml add the following lines:
<env-entry>
<env-entry-name>tentackle.properties</env-entry-name>
<env-entry-value>../myapplication.properties</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
This will load the properties-file myapplication.properties from
the WEB-INF-directory (without "../" from WEB-INF/classes)
and modify the properties given by props.T - the web session key typeclazz - the application classprops - the properties to configure the applicationorg.tentackle.misc.ApplicationException - if failedpublic void setSessionInfo(org.tentackle.session.SessionInfo sessionInfo)
Overwridden to protect the sessioninfo once it is set.
setSessionInfo in class org.tentackle.app.AbstractApplicationpublic boolean isServer()
isServer in class org.tentackle.app.AbstractApplicationprotected void configurePreferences()
configurePreferences in class org.tentackle.app.AbstractApplicationpublic void addWebSession(T sessionKey, org.tentackle.session.SessionInfo userInfo)
sessionKey - the (unique) web session keyuserInfo - the user infopublic void removeWebSession(T sessionKey)
sessionKey - the (unique) web session keypublic Collection<T> getSessionKeys(org.tentackle.session.SessionInfo sessionInfo)
sessionInfo - the user session infopublic org.tentackle.session.Session getSession(T sessionKey)
sessionKey - the web session keypublic void putSession(org.tentackle.session.Session session)
session - the persistence session to releasepublic void start(Properties props) throws org.tentackle.misc.ApplicationException
props - the properties to configure the applicationorg.tentackle.misc.ApplicationException - if startup failedAbstractApplication.setProperties(java.util.Properties)public void stop()
throws org.tentackle.misc.ApplicationException
org.tentackle.misc.ApplicationException - if termination failedpublic org.tentackle.dbms.ConnectionManager createConnectionManager()
public org.tentackle.dbms.ConnectionManager getConnectionManager()
getConnectionManager in interface org.tentackle.dbms.ConnectionManagerProviderpublic org.tentackle.session.SessionPool createSessionPool()
public org.tentackle.session.SessionPool getSessionPool()
getSessionPool in interface org.tentackle.session.SessionPoolProviderprotected void configureSessionInfo(org.tentackle.session.SessionInfo sessionInfo)
sessionInfo - the session infoprotected void doLogin()
throws org.tentackle.misc.ApplicationException
org.tentackle.misc.ApplicationException - if login failedprotected void doFinishStartup()
throws org.tentackle.misc.ApplicationException
"--nomodthread" given, creates the connection cache, the Db pool,
and the DbServer instance.doFinishStartup in class org.tentackle.app.AbstractApplicationorg.tentackle.misc.ApplicationException - if failedprotected void doStop(int exitValue,
Exception ex)
exitValue - the doStop value for System.exit()ex - an exception causing the termination, null if noneprotected void doStop(int exitValue)
exitValue - the doStop value for System.exit()Tentackle - distributed, domain- and model-driven