Index: api2/src/java/javax/jdo/JDOHelper.java =================================================================== --- api2/src/java/javax/jdo/JDOHelper.java (revision 613500) +++ api2/src/java/javax/jdo/JDOHelper.java (working copy) @@ -50,6 +50,8 @@ import java.net.URL; import java.security.AccessController; import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; import java.util.Map; import java.util.HashMap; import java.util.Collections; @@ -1010,7 +1012,7 @@ throw new JDOFatalUserException (msg.msg ( "EXC_GetPMFNullPropsLoader")); //NOI18N try { - in = resourceLoader.getResourceAsStream(name); + in = getResourceAsStream(resourceLoader, name); if (in != null) { // then some kind of resource was found by the given name; // assume that it's a properties file and proceed as usual @@ -1114,11 +1116,11 @@ InvocationTargetException { Class implClass = cl.loadClass(pmfClassName); - Method m = implClass.getMethod( + Method m = getMethod(implClass, "getPersistenceManagerFactory", new Class[] { Map.class, Map.class }); - return (PersistenceManagerFactory) m.invoke( + return (PersistenceManagerFactory) invoke(m, null, new Object[] {overrides, properties}); } @@ -1217,7 +1219,7 @@ // get all JDO configurations Enumeration resources = - resourceLoader.getResources(jdoconfigResourceName); + getResources(resourceLoader, jdoconfigResourceName); if (resources.hasMoreElements()) { ArrayList processedResources = new ArrayList(); @@ -1376,17 +1378,17 @@ Class persistenceExceptionClass = null; Method createEntityManagerFactoryMethod = null; try { - persistenceClass = Class.forName( + persistenceClass = forName( "javax.persistence.Persistence", true, loader); - createEntityManagerFactoryMethod = persistenceClass.getMethod( + createEntityManagerFactoryMethod = getMethod(persistenceClass, "createEntityManagerFactory", new Class[] { String.class, Map.class }); persistenceExceptionClass = - Class.forName( + forName( "javax.persistence.PersistenceException", true, loader); @@ -1402,7 +1404,7 @@ Throwable t = null; try { entityManagerFactory = - createEntityManagerFactoryMethod.invoke( + invoke(createEntityManagerFactoryMethod, persistenceClass, new Object[] { name, properties }); } catch (InvocationTargetException x) { @@ -1465,7 +1467,7 @@ DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(getErrorHandler()); - in = url.openStream(); + in = openStream(url); Document doc = builder.parse(in); Element root = doc.getDocumentElement(); @@ -1906,4 +1908,142 @@ } ); } + + /** Get the named resource as a stream from the resource loader. + * Perform this operation in a doPrivileged block. + */ + private static InputStream getResourceAsStream( + final ClassLoader resourceLoader, final String name) { + return (InputStream)AccessController.doPrivileged( + new PrivilegedAction() { + public Object run() { + return resourceLoader.getResourceAsStream(name); + } + } + ); + } + + + /** Get the named Method from the named class. + * Perform this operation in a doPrivileged block. + * + * @param implClass the class + * @param methodName the name of the method + * @param parameterTypes the parameter types of the method + * @return + */ + private static Method getMethod( + final Class implClass, + final String methodName, + final Class[] parameterTypes) + throws NoSuchMethodException { + try { + return (Method) AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws NoSuchMethodException { + return implClass.getMethod(methodName, parameterTypes); + } + } + ); + } catch (PrivilegedActionException ex) { + throw (NoSuchMethodException)ex.getException(); + } + } + + /** Invoke the method. + * Perform this operation in a doPrivileged block. + * + */ + private static Object invoke(final Method method, + final Object instance, final Object[] parameters) + throws IllegalAccessException, InvocationTargetException { + try { + return (Object) AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() + throws IllegalAccessException, + InvocationTargetException { + return method.invoke (instance, parameters); + } + } + ); + } catch (PrivilegedActionException ex) { + Exception cause = (Exception)ex.getException(); + if (cause instanceof IllegalAccessException) + throw (IllegalAccessException)cause; + else //if (cause instanceof InvocationTargetException) + throw (InvocationTargetException)cause; + } + } + + /** Get resources of the resource loader. + * Perform this operation in a doPrivileged block. + * @param resourceLoader + * @param jdoconfigResourceName + * @return + */ + private static Enumeration getResources( + final ClassLoader resourceLoader, + final String jdoconfigResourceName) + throws IOException { + try { + return (Enumeration) AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws IOException { + return resourceLoader.getResources(jdoconfigResourceName); + } + } + ); + } catch (PrivilegedActionException ex) { + throw (IOException)ex.getException(); + } + } + + /** Get the named class. + * Perform this operation in a doPrivileged block. + * + * @param name + * @param init + * @param loader + * @return + */ + private static Class forName( + final String name, + final boolean init, + final ClassLoader loader) + throws ClassNotFoundException { + try { + return (Class) AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws ClassNotFoundException { + return Class.forName(name, init, loader); + } + } + ); + } catch (PrivilegedActionException ex) { + throw (ClassNotFoundException)ex.getException(); + } + } + + /** Open an input stream on the url. + * Perform this operation in a doPrivileged block. + * + * @param url + * @return + */ + private static InputStream openStream(final URL url) + throws IOException { + try { + return (InputStream) AccessController.doPrivileged( + new PrivilegedExceptionAction() { + public Object run() throws IOException { + return url.openStream(); + } + } + ); + } catch (PrivilegedActionException ex) { + throw (IOException)ex.getException(); + } + } + }