Package javax.jdo

Class JDOHelper

  • All Implemented Interfaces:
    Constants

    public class JDOHelper
    extends Object
    implements Constants
    This class can be used by a JDO-aware application to call the JDO behavior of PersistenceCapable instances without declaring them to be PersistenceCapable.

    It is also used to acquire a PersistenceManagerFactory via various methods.

    This helper class defines static methods that allow a JDO-aware application to examine the runtime state of instances. For example, an application can discover whether the instance is persistent, transactional, dirty, new, deleted, or detached; and to get its associated PersistenceManager if it has one.

    Version:
    2.1
    • Constructor Detail

      • JDOHelper

        public JDOHelper()
        Some applications might prefer to use instance methods instead of static methods.
        Since:
        2.1
    • Method Detail

      • getInstance

        public static JDOHelper getInstance()
        Return the singleton instance of JDOHelper. This instance is thread-safe.
        Returns:
        the thread-safe singleton JDOHelper
        Since:
        2.1
      • getPersistenceManager

        public static PersistenceManager getPersistenceManager​(Object pc)
        Return the associated PersistenceManager if there is one. Transactional and persistent instances return the associated PersistenceManager.

        Transient non-transactional instances and instances of classes that do not implement PersistenceCapable return null.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        the PersistenceManager associated with the parameter instance.
        See Also:
        PersistenceCapable.jdoGetPersistenceManager()
      • makeDirty

        public static void makeDirty​(Object pc,
                                     String fieldName)
        Explicitly mark the parameter instance and field dirty. Normally, PersistenceCapable classes are able to detect changes made to their fields. However, if a reference to an array is given to a method outside the class, and the array is modified, then the persistent instance is not aware of the change. This API allows the application to notify the instance that a change was made to a field.

        Transient instances and instances of classes that do not implement PersistenceCapable ignore this method.

        Parameters:
        pc - the PersistenceCapable instance.
        fieldName - the name of the field to be marked dirty.
        See Also:
        PersistenceCapable.jdoMakeDirty(String fieldName)
      • getObjectId

        public static Object getObjectId​(Object pc)
        Return a copy of the JDO identity associated with the parameter instance.

        Persistent instances of PersistenceCapable classes have a JDO identity managed by the PersistenceManager. This method returns a copy of the ObjectId that represents the JDO identity.

        Transient instances and instances of classes that do not implement PersistenceCapable return null.

        The ObjectId may be serialized and later restored, and used with a PersistenceManager from the same JDO implementation to locate a persistent instance with the same data store identity.

        If the JDO identity is managed by the application, then the ObjectId may be used with a PersistenceManager from any JDO implementation that supports the PersistenceCapable class.

        If the JDO identity is not managed by the application or the data store, then the ObjectId returned is only valid within the current transaction.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        a copy of the ObjectId of the parameter instance as of the beginning of the transaction.
        See Also:
        PersistenceManager.getObjectId(Object pc), PersistenceCapable.jdoGetObjectId(), PersistenceManager.getObjectById(Object oid, boolean validate)
      • getObjectIds

        public static Collection<Object> getObjectIds​(Collection<?> pcs)
        Get object ids for a collection of instances. For each instance in the parameter, the getObjectId method is called. This method returns one identity instance for each element in the parameter. The order of iteration of the returned Collection exactly matches the order of iteration of the parameter Collection.
        Parameters:
        pcs - the persistence-capable instances
        Returns:
        the object ids of the parameters
        Since:
        2.0
        See Also:
        getObjectId(Object pc), getObjectIds(Object[] pcs)
      • getObjectIds

        public static Object[] getObjectIds​(Object[] pcs)
        Get object ids for an array of instances. For each instance in the parameter, the getObjectId method is called. This method returns one identity instance for each element in the parameter. The order of instances of the returned array exactly matches the order of instances of the parameter array.
        Parameters:
        pcs - the persistence-capable instances
        Returns:
        the object ids of the parameters
        Since:
        2.0
        See Also:
        getObjectId(Object pc), getObjectIds(Collection pcs)
      • getVersion

        public static Object getVersion​(Object pc)
        Return the version of the instance.
        Parameters:
        pc - the instance
        Returns:
        the version of the instance
        Since:
        2.0
      • isDirty

        public static boolean isDirty​(Object pc)
        Tests whether the parameter instance is dirty. Instances that have been modified, deleted, or newly made persistent in the current transaction return true.

        Transient instances and instances of classes that do not implement PersistenceCapable return false.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        true if the parameter instance has been modified in the current transaction.
        See Also:
        StateManager.makeDirty(PersistenceCapable pc, String fieldName), PersistenceCapable.jdoIsDirty()
      • isTransactional

        public static boolean isTransactional​(Object pc)
        Tests whether the parameter instance is transactional. Instances whose state is associated with the current transaction return true.

        Transient instances and instances of classes that do not implement PersistenceCapable return false.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        true if the parameter instance is transactional.
        See Also:
        PersistenceCapable.jdoIsTransactional()
      • isPersistent

        public static boolean isPersistent​(Object pc)
        Tests whether the parameter instance is persistent. Instances that represent persistent objects in the data store return true.

        Transient instances and instances of classes that do not implement PersistenceCapable return false.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        true if the parameter instance is persistent.
        See Also:
        PersistenceManager.makePersistent(Object pc), PersistenceCapable.jdoIsPersistent()
      • isNew

        public static boolean isNew​(Object pc)
        Tests whether the parameter instance has been newly made persistent. Instances that have been made persistent in the current transaction return true.

        Transient instances and instances of classes that do not implement PersistenceCapable return false.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        true if the parameter instance was made persistent in the current transaction.
        See Also:
        PersistenceManager.makePersistent(Object pc), PersistenceCapable.jdoIsNew()
      • isDeleted

        public static boolean isDeleted​(Object pc)
        Tests whether the parameter instance has been deleted. Instances that have been deleted in the current transaction return true.

        Transient instances and instances of classes that do not implement PersistenceCapable return false.

        Parameters:
        pc - the PersistenceCapable instance.
        Returns:
        true if the parameter instance was deleted in the current transaction.
        See Also:
        PersistenceManager.deletePersistent(Object pc), PersistenceCapable.jdoIsDeleted()
      • isDetached

        public static boolean isDetached​(Object pc)
        Tests whether the parameter instance has been detached. Instances that have been detached return true.

        Transient instances return false.

        Parameters:
        pc - the instance
        Returns:
        true if this instance is detached.
        Since:
        2.0
        See Also:
        PersistenceCapable.jdoIsDetached()
      • getObjectState

        public static ObjectState getObjectState​(Object pc)
        Accessor for the state of the passed object.
        Parameters:
        pc - The object
        Returns:
        The object state
        Since:
        2.1
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory()
        Get the anonymous PersistenceManagerFactory configured via the standard configuration file resource "META-INF/jdoconfig.xml", using the current thread's context class loader to locate the configuration file resource(s).
        Returns:
        the anonymous PersistenceManagerFactory.
        Since:
        2.1
        See Also:
        getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(ClassLoader pmfClassLoader)
        Get the anonymous PersistenceManagerFactory configured via the standard configuration file resource "META-INF/jdoconfig.xml", using the given class loader.
        Parameters:
        pmfClassLoader - the ClassLoader used to load resources and classes
        Returns:
        the anonymous PersistenceManagerFactory.
        Since:
        2.1
        See Also:
        getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(Map<?,​?> props)
        Get a PersistenceManagerFactory based on a Properties instance, using the current thread's context class loader to locate the PersistenceManagerFactory class.
        Parameters:
        props - a Properties instance with properties of the PersistenceManagerFactory.
        Returns:
        the PersistenceManagerFactory.
        See Also:
        getPersistenceManagerFactory(java.util.Map,ClassLoader)
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(Map<?,​?> props,
                                                                             ClassLoader pmfClassLoader)
        Get a PersistenceManagerFactory based on a Map and a class loader. This method delegates to the getPersistenceManagerFactory method that takes a Map of overrides and a Map of properties, passing null as the overrides parameter.
        Parameters:
        props - a Map with properties of the PersistenceManagerFactory.
        pmfClassLoader - the class loader used to load the PersistenceManagerFactory class
        Returns:
        the PersistenceManagerFactory.
        Since:
        1.0
        See Also:
        getPersistenceManagerFactory(java.util.Map, java.util.Map, ClassLoader)
      • getPersistenceManagerFactory

        protected static PersistenceManagerFactory getPersistenceManagerFactory​(Map<?,​?> overrides,
                                                                                Map<?,​?> props,
                                                                                ClassLoader pmfClassLoader)
        Get a PersistenceManagerFactory based on a Map of overrides, a Map of properties, and a class loader. The following are standard key names:
        "javax.jdo.PersistenceManagerFactoryClass"
        "javax.jdo.option.Optimistic",
        "javax.jdo.option.RetainValues",
        "javax.jdo.option.RestoreValues",
        "javax.jdo.option.IgnoreCache",
        "javax.jdo.option.NontransactionalRead",
        "javax.jdo.option.NontransactionalWrite",
        "javax.jdo.option.Multithreaded",
        "javax.jdo.option.ConnectionUserName",
        "javax.jdo.option.ConnectionPassword",
        "javax.jdo.option.ConnectionURL",
        "javax.jdo.option.ConnectionFactoryName",
        "javax.jdo.option.ConnectionFactory2Name",
        "javax.jdo.option.Mapping",
        "javax.jdo.mapping.Catalog",
        "javax.jdo.mapping.Schema",
        "javax.jdo.option.PersistenceUnitName",
        "javax.jdo.option.DetachAllOnCommit",
        "javax.jdo.option.CopyOnAttach",
        "javax.jdo.option.ReadOnly",
        "javax.jdo.option.TransactionIsolationLevel",
        "javax.jdo.option.TransactionType",
        "javax.jdo.option.ServerTimeZoneID",
        "javax.jdo.option.DatastoreReadTimeoutMillis",
        "javax.jdo.option.DatastoreWriteTimeoutMillis",
        "javax.jdo.option.Name".
        and properties of the form
        javax.jdo.option.InstanceLifecycleListener.{listenerClass}[=[{pcClasses}]] where {listenerClass} is the fully qualified name of a class that implements InstanceLifecycleListener, and {pcClasses} is an optional comma- or whitespace-delimited list of persistence-capable classes to be observed; the absence of a value for a property of this form means that instances of all persistence-capable classes will be observed by an instance of the given listener class.

        JDO implementations are permitted to define key values of their own. Any key values not recognized by the implementation must be ignored. Key values that are recognized but not supported by an implementation must result in a JDOFatalUserException thrown by the method.

        The returned PersistenceManagerFactory is not configurable (the setXXX methods will throw an exception).

        JDO implementations might manage a map of instantiated PersistenceManagerFactory instances based on specified property key values, and return a previously instantiated PersistenceManagerFactory instance. In this case, the properties of the returned instance must exactly match the requested properties.

        Parameters:
        overrides - Overrides of properties
        props - a Properties instance with properties of the PersistenceManagerFactory.
        pmfClassLoader - the class loader to use to load the PersistenceManagerFactory class
        Returns:
        the PersistenceManagerFactory.
        Throws:
        JDOFatalUserException - if
        • the pmfClassLoader passed is invalid; or
        • a valid class name cannot be obtained from either props or system resources (an entry in META-INF/services/javax.jdo.PersistenceManagerFactory); or
        • all implementations throw an exception.
        Since:
        2.1
      • getClassNameFromURL

        protected static String getClassNameFromURL​(URL url)
                                             throws IOException
        Get a class name from a URL. The URL is from getResources with e.g. META-INF/services/javax.jdo.PersistenceManagerFactory as the parameter. Parse the file, removing blank lines, comment lines, and comments.
        Parameters:
        url - the URL of the services file
        Returns:
        the name of the class contained in the file
        Throws:
        IOException - Throw if an error occurs on accessing this URL
        Since:
        2.1
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(Map<?,​?> overrides,
                                                                             String name,
                                                                             ClassLoader resourceLoader,
                                                                             ClassLoader pmfLoader)
        Returns a PersistenceManagerFactory configured based on the properties stored in the resource at name, or, if not found, returns a PersistenceManagerFactory with the given name or, if not found, returns a javax.persistence.EntityManagerFactory cast to a PersistenceManagerFactory. If the name given is null or consists only of whitespace, it is interpreted as Constants.ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME. The following are standard key names:
        "javax.jdo.PersistenceManagerFactoryClass"
        "javax.jdo.option.Optimistic",
        "javax.jdo.option.RetainValues",
        "javax.jdo.option.RestoreValues",
        "javax.jdo.option.IgnoreCache",
        "javax.jdo.option.NontransactionalRead",
        "javax.jdo.option.NontransactionalWrite",
        "javax.jdo.option.Multithreaded",
        "javax.jdo.option.ConnectionUserName",
        "javax.jdo.option.ConnectionPassword",
        "javax.jdo.option.ConnectionURL",
        "javax.jdo.option.ConnectionFactoryName",
        "javax.jdo.option.ConnectionFactory2Name",
        "javax.jdo.option.Mapping",
        "javax.jdo.mapping.Catalog",
        "javax.jdo.mapping.Schema",
        "javax.jdo.option.PersistenceUnitName".
        "javax.jdo.option.DetachAllOnCommit".
        "javax.jdo.option.CopyOnAttach".
        "javax.jdo.option.TransactionType".
        "javax.jdo.option.ServerTimeZoneID".
        "javax.jdo.option.DatastoreReadTimeoutMillis",
        "javax.jdo.option.DatastoreWriteTimeoutMillis",
        "javax.jdo.option.Name".
        and properties of the form
        javax.jdo.option.InstanceLifecycleListener.{listenerClass}[=[{pcClasses}]] where {listenerClass} is the fully qualified name of a class that implements InstanceLifecycleListener, and {pcClasses} is an optional comma- or whitespace-delimited list of persistence-capable classes to be observed; the absence of a value for a property of this form means that instances of all persistence-capable classes will be observed by an instance of the given listener class.

        JDO implementations are permitted to define key values of their own. Any key values not recognized by the implementation must be ignored. Key values that are recognized but not supported by an implementation must result in a JDOFatalUserException thrown by the method.

        The returned PersistenceManagerFactory is not configurable (the setXXX methods will throw an exception). This method loads the properties found at name, if any, via resourceLoader, and creates a PersistenceManagerFactory with pmfLoader. Any exceptions thrown during resource loading will be wrapped in a JDOFatalUserException. If multiple PMFs with the requested name are found, a JDOFatalUserException is thrown.

        Parameters:
        overrides - a Map containing properties that override properties defined in any resources loaded according to the "name" parameter
        name - interpreted as the name of the resource containing the PMF properties, the name of the PMF, or the persistence unit name, in that order; if name is null, blank or whitespace, it is interpreted as indicating the anonymous PersistenceManagerFactory.
        resourceLoader - the class loader to use to load properties file resources; must be non-null if name is non-null or blank
        pmfLoader - the class loader to use to load the PersistenceManagerFactory or javax.persistence.EntityManagerFactory classes
        Returns:
        the PersistenceManagerFactory with properties in the given resource, with the given name, or with the given persitence unit name
        Since:
        2.1
        See Also:
        Constants.ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME
      • invokeGetPersistenceManagerFactoryOnImplementation

        protected static PersistenceManagerFactory invokeGetPersistenceManagerFactoryOnImplementation​(String pmfClassName,
                                                                                                      Map<?,​?> overrides,
                                                                                                      Map<?,​?> properties,
                                                                                                      ClassLoader cl)
        Invoke the getPersistenceManagerFactory method on the implementation. If the overrides parameter to this method is not null, the static method with Map overrides, Map properties parameters will be invoked. If the overrides parameter to this method is null, the static method with Map properties parameter will be invoked.
        Parameters:
        pmfClassName - the name of the implementation factory class
        overrides - a Map of overrides
        properties - a Map of properties
        cl - the class loader to use to load the implementation class
        Returns:
        the PersistenceManagerFactory
      • loadPropertiesFromResource

        protected static Map<Object,​Object> loadPropertiesFromResource​(ClassLoader resourceLoader,
                                                                             String name)
        Load a Properties instance by name from the class loader.
        Parameters:
        resourceLoader - the class loader from which to load the properties
        name - the name of the resource
        Returns:
        a Properties instance or null if no resource is found
      • getNamedPMFProperties

        protected static Map<Object,​Object> getNamedPMFProperties​(String name,
                                                                        ClassLoader resourceLoader,
                                                                        String jdoconfigResourceName)
        Find and return the named PersistenceManagerFactory's properties, or null if not found. If multiple named PMF property sets with the given name are found (including anonymous ones), throw JDOFatalUserException. This method is here only to facilitate testing; the parameter "jdoconfigResourceName" in public usage should always have the value given in the constant Constants.JDOCONFIG_RESOURCE_NAME.
        Parameters:
        name - The persistence unit name; null is disallowed.
        resourceLoader - The ClassLoader used to load the standard JDO configuration file.
        jdoconfigResourceName - The name of the configuration file to read. In public usage, this should always be the value of Constants.JDOCONFIG_RESOURCE_NAME.
        Returns:
        The named PersistenceManagerFactory properties if found, null if not.
        Throws:
        JDOFatalUserException - if multiple named PMF property sets are found with the given name, or any other exception is encountered.
        Since:
        2.1
      • getDefaultDocumentBuilderFactory

        protected static DocumentBuilderFactory getDefaultDocumentBuilderFactory()
      • getErrorHandler

        protected static ErrorHandler getErrorHandler()
      • getDefaultErrorHandler

        protected static ErrorHandler getDefaultErrorHandler()
      • readNamedPMFProperties

        protected static Map<String,​Map<Object,​Object>> readNamedPMFProperties​(URL url,
                                                                                           String requestedPMFName,
                                                                                           DocumentBuilderFactory factory)
        Reads JDO configuration file, creates a Map for each persistence-manager-factory, then returns the map.
        Parameters:
        url - URL of a JDO configuration file compliant with javax/jdo/jdoconfig.xsd.
        requestedPMFName - The name of the requested persistence unit (allows for fail-fast).
        factory - The DocumentBuilderFactory to use for XML parsing.
        Returns:
        a Map<String,Map> holding persistence unit configurations; for the anonymous persistence unit, the value of the String key is the empty string, "".
      • readPropertiesFromPMFElementAttributes

        protected static Properties readPropertiesFromPMFElementAttributes​(Node pmfElement)
      • readPropertiesFromPMFSubelements

        protected static Properties readPropertiesFromPMFSubelements​(Node pmfElement,
                                                                     URL url)
      • isNullOrBlank

        protected static boolean isNullOrBlank​(String s)
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(File propsFile,
                                                                             ClassLoader loader)
        Returns a PersistenceManagerFactory configured based on the properties stored in the file at propsFile. Creates a PersistenceManagerFactory with loader. Any IOExceptions or FileNotFoundExceptions thrown during resource loading will be wrapped in a JDOFatalUserException.
        Parameters:
        propsFile - the file containing the Properties
        loader - the class loader to use to load the PersistenceManagerFactory class
        Returns:
        the PersistenceManagerFactory
        Since:
        2.0
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(String jndiLocation,
                                                                             Context context)
        Returns a PersistenceManagerFactory at the JNDI location specified by jndiLocation in the context context. If context is null, new InitialContext() will be used. This method is equivalent to invoking getPersistenceManagerFactory(String,Context,ClassLoader) with Thread.currentThread().getContextClassLoader() as the loader argument.
        Parameters:
        jndiLocation - the JNDI location containing the PersistenceManagerFactory
        context - the context in which to find the named PersistenceManagerFactory
        Returns:
        the PersistenceManagerFactory
        Since:
        2.0
      • getPersistenceManagerFactory

        public static PersistenceManagerFactory getPersistenceManagerFactory​(String jndiLocation,
                                                                             Context context,
                                                                             ClassLoader loader)
        Returns a PersistenceManagerFactory at the JNDI location specified by jndiLocation in the context context. If context is null, new InitialContext() will be used. Creates a PersistenceManagerFactory with loader. Any NamingExceptions thrown will be wrapped in a JDOFatalUserException.
        Parameters:
        jndiLocation - the JNDI location containing the PersistenceManagerFactory
        context - the context in which to find the named PersistenceManagerFactory
        loader - the class loader to use to load the PersistenceManagerFactory class
        Returns:
        the PersistenceManagerFactory
        Since:
        2.0
      • getEnhancer

        public static JDOEnhancer getEnhancer()
        Get a JDOEnhancer using the available enhancer(s) specified in "META-INF/services/JDOEnhancer" using the context class loader.
        Returns:
        the JDOEnhancer.
        Throws:
        JDOFatalUserException - if no available enhancer
        Since:
        3.0
      • getEnhancer

        public static JDOEnhancer getEnhancer​(ClassLoader loader)
        Get a JDOEnhancer using the available enhancer(s) specified in "META-INF/services/JDOEnhancer"
        Parameters:
        loader - the loader to use for loading the JDOEnhancer class (if any)
        Returns:
        the JDOEnhancer.
        Throws:
        JDOFatalUserException - if no available enhancer
        Since:
        3.0
      • getResources

        protected static Enumeration<URL> getResources​(ClassLoader resourceLoader,
                                                       String resourceName)
                                                throws IOException
        Get resources of the resource loader. Perform this operation in a doPrivileged block.
        Parameters:
        resourceLoader - ClassLoader to use for loading resources
        resourceName - Name of the resource
        Returns:
        the resources
        Throws:
        IOException - if an error occurs accessing the resources