org.opencms.db.jpa
Class CmsSqlManager

java.lang.Object
  extended by org.opencms.db.CmsSqlManager
      extended by org.opencms.db.jpa.CmsSqlManager

public class CmsSqlManager
extends CmsSqlManager

JPA database server implementation of the SQL manager interface.

Since:
8.0.0

Field Summary
static int DEFAULT_ENTITY_MANAGER_POOL_SIZE
          Default pool size for EntityManager instances.
static String JPA_PERSISTENCE_UNIT
          The persistence unit name in the persistence.xml file for OpenCms'es persistence classes.
static String JPA_POOL_SIZE_PROPERTY_NAME
          Property name for pool size configuration.
static String KEY_DRIVER_CLASS_NAME
          The fully qualified Java class name of the JDBC driver to be used.
static String KEY_INITIAL_SIZE
          The initial number of connections that are created when the pool is started.
static String KEY_MAX_ACTIVE
          The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.
static String KEY_MAX_IDLE
          The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.
static String KEY_MAX_WAIT
          The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or <= 0 to wait indefinitely.
static String KEY_MIN_EVICTABLE_IDLE_TIME
          The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).
static String KEY_MIN_IDLE
          The minimum number of active connections that can remain idle in the pool, without extra ones being created, or 0 to create none.
static String KEY_NUM_TESTS_PER_EVICTION_RUN
          The number of objects to examine during each run of the idle object evictor thread (if any).
static String KEY_PASS
          The connection password to be passed to our JDBC driver to establish a connection.
static String KEY_PREP_STATEMENTS
          Prepared statement pooling for this pool.
static String KEY_TEST_ON_BORROW
          The indication of whether objects will be validated before being borrowed from the pool.
static String KEY_TEST_WHILE_IDLE
          The indication of whether objects will be validated by the idle object evictor (if any).
static String KEY_TIME_BETWEEN_EVICTION_RUNS
          The number of milliseconds to sleep between runs of the idle object evictor thread.
static String KEY_URL
          The connection URL to be passed to our JDBC driver to establish a connection.
static String KEY_USER
          The connection username to be passed to our JDBC driver to establish a connection.
static String KEY_VALIDATION_QUERY
          The SQL query that will be used to validate connections from this pool before returning them to the caller.
protected  Hashtable<String,String> m_cachedQueries
          A map to cache queries with replaced search patterns.
protected static org.apache.commons.pool.ObjectPool m_openCmsEmPool
          Poll of EntityManager instances for OpenCms.
protected  Hashtable<String,String> m_queries
          A map holding all JPQL queries.
protected  Hashtable<String,String> m_queriesWithParameters
          Queries with parameters.
protected static String OFFLINE_PROJECT
          The value to be replaced with for online project.
protected static String ONLINE_PROJECT
          The value to be replaced with for online project.
protected static String QUERY_PROJECT_SEARCH_PATTERN
          A pattern being replaced in JPQL queries to generate JPQL queries to access online/offline tables.
protected static String QUERY_PROJECT_STRING
          String which indicates project parameter in the queries.
 
Constructor Summary
CmsSqlManager()
          The constructor.
 
Method Summary
static javax.persistence.EntityManager createEntityManager(String unitName)
          Create EntityManager instance for given unit name.
 javax.persistence.Query createNativeQuery(CmsDbContext dbc, CmsUUID projectId, String queryKey)
          Returns a Query for a EntityManagerContext specified by the key of a SQL query and the project-ID.
 javax.persistence.Query createQuery(CmsDbContext dbc, CmsProject project, String queryKey)
          Returns a Query for a JDBC connection specified by the key of a JPQL query and the CmsProject.
 javax.persistence.Query createQuery(CmsDbContext dbc, CmsUUID projectId, String queryKey)
          Returns a Query for a EntityManagerContext specified by the key of a JPQL query and the project-ID.
 javax.persistence.Query createQuery(CmsDbContext dbc, String queryKey)
          Returns a Query for a EntityManagerContext specified by the key of a JPQL query.
 javax.persistence.Query createQueryFromJPQL(CmsDbContext dbc, String query)
          Returns a Query for a JDBC connection specified by the JPQL query.
 javax.persistence.Query createQueryWithParametersFromJPQL(CmsDbContext dbc, String query, List<Object> params)
          Returns a Query for a JDBC connection specified by the JPQL query.
static void destroy()
          Close all instances of EntityManagerFactory.
<T> T
find(CmsDbContext dbc, Class<T> cls, Object o)
          Finds an object in the db and returns it.
static javax.persistence.EntityManager getEntityManager()
          Creates EntityManager from OpenCms's factory.
 javax.persistence.EntityManager getEntityManager(CmsDbContext dbc)
          Returns the entity manager from the current dbc.
static javax.persistence.EntityManagerFactory getFactory(String unitName)
          Returns EntityManagerFactory for given unit name.
static CmsSqlManager getInstance(String classname)
          Creates a new instance of a SQL manager.
static void init(CmsParameterConfiguration config)
          Initialize the static part of the class.
protected  void loadQueryProperties(String propertyFilename)
          Loads a Java properties hash containing JPQL queries.
 void persist(CmsDbContext dbc, Object o)
          Persists an object.
 String readQuery(CmsProject project, String queryKey)
          Searches for the JPQL query with the specified key and CmsProject.
 String readQuery(CmsUUID projectId, String queryKey)
          Searches for the JPQL query with the specified key and project-ID.
 String readQuery(String queryKey)
          Searches for the JPQL query with the specified key.
 void remove(CmsDbContext dbc, Object o)
          Removes an object from the db.
protected  void replaceQuerySearchPatterns()
          Replaces patterns ${XXX} by another property value, if XXX is a property key with a value.
static void returnEntityManager(javax.persistence.EntityManager em)
          Returns EntityManager instance from OpenCms, back to pool.
 String validateEmpty(String value)
          Replaces null or empty Strings with a String with one space character " ".
 
Methods inherited from class org.opencms.db.CmsSqlManager
getActiveConnections, getConnection, getConnectionByUrl, getDbPoolUrls, getDefaultDbPoolName, getIdleConnections
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ENTITY_MANAGER_POOL_SIZE

public static final int DEFAULT_ENTITY_MANAGER_POOL_SIZE
Default pool size for EntityManager instances.

See Also:
Constant Field Values

JPA_PERSISTENCE_UNIT

public static final String JPA_PERSISTENCE_UNIT
The persistence unit name in the persistence.xml file for OpenCms'es persistence classes.

See Also:
Constant Field Values

JPA_POOL_SIZE_PROPERTY_NAME

public static final String JPA_POOL_SIZE_PROPERTY_NAME
Property name for pool size configuration.

See Also:
Constant Field Values

KEY_DRIVER_CLASS_NAME

public static final String KEY_DRIVER_CLASS_NAME
The fully qualified Java class name of the JDBC driver to be used.

See Also:
Constant Field Values

KEY_INITIAL_SIZE

public static final String KEY_INITIAL_SIZE
The initial number of connections that are created when the pool is started.

See Also:
Constant Field Values

KEY_MAX_ACTIVE

public static final String KEY_MAX_ACTIVE
The maximum number of active connections that can be allocated from this pool at the same time, or negative for no limit.

See Also:
Constant Field Values

KEY_MAX_IDLE

public static final String KEY_MAX_IDLE
The maximum number of connections that can remain idle in the pool, without extra ones being released, or negative for no limit.

See Also:
Constant Field Values

KEY_MAX_WAIT

public static final String KEY_MAX_WAIT
The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception, or <= 0 to wait indefinitely.

See Also:
Constant Field Values

KEY_MIN_EVICTABLE_IDLE_TIME

public static final String KEY_MIN_EVICTABLE_IDLE_TIME
The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).

See Also:
Constant Field Values

KEY_MIN_IDLE

public static final String KEY_MIN_IDLE
The minimum number of active connections that can remain idle in the pool, without extra ones being created, or 0 to create none.

See Also:
Constant Field Values

KEY_NUM_TESTS_PER_EVICTION_RUN

public static final String KEY_NUM_TESTS_PER_EVICTION_RUN
The number of objects to examine during each run of the idle object evictor thread (if any).

See Also:
Constant Field Values

KEY_PASS

public static final String KEY_PASS
The connection password to be passed to our JDBC driver to establish a connection.

See Also:
Constant Field Values

KEY_PREP_STATEMENTS

public static final String KEY_PREP_STATEMENTS
Prepared statement pooling for this pool.

See Also:
Constant Field Values

KEY_TEST_ON_BORROW

public static final String KEY_TEST_ON_BORROW
The indication of whether objects will be validated before being borrowed from the pool.

See Also:
Constant Field Values

KEY_TEST_WHILE_IDLE

public static final String KEY_TEST_WHILE_IDLE
The indication of whether objects will be validated by the idle object evictor (if any).

See Also:
Constant Field Values

KEY_TIME_BETWEEN_EVICTION_RUNS

public static final String KEY_TIME_BETWEEN_EVICTION_RUNS
The number of milliseconds to sleep between runs of the idle object evictor thread.

See Also:
Constant Field Values

KEY_URL

public static final String KEY_URL
The connection URL to be passed to our JDBC driver to establish a connection.

See Also:
Constant Field Values

KEY_USER

public static final String KEY_USER
The connection username to be passed to our JDBC driver to establish a connection.

See Also:
Constant Field Values

KEY_VALIDATION_QUERY

public static final String KEY_VALIDATION_QUERY
The SQL query that will be used to validate connections from this pool before returning them to the caller.

See Also:
Constant Field Values

m_openCmsEmPool

protected static org.apache.commons.pool.ObjectPool m_openCmsEmPool
Poll of EntityManager instances for OpenCms.


OFFLINE_PROJECT

protected static final String OFFLINE_PROJECT
The value to be replaced with for online project.

See Also:
Constant Field Values

ONLINE_PROJECT

protected static final String ONLINE_PROJECT
The value to be replaced with for online project.

See Also:
Constant Field Values

QUERY_PROJECT_SEARCH_PATTERN

protected static final String QUERY_PROJECT_SEARCH_PATTERN
A pattern being replaced in JPQL queries to generate JPQL queries to access online/offline tables.

See Also:
Constant Field Values

QUERY_PROJECT_STRING

protected static final String QUERY_PROJECT_STRING
String which indicates project parameter in the queries.

See Also:
Constant Field Values

m_cachedQueries

protected Hashtable<String,String> m_cachedQueries
A map to cache queries with replaced search patterns.


m_queries

protected Hashtable<String,String> m_queries
A map holding all JPQL queries.


m_queriesWithParameters

protected Hashtable<String,String> m_queriesWithParameters
Queries with parameters.

Constructor Detail

CmsSqlManager

public CmsSqlManager()
              throws CmsDbException
The constructor.

Throws:
CmsDbException - if the manager is not initialized yet
Method Detail

createEntityManager

public static javax.persistence.EntityManager createEntityManager(String unitName)
Create EntityManager instance for given unit name. If factory for this unit is not already created it creates it.

Parameters:
unitName - - the unit name in the persistence.xml file
Returns:
EntityManager instance for given unit name

destroy

public static void destroy()
Close all instances of EntityManagerFactory.


getEntityManager

public static javax.persistence.EntityManager getEntityManager()
Creates EntityManager from OpenCms's factory.

Returns:
EntityManager created from OpenCms's factory

getFactory

public static javax.persistence.EntityManagerFactory getFactory(String unitName)
Returns EntityManagerFactory for given unit name. If the factory does not already exists it creates one.

Parameters:
unitName - - the unit name in the persistence.xml file
Returns:
EntityManagerFactory for given unit name

getInstance

public static CmsSqlManager getInstance(String classname)
Creates a new instance of a SQL manager.

Parameters:
classname - the classname of the SQL manager
Returns:
a new instance of the SQL manager

init

public static void init(CmsParameterConfiguration config)
Initialize the static part of the class.

Parameters:
config - the combined configuration of "opencms.properties" and the "persistence.xml"

returnEntityManager

public static void returnEntityManager(javax.persistence.EntityManager em)
Returns EntityManager instance from OpenCms, back to pool.

Parameters:
em - - instance which returns back to pool of OpenCmsJPAPool persistence context.

createNativeQuery

public javax.persistence.Query createNativeQuery(CmsDbContext dbc,
                                                 CmsUUID projectId,
                                                 String queryKey)
Returns a Query for a EntityManagerContext specified by the key of a SQL query and the project-ID.

Parameters:
dbc - the the db context
projectId - the ID of the specified CmsProject
queryKey - the key of the SQL query
Returns:
Query a new Query containing the pre-compiled SQL statement

createQuery

public javax.persistence.Query createQuery(CmsDbContext dbc,
                                           CmsProject project,
                                           String queryKey)
Returns a Query for a JDBC connection specified by the key of a JPQL query and the CmsProject.

Parameters:
dbc - the db context
project - the specified CmsProject
queryKey - the key of the JPQL query
Returns:
Query a new Query containing the pre-compiled JPQL statement

createQuery

public javax.persistence.Query createQuery(CmsDbContext dbc,
                                           CmsUUID projectId,
                                           String queryKey)
Returns a Query for a EntityManagerContext specified by the key of a JPQL query and the project-ID.

Parameters:
dbc - the dbc context
projectId - the ID of the specified CmsProject
queryKey - the key of the JPQL query
Returns:
Query a new Query containing the pre-compiled JPQL statement

createQuery

public javax.persistence.Query createQuery(CmsDbContext dbc,
                                           String queryKey)
Returns a Query for a EntityManagerContext specified by the key of a JPQL query.

Parameters:
dbc - the db context
queryKey - the key of the JPQL query
Returns:
Query a new Query containing the pre-compiled JPQL statement

createQueryFromJPQL

public javax.persistence.Query createQueryFromJPQL(CmsDbContext dbc,
                                                   String query)
Returns a Query for a JDBC connection specified by the JPQL query.

Parameters:
dbc - the db context object
query - the JPQL query
Returns:
Query a new Query containing the pre-compiled JPQL statement

createQueryWithParametersFromJPQL

public javax.persistence.Query createQueryWithParametersFromJPQL(CmsDbContext dbc,
                                                                 String query,
                                                                 List<Object> params)
Returns a Query for a JDBC connection specified by the JPQL query.

Parameters:
dbc - the db context object
query - the JPQL query
params - the parameters to insert into the query
Returns:
Query a new Query containing the pre-compiled JPQL statement

find

public <T> T find(CmsDbContext dbc,
                  Class<T> cls,
                  Object o)
Finds an object in the db and returns it.

Type Parameters:
T - the class to be returned
Parameters:
dbc - the current dbc
cls - the class information of the object to be returned
o - the object to search for
Returns:
returns the found object

getEntityManager

public javax.persistence.EntityManager getEntityManager(CmsDbContext dbc)
Returns the entity manager from the current dbc.

Parameters:
dbc - the current dbc
Returns:
the according entity manager

persist

public void persist(CmsDbContext dbc,
                    Object o)
Persists an object.

Parameters:
dbc - the current dbc
o - the object to persist

readQuery

public String readQuery(CmsProject project,
                        String queryKey)
Searches for the JPQL query with the specified key and CmsProject.

Parameters:
project - the specified CmsProject
queryKey - the key of the JPQL query
Returns:
the the JPQL or JPQL query in this property list with the specified key

readQuery

public String readQuery(CmsUUID projectId,
                        String queryKey)
Searches for the JPQL query with the specified key and project-ID.

For projectIds ≠ 0, the pattern QUERY_PROJECT_SEARCH_PATTERN in table names of queries is replaced with "Online" or "Offline" to choose the right database tables for JPQL queries that are project dependent!

Parameters:
projectId - the ID of the specified CmsProject
queryKey - the key of the JPQL query
Returns:
the the JPQL query in this property list with the specified key

readQuery

public String readQuery(String queryKey)
Searches for the JPQL query with the specified key.

Parameters:
queryKey - the JPQL query key
Returns:
the the JPQL query in this property list with the specified key

remove

public void remove(CmsDbContext dbc,
                   Object o)
Removes an object from the db.

Parameters:
dbc - the current dbc
o - the object to remove

validateEmpty

public String validateEmpty(String value)
Replaces null or empty Strings with a String with one space character " ".

Parameters:
value - the string to validate
Returns:
the validate string or a String with one space character if the validated string is null or empty

loadQueryProperties

protected void loadQueryProperties(String propertyFilename)
Loads a Java properties hash containing JPQL queries.

Parameters:
propertyFilename - the package/filename of the properties hash

replaceQuerySearchPatterns

protected void replaceQuerySearchPatterns()
Replaces patterns ${XXX} by another property value, if XXX is a property key with a value.