com.xpn.xwiki.store
Class XWikiHibernateBaseStore

java.lang.Object
  extended by com.xpn.xwiki.store.XWikiHibernateBaseStore
All Implemented Interfaces:
org.xwiki.component.phase.Initializable
Direct Known Subclasses:
HibernateAttachmentRecycleBinStore, HibernateAttachmentVersioningStore, XWikiHibernateAttachmentStore, XWikiHibernateRecycleBinStore, XWikiHibernateStore, XWikiHibernateVersioningStore

public class XWikiHibernateBaseStore
extends Object
implements org.xwiki.component.phase.Initializable


Nested Class Summary
static interface XWikiHibernateBaseStore.HibernateCallback<T>
          Callback (closure) interface for operations in hibernate.
 
Field Summary
protected  org.xwiki.logging.LoggerManager loggerManager
          LoggerManager to suspend logging during normal faulty SQL operation.
 
Constructor Summary
XWikiHibernateBaseStore()
          Empty constructor needed for component manager.
XWikiHibernateBaseStore(String hibpath)
          Deprecated. 1.6M1. Use ComponentManager.lookup(String) instead.
XWikiHibernateBaseStore(XWiki xwiki, XWikiContext context)
          Deprecated. 1.6M1. Use ComponentManager.lookup(String) instead.
 
Method Summary
 boolean beginTransaction(boolean withTransaction, XWikiContext context)
          Deprecated. since 4.0M1, use beginTransaction(SessionFactory, XWikiContext)
 boolean beginTransaction(org.hibernate.SessionFactory sfactory, boolean withTransaction, XWikiContext context)
          Deprecated. since 4.0M1, use beginTransaction(SessionFactory, XWikiContext)
 boolean beginTransaction(org.hibernate.SessionFactory sfactory, XWikiContext context)
          Begins a transaction with a specific SessionFactory.
 boolean beginTransaction(XWikiContext context)
          Begins a transaction if the context does not contains any.
 void checkHibernate(XWikiContext context)
          Initializes hibernate
 void cleanUp(XWikiContext context)
          Cleanup all sessions Used at the shutdown time
 String dynamicMappingTableName(String className)
          Return the name generated for a dynamic mapped object.
 void endTransaction(XWikiContext context, boolean commit)
          Ends a transaction and close the session.
 void endTransaction(XWikiContext context, boolean commit, boolean withTransaction)
          Deprecated. since 4.0M1, use endTransaction(XWikiContext, boolean)
protected  String escapeSchema(String schema, XWikiContext context)
          Escape schema name depending of the database engine.
<T> T
execute(XWikiContext context, boolean bTransaction, boolean doCommit, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Deprecated. since 4.0M1, use execute(XWikiContext, boolean, HibernateCallback) or failSafeExecute(XWikiContext, boolean, HibernateCallback)
<T> T
execute(XWikiContext context, boolean doCommit, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute method for operations in hibernate.
<T> T
executeRead(XWikiContext context, boolean bTransaction, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Deprecated. since 4.0M1, use executeRead(XWikiContext, HibernateCallback) or failSafeExecuteRead(XWikiContext, HibernateCallback)
<T> T
executeRead(XWikiContext context, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute method for read-only operations in hibernate.
<T> T
executeWrite(XWikiContext context, boolean bTransaction, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Deprecated. since 4.0M1, use executeWrite(XWikiContext, HibernateCallback) or failSafeExecuteWrite(XWikiContext, HibernateCallback)
<T> T
executeWrite(XWikiContext context, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute method for read-write operations in hibernate.
<T> T
failSafeExecute(XWikiContext context, boolean doCommit, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute method for operations in hibernate in an independent session (but not closing the current one if any).
<T> T
failSafeExecuteRead(XWikiContext context, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute hibernate read-only operation in a independent session (but not closing the current one if any).
<T> T
failSafeExecuteWrite(XWikiContext context, XWikiHibernateBaseStore.HibernateCallback<T> cb)
          Execute hibernate read-only operation in a independent session (but not closing the current one if any).
 org.hibernate.cfg.Configuration getConfiguration()
           
 Map<String,String> getConnections()
           
 DatabaseProduct getDatabaseProductName()
          Retrieve the current database product name.
 DatabaseProduct getDatabaseProductName(XWikiContext context)
          Deprecated. since 4.0M1 use getDatabaseProductName()
protected  org.hibernate.cfg.Configuration getMapping(String className, String customMapping)
          Build a Configuration containing the provide mapping.
 int getNbConnections()
           
 String getPath()
          Allows to get the current hibernate config file path
protected  String getSchemaFromWikiName(String wikiName, DatabaseProduct databaseProduct, XWikiContext context)
          Convert wiki name in database/schema name.
protected  String getSchemaFromWikiName(String wikiName, XWikiContext context)
          Convert wiki name in database/schema name.
protected  String getSchemaFromWikiName(XWikiContext context)
          Convert context's database in real database/schema name.
 String[] getSchemaUpdateScript(org.hibernate.cfg.Configuration config, XWikiContext context)
          This function gets the schema update scripts generated by comparing the current database with the current hibernate mapping config.
 org.hibernate.Session getSession(XWikiContext context)
          This get's the current session.
 org.hibernate.SessionFactory getSessionFactory()
           
 org.hibernate.Transaction getTransaction(XWikiContext context)
          Allows to get the current transaction from the context This is set in beginTransaction
 void initialize()
           
protected  boolean isInSchemaMode()
           
protected  boolean isVirtual(XWikiContext context)
          Checks if this xwiki setup is virtual meaning if multiple wikis can be accessed using the same database pool
protected  String makeMapping(String className, String customMapping)
          Build a new XML string to define the provided mapping.
 void setDatabase(org.hibernate.Session session, XWikiContext context)
          Virtual Wikis Allows to switch database connection
 void setNbConnections(int nbConnections)
           
 void setPath(String hibpath)
          Allows to set the current hibernate config file path
 void setSession(org.hibernate.Session session, XWikiContext context)
          Allows to set the current session in the context This is set in beginTransaction
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
           
 void setTransaction(org.hibernate.Transaction transaction, XWikiContext context)
          Allows to set the current transaction This is set in beginTransaction
 void shutdownHibernate(XWikiContext context)
          Allows to shut down the hibernate configuration Closing all pools and connections
 void updateSchema(BaseClass bclass, XWikiContext context)
          Custom Mapping This function update the schema based on the dynamic custom mapping provided by the class
 void updateSchema(String[] createSQL, XWikiContext context)
          Runs the update script on the current database
 void updateSchema(XWikiContext context)
          Allows to update the schema to match the hibernate mapping
 void updateSchema(XWikiContext context, boolean force)
          Allows to update the schema to match the hibernate mapping
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

loggerManager

@Inject
protected org.xwiki.logging.LoggerManager loggerManager
LoggerManager to suspend logging during normal faulty SQL operation.

Constructor Detail

XWikiHibernateBaseStore

@Deprecated
public XWikiHibernateBaseStore(XWiki xwiki,
                                          XWikiContext context)
Deprecated. 1.6M1. Use ComponentManager.lookup(String) instead.

THis allows to initialize our storage engine. The hibernate config file path is taken from xwiki.cfg or directly in the WEB-INF directory.

Parameters:
xwiki -
context -

XWikiHibernateBaseStore

@Deprecated
public XWikiHibernateBaseStore(String hibpath)
Deprecated. 1.6M1. Use ComponentManager.lookup(String) instead.

Initialize the storage engine with a specific path This is used for tests.

Parameters:
hibpath -

XWikiHibernateBaseStore

public XWikiHibernateBaseStore()
Empty constructor needed for component manager.

Method Detail

initialize

public void initialize()
                throws org.xwiki.component.phase.InitializationException
Specified by:
initialize in interface org.xwiki.component.phase.Initializable
Throws:
org.xwiki.component.phase.InitializationException

getPath

public String getPath()
Allows to get the current hibernate config file path


setPath

public void setPath(String hibpath)
Allows to set the current hibernate config file path

Parameters:
hibpath -

getDatabaseProductName

public DatabaseProduct getDatabaseProductName()
Retrieve the current database product name. If no current session is available, obtains a connection from the Hibernate connection provider attached to the current Session Factory.

Returns:
the database product name, see DatabaseProduct
Since:
4.0M1

getDatabaseProductName

@Deprecated
public DatabaseProduct getDatabaseProductName(XWikiContext context)
Deprecated. since 4.0M1 use getDatabaseProductName()

Returns:
the database product name

getSession

public org.hibernate.Session getSession(XWikiContext context)
This get's the current session. This is set in beginTransaction

Parameters:
context -

setSession

public void setSession(org.hibernate.Session session,
                       XWikiContext context)
Allows to set the current session in the context This is set in beginTransaction

Parameters:
session -
context -

getTransaction

public org.hibernate.Transaction getTransaction(XWikiContext context)
Allows to get the current transaction from the context This is set in beginTransaction

Parameters:
context -

setTransaction

public void setTransaction(org.hibernate.Transaction transaction,
                           XWikiContext context)
Allows to set the current transaction This is set in beginTransaction

Parameters:
transaction -
context -

shutdownHibernate

public void shutdownHibernate(XWikiContext context)
                       throws org.hibernate.HibernateException
Allows to shut down the hibernate configuration Closing all pools and connections

Parameters:
context -
Throws:
org.hibernate.HibernateException

updateSchema

public void updateSchema(XWikiContext context)
                  throws org.hibernate.HibernateException
Allows to update the schema to match the hibernate mapping

Parameters:
context -
Throws:
org.hibernate.HibernateException

updateSchema

public void updateSchema(XWikiContext context,
                         boolean force)
                  throws org.hibernate.HibernateException
Allows to update the schema to match the hibernate mapping

Parameters:
context -
force - defines wether or not to force the update despite the xwiki.cfg settings
Throws:
org.hibernate.HibernateException

getSchemaFromWikiName

protected String getSchemaFromWikiName(String wikiName,
                                       DatabaseProduct databaseProduct,
                                       XWikiContext context)
Convert wiki name in database/schema name.

Parameters:
wikiName - the wiki name to convert.
databaseProduct - the database engine type.
context - the XWiki context.
Returns:
the database/schema name.
Since:
XWiki Core 1.1.2, XWiki Core 1.2M2

getSchemaFromWikiName

protected String getSchemaFromWikiName(String wikiName,
                                       XWikiContext context)
Convert wiki name in database/schema name.

Need hibernate to be initialized.

Parameters:
wikiName - the wiki name to convert.
context - the XWiki context.
Returns:
the database/schema name.
Since:
XWiki Core 1.1.2, XWiki Core 1.2M2

getSchemaFromWikiName

protected String getSchemaFromWikiName(XWikiContext context)
Convert context's database in real database/schema name.

Need hibernate to be initialized.

Parameters:
context - the XWiki context.
Returns:
the database/schema name.
Since:
XWiki Core 1.1.2, XWiki Core 1.2M2

getSchemaUpdateScript

public String[] getSchemaUpdateScript(org.hibernate.cfg.Configuration config,
                                      XWikiContext context)
                               throws org.hibernate.HibernateException
This function gets the schema update scripts generated by comparing the current database with the current hibernate mapping config.

Parameters:
config -
context -
Throws:
org.hibernate.HibernateException

updateSchema

public void updateSchema(String[] createSQL,
                         XWikiContext context)
                  throws org.hibernate.HibernateException
Runs the update script on the current database

Parameters:
createSQL -
context -
Throws:
org.hibernate.HibernateException

updateSchema

public void updateSchema(BaseClass bclass,
                         XWikiContext context)
                  throws XWikiException,
                         org.hibernate.HibernateException
Custom Mapping This function update the schema based on the dynamic custom mapping provided by the class

Parameters:
bclass -
context -
Throws:
XWikiException
org.hibernate.HibernateException

checkHibernate

public void checkHibernate(XWikiContext context)
                    throws org.hibernate.HibernateException
Initializes hibernate

Parameters:
context -
Throws:
org.hibernate.HibernateException

isVirtual

protected boolean isVirtual(XWikiContext context)
Checks if this xwiki setup is virtual meaning if multiple wikis can be accessed using the same database pool

Parameters:
context - the XWiki context.
Returns:
true if multi-wiki, false otherwise.

setDatabase

public void setDatabase(org.hibernate.Session session,
                        XWikiContext context)
                 throws XWikiException
Virtual Wikis Allows to switch database connection

Parameters:
session -
context -
Throws:
XWikiException

escapeSchema

protected String escapeSchema(String schema,
                              XWikiContext context)
Escape schema name depending of the database engine.

Parameters:
schema - the schema name to escape
context - the XWiki context to get database engine identifier
Returns:
the escaped version

beginTransaction

public boolean beginTransaction(XWikiContext context)
                         throws XWikiException
Begins a transaction if the context does not contains any.

Parameters:
context - the current XWikiContext
Returns:
true if a new transaction has been created, false otherwise.
Throws:
XWikiException - if an error occurs while retrieving or creating a new session and transaction.

beginTransaction

@Deprecated
public boolean beginTransaction(boolean withTransaction,
                                           XWikiContext context)
                         throws XWikiException
Deprecated. since 4.0M1, use beginTransaction(SessionFactory, XWikiContext)

Begins a transaction

Parameters:
withTransaction - this argument is unused
context - the current XWikiContext
Returns:
true if a new transaction has been created, false otherwise.
Throws:
XWikiException - if an error occurs while retrieving or creating a new session and transaction.

beginTransaction

@Deprecated
public boolean beginTransaction(org.hibernate.SessionFactory sfactory,
                                           boolean withTransaction,
                                           XWikiContext context)
                         throws XWikiException
Deprecated. since 4.0M1, use beginTransaction(SessionFactory, XWikiContext)

Begins a transaction with a specific SessionFactory.

Parameters:
sfactory - the session factory used to begin a new session if none are available
withTransaction - this argument is unused
context - the current XWikiContext
Returns:
true if a new transaction has been created, false otherwise.
Throws:
XWikiException - if an error occurs while retrieving or creating a new session and transaction.

beginTransaction

public boolean beginTransaction(org.hibernate.SessionFactory sfactory,
                                XWikiContext context)
                         throws XWikiException
Begins a transaction with a specific SessionFactory.

Parameters:
sfactory - the session factory used to begin a new session if none are available
context - the current XWikiContext
Returns:
true if a new transaction has been created, false otherwise.
Throws:
XWikiException - if an error occurs while retrieving or creating a new session and transaction.

endTransaction

@Deprecated
public void endTransaction(XWikiContext context,
                                      boolean commit,
                                      boolean withTransaction)
                    throws org.hibernate.HibernateException
Deprecated. since 4.0M1, use endTransaction(XWikiContext, boolean)

Ends a transaction and close the session.

Parameters:
context - the current XWikiContext
commit - should we commit or not
withTransaction -
Throws:
org.hibernate.HibernateException

endTransaction

public void endTransaction(XWikiContext context,
                           boolean commit)
Ends a transaction and close the session.

Parameters:
context - the current XWikiContext
commit - should we commit or not

cleanUp

public void cleanUp(XWikiContext context)
Cleanup all sessions Used at the shutdown time

Parameters:
context -

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory sessionFactory)

getConfiguration

public org.hibernate.cfg.Configuration getConfiguration()

getConnections

public Map<String,String> getConnections()

getNbConnections

public int getNbConnections()

setNbConnections

public void setNbConnections(int nbConnections)

dynamicMappingTableName

public String dynamicMappingTableName(String className)
Return the name generated for a dynamic mapped object.

Parameters:
className - the classname of the object.
Returns:
a name in the form xwikicustom_space_class
Since:
4.0M1

getMapping

protected org.hibernate.cfg.Configuration getMapping(String className,
                                                     String customMapping)
Build a Configuration containing the provide mapping. Before 4.0M1, this function was called makeMapping. In 4.0M1, it enter in conflict with makeMapping(String, String)

Parameters:
className - the classname of the class to map.
customMapping - the custom mapping
Returns:
a new Configuration containing this mapping alone.
Since:
4.0M1

makeMapping

protected String makeMapping(String className,
                             String customMapping)
Build a new XML string to define the provided mapping. Since 4.0M1, the ids are longs, and a confitionnal mapping is made for Oracle.

Parameters:
className - the name of the class to map.
customMapping - the custom mapping
Returns:
a XML definition for the given mapping, using XWO_ID column for the object id.

execute

@Deprecated
public <T> T execute(XWikiContext context,
                                boolean bTransaction,
                                boolean doCommit,
                                XWikiHibernateBaseStore.HibernateCallback<T> cb)
          throws XWikiException
Deprecated. since 4.0M1, use execute(XWikiContext, boolean, HibernateCallback) or failSafeExecute(XWikiContext, boolean, HibernateCallback)

Execute method for operations in hibernate. spring like.

Parameters:
context - - used everywhere.
bTransaction - - should store use old transaction(false) or create new (true)
doCommit - - should store commit changes(if any), or rollback it.
cb - - callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error

failSafeExecute

public <T> T failSafeExecute(XWikiContext context,
                             boolean doCommit,
                             XWikiHibernateBaseStore.HibernateCallback<T> cb)
Execute method for operations in hibernate in an independent session (but not closing the current one if any). Never throw any error, but there is no warranty that the operation has been completed successfully.

Parameters:
context - - used everywhere.
doCommit - - should store commit changes(if any), or rollback it.
cb - - callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session), returns null if the callback throw an error.

execute

public <T> T execute(XWikiContext context,
                     boolean doCommit,
                     XWikiHibernateBaseStore.HibernateCallback<T> cb)
          throws XWikiException
Execute method for operations in hibernate. spring like.

Parameters:
context - - used everywhere.
doCommit - - should store commit changes(if any), or rollback it.
cb - - callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error

executeRead

@Deprecated
public <T> T executeRead(XWikiContext context,
                                    boolean bTransaction,
                                    XWikiHibernateBaseStore.HibernateCallback<T> cb)
              throws XWikiException
Deprecated. since 4.0M1, use executeRead(XWikiContext, HibernateCallback) or failSafeExecuteRead(XWikiContext, HibernateCallback)

Execute method for read-only operations in hibernate. spring like.

Parameters:
context - the current XWikiContext
bTransaction - this argument is unused
cb - the callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error
See Also:
execute(XWikiContext, boolean, HibernateCallback)

failSafeExecuteRead

public <T> T failSafeExecuteRead(XWikiContext context,
                                 XWikiHibernateBaseStore.HibernateCallback<T> cb)
Execute hibernate read-only operation in a independent session (but not closing the current one if any). Never throw any error, but there is no warranty that the operation has been completed successfully.

Parameters:
context - the current XWikiContext
cb - the callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session), returns null if the callback throw an error.
See Also:
failSafeExecute(XWikiContext, boolean, HibernateCallback)

executeRead

public <T> T executeRead(XWikiContext context,
                         XWikiHibernateBaseStore.HibernateCallback<T> cb)
              throws XWikiException
Execute method for read-only operations in hibernate. spring like.

Parameters:
context - - used everywhere.
cb - - callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error
See Also:
execute(XWikiContext, boolean, HibernateCallback)

executeWrite

@Deprecated
public <T> T executeWrite(XWikiContext context,
                                     boolean bTransaction,
                                     XWikiHibernateBaseStore.HibernateCallback<T> cb)
               throws XWikiException
Deprecated. since 4.0M1, use executeWrite(XWikiContext, HibernateCallback) or failSafeExecuteWrite(XWikiContext, HibernateCallback)

Execute method for read-write operations in hibernate. spring like.

Parameters:
context - the current XWikiContext
bTransaction - this argument is unused
cb - the callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error
See Also:
execute(XWikiContext, boolean, HibernateCallback)

failSafeExecuteWrite

public <T> T failSafeExecuteWrite(XWikiContext context,
                                  XWikiHibernateBaseStore.HibernateCallback<T> cb)
Execute hibernate read-only operation in a independent session (but not closing the current one if any). Never throw any error, but there is no warranty that the operation has been completed successfully.

Parameters:
context - the current XWikiContext
cb - the callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
See Also:
execute(XWikiContext, boolean, HibernateCallback)

executeWrite

public <T> T executeWrite(XWikiContext context,
                          XWikiHibernateBaseStore.HibernateCallback<T> cb)
               throws XWikiException
Execute method for read-write operations in hibernate. spring like.

Parameters:
context - the current XWikiContext
cb - the callback to execute
Returns:
XWikiHibernateBaseStore.HibernateCallback.doInHibernate(Session)
Throws:
XWikiException - if any error
See Also:
execute(XWikiContext, boolean, HibernateCallback)

isInSchemaMode

protected boolean isInSchemaMode()
Returns:
true if the user has configured Hibernate to use XWiki in schema mode (vs database mode)
Since:
4.5M1


Copyright © 2004-2013 XWiki. All Rights Reserved.