Package rs.data.impl

Class AbstractDaoFactory

java.lang.Object
rs.data.impl.AbstractDaoFactory
All Implemented Interfaces:
IConfigurable, IDaoFactory

public abstract class AbstractDaoFactory
extends java.lang.Object
implements IDaoFactory, IConfigurable
The basic implementation of a DAO factory.
Author:
ralph
  • Constructor Details

  • Method Details

    • getLog

      protected org.slf4j.Logger getLog()
      Returns the logger object.
      Returns:
      the logger object
    • configure

      public void configure​(org.apache.commons.configuration.Configuration cfg) throws org.apache.commons.configuration.ConfigurationException
      Specified by:
      configure in interface IConfigurable
      Throws:
      org.apache.commons.configuration.ConfigurationException
    • loadDaoMaster

      public IDaoMaster loadDaoMaster​(org.apache.commons.configuration.SubnodeConfiguration config)
      Loads an object from a configuration. The object is configured if it is an instance of IConfigurable.
      Parameters:
      config - configuration
      Returns:
      the object
    • setDaoMaster

      public void setDaoMaster​(java.lang.String id, IDaoMaster daoMaster)
      Sets the DAO master.
      Parameters:
      id - id of master
      daoMaster - master to bet set
    • getDaoMaster

      public IDaoMaster getDaoMaster​(java.lang.String id)
      Returns the DAO master with given ID.
      Specified by:
      getDaoMaster in interface IDaoFactory
      Parameters:
      id - ID of master
      Returns:
      the master or null
    • shutdown

      public void shutdown()
      Shutdown the factory.
      Specified by:
      shutdown in interface IDaoFactory
    • loadDao

      public IGeneralDAO<? extends java.io.Serializable,​? extends IGeneralBO<? extends java.io.Serializable>> loadDao​(org.apache.commons.configuration.HierarchicalConfiguration config)
      Loads an object from a configuration. The object is configured if it is an instance of IConfigurable.
      Parameters:
      config - configuration
      Returns:
      the object
    • getProperty

      public java.lang.Object getProperty​(java.lang.String name)
      Returns the factory property.
      Specified by:
      getProperty in interface IDaoFactory
      Parameters:
      name - name of property
      Returns:
      value or NULL
    • setProperty

      public void setProperty​(java.lang.String name, java.lang.Object value)
      Sets the property.
      Specified by:
      setProperty in interface IDaoFactory
      Parameters:
      name - name of property
      value - value of property
    • getParameter

      public java.lang.String getParameter​(java.lang.String name)
      Returns the factory parameter.
      Specified by:
      getParameter in interface IDaoFactory
      Parameters:
      name - name of parameter
      Returns:
      value or NULL
    • getParameterKeys

      public java.util.Iterator<java.lang.String> getParameterKeys()
      Returns an iterator of all property keys.
      Specified by:
      getParameterKeys in interface IDaoFactory
      Returns:
      iterator
    • getParameterUrl

      public java.net.URL getParameterUrl​(java.lang.String name) throws java.net.MalformedURLException
      Returns the factory parameter as a URL.
      Specified by:
      getParameterUrl in interface IDaoFactory
      Parameters:
      name - name of parameter
      Returns:
      value or NULL
      Throws:
      java.net.MalformedURLException - when the parameter URL cannot be created
    • registerDao

      public void registerDao​(IGeneralDAO<? extends java.io.Serializable,​? extends IGeneralBO<? extends java.io.Serializable>> dao)
      Registers a DAO with this factory (under its class name).
      Specified by:
      registerDao in interface IDaoFactory
      Parameters:
      dao - the DAO to be registered.
    • registerDao

      public void registerDao​(java.lang.String name, IGeneralDAO<? extends java.io.Serializable,​? extends IGeneralBO<? extends java.io.Serializable>> dao)
      Registers a DAO with this factory.
      Specified by:
      registerDao in interface IDaoFactory
      Parameters:
      name - name of registration
      dao - the DAO to be registered.
    • getDao

      public <X extends IGeneralDAO<?,​ ?>> X getDao​(java.lang.Class<X> clazz)
      Returns a registered DAO of given class (registered under its class name).

      The method will try to return the DAO registered under the class name. If that cannopt be found then the first object found being an instance of the class will be returned.

      Specified by:
      getDao in interface IDaoFactory
      Type Parameters:
      X - the DAO class
      Parameters:
      clazz - the class of the DAO to be returned
      Returns:
      the DAO or null if not registered
    • setCacheEnabled

      public void setCacheEnabled​(boolean cacheEnabled)
      Enable or disable caching for all registered DAOs.
      Specified by:
      setCacheEnabled in interface IDaoFactory
      Parameters:
      cacheEnabled - true when cache shall be enabled
      See Also:
      IGeneralDAO.setCacheEnabled(boolean)
    • getDao

      public <X extends IGeneralDAO<?,​ ?>> X getDao​(java.lang.String name, java.lang.Class<X> clazz)
      Returns a registered DAO of given class.
      Specified by:
      getDao in interface IDaoFactory
      Type Parameters:
      X - the DAO class
      Parameters:
      name - the name of the registration
      clazz - the class of the DAO to be returned
      Returns:
      the DAO or null if not registered
    • getDaoFor

      public <K extends java.io.Serializable,​ T extends GeneralDTO<K>> IGeneralDAO<K,​?> getDaoFor​(T o)
      Returns the correct DAO for the given transfer object.
      Specified by:
      getDaoFor in interface IDaoFactory
      Type Parameters:
      K - the key class
      T - the transfer class
      Parameters:
      o - the object to look for
      Returns:
      DAO or null if not found
    • getDaoFor

      public <B extends IGeneralBO<?>> IGeneralDAO<?,​B> getDaoFor​(B o)
      Returns the correct DAO for the given business object.
      Specified by:
      getDaoFor in interface IDaoFactory
      Type Parameters:
      B - the BO class
      Parameters:
      o - the object to look for
      Returns:
      DAO or null if not found
    • getDaoFor

      public <B extends IGeneralBO<?>> IGeneralDAO<?,​B> getDaoFor​(java.lang.Class<B> clazz)
      Returns the correct DAO for the given business object class.
      Specified by:
      getDaoFor in interface IDaoFactory
      Type Parameters:
      B - the BO class
      Parameters:
      clazz - the BO clazz to look for
      Returns:
      DAO or null if not found
    • getDaos

      public java.lang.Iterable<IGeneralDAO<?,​?>> getDaos()
      Return an iterable of the registered DAOs.
      Specified by:
      getDaos in interface IDaoFactory
      Returns:
      the iterable
    • clearCache

      public void clearCache()
      Clear cache of all DAOs.
      Specified by:
      clearCache in interface IDaoFactory
    • getUrlTransformer

      public IUrlTransformer getUrlTransformer()
      Returns the urlTransformer.
      Specified by:
      getUrlTransformer in interface IDaoFactory
      Returns:
      the urlTransformer
    • setUrlTransformer

      public void setUrlTransformer​(IUrlTransformer urlTransformer)
      Sets the urlTransformer.
      Specified by:
      setUrlTransformer in interface IDaoFactory
      Parameters:
      urlTransformer - the urlTransformer to set
    • getTransactionManager

      public javax.transaction.TransactionManager getTransactionManager()
      Returns the TX manager used. If there was no TX manager set before, this method will create a TX manager.
      Specified by:
      getTransactionManager in interface IDaoFactory
      Returns:
      the transaction manager
    • createTransactionManager

      protected javax.transaction.TransactionManager createTransactionManager()
      Creates a new transaction manager.
      Returns:
      a new transaction manager
    • setTransactionManager

      public void setTransactionManager​(javax.transaction.TransactionManager txManager)
      Sets the given TX manager to be used. This method will throw an exception if there is already a TX manager active.
      Specified by:
      setTransactionManager in interface IDaoFactory
      Parameters:
      txManager - the TX manager.
    • getDefaultTransactionTimeout

      public long getDefaultTransactionTimeout()
      Returns the default timeout for transactions.
      Specified by:
      getDefaultTransactionTimeout in interface IDaoFactory
      Returns:
      timeout in ms
    • setDefaultTransactionTimeout

      public void setDefaultTransactionTimeout​(long defaultTimeout)
      Sets the default timeout for transactions.
      Specified by:
      setDefaultTransactionTimeout in interface IDaoFactory
      Parameters:
      defaultTimeout - timeout in ms, IDaoFactory.DEFAULT_TX_TIMEOUT will be used when value is 0.
    • begin

      public void begin()
      Begins a transaction. This method does nothing if the thread is already within a transaction.
      Specified by:
      begin in interface IDaoFactory
    • getThreadVerificationPeriod

      public long getThreadVerificationPeriod()
      Returns the time in ms after the TX contexts shall be verified.

      The verification period is a minimum requirement. All contexts assigned to dead threads will be removed.

      Returns:
      the threadVerificationPeriod or -1 if not set
    • setThreadVerificationPeriod

      public void setThreadVerificationPeriod​(long threadVerificationPeriod)
      Sets the threadVerificationPeriod.

      The verification period is a minimum requirement. All contexts assigned to dead threads will be removed.

      Parameters:
      threadVerificationPeriod - the threadVerificationPeriod to set, -1 for unset
    • begin

      public void begin​(long timeout)
      Begins a transaction. This method does nothing if the thread is already within a transaction.
      Specified by:
      begin in interface IDaoFactory
      Parameters:
      timeout - timeout of transaction in ms
    • commit

      public void commit()
      Commits a transaction. If the last call to IDaoFactory.begin() did not start a new TX then this method does nothing.
      Specified by:
      commit in interface IDaoFactory
    • rollback

      public void rollback()
      Rolls back a transaction. If the last call to IDaoFactory.begin() did not start a new TX then this method does nothing.
      Specified by:
      rollback in interface IDaoFactory
    • getTransaction

      public javax.transaction.Transaction getTransaction() throws javax.transaction.SystemException
      Returns the current active transaction.
      Specified by:
      getTransaction in interface IDaoFactory
      Returns:
      current transaction
      Throws:
      javax.transaction.SystemException - if there is a problem
    • getTransactionStartTime

      public long getTransactionStartTime()
      Returns the start time of the current or last transaction in ms.
      Specified by:
      getTransactionStartTime in interface IDaoFactory
      Returns:
      the start time in ms
    • getTransactionEndTime

      public long getTransactionEndTime()
      Returns the end time of the current or last transaction in ms.
      Specified by:
      getTransactionEndTime in interface IDaoFactory
      Returns:
      the end time in ms
    • getTransactionDuration

      public long getTransactionDuration()
      Returns the duration of the current or last transaction in ms.
      Specified by:
      getTransactionDuration in interface IDaoFactory
      Returns:
      the duration in ms
    • getLastTxBeginStatus

      public java.lang.String getLastTxBeginStatus()
    • cleanTransactionContext

      public void cleanTransactionContext()
      Ensures that the current thread has no left-overs from last TX activity.
      Specified by:
      cleanTransactionContext in interface IDaoFactory
    • addDaoFactoryListener

      public void addDaoFactoryListener​(IDaoFactoryListener listener)
      Adds a factory listener.
      Specified by:
      addDaoFactoryListener in interface IDaoFactory
      Parameters:
      listener - listener
    • removeDaoFactoryListener

      public void removeDaoFactoryListener​(IDaoFactoryListener listener)
      Removes a factory listener.
      Specified by:
      removeDaoFactoryListener in interface IDaoFactory
      Parameters:
      listener - listener
    • fireDaoFactoryEvent

      protected void fireDaoFactoryEvent​(DaoFactoryEvent event)
      Fires a factory event.
      Parameters:
      event - event to be fired
    • modelChanged

      public void modelChanged()
      Registers the current context that the model changed underneath.
    • isDebugTransactions

      public boolean isDebugTransactions()
      Returns whether debugging of transaction demarcations.
      Returns:
      true when debugging shall be enabled (via SLF4J)
    • setDebugTransactions

      public void setDebugTransactions​(boolean debug)
      Sets debugging of transaction demarcations.
      Parameters:
      debug - true when debugging shall be enabled (via SLF4J)
    • isTraceTransactions

      public boolean isTraceTransactions()
      Returns whether stacktracing is enabled with transaction demarcation debugging option.
      Returns:
      true when stacktrace shall be enabled (via SLF4J)
    • setTraceTransactions

      public void setTraceTransactions​(boolean trace)
      Sets whether stacktracing is enabled with transaction demarcation debugging option.
      Parameters:
      trace - true when stacktrace shall be enabled (via SLF4J)