Index: api2/test/schema/jdoconfig/Positive02/META-INF/jdoconfig.xml
===================================================================
--- api2/test/schema/jdoconfig/Positive02/META-INF/jdoconfig.xml (revision 0)
+++ api2/test/schema/jdoconfig/Positive02/META-INF/jdoconfig.xml (working copy)
@@ -9,7 +9,6 @@
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
- PersistenceManagerFactory.
* @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory() {
ClassLoader cl = getContextClassLoader();
- return getPersistenceManagerFactory ((String)null, cl);
+ return getPersistenceManagerFactory
+ (null, ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, cl, cl);
}
/** Get the anonymous PersistenceManagerFactory configured via
- * the standard
- * configuration file resource "META-INF/jdoconfig.xml", using the given
- * class loader
- * to locate the configuration file resource(s).
+ * the standard configuration file resource "META-INF/jdoconfig.xml", using
+ * the given class loader.
* @return the anonymous PersistenceManagerFactory.
- * @param cl the ClassLoader used to load resources and classes
+ * @param pmfClassLoader the ClassLoader used to load resources and classes
* @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory(
- ClassLoader cl
+ ClassLoader pmfClassLoader
) {
- return getPersistenceManagerFactory((String)null, cl, cl);
+ return getPersistenceManagerFactory(
+ null,
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME,
+ getContextClassLoader(),
+ pmfClassLoader);
}
- /** Get the anonymous PersistenceManagerFactory configured via
- * the standard
- * configuration file resource "META-INF/jdoconfig.xml", using the given
- * resource class loader & class loader
- * to locate the configuration file resource(s).
- * @return the anonymous PersistenceManagerFactory.
- * @param resourceLoader the class loader to use to load resources
- * @param pmfLoader the class loader to use to load the classes
- * @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
- */
- public static PersistenceManagerFactory getPersistenceManagerFactory(
- ClassLoader resourceLoader,
- ClassLoader pmfLoader
- ) {
- return getPersistenceManagerFactory((String)null, resourceLoader, pmfLoader);
- }
-
/** Get a PersistenceManagerFactory based on a Properties
* instance, using the current thread's context class loader to locate the
* PersistenceManagerFactory class.
@@ -707,16 +692,9 @@
public static PersistenceManagerFactory getPersistenceManagerFactory
(Map props)
{
- ClassLoader cl = getContextClassLoader();
- return getPersistenceManagerFactory (props, cl);
+ return getPersistenceManagerFactory(props, getContextClassLoader());
}
- public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props, ClassLoader cl)
- {
- return getPersistenceManagerFactory(props, cl, cl);
- }
-
/**
* Get a PersistenceManagerFactory based on a
* Map instance and a class loader.
@@ -741,7 +719,7 @@
*
"javax.jdo.option.Name".
*
* and properties of the form
- * javax.jdo.option.InstanceLifecycleListener.{listenerClass}={pcClasses}
+ * javax.jdo.option.InstanceLifecycleListener.{listenerClass}[=[{pcClasses}]]
* where {listenerClass} is the fully qualified name of a
* class that implements
* {@link javax.jdo.listener.InstanceLifecycleListener}, and
@@ -767,21 +745,23 @@
* @return the PersistenceManagerFactory.
* @param props a Properties instance with properties of the
* PersistenceManagerFactory.
- * @param resourceLoader the class loader to use to load resources (if
- * resource loading is necessary)
* @param pmfClassLoader the class loader to use to load the
* PersistenceManagerFactory class
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props, ClassLoader resourceLoader, ClassLoader pmfClassLoader)
+ (Map props, ClassLoader pmfClassLoader)
{
+ if (pmfClassLoader == null)
+ throw new JDOFatalUserException (msg.msg (
+ "EXC_GetPMFNullLoader")); //NOI18N
+
String pmfClassName = (String) props.get (
PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS);
if (pmfClassName == null) {
// no PMF class name property -- try to find via services lookup
try {
- pmfClassName = getPMFClassNameViaServiceLookup(resourceLoader);
+ pmfClassName = getPMFClassNameViaServiceLookup(pmfClassLoader);
}
catch (IOException e) {
throw new JDOFatalInternalException(msg.msg(
@@ -906,85 +886,129 @@
}
/**
- * Returns a named {@link PersistenceManagerFactory} with the given persistence unit name or,
- * if not found, a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the resource at
- * name. This method is equivalent to
- * invoking {@link
- * #getPersistenceManagerFactory(String,ClassLoader)} with
- * Thread.currentThread().getContextClassLoader() as
- * the loader argument.
- * If multiple persistence units with the name given are found, a {@link JDOFatalUserException} is thrown.
- * @since 2.0
- * @param name the persistence unit name or resource containing the Properties
- * @return the PersistenceManagerFactory
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
(String name) {
- return getPersistenceManagerFactory (name,
- getContextClassLoader());
+ ClassLoader cl = getContextClassLoader();
+ return getPersistenceManagerFactory(null, name, cl, cl);
}
/**
- * Returns a named {@link PersistenceManagerFactory} with the given persistence unit name or,
- * if not found, a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the resource at
- * name. Loads the resource via
- * loader, and creates a {@link
- * PersistenceManagerFactory} with loader. Any
- * IOExceptions thrown during resource loading will
- * be wrapped in a {@link JDOFatalUserException}.
- * If multiple persistence units with the name given are found, a {@link JDOFatalUserException} is thrown.
- * @since 2.0
- * @param name the persistence unit name or resource containing the Properties
- * @param loader the class loader to use to load both the name and
- * the PersistenceManagerFactory class
- * @return the PersistenceManagerFactory
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 1.0
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
(String name, ClassLoader loader) {
- return getPersistenceManagerFactory(name, loader, loader);
+
+ return getPersistenceManagerFactory(null, name, loader, loader);
}
-
+
/**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.0
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (String name, ClassLoader resourceLoader, ClassLoader pmfLoader) {
+
+ return getPersistenceManagerFactory(
+ null, name, resourceLoader, pmfLoader);
+ }
+
+ /**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map overrides, String name) {
+
+ ClassLoader cl = getContextClassLoader();
+ return getPersistenceManagerFactory(overrides, name, cl, cl);
+ }
+
+ /**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map overrides, String name, ClassLoader resourceLoader) {
+
+ return getPersistenceManagerFactory(
+ overrides, name, resourceLoader, resourceLoader);
+ }
+
+
+ /**
* Returns a {@link PersistenceManagerFactory} configured based
* on the properties stored in the resource at
* name, or, if not found, returns a
* {@link PersistenceManagerFactory} with the given
* name or, if not found, returns a
* javax.persistence.EntityManagerFactory cast to a
- * {@link PersistenceManagerFactory}. Loads the properties via
+ * {@link PersistenceManagerFactory}. If the name given is null or consists
+ * only of whitespace, it is interpreted as
+ * {@link Constants#ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME}.
+ *
+ * This method loads the properties found at name, if any, via
* resourceLoader, and creates a {@link
* PersistenceManagerFactory} with pmfLoader. Any
* exceptions thrown during resource loading will
* be wrapped in a {@link JDOFatalUserException}.
* If multiple PMFs with the requested name are found, a
* {@link JDOFatalUserException} is thrown.
- * @since 2.0
+ * @since 2.1
+ * @param overrides a Map containing properties that override properties
+ * defined in any resources loaded according to the "name" parameter
* @param 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.
- * @param resourceLoader the class loader to use to load properties or
- * configuration file resources
+ * order; if name is null, blank or whitespace, it is interpreted as
+ * indicating the anonymous {@link PersistenceManagerFactory}.
+ * @param resourceLoader the class loader to use to load properties file
+ * resources; must be non-null if name is non-null or blank
* @param pmfLoader the class loader to use to load the
- * PersistenceManagerFactory or
+ * {@link PersistenceManagerFactory} or
* javax.persistence.EntityManagerFactory classes
- * @return the PersistenceManagerFactory with properties in the given
- * resource, with the given name, or with the given persitence unit name
+ * @return the {@link PersistenceManagerFactory} with properties in the
+ * given resource, with the given name, or with the given persitence unit
+ * name
+ * @see Constants#ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME
*/
- public static PersistenceManagerFactory getPersistenceManagerFactory
- (String name, ClassLoader resourceLoader, ClassLoader pmfLoader) {
-
- if (resourceLoader == null)
- throw new JDOFatalUserException (msg.msg (
- "EXC_GetPMFNullPropsLoader")); //NOI18N
+ public static PersistenceManagerFactory getPersistenceManagerFactory(
+ Map overrides,
+ String name,
+ ClassLoader resourceLoader,
+ ClassLoader pmfLoader
+ ) {
if (pmfLoader == null)
throw new JDOFatalUserException (msg.msg (
"EXC_GetPMFNullPMFLoader")); //NOI18N
Properties props = null;
InputStream in = null;
- if (name != null) { // then try to load resources from properties file
+ if (name != null
+ && !ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(
+ (name = name.trim())))
+ {
+ // then try to load resources from properties file
+ if (resourceLoader == null)
+ throw new JDOFatalUserException (msg.msg (
+ "EXC_GetPMFNullPropsLoader")); //NOI18N
try {
in = resourceLoader.getResourceAsStream(name);
if (in != null) {
@@ -992,6 +1016,12 @@
// assume that it's a properties file and proceed as usual
props = new Properties();
props.load(in);
+ // JDO 2.1:
+ // add the SPI property to inform the implementation that
+ // the PMF was configured by the given resource name
+ // and not via named PMF for proper deserialization
+ props.put(PROPERTY_SPI_RESOURCE_NAME, name);
+ props.put(PROPERTY_NAME, null);
}
}
catch (IOException ioe) {
@@ -1005,34 +1035,63 @@
} catch (IOException ioe) { }
}
}
- // JDO 2.1: else name was null or no resource found by given name;
- // first assume that name represents name of named PMF
+ // JDO 2.1: else name was null, empty string, or no resource found by
+ // given name; first see if name represents name of named PMF
+
+ // as a convenience in public usage, convert null to anonymous PMF name
+ if (name == null) {
+ name = ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME;
+ }
+
Map properties = props;
if (properties == null) {
- properties = getNamedPMFProperties(name, resourceLoader);
+ properties = getNamedPMFProperties(name, pmfLoader);
+ if (properties != null) {
+ // inform the impl that the config came from a jdoconfig.xml
+ // element with the given name
+ properties.put(PROPERTY_NAME, name);
+ }
}
if (properties != null) {
- // see if there's already a name property in properties
- String nameInProperties = (String) properties.get(PROPERTY_NAME);
- if (nameInProperties != null) {
- // name given in properties; go ahead & use it
- nameInProperties = nameInProperties.trim();
+ // try to resolve PMF class name
+ String pmfClassName = null;
+ try {
+ pmfClassName = resolvePMFClassName(
+ overrides, properties, pmfLoader);
+ } catch (IOException ioe) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_IOExceptionDuringServiceLookup"), ioe); //NOI18N
}
- else {
- // no name property given; put one for the anonymous PMF
- name = name == null ? "" : name.trim();
- properties.put(PROPERTY_NAME, name);
+
+ if (pmfClassName == null) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"));
}
- return getPersistenceManagerFactory(
- properties, resourceLoader, pmfLoader);
+
+ try {
+ return invokeGetPersistenceManagerFactoryOnImplementation(
+ pmfClassName, overrides, properties, pmfLoader);
+ } catch (ClassNotFoundException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFClassNotFound", pmfClassName), e);
+ } catch (NoSuchMethodException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+ } catch (IllegalAccessException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFIllegalAccess", pmfClassName), e);
+ } catch (InvocationTargetException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFInvocationTargetException", pmfClassName), e);
+ }
}
- // else no properties found; next, assume name is a PU name
- if (name != null && !"".equals(name = name.trim())) {
+ // else no properties found; next, assume name is a JPA PU name
+ if (!ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(name)) {
PersistenceManagerFactory pmf =
- getPMFFromEMF(name, null, pmfLoader);
+ getPMFFromEMF(name, overrides, pmfLoader);
if (pmf != null) {
return pmf;
}
@@ -1041,10 +1100,79 @@
// else no PMF found
throw new JDOFatalUserException (msg.msg (
"EXC_NoPMFConfigurableViaPropertiesOrXML",
- name,
- resourceLoader)); //NOI18N
+ name)); //NOI18N
}
+ protected static PersistenceManagerFactory
+ invokeGetPersistenceManagerFactoryOnImplementation(
+ String pmfClassName, Map overrides, Map properties, ClassLoader cl)
+ throws
+ ClassNotFoundException,
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException
+ {
+ Class implClass = cl.loadClass(pmfClassName);
+ Method m = implClass.getMethod(
+ "getPersistenceManagerFactory",
+ new Class[] { Map.class, Map.class });
+
+ return (PersistenceManagerFactory) m.invoke(
+ null, new Object[] {overrides, properties});
+ }
+
+
+ /**
+ * Tries to get the PMF class name property in the following locations
+ * in this order:
PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
+ * parameter "overrides"PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
+ * parameter "properties"META-INF/services/javax.jdo.PeristenceManagerFactoryPersistenceManagerFactory properties if
+ * found, null if not.
* @since 2.1
* @throws JDOFatalUserException if multiple named PMF property sets are
* found with the given name, or any other exception is encountered.
@@ -1083,14 +1208,6 @@
ClassLoader resourceLoader,
String jdoconfigResourceName
) {
- /* JDO 2.1:
- Attempt to find & return named PMF properties here.
- If name == null or name == "", then we're looking for the anonymous PMF.
-
- If we can't find it, this method returns null.
- */
- name = name == null ? "" : name.trim(); // for use as key in Maps
-
// key is PU name, value is Map of PU properties
Map/*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).
* @return the anonymous PersistenceManagerFactory.
* @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory() {
ClassLoader cl = getContextClassLoader();
- return getPersistenceManagerFactory ((String)null, cl);
+ return getPersistenceManagerFactory
+ (null, ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME, cl, cl);
}
/** Get the anonymous PersistenceManagerFactory configured via
- * the standard
- * configuration file resource "META-INF/jdoconfig.xml", using the given
- * class loader
- * to locate the configuration file resource(s).
+ * the standard configuration file resource "META-INF/jdoconfig.xml", using
+ * the given class loader.
* @return the anonymous PersistenceManagerFactory.
- * @param cl the ClassLoader used to load resources and classes
+ * @param pmfClassLoader the ClassLoader used to load resources and classes
* @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory(
- ClassLoader cl
+ ClassLoader pmfClassLoader
) {
- return getPersistenceManagerFactory((String)null, cl, cl);
+ return getPersistenceManagerFactory(
+ null,
+ ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME,
+ getContextClassLoader(),
+ pmfClassLoader);
}
- /** Get the anonymous PersistenceManagerFactory configured via
- * the standard
- * configuration file resource "META-INF/jdoconfig.xml", using the given
- * resource class loader & class loader
- * to locate the configuration file resource(s).
- * @return the anonymous PersistenceManagerFactory.
- * @param resourceLoader the class loader to use to load resources
- * @param pmfLoader the class loader to use to load the classes
- * @since 2.1
- * @see #getPersistenceManagerFactory(String,ClassLoader)
- */
- public static PersistenceManagerFactory getPersistenceManagerFactory(
- ClassLoader resourceLoader,
- ClassLoader pmfLoader
- ) {
- return getPersistenceManagerFactory((String)null, resourceLoader, pmfLoader);
- }
-
/** Get a PersistenceManagerFactory based on a Properties
* instance, using the current thread's context class loader to locate the
* PersistenceManagerFactory class.
@@ -632,16 +617,9 @@
public static PersistenceManagerFactory getPersistenceManagerFactory
(Map props)
{
- ClassLoader cl = getContextClassLoader();
- return getPersistenceManagerFactory (props, cl);
+ return getPersistenceManagerFactory(props, getContextClassLoader());
}
- public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props, ClassLoader cl)
- {
- return getPersistenceManagerFactory(props, cl, cl);
- }
-
/**
* Get a PersistenceManagerFactory based on a
* Map instance and a class loader.
@@ -666,7 +644,7 @@
* javax.jdo.option.InstanceLifecycleListener.{listenerClass}={pcClasses}
+ * javax.jdo.option.InstanceLifecycleListener.{listenerClass}[=[{pcClasses}]]
* where {listenerClass} is the fully qualified name of a
* class that implements
* {@link javax.jdo.listener.InstanceLifecycleListener}, and
@@ -692,21 +670,23 @@
* @return the PersistenceManagerFactory.
* @param props a Properties instance with properties of the
* PersistenceManagerFactory.
- * @param resourceLoader the class loader to use to load resources (if
- * resource loading is necessary)
* @param pmfClassLoader the class loader to use to load the
* PersistenceManagerFactory class
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (Map props, ClassLoader resourceLoader, ClassLoader pmfClassLoader)
+ (Map props, ClassLoader pmfClassLoader)
{
+ if (pmfClassLoader == null)
+ throw new JDOFatalUserException (msg.msg (
+ "EXC_GetPMFNullLoader")); //NOI18N
+
String pmfClassName = (String) props.get (
PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS);
if (pmfClassName == null) {
// no PMF class name property -- try to find via services lookup
try {
- pmfClassName = getPMFClassNameViaServiceLookup(resourceLoader);
+ pmfClassName = getPMFClassNameViaServiceLookup(pmfClassLoader);
}
catch (IOException e) {
throw new JDOFatalInternalException(msg.msg(
@@ -831,85 +811,129 @@
}
/**
- * Returns a named {@link PersistenceManagerFactory} with the given persistence unit name or,
- * if not found, a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the resource at
- * name. This method is equivalent to
- * invoking {@link
- * #getPersistenceManagerFactory(String,ClassLoader)} with
- * Thread.currentThread().getContextClassLoader() as
- * the loader argument.
- * If multiple persistence units with the name given are found, a {@link JDOFatalUserException} is thrown.
- * @since 2.0
- * @param name the persistence unit name or resource containing the Properties
- * @return the PersistenceManagerFactory
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
(String name) {
- return getPersistenceManagerFactory (name,
- getContextClassLoader());
+ ClassLoader cl = getContextClassLoader();
+ return getPersistenceManagerFactory(null, name, cl, cl);
}
/**
- * Returns a named {@link PersistenceManagerFactory} with the given persistence unit name or,
- * if not found, a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the resource at
- * name. Loads the resource via
- * loader, and creates a {@link
- * PersistenceManagerFactory} with loader. Any
- * IOExceptions thrown during resource loading will
- * be wrapped in a {@link JDOFatalUserException}.
- * If multiple persistence units with the name given are found, a {@link JDOFatalUserException} is thrown.
- * @since 2.0
- * @param name the persistence unit name or resource containing the Properties
- * @param loader the class loader to use to load both the name and
- * the PersistenceManagerFactory class
- * @return the PersistenceManagerFactory
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 1.0
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
(String name, ClassLoader loader) {
- return getPersistenceManagerFactory(name, loader, loader);
+
+ return getPersistenceManagerFactory(null, name, loader, loader);
}
-
+
/**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.0
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (String name, ClassLoader resourceLoader, ClassLoader pmfLoader) {
+
+ return getPersistenceManagerFactory(
+ null, name, resourceLoader, pmfLoader);
+ }
+
+ /**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map overrides, String name) {
+
+ ClassLoader cl = getContextClassLoader();
+ return getPersistenceManagerFactory(overrides, name, cl, cl);
+ }
+
+ /**
+ * Returns a named {@link PersistenceManagerFactory} or persistence
+ * unit.
+ *
+ * @since 2.1
+ * @see #getPersistenceManagerFactory(Map,String,ClassLoader,ClassLoader)
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (Map overrides, String name, ClassLoader resourceLoader) {
+
+ return getPersistenceManagerFactory(
+ overrides, name, resourceLoader, resourceLoader);
+ }
+
+
+ /**
* Returns a {@link PersistenceManagerFactory} configured based
* on the properties stored in the resource at
* name, or, if not found, returns a
* {@link PersistenceManagerFactory} with the given
* name or, if not found, returns a
* javax.persistence.EntityManagerFactory cast to a
- * {@link PersistenceManagerFactory}. Loads the properties via
+ * {@link PersistenceManagerFactory}. If the name given is null or consists
+ * only of whitespace, it is interpreted as
+ * {@link Constants#ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME}.
+ *
+ * This method loads the properties found at name, if any, via
* resourceLoader, and creates a {@link
* PersistenceManagerFactory} with pmfLoader. Any
* exceptions thrown during resource loading will
* be wrapped in a {@link JDOFatalUserException}.
* If multiple PMFs with the requested name are found, a
* {@link JDOFatalUserException} is thrown.
- * @since 2.0
+ * @since 2.1
+ * @param overrides a Map containing properties that override properties
+ * defined in any resources loaded according to the "name" parameter
* @param 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.
- * @param resourceLoader the class loader to use to load properties or
- * configuration file resources
+ * order; if name is null, blank or whitespace, it is interpreted as
+ * indicating the anonymous {@link PersistenceManagerFactory}.
+ * @param resourceLoader the class loader to use to load properties file
+ * resources; must be non-null if name is non-null or blank
* @param pmfLoader the class loader to use to load the
- * PersistenceManagerFactory or
+ * {@link PersistenceManagerFactory} or
* javax.persistence.EntityManagerFactory classes
- * @return the PersistenceManagerFactory with properties in the given
- * resource, with the given name, or with the given persitence unit name
+ * @return the {@link PersistenceManagerFactory} with properties in the
+ * given resource, with the given name, or with the given persitence unit
+ * name
+ * @see Constants#ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME
*/
- public static PersistenceManagerFactory getPersistenceManagerFactory
- (String name, ClassLoader resourceLoader, ClassLoader pmfLoader) {
-
- if (resourceLoader == null)
- throw new JDOFatalUserException (msg.msg (
- "EXC_GetPMFNullPropsLoader")); //NOI18N
+ public static PersistenceManagerFactory getPersistenceManagerFactory(
+ Map overrides,
+ String name,
+ ClassLoader resourceLoader,
+ ClassLoader pmfLoader
+ ) {
if (pmfLoader == null)
throw new JDOFatalUserException (msg.msg (
"EXC_GetPMFNullPMFLoader")); //NOI18N
Properties props = null;
InputStream in = null;
- if (name != null) { // then try to load resources from properties file
+ if (name != null
+ && !ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(
+ (name = name.trim())))
+ {
+ // then try to load resources from properties file
+ if (resourceLoader == null)
+ throw new JDOFatalUserException (msg.msg (
+ "EXC_GetPMFNullPropsLoader")); //NOI18N
try {
in = resourceLoader.getResourceAsStream(name);
if (in != null) {
@@ -917,6 +941,12 @@
// assume that it's a properties file and proceed as usual
props = new Properties();
props.load(in);
+ // JDO 2.1:
+ // add the SPI property to inform the implementation that
+ // the PMF was configured by the given resource name
+ // and not via named PMF for proper deserialization
+ props.put(PROPERTY_SPI_RESOURCE_NAME, name);
+ props.put(PROPERTY_NAME, null);
}
}
catch (IOException ioe) {
@@ -930,34 +960,63 @@
} catch (IOException ioe) { }
}
}
- // JDO 2.1: else name was null or no resource found by given name;
- // first assume that name represents name of named PMF
+ // JDO 2.1: else name was null, empty string, or no resource found by
+ // given name; first see if name represents name of named PMF
+
+ // as a convenience in public usage, convert null to anonymous PMF name
+ if (name == null) {
+ name = ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME;
+ }
+
Map properties = props;
if (properties == null) {
- properties = getNamedPMFProperties(name, resourceLoader);
+ properties = getNamedPMFProperties(name, pmfLoader);
+ if (properties != null) {
+ // inform the impl that the config came from a jdoconfig.xml
+ // element with the given name
+ properties.put(PROPERTY_NAME, name);
+ }
}
if (properties != null) {
- // see if there's already a name property in properties
- String nameInProperties = (String) properties.get(PROPERTY_NAME);
- if (nameInProperties != null) {
- // name given in properties; go ahead & use it
- nameInProperties = nameInProperties.trim();
+ // try to resolve PMF class name
+ String pmfClassName = null;
+ try {
+ pmfClassName = resolvePMFClassName(
+ overrides, properties, pmfLoader);
+ } catch (IOException ioe) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_IOExceptionDuringServiceLookup"), ioe); //NOI18N
}
- else {
- // no name property given; put one for the anonymous PMF
- name = name == null ? "" : name.trim();
- properties.put(PROPERTY_NAME, name);
+
+ if (pmfClassName == null) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"));
}
- return getPersistenceManagerFactory(
- properties, resourceLoader, pmfLoader);
+
+ try {
+ return invokeGetPersistenceManagerFactoryOnImplementation(
+ pmfClassName, overrides, properties, pmfLoader);
+ } catch (ClassNotFoundException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFClassNotFound", pmfClassName), e);
+ } catch (NoSuchMethodException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+ } catch (IllegalAccessException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFIllegalAccess", pmfClassName), e);
+ } catch (InvocationTargetException e) {
+ throw new JDOFatalUserException(msg.msg(
+ "EXC_GetPMFInvocationTargetException", pmfClassName), e);
+ }
}
- // else no properties found; next, assume name is a PU name
- if (name != null && !"".equals(name = name.trim())) {
+ // else no properties found; next, assume name is a JPA PU name
+ if (!ANONYMOUS_PERSISTENCE_MANAGER_FACTORY_NAME.equals(name)) {
PersistenceManagerFactory pmf =
- getPMFFromEMF(name, null, pmfLoader);
+ getPMFFromEMF(name, overrides, pmfLoader);
if (pmf != null) {
return pmf;
}
@@ -966,10 +1025,79 @@
// else no PMF found
throw new JDOFatalUserException (msg.msg (
"EXC_NoPMFConfigurableViaPropertiesOrXML",
- name,
- resourceLoader)); //NOI18N
+ name)); //NOI18N
}
+ protected static PersistenceManagerFactory
+ invokeGetPersistenceManagerFactoryOnImplementation(
+ String pmfClassName, Map overrides, Map properties, ClassLoader cl)
+ throws
+ ClassNotFoundException,
+ NoSuchMethodException,
+ IllegalAccessException,
+ InvocationTargetException
+ {
+ Class implClass = cl.loadClass(pmfClassName);
+ Method m = implClass.getMethod(
+ "getPersistenceManagerFactory",
+ new Class[] { Map.class, Map.class });
+
+ return (PersistenceManagerFactory) m.invoke(
+ null, new Object[] {overrides, properties});
+ }
+
+
+ /**
+ * Tries to get the PMF class name property in the following locations
+ * in this order:PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
+ * parameter "overrides"PROPERY_PERSISTENCE_MANAGER_FACTORY_CLASS in
+ * parameter "properties"META-INF/services/javax.jdo.PeristenceManagerFactoryPersistenceManagerFactory properties if
+ * found, null if not.
* @since 2.1
* @throws JDOFatalUserException if multiple named PMF property sets are
* found with the given name, or any other exception is encountered.
@@ -1008,14 +1133,6 @@
ClassLoader resourceLoader,
String jdoconfigResourceName
) {
- /* JDO 2.1:
- Attempt to find & return named PMF properties here.
- If name == null or name == "", then we're looking for the anonymous PMF.
-
- If we can't find it, this method returns null.
- */
- name = name == null ? "" : name.trim(); // for use as key in Maps
-
// key is PU name, value is Map of PU properties
Map/*