FileDocCategorySizeDatePackage
PersistenceManagerFactoryImpl.javaAPI DocGlassfish v2 API32281Mon Jun 04 15:11:40 BST 2007com.sun.jdo.spi.persistence.support.sqlstore.impl

PersistenceManagerFactoryImpl

public class PersistenceManagerFactoryImpl extends Object implements PersistenceManagerFactory
author
Marina Vatkina
version
0.1

Fields Summary
private String
URL
PersistenceManagerFactory properties
private String
userName
private String
password
private String
driverName
private ConnectionFactory
connectionFactory
private Object
dataSource
private String
connectionFactoryName
private String
identifier
private int
connectionMaxPool
private int
connectionMinPool
private int
connectionMsInterval
private int
connectionLoginTimeout
private int
connectionMsWait
private int
txIsolation
private PrintWriter
connectionLogWriter
private boolean
optimistic
private boolean
retainValues
private boolean
nontransactionalRead
private boolean
ignoreCache
private int
queryTimeout
private int
updateTimeout
private int
maxPool
private int
minPool
private boolean
supersedeDeletedInstance
private boolean
requireCopyObjectId
private boolean
requireTrackedSCO
private static final int
NOT_SET
private static final int
SET_AS_CONNECTIONFACTORY
private static final int
SET_AS_DATASOURCE
private int
providedConnectionFactory
flag for ConnectionFactory setup
private transient SQLPersistenceManagerFactory
pmFactory
Reference to the internal PersistenceManagerFactory implementation to deligate actual stuff
private static final ResourceBundle
messages
I18N message handler
Constructors Summary
public PersistenceManagerFactoryImpl()
Creates new PersistenceManagerFactoryImpl without any user info


	       	 
	  
            EJBHelper.setPersistenceManagerFactoryDefaults(this);
	
public PersistenceManagerFactoryImpl(String URL, String userName, String password, String driverName)
Creates new PersistenceManagerFactoryImpl with user info

param
URL connection URL
param
userName database user
param
password database user password
param
driverName driver name

            EJBHelper.setPersistenceManagerFactoryDefaults(this);

		this.URL = URL;
		this.userName = userName;
		this.password = password;
		this.driverName = driverName;

	
Methods Summary
private voidassertConnectionWait()
INTERNAL Asserts that MsWait and MsInterval are properly configured

		if ( connectionMsWait < 0 )
		{
			throw new JDOUserException(I18NHelper.getMessage(messages,
                                             "connection.connectionmanager.mswaitvalue")); // NOI18N
		}
		else if ( connectionMsInterval < 0 ||
			connectionMsInterval > connectionMsWait ||
			(connectionMsWait > 0 && connectionMsInterval == 0) )
		{
				throw new JDOUserException(I18NHelper.getMessage(messages,
                                             "connection.connectionmanager.msintervalvalue")); // NOI18N
		}
	
private voidassertNotConfigured()
INTERNAL Asserts that change to the property is allowed

		if ( pmFactory != null) {
			throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
                                "persistencemanagerfactoryimpl.configured")); //NOI18N
		}
	
private booleanequalBooleanProperties(PersistenceManagerFactory pmf)
Compares boolean setting on 2 PersistenceManagerFactory instances.

param
pmf the PersistenceManagerFactory instance to compare with this instance.

            return (pmf.getOptimistic() == optimistic &&
                       pmf.getRetainValues() == retainValues &&
                       pmf.getNontransactionalRead() == nontransactionalRead &&
                       pmf.getIgnoreCache() == ignoreCache &&
                       pmf.getSupersedeDeletedInstance() == supersedeDeletedInstance &&
                       pmf.getRequireCopyObjectId() == requireCopyObjectId &&
                       pmf.getRequireTrackedSCO() == requireTrackedSCO
                   );

        
public booleanequals(java.lang.Object obj)
Determines whether obj is a PersistenceManagerFactoryImpl with the same configuration

param
obj The possibly null object to check.
return
true if obj is equal to this PersistenceManagerFactoryImpl; false otherwise.

       		if ((obj == null) || !(obj instanceof PersistenceManagerFactoryImpl)) {
			return false;
		}
       		PersistenceManagerFactoryImpl pmf = (PersistenceManagerFactoryImpl)obj;

		if (pmf.providedConnectionFactory == this.providedConnectionFactory) {
			if (pmf.providedConnectionFactory == SET_AS_CONNECTIONFACTORY) {
	  	 		return (pmf.connectionFactory.equals(this.connectionFactory) &&
                                       equalBooleanProperties(pmf));

			} else if (pmf.providedConnectionFactory == SET_AS_DATASOURCE) {
				return (pmf.dataSource.equals(this.dataSource) &&
                                       equalBooleanProperties(pmf));

			} else if (pmf.connectionFactoryName != null) {
				return (pmf.connectionFactoryName.equals(this.connectionFactoryName) &&
                                       equalBooleanProperties(pmf));

			}
			return (pmf.URL.equals(this.URL) && pmf.userName.equals(this.userName) &&
				pmf.password.equals(this.password) &&
				pmf.driverName.equals(this.driverName) &&
                                       equalBooleanProperties(pmf));
		}
		return false;
    	
public java.lang.StringgetConnectionDriverName()
Returns JDBC driver name

return
driver name

		if (connectionFactory != null)
                        return connectionFactory.getDriverName();
		return driverName;

	
public java.lang.ObjectgetConnectionFactory()
Returns ConnectionFactory

return
ConnectionFactory

		if (dataSource != null)
			return dataSource;

		return connectionFactory;
	
public java.lang.StringgetConnectionFactoryName()
Returns ConnectionFactory name

return
ConnectionFactoryName

		return connectionFactoryName;
	
public java.io.PrintWritergetConnectionLogWriter()
Returns the LogWriter to which messages should be sent

return
LogWriter

		return connectionLogWriter;
	
public intgetConnectionLoginTimeout()
Returns the number of seconds to wait for a new connection to be established to the data source

return
wait time in seconds

		if (connectionFactory != null) {
                        return connectionFactory.getLoginTimeout();
/*
		} else if (dataSource != null) {
                        return dataSource.getLoginTimeout();
*/
		} else {
			return connectionLoginTimeout;
		}
	
public intgetConnectionMaxPool()
Returns maximum number of connections in the connection pool

return
connectionMaxPool

		if (connectionFactory != null)
                        return connectionFactory.getMaxPool();

		return connectionMaxPool;
	
public intgetConnectionMinPool()
Returns minimum number of connections in the connection pool

return
connectionMinPool

		if (connectionFactory != null)
                        return connectionFactory.getMinPool();
		return connectionMinPool;
	
public intgetConnectionMsInterval()
Returns the amount of time, in milliseconds, between the connection manager's attempts to get a pooled connection.

return
the length of the interval between tries in milliseconds

		if (connectionFactory != null)
                        return connectionFactory.getMsInterval();

		return connectionMsInterval;
	
public intgetConnectionMsWait()
Returns the number of milliseconds to wait for an available connection from the connection pool before throwing an exception

return
number in milliseconds

		if (connectionFactory != null)
                        return connectionFactory.getMsWait();

		return connectionMsWait;
	
public intgetConnectionTransactionIsolation()
Returns current transaction isolation level for connections of this PersistenceManagerFactory.

return
the current transaction isolation mode value as java.sql.Connection.TRANSACTION_*

		if (connectionFactory != null)
                        return connectionFactory.getTransactionIsolation();

		return txIsolation;
	
public java.lang.StringgetConnectionURL()
Returns connection URL

return
connection URL

		if (connectionFactory != null)
                        return connectionFactory.getURL();

		return URL;

	
public java.lang.StringgetConnectionUserName()
Returns database user name

return
current database user name

		if (connectionFactory != null)
			return connectionFactory.getUserName();

		return userName;

	
public java.lang.StringgetIdentifier()
Gets Identifier. An identifier is a string that user can use to identify the PersistenceManagerFactory in a given environment. Identifier can be particularly useful in an environment where multiple PersistenceManagerFactories are initialized in a system.

return
identifier

		return identifier;
	
public booleangetIgnoreCache()
Returns the boolean value for the flag that allows the user to request that queries be optimized to return approximate results by ignoring changed values in the cache.

return
boolean setting for the flag

		return ignoreCache;
	
public intgetMaxPool()
Returns maximum number of PersistenceManager instances in the pool

return
maxPool

		return maxPool;
	
public intgetMinPool()
Returns minimum number of PersistenceManager instances in the pool

return
minPool

		return minPool;
	
public booleangetNontransactionalRead()
Returns the boolean value for the flag that allows non-transactional instances to be managed in the cache.

return
boolean setting for the flag

		return nontransactionalRead;
	
public booleangetOptimistic()
Returns the boolean value of the optimistic flag for all PersistenceManagers

return
boolean optimistic flag

		return optimistic;
	
public PersistenceManagergetPersistenceManager()
Creates new PersistenceManager without extra info

return
the persistence manager

		return getPersistenceManager(null, null);
	
public PersistenceManagergetPersistenceManager(java.lang.String username, java.lang.String passwd)
Creates new PersistenceManager with specific username and password. Used to call ConnectionFactory.getConnection(String, String)

param
username datasource user
param
passwd datasource user password
return
the persistence manager

	    synchronized (this) {

		if (pmFactory == null) {
		    // Nothing there yet. Check and create
			if (providedConnectionFactory == NOT_SET) {

			    if (connectionFactoryName == null) {

				// Validate that MsWait/MsInterval are correct
				assertConnectionWait();

				// only PMFactory was configured
				// Create a default ConnectionFactoryImpl and
				// set all the parameters. With 1st connection
				// which happens during configuration od SqlStore
				// the actaul connectionManager will be created
				connectionFactory = new ConnectionFactoryImpl();

				connectionFactory.setURL(URL);
				connectionFactory.setUserName(userName);
				connectionFactory.setPassword(password);
                                connectionFactory.setDriverName(driverName);
                                connectionFactory.setMinPool(connectionMinPool);
                                connectionFactory.setMaxPool(connectionMaxPool);

				// MsWait MUST be set BEFORE MsInterval
				connectionFactory.setMsWait(this.connectionMsWait);
				connectionFactory.setMsInterval(this.connectionMsInterval);
				connectionFactory.setLogWriter(this.connectionLogWriter);
				connectionFactory.setLoginTimeout(this.connectionLoginTimeout);
				if (txIsolation > 0)
					connectionFactory.setTransactionIsolation(txIsolation);
			    } else {
				// Do JNDI lookup
				try {
					javax.naming.InitialContext ctx =
						(javax.naming.InitialContext) Class.forName("javax.naming.InitialContext").newInstance(); //NOI18N
                                	Object o = ctx.lookup(connectionFactoryName);
					if (EJBHelper.isManaged() || (o instanceof DataSource))
                                		dataSource = o;
					else if (o instanceof ConnectionFactory)
                                		connectionFactory = (ConnectionFactory) o;
					else
						throw new JDOUserException(I18NHelper.getMessage(
							messages,
                                                "persistencemanagerfactoryimpl.wrongtype")); //NOI18N

				} catch (JDOException e) {
					throw e; 	// rethrow it.

				} catch (ClassNotFoundException e) {
					throw new JDOUserException(I18NHelper.getMessage(messages,
                                                "persistencemanagerfactoryimpl.initialcontext")); //NOI18N

				} catch (Exception e) {
					throw new JDOUserException(I18NHelper.getMessage(messages,
                                		"persistencemanagerfactoryimpl.lookup"), e); //NOI18N
				}
			    }
			}

			//If identifier is not yet set, set it to name of connection factory
			if(getIdentifier() == null) {
				setIdentifier(getConnectionFactoryName());
			}
			// create new
			pmFactory = new SQLPersistenceManagerFactory(this);
	    		// Check EJBHelper
			pmFactory =
                            (SQLPersistenceManagerFactory)EJBHelper.replaceInternalPersistenceManagerFactory(pmFactory);

		    }
		} // end synchronized (this)

		// Should not be called
		if (username != null && connectionFactory != null) {
			throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
				"persistencemanagerfactoryimpl.notsupported")); //NOI18N
		}

		return pmFactory.getPersistenceManager(username, passwd);

	
public java.util.PropertiesgetProperties()
Returns non-operational properties to be available to the application via a Properties instance.

return
Properties object

		if (pmFactory != null)
		{
			return pmFactory.getProperties();
		}
		return RuntimeVersion.getVendorProperties(
			"/com/sun/jdo/spi/persistence/support/sqlstore/sys.properties"); //NOI18N
	
public intgetQueryTimeout()
Gets the number of seconds to wait for a query statement to execute in the datastore associated with this PersistenceManagerFactory.

return
timout value in seconds; zero means unlimited

		return queryTimeout;
	
public booleangetRequireCopyObjectId()
Returns the default value of the requireCopyObjectId flag for this PersistenceManagerFactoryImpl. If set to false, the PersistenceManager will not create a copy of an ObjectId for PersistenceManager.getObjectId(Object pc) and PersistenceManager.getObjectById(Object oid) requests.

see
PersistenceManager#getObjectId(Object pc)
see
PersistenceManager#getObjectById(Object oid)
return
boolean requireCopyObjectId flag

        return requireCopyObjectId;
    
public booleangetRequireTrackedSCO()
Returns the boolean value of the requireTrackedSCO flag. If set to false, by default the PersistenceManager will not create tracked SCO instances for new persistent instances at commit with retainValues set to true and while retrieving data from a datastore.

return
boolean requireTrackedSCO flag

        return requireTrackedSCO;
    
public booleangetRetainValues()
Returns the boolean value for the flag that will not cause the eviction of persistent instances after transaction completion.

return
boolean setting for the flag

		return retainValues;
	
public booleangetSupersedeDeletedInstance()
Returns the boolean value of the supersedeDeletedInstance flag for all PersistenceManagers. If set to true, deleted instances are allowed to be replaced with persistent-new instances with the equal Object Id.

return
boolean supersedeDeletedInstance flag

        return supersedeDeletedInstance;
    
public intgetUpdateTimeout()
Gets the number of seconds to wait for an update statement to execute in the datastore associated with this PersistenceManagerFactory.

return
timout value in seconds; zero means unlimited

                return updateTimeout;
        
public inthashCode()
Computes the hash code of this PersistenceManagerFactory.

return
A hash code of the owning PersistenceManagerFactory as an int.

                if (providedConnectionFactory == SET_AS_CONNECTIONFACTORY) {
			return connectionFactory.hashCode();
                } else if (providedConnectionFactory == SET_AS_DATASOURCE) {
			return dataSource.hashCode();
		} else if (connectionFactoryName != null) {
			return connectionFactoryName.hashCode();
                }
		return URL.hashCode() + userName.hashCode() + password.hashCode() + driverName.hashCode();
        
public voidsetBooleanProperty(java.lang.String name, boolean value)
Sets default value of a known boolean property.

param
name the name of the property to be set.
param
value the default boolean value.

           // These if-else statements will be replaced by the JDO implementation...
           if (name.equals("optimistic")) { // NOI18N
               setOptimistic(value);

           } else if (name.equals("retainValues")) { // NOI18N
               setRetainValues(value);

           } else if (name.equals("nontransactionalRead")) { // NOI18N
               setNontransactionalRead(value);

           } else if (name.equals("ignoreCache")) { // NOI18N
               setIgnoreCache(value);

           } else if (name.equals("supersedeDeletedInstance")) { // NOI18N
               setSupersedeDeletedInstance(value);

           } else if (name.equals("requireCopyObjectId")) { // NOI18N
               setRequireCopyObjectId(value);

           } else if (name.equals("requireTrackedSCO")) { // NOI18N
               setRequireTrackedSCO(value);

           } // else ignore it.

	
public voidsetConnectionDriverName(java.lang.String driverName)
Sets JDBC driver name

param
driverName JDBC driver name

		assertNotConfigured();
		this.driverName = driverName;
	
public voidsetConnectionFactory(java.lang.Object connectionFactory)
Sets ConnectionFactory that can be one of two types: ConnectionFactory or javax.sql.DataSource

param
connectionFactory as java.lang.Object

		assertNotConfigured();
		if (connectionFactory == null) {
			this.connectionFactory = null;
			this.dataSource = null;
			providedConnectionFactory = NOT_SET;

		} else {
			if (EJBHelper.isManaged() || (connectionFactory instanceof DataSource)) {
				this.dataSource = connectionFactory;
				providedConnectionFactory = SET_AS_DATASOURCE;
			} else if (connectionFactory instanceof ConnectionFactory) {
				this.connectionFactory = (ConnectionFactory)connectionFactory;
				providedConnectionFactory = SET_AS_CONNECTIONFACTORY;
			} else {
				throw new JDOUserException(I18NHelper.getMessage( messages,
                                	"persistencemanagerfactoryimpl.wrongtype")); //NOI18N
			}
		}
	
public voidsetConnectionFactoryName(java.lang.String connectionFactoryName)
Sets ConnectionFactory name

param
connectionFactoryName ConnectionFactory name

		assertNotConfigured();
		this.connectionFactoryName = connectionFactoryName;
	
public voidsetConnectionLogWriter(java.io.PrintWriter pw)
Sets the LogWriter to which messages should be sent

param
pw LogWriter

		assertNotConfigured();
		this.connectionLogWriter = pw;
	
public voidsetConnectionLoginTimeout(int LoginTimeout)
Sets the number of seconds to wait for a new connection to be established to the data source

param
LoginTimeout wait time in seconds

		assertNotConfigured();
		this.connectionLoginTimeout = LoginTimeout;
	
public voidsetConnectionMaxPool(int MaxPool)
Sets maximum number of connections in the connection pool

param
MaxPool maximum number of connections

		assertNotConfigured();
		this.connectionMaxPool = MaxPool;
	
public voidsetConnectionMinPool(int MinPool)
Sets minimum number of connections in the connection pool

param
MinPool minimum number of connections

		assertNotConfigured();
		this.connectionMinPool = MinPool;
	
public voidsetConnectionMsInterval(int MsInterval)
Sets the amount of time, in milliseconds, between the connection manager's attempts to get a pooled connection.

param
MsInterval the interval between attempts to get a database connection, in milliseconds.

		assertNotConfigured();
		this.connectionMsInterval = MsInterval;
	
public voidsetConnectionMsWait(int MsWait)
Sets the number of milliseconds to wait for an available connection from the connection pool before throwing an exception

param
MsWait number in milliseconds

		assertNotConfigured();
		this.connectionMsWait = MsWait;
	
public voidsetConnectionPassword(java.lang.String password)
Sets database user password

param
password database user password

		assertNotConfigured();
		this.password = password;
	
public voidsetConnectionTransactionIsolation(int level)
Sets transaction isolation level for all connections of this PersistenceManagerFactory. All validation is done by java.sql.Connection itself, so e.g. while Oracle will not allow to set solation level to TRANSACTION_REPEATABLE_READ, this method does not have any explicit restrictions

param
level - one of the java.sql.Connection.TRANSACTION_* isolation values

		assertNotConfigured();
		txIsolation = level;
	
public voidsetConnectionURL(java.lang.String URL)
Sets JDBC connection URL

param
URL connection URL

		assertNotConfigured();
		this.URL = URL;
	
public voidsetConnectionUserName(java.lang.String userName)
Sets database user

param
userName database user

		assertNotConfigured();
		this.userName = userName;
	
public voidsetIdentifier(java.lang.String identifier)
Sets Identifier. An identifier is a string that user can use to identify the PersistenceManagerFactory in a given environment. Identifier can be particularly useful in an environment where multiple PersistenceManagerFactories are initialized in a system.

param
identifier

		assertNotConfigured();
		this.identifier = identifier;
	
public voidsetIgnoreCache(java.lang.String flag)
Sets the IgnoreCache flag for all PersistenceManagers

param
flag String IgnoreCache flag

                setIgnoreCache(Boolean.parseBoolean(flag));
        
public voidsetIgnoreCache(boolean flag)
Sets the flag that allows the user to request that queries be optimized to return approximate results by ignoring changed values in the cache.

param
flag boolean flag passed

		assertNotConfigured();
		throw new JDOUnsupportedOptionException(I18NHelper.getMessage(messages,
                            "persistencemanagerfactoryimpl.notsupported")); //NOI18N
		//ignoreCache = flag;
	
public voidsetMaxPool(int MaxPool)
Sets maximum number of PersistenceManager instances in the pool

param
MaxPool maximum number of PersistenceManager instances

		assertNotConfigured();
		this.maxPool = MaxPool;
	
public voidsetMinPool(int MinPool)
Sets minimum number of PersistenceManager instances in the pool

param
MinPool minimum number of PersistenceManager instances

		assertNotConfigured();
		this.minPool = MinPool;
	
public voidsetNontransactionalRead(java.lang.String flag)
Sets the NontransactionalRead flag for all PersistenceManagers

param
flag String NontransactionalRead flag

                setNontransactionalRead(Boolean.parseBoolean(flag));
        
public voidsetNontransactionalRead(boolean flag)
Sets the flag that allows non-transactional instances to be managed in the cache.

param
flag boolean flag passed

		assertNotConfigured();
		nontransactionalRead = flag;

		// Adjust depending flags
		if (flag == false)
		{
			retainValues = flag;
			optimistic = flag;
		}
	
public voidsetOptimistic(java.lang.String flag)
Sets the optimistic flag for all PersistenceManagers

param
flag String optimistic flag

		setOptimistic(Boolean.parseBoolean(flag));
	
public voidsetOptimistic(boolean flag)
Sets the optimistic flag for all PersistenceManagers

param
flag boolean optimistic flag

		assertNotConfigured();
		optimistic = flag;

		// Adjust depending flags
		if (flag)
			 nontransactionalRead = flag;
	
public voidsetQueryTimeout(java.lang.String timeout)
Sets the queryTimeout for all PersistenceManagers

param
timeout the timout to be set

        setQueryTimeout(Integer.parseInt(timeout));
    
public voidsetQueryTimeout(int timeout)
Sets the number of seconds to wait for a query statement to execute in the datastore associated with this PersistenceManagerFactory.

param
timeout new timout value in seconds; zero means unlimited

		assertNotConfigured();
		this.queryTimeout = timeout;
	
public voidsetRequireCopyObjectId(boolean flag)
Sets the default value of the requireCopyObjectId flag. If set to false, by default a PersistenceManager will not create a copy of an ObjectId for PersistenceManager.getObjectId(Object pc) and PersistenceManager.getObjectById(Object oid) requests.

see
PersistenceManager#getObjectId(Object pc)
see
PersistenceManager#getObjectById(Object oid)
param
flag boolean requireCopyObjectId flag

        assertNotConfigured();
        requireCopyObjectId = flag;
    
public voidsetRequireTrackedSCO(boolean flag)
Sets the requireTrackedSCO flag for this PersistenceManagerFactory. If set to false, by default the PersistenceManager will not create tracked SCO instances for new persistent instances at commit with retainValues set to true and while retrieving data from a datastore.

param
flag boolean requireTrackedSCO flag

        assertNotConfigured();
        requireTrackedSCO = flag;
    
public voidsetRetainValues(java.lang.String flag)
Sets the RetainValues flag for all PersistenceManagers

param
flag String RetainValues flag

                setRetainValues(Boolean.parseBoolean(flag));
        
public voidsetRetainValues(boolean flag)
Sets flag that will not cause the eviction of persistent instances after transaction completion.

param
flag boolean flag passed

		assertNotConfigured();
		retainValues = flag;

		// Adjust depending flags
		if (flag) {
                        nontransactionalRead = flag;
		}
	
public voidsetSupersedeDeletedInstance(boolean flag)
Sets the supersedeDeletedInstance flag for all PersistenceManagers.

param
flag boolean supersedeDeletedInstance flag

        assertNotConfigured();
        supersedeDeletedInstance = flag;
    
public voidsetUpdateTimeout(java.lang.String timeout)
Sets the updateTimeout for all PersistenceManagers

param
timeout the timout to be set

        setUpdateTimeout(Integer.parseInt(timeout));
    
public voidsetUpdateTimeout(int timeout)
Sets the number of seconds to wait for an update statement to execute in the datastore associated with this PersistenceManagerFactory.

param
timeout new timout value in seconds; zero means unlimited

                assertNotConfigured();
                this.updateTimeout = timeout;