com.boylesoftware.cb2
Class BLManager

java.lang.Object
  extended bycom.boylesoftware.cb2.BLManager
All Implemented Interfaces:
StatusProvider

public final class BLManager
extends java.lang.Object
implements StatusProvider

Business Level Manager singleton, which represents the Business Level of a CB2-based application. One instance of this class exists in the application, usually created at the application startup, and it is the second level up from the application context in terms of provided services. The BL Manager supports notion of user sessions, represented by BLO containers, and Business Level Objects (BLOs) that populate BLO containers and implement the application's business logic. A dedicated single BLO container is maintained by the BL Manager for shared BLOs - the Shared BLO Container. Also, the BL Manager maintains DAO instances for each configured in the application context data source, which it provides to the BLOs for database operations.

Clients of the BL Manager, usually the Presentation Level implementation, have responsibility to manage user sessions within the BL Manager creating and destroying them via createUserSession and destroyUserSession methods. It is also a responsibility of the client, if required by the application, to notify the BL Manager about user authentication events, such as user login and logout, by calling appropriate methods.

When the client needs to perform a business level operation, that is to call a business method on a BLO, it first requests the BLO container for the user session by calling getBLOContainer method and passing to it the session id, previously aquired from the createUserSession call. Then, the required BLO is looked up by its deployment name in the BLO container and appropriate business methods are called on it. When the BL Manager returns the BLO container to the getBLOContainer caller, it locks the container, so any consequent call to getBLOContainer with the same session id will block until the BLO container is released by the previous requester by calling releaseBLOContainer method (or release convenience method on the container itself). This mechanism allows to forget about providing access synchronization for the BLOs if used correctly, that is if references to BLOs are not stored somewhere outside BLO container and they are not called without aquiring and locking the BLO container via getBLOContainer first. This does not concern the Shared BLO Container, which never gets locked providing concurring access to the shared BLOs. That is why appropriate access synchronization should be implemented in the BLO level for shared BLOs.

The BL Manager is configured by file called blo-config.xml and located in the configuration directory (see application context's getConfigDirPath method). This configuration file contains BLO deployment descriptors and configuration for the DAOs. The DTD can be found at http://www.cb2project.com/dtd/blo-config_1_0.dtd.

A few application properties control the BL Manager's behaviour:

This class logs messages related to its operation under "com.boylesoftware.cb2.BLOMGR" log channel.

Version:
$Id: BLManager.java,v 1.11 2004/04/30 18:54:08 levahim Exp $
Author:
Lev Himmelfarb

Field Summary
 
Fields inherited from interface com.boylesoftware.cb2.StatusProvider
TIMESTAMP_FORMAT
 
Constructor Summary
BLManager(ApplicationContext appCtx)
          Creates BLManager and initializes the BLO subsystem.
 
Method Summary
 java.lang.String createUserSession()
          Creates and initializes a new user session with BLO container and all the BLOs.
 void destroy()
          Destroys the BLO subsystem.
 boolean destroyUserSession(java.lang.String sessionId)
          Destroyes a user session including its BLO container and all the BLOs in it.
 ApplicationContext getApplicationContext()
          Gets application context to which this BL Manager belongs.
 BLOContainer getBLOContainer(java.lang.String sessionId)
          Gets BLO container for the specified user session.
 EventStatistics getEventStats(java.lang.String eventName)
          Not used in this class, always returns null.
 BLObject getSharedBLO(java.lang.String bloName)
          Gets a shared BLO from the Shared BLO Container.
 byte[] getStats()
          Retrieves information about the Business Level as an XML text.
 void reconfigureDAL(org.w3c.dom.Document bloConfig)
          Re/initializes the database access level.
 void releaseBLOContainer(java.lang.String sessionId)
          Releases BLO container that was previously locked by a getBLOContainer call.
 void restoreUserSession(java.lang.String sessionId, BLOContainer bloContainer)
          Binds the specified BLO container instance with the specified session id effectively creating a new session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BLManager

public BLManager(ApplicationContext appCtx)
          throws ApplicationException
Creates BLManager and initializes the BLO subsystem. There should be only one BLManager instance in the application. Usually an instance of BLManager created once during the application startup just after the application context is created and intialized.

Parameters:
appCtx - the application context.
Throws:
ApplicationException - the BLO subsystem cannot be initialized and is unavailable.
Method Detail

reconfigureDAL

public void reconfigureDAL(org.w3c.dom.Document bloConfig)
                    throws BLException
Re/initializes the database access level. The BLO configuration file is reread and new DAOs along with DAO config objects are created and configured for each data source.

Parameters:
bloConfig - a preparsed blo-config.xml document or null if you want the method to find and parse the file itself.
Throws:
BLException - the DAL cannot be reconfigured, the application is probably left in an unstable state.

destroy

public void destroy()
Destroys the BLO subsystem. This method is usually called when the application is shutting down.

Unless com.boylesoftware.cb2.keepBLSessions application property is set to "true", calling this method causes the BL Manager to explicitely destroy all BLO containers within it, which, in turn, causes the BLO containers to explicitely destroy all BLOs contained within them by calling their destroy methods.

If at the moment of this call threads exist that are waiting for BLO containers in getBLOContainer method, they will get a NoBLOContainerException. Basically, it is a good practice to write the presentation level in a way that there is a guarantee that no BLO containers are being used at the moment of the destroy call.


getSharedBLO

public BLObject getSharedBLO(java.lang.String bloName)
Gets a shared BLO from the Shared BLO Container.

All BLO containers perform lookup in the Shared BLO Container if there is no requested BLO in them, so it is a relatively rare case when this method needs to be called directly. The need may arise when you don't have any context of current user session, or for some reason don't want to lock the session's BLO container, while all you need is a shared BLO. Then it can be aquired via this method.

Parameters:
bloName - case-insensitive deployment name of a shared BLO.
Returns:
reference to the shared BLO or null if there is no BLO under the specified name in the Shared BLO Container.

getApplicationContext

public ApplicationContext getApplicationContext()
Gets application context to which this BL Manager belongs.

Returns:
reference to the application context.

restoreUserSession

public void restoreUserSession(java.lang.String sessionId,
                               BLOContainer bloContainer)
                        throws BLException
Binds the specified BLO container instance with the specified session id effectively creating a new session.

This method can be used to restore a session from a serialized BLO container. It also internally "activates" the specified BLO container instance, which causes activate methods to be called on the BLOs in the container.

Parameters:
sessionId - session id, to which to bind the container. Must not already exist, or a BLException is thrown.
bloContainer - BLO container instance restored from the serialized form (transient fields are not set).
Throws:
BLException - if the session cannot be restored.

createUserSession

public java.lang.String createUserSession()
                                   throws BLException
Creates and initializes a new user session with BLO container and all the BLOs. When a session gets created the BLOs are instantiated, or borrowed from the pool, and initialized through calling their init methods.

Returns:
the new session's id.
Throws:
BLException - could not initialize the BLO container or one of the BLOs (a BLO's init method has thrown an exception).

destroyUserSession

public boolean destroyUserSession(java.lang.String sessionId)
Destroyes a user session including its BLO container and all the BLOs in it. When a session gets destroyed, the BLOs are notified through calling their destroy methods.

Note, that if the BLO container is locked at the moment of the call, the method blocks and waits indefinitely until the BLO container gets unlocked by another thread.

Parameters:
sessionId - id of the session to destroy.
Returns:
true if the session has been successfully destroyed by this call, false if no session with the specified id exists or the session is already being destroyed by another call from a different thread.

getBLOContainer

public BLOContainer getBLOContainer(java.lang.String sessionId)
                             throws NoBLOContainerException,
                                    TooManyWaitingThreadsException,
                                    BLException
Gets BLO container for the specified user session.

When this method returns a BLO container it locks it, meaning that any subsequent getBLOContainer call for the same session will block and wait indefinitely until the BLO container is released by the previous requester through calling releaseBLOContainer method (or release convenience method on the BLO container itself). We suggest having an invocation of release method in a finally block of your client code so there is a guarantee that the BLO container always gets unlocked.

Never keep references to BLOs that are got from a BLO container after the BLO container is released, nor any references to the BLO container itself - always reaquire the BLO container when you need any service provided by the BLOs.

Parameters:
sessionId - id of the user session, for which we are requesting the BLO container.
Returns:
reference to the BLO container corresponding the session.
Throws:
NoBLOContainer - if there is no BLO container for the requested session. Note, that the session can be destroyed by another thread during this method call. If that happens, this exception is thrown with an appropriate message.
TooManyWaitingThreadsException - if the BLO container is locked and there are already too many threads waiting for its release. The maximum number of waiting threads is specified by com.boylesoftware.cb2.maxWaitingThreads application property. If it is not specified, the number of waiting threads is not limited.
BLException - if an error happens in a BLO implementing BLOContainerStateListener interface and it throws an exception.
NoBLOContainerException
See Also:
releaseBLOContainer(String), BLOContainer.release()

releaseBLOContainer

public void releaseBLOContainer(java.lang.String sessionId)
                         throws BLException
Releases BLO container that was previously locked by a getBLOContainer call. After the BLO conatiner has been unlocked by this call, it can be reaquired again by another getBLOContainer mathod invocation.

Note, that it is usually more convenient to call release method on the BLO container itself, which calls this method internally, but does not require session id.

Parameters:
sessionId - id of the user session, whose BLO container we are releasing.
Throws:
BLException - if an error happens in a BLO implementing BLOContainerStateListener interface and it throws an exception. Note, that the BLO container gets unlocked anyway.
java.lang.IllegalArgumentException - if there is no session with the specified session id.
See Also:
getBLOContainer(String), BLOContainer.release()

getStats

public byte[] getStats()
                throws java.lang.Exception
Retrieves information about the Business Level as an XML text.

Specified by:
getStats in interface StatusProvider
Returns:
XML for the general BL info.
Throws:
java.lang.Exception - if an unexpected error happens.

getEventStats

public EventStatistics getEventStats(java.lang.String eventName)
Not used in this class, always returns null.

Specified by:
getEventStats in interface StatusProvider
Parameters:
eventName - implementation specific event name, for which current statistics are requested.
Returns:
statistics information for the specified event or null if the event is unknown.


Copyright © 2002,2003,2004 - Boyle Software, Inc.