public 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 e) {
     throw (IOException) e.getException();
   }
 }
 public static InputStream getResourceAsStream(final Class c, final String name)
     throws IOException {
   try {
     return (InputStream)
         AccessController.doPrivileged(
             new PrivilegedExceptionAction() {
               public Object run() throws IOException {
                 return c.getResourceAsStream(name);
               }
             });
   } catch (PrivilegedActionException e) {
     throw (IOException) e.getException();
   }
 }
 public URL getResource(final String name) {
   try {
     return (URL)
         AccessController.doPrivileged(
             new PrivilegedExceptionAction<Object>() {
               public Object run() throws Exception {
                 return httpContext.getResource(name);
               }
             },
             acc);
   } catch (PrivilegedActionException e) {
     log(e.getException().getMessage(), e.getException());
   }
   return null;
 }
Exemplo n.º 4
0
 /** Invoke a method on an object using reflection. */
 public static Object invoke(final Object target, final Method method, final Object... args) {
   try {
     return AccessController.doPrivileged(
         new PrivilegedExceptionAction<Object>() {
           public Object run() throws IllegalAccessException, InvocationTargetException {
             if (!method.isAccessible())
               method.setAccessible(
                   true); // Say please - else invoke() will fail if method is protected
             return method.invoke(target, args);
           }
         });
   } catch (PrivilegedActionException e) {
     e.printStackTrace();
   }
   return null;
 }