You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by cl...@apache.org on 2005/07/06 23:04:20 UTC
svn commit: r209514 - in /incubator/jdo/trunk/api20:
src/java/javax/jdo/Bundle.properties src/java/javax/jdo/JDOHelper.java
test/java/javax/jdo/JDOHelperTest.java
Author: clr
Date: Wed Jul 6 14:04:20 2005
New Revision: 209514
URL: http://svn.apache.org/viewcvs?rev=209514&view=rev
Log:
JDO-79 synchronize the JDOHelper with the latest specification
Modified:
incubator/jdo/trunk/api20/src/java/javax/jdo/Bundle.properties
incubator/jdo/trunk/api20/src/java/javax/jdo/JDOHelper.java
incubator/jdo/trunk/api20/test/java/javax/jdo/JDOHelperTest.java
Modified: incubator/jdo/trunk/api20/src/java/javax/jdo/Bundle.properties
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/src/java/javax/jdo/Bundle.properties?rev=209514&r1=209513&r2=209514&view=diff
==============================================================================
--- incubator/jdo/trunk/api20/src/java/javax/jdo/Bundle.properties (original)
+++ incubator/jdo/trunk/api20/src/java/javax/jdo/Bundle.properties Wed Jul 6 14:04:20 2005
@@ -20,29 +20,49 @@
ERR_StateManagerClassCast=An element of the parameter collection is of class {0}. \
The parameter collection must contain only elements that are instances of java.lang.Class.
ERR_NoMetadata=No metadata has been registered for class {0}.
-ERR_NoSuchMethod=Method getPersistenceManagerFactory(Properties) does not exist or is not public
-ERR_UnexpectedException=Unexpected exception caught
-EXC_ClassNotFound=Class {0} was not found
-EXC_IllegalAccess=Illegal Access for class {0}
-EXC_NoClassNameProperty=A property named PersistenceManagerFactoryClass must be specified.
-EXC_getPersistenceManagerFactory=Exception thrown by getPersistenceManagerFactory(Properties)
+EXC_GetPMFNoSuchMethod=Method getPersistenceManagerFactory(Properties) does not exist or is not public.
+EXC_GetPMFUnexpectedException=Unexpected exception caught.
+EXC_GetPMFClassNotFound=Class {0} was not found.
+EXC_GetPMFIllegalAccess=Illegal Access for class {0}.
+EXC_GetPMFNoClassNameProperty=A property named javax.jdo.PersistenceManagerFactoryClass must be specified.
MSG_FailedObject=FailedObject:
MSG_NestedThrowables=NestedThrowables:
MSG_NestedThrowablesStackTrace=NestedThrowablesStackTrace:
MSG_ExceptionGettingFailedToString=Exception getting failed.toString(): ''{0}''.
MSG_ExceptionGettingFailedToStringObjectId=Exception getting failed.toString(): ''{0}''. ObjectId of failed instance: ''{1}''.
ERR_NullClass=JDO implementation error: the parameter class object must not be null.
-EXC_NullResource: The resource name argument to this method cannot be null.
-EXC_NullLoader: The class loader argument to this method cannot be null.
-EXC_NoResource: The resource named "{0}" could not be found in the class loader "{1}".
-EXC_IOExceptionRsrc: An IOException was thrown while loading the resource \
+EXC_GetPMFNullResource: The resource name argument to this method cannot be null.
+EXC_GetPMFNullLoader: The class loader argument to this method cannot be null.
+EXC_GetPMFNullPropsLoader: The class loader argument to this method cannot be null.
+EXC_GetPMFNullPMFLoader: The class loader argument to this method cannot be null.
+EXC_GetPMFNullStream: The stream argument to this method cannot be null.
+EXC_GetPMFNoResource: The resource named "{0}" could not be found in the class loader "{1}".
+EXC_GetPMFIOExceptionRsrc: An IOException was thrown while loading the resource \
named "{0}" into a java.util.Properties object.
-EXC_NullFile: The file argument to this method cannot be null.
-EXC_NoFile: The file named "{0}" could not be found.
-EXC_IOExceptionFile: An IOException was thrown while loading the file \
+EXC_GetPMFIOExceptionStream: An IOException was thrown while loading the stream \
+into a java.util.Properties object.
+EXC_GetPMFNullFile: The file argument to this method cannot be null.
+EXC_GetPMFNoFile: The file named "{0}" could not be found.
+EXC_GetPMFIOExceptionFile: An IOException was thrown while loading the file \
named "{0}" into a java.util.Properties object.
-EXC_NullJndiLoc: The JNDI location argument to this method cannot be null.
-EXC_NamingException: A NamingException was thrown while obtaining the \
+EXC_GetPMFNullJndiLoc: The JNDI location argument to this method cannot be null.
+EXC_GetPMFNamingException: A NamingException was thrown while obtaining the \
PersistenceManagerFactory at "{0}" from JNDI.
+EXC_GetPMFNullPointerException: The PersistenceManagerFactory class must define a static \
+method \nPersistenceManagerFactory getPersistenceManagerFactory(Map props). \nThe class "{0}"\n\
+defines a non-static getPersistenceManagerFactory(Map props) method.
+EXC_GetPMFClassCastException: The PersistenceManagerFactory class must define a static \
+method \nPersistenceManagerFactory getPersistenceManagerFactory(Map props). \nThe class "{0}"\n\
+has the wrong return type for the getPersistenceManagerFactory(Map props) method.
EXC_StringWrongLength: There must be exactly one character in the id in the input String for CharIdentity.
EXC_IllegalEventType:The event type is outside the range of valid event types.
+EXC_ObjectIdentityStringConstruction: The instance could not be constructed from \
+the parameter String "{0}". \nThe exception thrown was: "{1}". \n\
+Parsing the class name as "{2}" and key as "{3}".
+EXC_ObjectIdentityStringConstructionNoDelimiter: Missing delimiter ":".
+EXC_ObjectIdentityStringConstructionTooShort: Parameter is too short.
+EXC_ObjectIdentityStringConstructionUsage: The instance could not be constructed \
+from the parameter String "{0}". \
+\nThe parameter String is of the form "<className>:<keyString>".
+EXC_CreateKeyAsObjectMustNotBeCalled: The method createKeyAsObject must not be called \
+because the keyAsObject field must never be null for this class.
Modified: incubator/jdo/trunk/api20/src/java/javax/jdo/JDOHelper.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/src/java/javax/jdo/JDOHelper.java?rev=209514&r1=209513&r2=209514&view=diff
==============================================================================
--- incubator/jdo/trunk/api20/src/java/javax/jdo/JDOHelper.java (original)
+++ incubator/jdo/trunk/api20/src/java/javax/jdo/JDOHelper.java Wed Jul 6 14:04:20 2005
@@ -30,8 +30,12 @@
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
import java.util.Properties;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
import javax.jdo.spi.I18NHelper;
import javax.jdo.spi.PersistenceCapable;
import javax.jdo.spi.StateManager; // for javadoc
@@ -47,12 +51,12 @@
* This class can be used by a JDO-aware application to call the JDO behavior
* of <code>PersistenceCapable</code> instances without declaring them to be
* <code>PersistenceCapable</code>.
- * It is also used to acquire a <code>PersistenceManagerFactory</code> via
+ * <P>It is also used to acquire a <code>PersistenceManagerFactory</code> via
* various methods.
* <P>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, or deleted; and to get its associated
+ * dirty, new, deleted, or detached; and to get its associated
* <code>PersistenceManager</code> if it has one.
*
* @version 2.0
@@ -138,7 +142,10 @@
}
/**
+ * Return the version of the instance.
* @since 2.0
+ * @param pc the instance
+ * @return the version of the instance
*/
public static Object getVersion (Object pc) {
return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoGetVersion():null;
@@ -227,15 +234,17 @@
return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDeleted():false;
}
- /** Tests whether the parameter instance has been detached.
- *
+ /**
+ * Tests whether the parameter instance has been detached.
+ *
* Instances that have been detached return true.
- *
- *<P>Transient instances return false.
- *<P>
+ *
+ * <P>Transient instances return false.
+ * <P>
* @see PersistenceCapable#jdoIsDetached()
* @return <code>true</code> if this instance is detached.
* @since 2.0
+ * @param pc the instance
*/
public static boolean isDetached(Object pc) {
return pc instanceof PersistenceCapable?((PersistenceCapable)pc).jdoIsDetached():false;
@@ -246,15 +255,16 @@
* <code>PersistenceManagerFactory</code> class.
* @return the <code>PersistenceManagerFactory</code>.
* @param props a <code>Properties</code> instance with properties of the <code>PersistenceManagerFactory</code>.
- * @see #getPersistenceManagerFactory(Properties,ClassLoader)
+ * @see #getPersistenceManagerFactory(Map,ClassLoader)
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (Properties props) {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ (Map props) {
+ ClassLoader cl = getContextClassLoader();
return getPersistenceManagerFactory (props, cl);
}
- /** Get a <code>PersistenceManagerFactory</code> based on a <code>Properties</code>
+ /**
+ * Get a <code>PersistenceManagerFactory</code> based on a <code>Properties</code>
* instance and a class loader.
* The following are standard key values:
* <BR><code>"javax.jdo.PersistenceManagerFactoryClass"
@@ -284,35 +294,74 @@
* instance must exactly match the requested properties.
* @return the <code>PersistenceManagerFactory</code>.
* @param props a <code>Properties</code> instance with properties of the <code>PersistenceManagerFactory</code>.
- * @param cl a class loader to use to load the <code>PersistenceManagerFactory</code> class.
+ * @param cl the class loader to use to load the <code>PersistenceManagerFactory</code> class
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (Properties props, ClassLoader cl) {
+ (Map props, ClassLoader cl) {
String pmfClassName = (String) props.get ("javax.jdo.PersistenceManagerFactoryClass"); //NOI18N
if (pmfClassName == null) {
- throw new JDOFatalUserException (msg.msg("EXC_NoClassNameProperty")); // NOI18N
+ throw new JDOFatalUserException (msg.msg("EXC_GetPMFNoClassNameProperty")); // NOI18N
}
+ Method propsMethod = null;
+ Exception propsGetMethodException = null;
+ Method mapMethod = null;
+ Exception mapGetMethodException = null;
+ Method pmfMethod = null;
try {
Class pmfClass = cl.loadClass (pmfClassName);
- Method pmfMethod = pmfClass.getMethod ("getPersistenceManagerFactory", //NOI18N
- new Class[] {Properties.class});
+ try {
+ propsMethod = pmfClass.getMethod("getPersistenceManagerFactory", //NOI18N
+ new Class[] {Properties.class});
+ } catch (NoSuchMethodException nsme) {
+ propsGetMethodException = new JDOFatalInternalException
+ (msg.msg("EXC_GetPMFNoSuchMethod"), nsme); //NOI18Nnsme;
+ }
+ try {
+ mapMethod = pmfClass.getMethod("getPersistenceManagerFactory", //NOI18N
+ new Class[] {Map.class});
+ } catch (NoSuchMethodException nsme) {
+ mapGetMethodException = new JDOFatalInternalException
+ (msg.msg("EXC_GetPMFNoSuchMethod"), nsme); //NOI18Nnsme;
+ }
+ /* If the parameter is not a Properties,
+ * we need a mapMethod or else throw an exception.
+ */
+ if (!(props instanceof Properties)) {
+ if (mapMethod != null) {
+ pmfMethod = mapMethod;
+ } else {
+ throw mapGetMethodException;
+ }
+ } else { // the parameter is a Properties; use either method.
+ if (mapMethod != null) {
+ pmfMethod = mapMethod;
+ } else if (propsMethod != null) {
+ pmfMethod = propsMethod;
+ } else {
+ throw mapGetMethodException;
+ }
+ }
return (PersistenceManagerFactory) pmfMethod.invoke (null, new Object[] {props});
} catch (ClassNotFoundException cnfe) {
- throw new JDOFatalUserException (msg.msg("EXC_ClassNotFound", pmfClassName), cnfe); //NOI18N
+ throw new JDOFatalUserException (msg.msg("EXC_GetPMFClassNotFound", pmfClassName), cnfe); //NOI18N
} catch (IllegalAccessException iae) {
- throw new JDOFatalUserException (msg.msg("EXC_IllegalAccess", pmfClassName), iae); //NOI18N
+ throw new JDOFatalUserException (msg.msg("EXC_GetPMFIllegalAccess", pmfClassName), iae); //NOI18N
} catch (NoSuchMethodException nsme) {
- throw new JDOFatalInternalException (msg.msg("ERR_NoSuchMethod"), nsme); //NOI18N
+ throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNoSuchMethod"), nsme); //NOI18N
} catch (InvocationTargetException ite) {
Throwable nested = ite.getTargetException();
if (nested instanceof JDOException) {
throw (JDOException)nested;
- } else throw new JDOFatalUserException (msg.msg("EXC_getPersistenceManagerFactory"), ite); //NOI18N
+ } else throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), ite); //NOI18N
+ } catch (NullPointerException e) {
+ throw new JDOFatalInternalException (msg.msg("EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
+ } catch (ClassCastException e) {
+ throw new JDOFatalInternalException (msg.msg("EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
} catch (Exception e) {
- throw new JDOFatalInternalException (msg.msg("ERR_UnexpectedException"), e); //NOI18N
+ throw new JDOFatalInternalException (msg.msg("EXC_GetPMFUnexpectedException"), e); //NOI18N
}
}
-
+
/**
* Returns a {@link PersistenceManagerFactory} configured based
* on the properties stored in the resource at
@@ -322,11 +371,13 @@
* <code>Thread.currentThread().getContextClassLoader()</code> as
* the <code>loader</code> argument.
* @since 2.0
+ * @param propsResource the resource containing the Properties
+ * @return the PersistenceManagerFactory
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
- (String propsResource) {
+ (String propsResource) {
return getPersistenceManagerFactory (propsResource,
- Thread.currentThread().getContextClassLoader());
+ getContextClassLoader());
}
/**
@@ -338,26 +389,50 @@
* <code>IOException</code>s thrown during resource loading will
* be wrapped in a {@link JDOFatalUserException}.
* @since 2.0
+ * @param propsResource the resource containing the Properties
+ * @param loader the class loader to use to load both the propsResource and the <code>PersistenceManagerFactory</code> class
+ * @return the PersistenceManagerFactory
*/
public static PersistenceManagerFactory getPersistenceManagerFactory
(String propsResource, ClassLoader loader) {
+ return getPersistenceManagerFactory(propsResource, loader, loader);
+ }
+
+ /**
+ * Returns a {@link PersistenceManagerFactory} configured based
+ * on the properties stored in the resource at
+ * <code>propsResource</code>. Loads the Properties via
+ * <code>propsLoader</code>, and creates a {@link
+ * PersistenceManagerFactory} with <code>pmfLoader</code>. Any
+ * <code>IOException</code>s thrown during resource loading will
+ * be wrapped in a {@link JDOFatalUserException}.
+ * @since 2.0
+ * @param propsResource the resource containing the Properties
+ * @param propsLoader the class loader to use to load the propsResource
+ * @param pmfLoader the class loader to use to load the <code>PersistenceManagerFactory</code> class
+ * @return the PersistenceManagerFactory
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (String propsResource, ClassLoader propsLoader, ClassLoader pmfLoader) {
if (propsResource == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullResource"));
- if (loader == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullLoader"));
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullResource")); //NOI18N
+ if (propsLoader == null)
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPropsLoader")); //NOI18N
+ if (pmfLoader == null)
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullPMFLoader")); //NOI18N
Properties props = new Properties ();
InputStream in = null;
try {
- in = loader.getResourceAsStream (propsResource);
+ in = propsLoader.getResourceAsStream (propsResource);
if (in == null)
throw new JDOFatalUserException
- (msg.msg ("EXC_NoResource", propsResource, loader));
+ (msg.msg ("EXC_GetPMFNoResource", propsResource, propsLoader)); //NOI18N
props.load (in);
} catch (IOException ioe) {
throw new JDOFatalUserException
- (msg.msg ("EXC_IOExceptionRsrc", propsResource), ioe);
+ (msg.msg ("EXC_GetPMFIOExceptionRsrc", propsResource), ioe); //NOI18N
}
finally {
if (in != null)
@@ -366,45 +441,47 @@
} catch (IOException ioe) { }
}
- return getPersistenceManagerFactory (props, loader);
+ return getPersistenceManagerFactory (props, pmfLoader);
}
- /**
- * Returns a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the file at
- * <code>propsResource</code>. This method is equivalent to
- * invoking {@link
- * #getPersistenceManagerFactory(File,ClassLoader)} with
- * <code>Thread.currentThread().getContextClassLoader()</code> as
- * the <code>loader</code> argument.
- *
- * @since 2.0
- */
+ /**
+ * Returns a {@link PersistenceManagerFactory} configured based
+ * on the properties stored in the file at
+ * <code>propsFile</code>. This method is equivalent to
+ * invoking {@link
+ * #getPersistenceManagerFactory(File,ClassLoader)} with
+ * <code>Thread.currentThread().getContextClassLoader()</code> as
+ * the <code>loader</code> argument.
+ * @since 2.0
+ * @param propsFile the file containing the Properties
+ * @return the PersistenceManagerFactory
+ */
public static PersistenceManagerFactory getPersistenceManagerFactory
- (File propsFile) {
+ (File propsFile) {
return getPersistenceManagerFactory (propsFile,
- Thread.currentThread().getContextClassLoader());
+ getContextClassLoader());
}
-
- /**
- * Returns a {@link PersistenceManagerFactory} configured based
- * on the properties stored in the file at
- * <code>propsResource</code>. Creates a {@link
- * PersistenceManagerFactory} with <code>loader</code>. Any
- * <code>IOException</code>s or
- * <code>FileNotFouldException</code>s thrown during resource
- * loading will be wrapped in a {@link JDOFatalUserException}.
- *
- * @since 2.0
- */
+ /**
+ * Returns a {@link PersistenceManagerFactory} configured based
+ * on the properties stored in the file at
+ * <code>propsFile</code>. Creates a {@link
+ * PersistenceManagerFactory} with <code>loader</code>. Any
+ * <code>IOException</code>s or
+ * <code>FileNotFoundException</code>s thrown during resource
+ * loading will be wrapped in a {@link JDOFatalUserException}.
+ * @since 2.0
+ * @param propsFile the file containing the Properties
+ * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
+ * @return the PersistenceManagerFactory
+ */
public static PersistenceManagerFactory getPersistenceManagerFactory
(File propsFile, ClassLoader loader) {
if (propsFile == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullFile"));
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullFile")); //NOI18N
if (loader == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullLoader"));
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
Properties props = new Properties ();
InputStream in = null;
try {
@@ -412,10 +489,10 @@
props.load (in);
} catch (FileNotFoundException fnfe) {
throw new JDOFatalUserException
- (msg.msg ("EXC_NoFile", propsFile, loader), fnfe);
+ (msg.msg ("EXC_GetPMFNoFile", propsFile, loader), fnfe); //NOI18N
} catch (IOException ioe) {
throw new JDOFatalUserException
- (msg.msg ("EXC_IOExceptionFile", propsFile), ioe);
+ (msg.msg ("EXC_GetPMFIOExceptionFile", propsFile), ioe); //NOI18N
} finally {
if (in != null)
try {
@@ -425,44 +502,47 @@
return getPersistenceManagerFactory (props, loader);
}
-
- /**
- * Returns a {@link PersistenceManagerFactory} at the JNDI
- * location specified by <code>jndiLocation</code> in the context
- * <code>context</code>. If <code>context</code> is
- * <code>null</code>, <code>new InitialContext()</code> will be
- * used. This method is equivalent to invoking {@link
- * #getPersistenceManagerFactory(String,Context,ClassLoader)}
- * with
- * <code>Thread.currentThread().getContextClassLoader()</code> as
- * the <code>loader</code> argument.
- *
- * @since 2.0
- */
+ /**
+ * Returns a {@link PersistenceManagerFactory} at the JNDI
+ * location specified by <code>jndiLocation</code> in the context
+ * <code>context</code>. If <code>context</code> is
+ * <code>null</code>, <code>new InitialContext()</code> will be
+ * used. This method is equivalent to invoking {@link
+ * #getPersistenceManagerFactory(String,Context,ClassLoader)}
+ * with <code>Thread.currentThread().getContextClassLoader()</code> as
+ * the <code>loader</code> argument.
+ * @since 2.0
+ * @param jndiLocation the JNDI location containing the PersistenceManagerFactory
+ * @param context the context in which to find the named PersistenceManagerFactory
+ * @return the PersistenceManagerFactory
+ */
public static PersistenceManagerFactory getPersistenceManagerFactory
(String jndiLocation, Context context) {
return getPersistenceManagerFactory (jndiLocation, context,
- Thread.currentThread ().getContextClassLoader ());
+ getContextClassLoader());
}
- /**
- * Returns a {@link PersistenceManagerFactory} at the JNDI
- * location specified by <code>jndiLocation</code> in the context
- * <code>context</code>. If <code>context</code> is
- * <code>null</code>, <code>new InitialContext()</code> will be
- * used. Creates a {@link PersistenceManagerFactory} with
- * <code>loader</code>. Any <code>NamingException</code>s thrown
- * will be wrapped in a {@link JDOFatalUserException}.
- *
- * @since 2.0
- */
+ /**
+ * Returns a {@link PersistenceManagerFactory} at the JNDI
+ * location specified by <code>jndiLocation</code> in the context
+ * <code>context</code>. If <code>context</code> is
+ * <code>null</code>, <code>new InitialContext()</code> will be
+ * used. Creates a {@link PersistenceManagerFactory} with
+ * <code>loader</code>. Any <code>NamingException</code>s thrown
+ * will be wrapped in a {@link JDOFatalUserException}.
+ * @since 2.0
+ * @param jndiLocation the JNDI location containing the PersistenceManagerFactory
+ * @param context the context in which to find the named PersistenceManagerFactory
+ * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
+ * @return the PersistenceManagerFactory
+ */
public static PersistenceManagerFactory getPersistenceManagerFactory
(String jndiLocation, Context context, ClassLoader loader) {
if (jndiLocation == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullJndiLoc"));
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullJndiLoc")); //NOI18N
if (loader == null)
- throw new JDOFatalUserException (msg.msg ("EXC_NullLoader"));
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
try {
if (context == null)
context = new InitialContext ();
@@ -472,7 +552,68 @@
(o, PersistenceManagerFactory.class);
} catch (NamingException ne) {
throw new JDOFatalUserException
- (msg.msg ("EXC_NamingException", jndiLocation, loader), ne);
+ (msg.msg ("EXC_GetPMFNamingException", jndiLocation, loader), ne); //NOI18N
+ }
+ }
+
+ /**
+ * Returns a {@link PersistenceManagerFactory} configured based
+ * on the Properties stored in the input stream at
+ * <code>stream</code>. This method is equivalent to
+ * invoking {@link
+ * #getPersistenceManagerFactory(InputStream,ClassLoader)} with
+ * <code>Thread.currentThread().getContextClassLoader()</code> as
+ * the <code>loader</code> argument.
+ * @since 2.0
+ * @param stream the stream containing the Properties
+ * @return the PersistenceManagerFactory
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (InputStream stream) {
+ return getPersistenceManagerFactory (stream,
+ getContextClassLoader());
+ }
+
+ /**
+ * Returns a {@link PersistenceManagerFactory} configured based
+ * on the Properties stored in the input stream at
+ * <code>stream</code>. Creates a {@link
+ * PersistenceManagerFactory} with <code>loader</code>. Any
+ * <code>IOException</code>s thrown during resource
+ * loading will be wrapped in a {@link JDOFatalUserException}.
+ * @since 2.0
+ * @param stream the stream containing the Properties
+ * @param loader the class loader to use to load the <code>PersistenceManagerFactory</code> class
+ * @return the PersistenceManagerFactory
+ */
+ public static PersistenceManagerFactory getPersistenceManagerFactory
+ (InputStream stream, ClassLoader loader) {
+ if (stream == null)
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullStream")); //NOI18N
+ if (loader == null)
+ throw new JDOFatalUserException (msg.msg ("EXC_GetPMFNullLoader")); //NOI18N
+ Properties props = new Properties ();
+ try {
+ props.load (stream);
+ } catch (IOException ioe) {
+ throw new JDOFatalUserException
+ (msg.msg ("EXC_GetPMFIOExceptionStream"), ioe); //NOI18N
}
+ return getPersistenceManagerFactory (props, loader);
+ }
+
+ /** Get the context class loader associated with the current thread.
+ * This is done in a doPrivileged block because it is a secure method.
+ * @return the current thread's context class loader.
+ * @since 2.0
+ */
+ private static ClassLoader getContextClassLoader() {
+ return (ClassLoader)AccessController.doPrivileged(
+ new PrivilegedAction () {
+ public Object run () {
+ return Thread.currentThread().getContextClassLoader();
+ }
+ }
+ );
}
}
Modified: incubator/jdo/trunk/api20/test/java/javax/jdo/JDOHelperTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/api20/test/java/javax/jdo/JDOHelperTest.java?rev=209514&r1=209513&r2=209514&view=diff
==============================================================================
--- incubator/jdo/trunk/api20/test/java/javax/jdo/JDOHelperTest.java (original)
+++ incubator/jdo/trunk/api20/test/java/javax/jdo/JDOHelperTest.java Wed Jul 6 14:04:20 2005
@@ -16,12 +16,20 @@
package javax.jdo;
+import java.io.File;
+import java.io.InputStream;
+
+import java.util.Map;
import java.util.Properties;
import javax.jdo.pc.PCPoint;
import javax.jdo.util.AbstractTest;
import javax.jdo.util.BatchTestRunner;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
/**
* Tests class javax.jdo.JDOHelper.
* <p>
@@ -121,10 +129,236 @@
// TBD test JDOHelper.isDeleted(pc) for persistent instance
}
+
+ /** Test null String resource with no class loader.
+ */
+ public void testGetPMFNullResource() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((String)null);
+ fail("Null resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
- /** */
- public void testGetPMF() {
- // test missing property javax.jdo.PersistenceManagerFactoryClass
+ /** Test null String resource with good class loader.
+ */
+ public void testGetPMFNullResourceGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((String)null, this.getClass().getClassLoader());
+ fail("Null resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad String resource with no class loader.
+ */
+ public void testGetPMFBadResource() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory("Whatever");
+ fail("Null resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null String resource with good class loader.
+ */
+ public void testGetPMFBadResourceGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory("Whatever", this.getClass().getClassLoader());
+ fail("Null resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null File resource with no class loader.
+ */
+ public void testGetPMFNullFile() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((File)null);
+ fail("Null file should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null File resource with good class loader.
+ */
+ public void testGetPMFNullFileGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((File)null, this.getClass().getClassLoader());
+ fail("Null file should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad File resource with no class loader.
+ */
+ public void testGetPMFBadFile() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(new File("Whatever"));
+ fail("Null file should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad File resource with good class loader.
+ */
+ public void testGetPMFBadFileGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(new File("Whatever"), this.getClass().getClassLoader());
+ fail("Null file should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null JNDI resource name with no class loader.
+ */
+ public void testGetPMFNullJNDI() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((String)null, getInitialContext());
+ fail("Null JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null JNDI resource name with good class loader.
+ */
+ public void testGetPMFNullJNDIGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((String)null, getInitialContext(), this.getClass().getClassLoader());
+ fail("Null JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad JNDI resource name with no class loader.
+ */
+ public void testGetPMFBadJNDI() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory("Whatever", getInitialContext());
+ fail("Bad JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad JNDI resource name with good class loader.
+ */
+ public void testGetPMFBadJNDIGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory("Whatever", getInitialContext(), this.getClass().getClassLoader());
+ fail("Bad JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null stream with no class loader.
+ */
+ public void testGetPMFNullStream() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((InputStream)null);
+ fail("Null JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null stream with good class loader.
+ */
+ public void testGetPMFNullStreamGoodClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory((InputStream)null, this.getClass().getClassLoader());
+ fail("Null JNDI resource name should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test null ClassLoader.
+ */
+ public void testGetPMFNullClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory
+ ("Whatever", (ClassLoader)null);
+ fail("Null ClassLoader should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test both null ClassLoaders.
+ */
+ public void testGetPMFBothNullClassLoader() {
+ PersistenceManagerFactory pmf = null;
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory
+ ("Whatever", (ClassLoader)null, (ClassLoader)null);
+ fail("Null ClassLoader should result in JDOFatalUserException");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test missing property javax.jdo.PersistenceManagerFactoryClass.
+ */
+ public void testGetPMFNoClassNameProperty() {
PersistenceManagerFactory pmf = null;
try {
pmf = JDOHelper.getPersistenceManagerFactory(new Properties());
@@ -134,9 +368,99 @@
if (verbose)
println("Caught expected exception " + ex);
}
+ }
- // TBD: valid PMF class
+ /** Test bad PMF class does not exist.
+ */
+ public void testBadPMFClassNotFound() {
+ PersistenceManagerFactory pmf = null;
+ Properties props = new Properties();
+ props.put("javax.jdo.PersistenceManagerFactoryClass",
+ "ThisClassDoesNotExist");
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(props);
+ fail("Bad PersistenceManagerFactoryClass should result in JDOFatalUserException ");
+ }
+ catch (JDOFatalUserException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
}
-}
+ /** Test bad PMF class no method getPersistenceManagerFactory.
+ */
+ public void testBadPMFNoGetPMFMethod() {
+ PersistenceManagerFactory pmf = null;
+ Properties props = new Properties();
+ props.put("javax.jdo.PersistenceManagerFactoryClass",
+ "javax.jdo.JDOHelperTest$BadPMFNoGetPMFMethod");
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(props);
+ fail("Bad PersistenceManagerFactoryClass should result in JDOFatalUserException ");
+ }
+ catch (JDOFatalInternalException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+ /** Test bad PMF class non-static getPMF method.
+ */
+ public void testBadPMFNonStaticGetPMFMethod() {
+ PersistenceManagerFactory pmf = null;
+ Properties props = new Properties();
+ props.put("javax.jdo.PersistenceManagerFactoryClass",
+ "javax.jdo.JDOHelperTest$BadPMFNonStaticGetPMFMethod");
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(props);
+ fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException ");
+ }
+ catch (JDOFatalInternalException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ /** Test bad PMF class doesn't implement PMF.
+ */
+ public void testBadPMFWrongReturnType() {
+ PersistenceManagerFactory pmf = null;
+ Properties props = new Properties();
+ props.put("javax.jdo.PersistenceManagerFactoryClass",
+ "javax.jdo.JDOHelperTest$BadPMFWrongReturnType");
+ try {
+ pmf = JDOHelper.getPersistenceManagerFactory(props);
+ fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException ");
+ }
+ catch (JDOFatalInternalException ex) {
+ if (verbose)
+ println("Caught expected exception " + ex);
+ }
+ }
+
+ private Context getInitialContext() {
+ try {
+ return new InitialContext();
+ } catch (NamingException ne) {
+ fail("Could not get Initial Context");
+ return null;
+ }
+ }
+
+ private class BadPMFNoGetPMFMethod {
+ }
+
+ private class BadPMFNonStaticGetPMFMethod {
+ public BadPMFNonStaticGetPMFMethod
+ getPersistenceManagerFactory(Map props) {
+ return new BadPMFNonStaticGetPMFMethod();
+ }
+ }
+
+ private static class BadPMFWrongReturnType {
+ public static BadPMFWrongReturnType
+ getPersistenceManagerFactory(Map props) {
+ return new BadPMFWrongReturnType();
+ }
+ }
+}