0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: */
0019: package org.apache.openjpa.lib.util;
0020:
0021: import java.io.File;
0022: import java.io.FileInputStream;
0023: import java.io.FileNotFoundException;
0024: import java.io.FileOutputStream;
0025: import java.io.IOException;
0026: import java.lang.reflect.AccessibleObject;
0027: import java.net.InetAddress;
0028: import java.net.MalformedURLException;
0029: import java.net.ServerSocket;
0030: import java.net.Socket;
0031: import java.net.URL;
0032: import java.net.URLConnection;
0033: import java.net.UnknownHostException;
0034: import java.security.AccessController;
0035: import java.security.PrivilegedAction;
0036: import java.security.PrivilegedExceptionAction;
0037: import java.util.zip.ZipFile;
0038:
0039: import serp.bytecode.BCClass;
0040: import serp.bytecode.BCClassLoader;
0041: import serp.bytecode.Code;
0042: import serp.bytecode.FieldInstruction;
0043: import serp.bytecode.Project;
0044:
0045: /**
0046: * Helper class to obtain the Privilege(Exception)Action object to perform
0047: * Java 2 doPrivilege security sensitive function call in the following
0048: * methods:
0049: * <ul>
0050: * <li>AccessibleObject.setAccessible
0051: * <li>Class.forName
0052: * <li>Class.getClassLoader
0053: * <li>Class.getDeclaredField
0054: * <li>Class.getDeclaredFields
0055: * <li>Class.getDeclaredMethod
0056: * <li>Class.getDeclaredMethods
0057: * <li>Class.getResource
0058: * <li>Class.newInstance
0059: * <li>ClassLoader.getParent
0060: * <li>ClassLoader.getResource
0061: * <li>ClassLoader.getResources
0062: * <li>ClassLoader.getSystemClassLoader
0063: * <li>File.delete
0064: * <li>File.exists
0065: * <li>File.getAbsoluteFile
0066: * <li>File.getAbsolutePath
0067: * <li>File.getCanonicalPath
0068: * <li>File.listFiles
0069: * <li>File.length
0070: * <li>File.isDirectory
0071: * <li>File.mkdirs
0072: * <li>File.renameTo
0073: * <li>File.toURL
0074: * <li>FileInputStream new
0075: * <li>FileOutputStream new
0076: * <li>System.getProperties
0077: * <li>InetAddress.getByName
0078: * <li>MultiClassLoader new
0079: * <li>ServerSocket new
0080: * <li>Socket new
0081: * <li>Socket.accept
0082: * <li>System.getProperty
0083: * <li>Thread.getContextClassLoader
0084: * <li>Thread new
0085: * <li>TemporaryClassLoader new
0086: * <li>URL.openStream
0087: * <li>URLConnection.getContent
0088: * <li>ZipFile new
0089: * <li>serp.bytecode.Code new
0090: * <li>serp.bytecode.BCClassLoader new
0091: * <li>serp.bytecode.BCClass.write
0092: * <li>serp.bytecode.BCClass.getFields
0093: * <li>serp.bytecode.FieldInstruction.getField
0094: * <li>serp.bytecode.Project.loadClass
0095: * </ul>
0096: *
0097: * If these methods are used, the following sample usage patterns should be
0098: * followed to ensure proper privilege is granted:
0099: * <xmp>
0100: * 1) No security risk method call. E.g.
0101: *
0102: * private static final String SEP = J2DoPrivHelper.getLineSeparator();
0103: *
0104: * 2) Methods with no exception thrown. PrivilegedAction is returned from
0105: * J2DoPrivHelper.*Action(). E.g.
0106: *
0107: * ClassLoader loader = (ClassLoader) AccessController.doPrivileged(
0108: * J2DoPrivHelper.getClassLoaderAction(clazz));
0109: *
0110: * ClassLoader loader = (ClassLoader) (System.getSecurityManager() == null)
0111: * ? clazz.getClassLoader()
0112: * : AccessController.doPrivileged(
0113: * J2DoPrivHelper.getClassLoaderAction(clazz));
0114: * 3) Methods with exception thrown. PrivilegedExceptionAction is returned
0115: * from J2DoPrivHelper.*Action(). E.g.
0116: *
0117: * try {
0118: * method = (Method) AccessController.doPrivileged(
0119: * J2DoPrivHelper.getDeclaredMethodAction(clazz, name, parameterType));
0120: * } catch (PrivilegedActionException pae) {
0121: * throw (NoSuchMethodException) pae.getException();
0122: * }
0123: *
0124: * try {
0125: * method = (System.getSecurityManager() == null)
0126: * ? clazz.getDeclaredMethod(name,parameterType)
0127: * : (Method) AccessController.doPrivileged(
0128: * J2DoPrivHelper.getDeclaredMethodAction(
0129: * clazz, name, parameterType));
0130: * } catch (PrivilegedActionException pae) {
0131: * throw (NoSuchMethodException) pae.getException()
0132: * }
0133: * </xmp>
0134: * @author Albert Lee
0135: */
0136:
0137: public abstract class J2DoPrivHelper {
0138: private static String lineSeparator = null;
0139: private static String pathSeparator = null;
0140:
0141: /**
0142: * Return the value of the "line.separator" system property.
0143: *
0144: * Requires security policy:
0145: * 'permission java.util.PropertyPermission "read";'
0146: */
0147: public static final String getLineSeparator() {
0148: if (lineSeparator == null) {
0149: lineSeparator = (String) AccessController
0150: .doPrivileged(new PrivilegedAction() {
0151: public Object run() {
0152: return System.getProperty("line.separator");
0153: }
0154: });
0155: }
0156: return lineSeparator;
0157: }
0158:
0159: /**
0160: * Return the value of the "path.separator" system property.
0161: *
0162: * Requires security policy:
0163: * 'permission java.util.PropertyPermission "read";'
0164: */
0165: public static final String getPathSeparator() {
0166: if (pathSeparator == null) {
0167: pathSeparator = (String) AccessController
0168: .doPrivileged(new PrivilegedAction() {
0169: public Object run() {
0170: return System.getProperty("path.separator");
0171: }
0172: });
0173: }
0174: return pathSeparator;
0175: }
0176:
0177: /**
0178: * Return a PrivilegeAction object for aObj.setAccessible().
0179: *
0180: * Requires security policy: 'permission java.lang.reflect.ReflectPermission
0181: * "suppressAccessChecks";'
0182: */
0183: public static final PrivilegedAction setAccessibleAction(
0184: final AccessibleObject aObj, final boolean flag) {
0185: return new PrivilegedAction() {
0186: public Object run() {
0187: aObj.setAccessible(flag);
0188: return (Object) null;
0189: }
0190: };
0191: }
0192:
0193: /**
0194: * Return a PrivilegeAction object for Class.forName().
0195: *
0196: * Notes: doPriv of Class.forName call is required only if the input
0197: * classloader argument is null. E.g.
0198: *
0199: * Class.forName("x", false, Collection.class.getClassLoader());
0200: *
0201: * Requires security policy: 'permission java.lang.RuntimePermission
0202: * "getClassLoader";'
0203: *
0204: * @return Classloader
0205: */
0206: public static final PrivilegedExceptionAction getForNameAction(
0207: final String className, final boolean initializeBoolean,
0208: final ClassLoader classLoader) {
0209: return new PrivilegedExceptionAction() {
0210: public Object run() throws ClassNotFoundException {
0211: return Class.forName(className, initializeBoolean,
0212: classLoader);
0213: }
0214: };
0215: }
0216:
0217: /**
0218: * Return a PrivilegeAction object for clazz.getClassloader().
0219: *
0220: * Notes: No doPrivilege wrapping is required in the caller if:
0221: * "the caller's class loader is not null and the caller's class loader
0222: * is not the same as or an ancestor of the class loader for the class
0223: * whose class loader is requested". E.g.
0224: *
0225: * this.getClass().getClassLoader();
0226: *
0227: * Requires security policy:
0228: * 'permission java.lang.RuntimePermission "getClassLoader";'
0229: *
0230: * @return Classloader
0231: */
0232: public static final PrivilegedAction getClassLoaderAction(
0233: final Class clazz) {
0234: return new PrivilegedAction() {
0235: public Object run() {
0236: return clazz.getClassLoader();
0237: }
0238: };
0239: }
0240:
0241: /**
0242: * Return a PrivilegedExceptionAction object for clazz.getDeclaredField().
0243: *
0244: * Requires security policy:
0245: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";'
0246: *
0247: * @return Field
0248: * @exception NoSuchFieldException
0249: */
0250: public static final PrivilegedExceptionAction getDeclaredFieldAction(
0251: final Class clazz, final String name) {
0252: return new PrivilegedExceptionAction() {
0253: public Object run() throws NoSuchFieldException {
0254: return clazz.getDeclaredField(name);
0255: }
0256: };
0257: }
0258:
0259: /**
0260: * Return a PrivilegeAction object for class.getDeclaredFields().
0261: *
0262: * Requires security policy:
0263: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";'
0264: *
0265: * @return Field[]
0266: */
0267: public static final PrivilegedAction getDeclaredFieldsAction(
0268: final Class clazz) {
0269: return new PrivilegedAction() {
0270: public Object run() {
0271: return clazz.getDeclaredFields();
0272: }
0273: };
0274: }
0275:
0276: /**
0277: * Return a PrivilegedExceptionAction object for clazz.getDeclaredMethod().
0278: *
0279: * Requires security policy
0280: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";'
0281: *
0282: * @return Method
0283: * @exception NoSuchMethodException
0284: */
0285: public static final PrivilegedExceptionAction getDeclaredMethodAction(
0286: final Class clazz, final String name,
0287: final Class[] parameterTypes) {
0288: return new PrivilegedExceptionAction() {
0289: public Object run() throws NoSuchMethodException {
0290: return clazz.getDeclaredMethod(name, parameterTypes);
0291: }
0292: };
0293: }
0294:
0295: /**
0296: * Return a PrivilegeAction object for clazz.getDeclaredMethods().
0297: *
0298: * Requires security policy:
0299: * 'permission java.lang.RuntimePermission "accessDeclaredMembers";'
0300: *
0301: * @return Method[]
0302: */
0303: public static final PrivilegedAction getDeclaredMethodsAction(
0304: final Class clazz) {
0305: return new PrivilegedAction() {
0306: public Object run() {
0307: return clazz.getDeclaredMethods();
0308: }
0309: };
0310: }
0311:
0312: /**
0313: * Return a PrivilegeAction object for clazz.getResource().
0314: *
0315: * Requires security policy:
0316: * 'permission java.io.FilePermission "read";'
0317: *
0318: * @return URL
0319: */
0320: public static final PrivilegedAction getResourceAction(
0321: final Class clazz, final String resource) {
0322: return new PrivilegedAction() {
0323: public Object run() {
0324: return clazz.getResource(resource);
0325: }
0326: };
0327: }
0328:
0329: /**
0330: * Return a PrivilegedExceptionAction object for clazz.newInstance().
0331: *
0332: * Requires security policy:
0333: * 'permission java.lang.RuntimePermission "getClassLoader";'
0334: *
0335: * @return Object
0336: * @exception IllegalAccessException
0337: * @exception InstantiationException
0338: */
0339: public static final PrivilegedExceptionAction newInstanceAction(
0340: final Class clazz) throws IllegalAccessException,
0341: InstantiationException {
0342: return new PrivilegedExceptionAction() {
0343: public Object run() throws IllegalAccessException,
0344: InstantiationException {
0345: return clazz.newInstance();
0346: }
0347: };
0348: }
0349:
0350: /**
0351: * Return a PrivilegeAction object for loader.getParent().
0352: *
0353: * Requires security policy:
0354: * 'permission java.lang.RuntimePermission "getClassLoader";'
0355: *
0356: * @return ClassLoader
0357: */
0358: public static final PrivilegedAction getParentAction(
0359: final ClassLoader loader) {
0360: return new PrivilegedAction() {
0361: public Object run() {
0362: return loader.getParent();
0363: }
0364: };
0365: }
0366:
0367: /**
0368: * Return a PrivilegeAction object for loader.getResource().
0369: *
0370: * Requires security policy:
0371: * 'permission java.io.FilePermission "read";'
0372: *
0373: * @return URL
0374: */
0375: public static final PrivilegedAction getResourceAction(
0376: final ClassLoader loader, final String resource) {
0377: return new PrivilegedAction() {
0378: public Object run() {
0379: return loader.getResource(resource);
0380: }
0381: };
0382: }
0383:
0384: /**
0385: * Return a PrivilegedExceptionAction object for loader.getResources().
0386: *
0387: * Requires security policy:
0388: * 'permission java.io.FilePermission "read";'
0389: *
0390: * @return Enumeration
0391: * @exception IOException
0392: */
0393: public static final PrivilegedExceptionAction getResourcesAction(
0394: final ClassLoader loader, final String resource)
0395: throws IOException {
0396: return new PrivilegedExceptionAction() {
0397: public Object run() throws IOException {
0398: return loader.getResources(resource);
0399: }
0400: };
0401: }
0402:
0403: /**
0404: * Return a PrivilegeAction object for ClassLoader.getSystemClassLoader().
0405: *
0406: * Requires security policy:
0407: * 'permission java.lang.RuntimePermission "getClassLoader";'
0408: *
0409: * @return ClassLoader
0410: */
0411: public static final PrivilegedAction getSystemClassLoaderAction() {
0412: return new PrivilegedAction() {
0413: public Object run() {
0414: return ClassLoader.getSystemClassLoader();
0415: }
0416: };
0417: }
0418:
0419: /**
0420: * Return a PrivilegeAction object for f.delete().
0421: *
0422: * Requires security policy:
0423: * 'permission java.io.FilePermission "delete";'
0424: *
0425: * @return Boolean
0426: */
0427: public static final PrivilegedAction deleteAction(final File f) {
0428: return new PrivilegedAction() {
0429: public Object run() {
0430: return f.delete() ? Boolean.TRUE : Boolean.FALSE;
0431: }
0432: };
0433: }
0434:
0435: /**
0436: * Return a PrivilegeAction object for f.exists().
0437: *
0438: * Requires security policy:
0439: * 'permission java.io.FilePermission "read";'
0440: *
0441: * @return Boolean
0442: */
0443: public static final PrivilegedAction existsAction(final File f) {
0444: return new PrivilegedAction() {
0445: public Object run() {
0446: try {
0447: return f.exists() ? Boolean.TRUE : Boolean.FALSE;
0448: } catch (NullPointerException npe) {
0449: return Boolean.FALSE;
0450: }
0451: }
0452: };
0453: }
0454:
0455: /**
0456: * Return a PrivilegeAction object for f.getAbsoluteFile().
0457: *
0458: * Requires security policy:
0459: * 'permission java.util.PropertyPermission "read";'
0460: *
0461: * @return File
0462: */
0463: public static final PrivilegedAction getAbsoluteFileAction(
0464: final File f) {
0465: return new PrivilegedAction() {
0466: public Object run() {
0467: return f.getAbsoluteFile();
0468: }
0469: };
0470: }
0471:
0472: /**
0473: * Return a PrivilegeAction object for f.getAbsolutePath().
0474: *
0475: * Requires security policy:
0476: * 'permission java.util.PropertyPermission "read";'
0477: *
0478: * @return String
0479: */
0480: public static final PrivilegedAction getAbsolutePathAction(
0481: final File f) {
0482: return new PrivilegedAction() {
0483: public Object run() {
0484: return f.getAbsolutePath();
0485: }
0486: };
0487: }
0488:
0489: /**
0490: * Return a PrivilegedExceptionAction object for f.getCanonicalPath().
0491: *
0492: * Requires security policy:
0493: * 'permission java.util.PropertyPermission "read";'
0494: *
0495: * @return String
0496: * @exception IOException
0497: */
0498: public static final PrivilegedExceptionAction getCanonicalPathAction(
0499: final File f) throws IOException {
0500: return new PrivilegedExceptionAction() {
0501: public Object run() throws IOException {
0502: return f.getCanonicalPath();
0503: }
0504: };
0505: }
0506:
0507: /**
0508: * Return a PrivilegeAction object for f.isDirectory().
0509: *
0510: * Requires security policy:
0511: * 'permission java.io.FilePermission "read";'
0512: *
0513: * @return Boolean
0514: */
0515: public static final PrivilegedAction isDirectoryAction(final File f) {
0516: return new PrivilegedAction() {
0517: public Object run() {
0518: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
0519: }
0520: };
0521: }
0522:
0523: /**
0524: * Return a PrivilegeAction object for f.isFile().
0525: *
0526: * Requires security policy:
0527: * 'permission java.io.FilePermission "read";'
0528: *
0529: * @return Boolean
0530: */
0531: public static final PrivilegedAction isFileAction(final File f) {
0532: return new PrivilegedAction() {
0533: public Object run() {
0534: return f.isFile() ? Boolean.TRUE : Boolean.FALSE;
0535: }
0536: };
0537: }
0538:
0539: /**
0540: * Return a PrivilegeAction object for f.length().
0541: *
0542: * Requires security policy:
0543: * 'permission java.io.FilePermission "read";'
0544: *
0545: * @return Long
0546: */
0547: public static final PrivilegedAction lengthAction(final File f) {
0548: return new PrivilegedAction() {
0549: public Object run() {
0550: return new Long(f.length());
0551: }
0552: };
0553: }
0554:
0555: /**
0556: * Return a PrivilegeAction object for f.listFiles().
0557: *
0558: * Requires security policy:
0559: * 'permission java.io.FilePermission "read";'
0560: *
0561: * @return File[]
0562: */
0563: public static final PrivilegedAction listFilesAction(final File f) {
0564: return new PrivilegedAction() {
0565: public Object run() {
0566: return f.listFiles();
0567: }
0568: };
0569: }
0570:
0571: /**
0572: * Return a PrivilegeAction object for f.mkdirs().
0573: *
0574: * Requires security policy:
0575: * 'permission java.io.FilePermission "write";'
0576: *
0577: * @return Boolean
0578: */
0579: public static final PrivilegedAction mkdirsAction(final File f) {
0580: return new PrivilegedAction() {
0581: public Object run() {
0582: return f.mkdirs() ? Boolean.TRUE : Boolean.FALSE;
0583: }
0584: };
0585: }
0586:
0587: /**
0588: * Return a PrivilegeAction object for f.renameTo().
0589: *
0590: * Requires security policy:
0591: * 'permission java.io.FilePermission "write";'
0592: *
0593: * @return Boolean
0594: */
0595: public static final PrivilegedAction renameToAction(
0596: final File from, final File to) {
0597: return new PrivilegedAction() {
0598: public Object run() {
0599: return from.renameTo(to) ? Boolean.TRUE : Boolean.FALSE;
0600: }
0601: };
0602: }
0603:
0604: /**
0605: * Return a PrivilegedExceptionAction object for f.toURL().
0606: *
0607: * Requires security policy:
0608: * 'permission java.io.FilePermission "read";'
0609: *
0610: * @return Boolean
0611: * @throws MalformedURLException
0612: */
0613: public static final PrivilegedExceptionAction toURLAction(
0614: final File file) throws MalformedURLException {
0615: return new PrivilegedExceptionAction() {
0616: public Object run() throws MalformedURLException {
0617: return file.toURL();
0618: }
0619: };
0620: }
0621:
0622: /**
0623: * Return a PrivilegedExceptionAction object for new FileInputStream().
0624: *
0625: * Requires security policy:
0626: * 'permission java.io.FilePermission "read";'
0627: *
0628: * @return FileInputStream
0629: * @throws FileNotFoundException
0630: */
0631: public static final PrivilegedExceptionAction newFileInputStreamAction(
0632: final File f) throws FileNotFoundException {
0633: return new PrivilegedExceptionAction() {
0634: public Object run() throws FileNotFoundException {
0635: return new FileInputStream(f);
0636: }
0637: };
0638: }
0639:
0640: /**
0641: * Return a PrivilegedExceptionAction object for new FileOutputStream().
0642: *
0643: * Requires security policy:
0644: * 'permission java.io.FilePermission "write";'
0645: *
0646: * @return FileOutputStream
0647: * @throws FileNotFoundException
0648: */
0649: public static final PrivilegedExceptionAction newFileOutputStreamAction(
0650: final File f) throws FileNotFoundException {
0651: return new PrivilegedExceptionAction() {
0652: public Object run() throws FileNotFoundException {
0653: return new FileOutputStream(f);
0654: }
0655: };
0656: }
0657:
0658: /**
0659: * Return a PrivilegedExceptionAction object for new FileOutputStream().
0660: *
0661: * Requires security policy:
0662: * 'permission java.io.FilePermission "write";'
0663: *
0664: * @return FileOutputStream
0665: * @throws FileNotFoundException
0666: */
0667: public static final PrivilegedExceptionAction newFileOutputStreamAction(
0668: final String f, final boolean append)
0669: throws FileNotFoundException {
0670: return new PrivilegedExceptionAction() {
0671: public Object run() throws FileNotFoundException {
0672: return new FileOutputStream(f, append);
0673: }
0674: };
0675: }
0676:
0677: /**
0678: * Return a PrivilegedExceptionAction object for InetAdress.getByName().
0679: *
0680: * Requires security policy:
0681: * 'permission java.net.SocketPermission "connect";'
0682: *
0683: * @return InetAddress
0684: * @throws UnknownHostException
0685: */
0686: public static final PrivilegedExceptionAction getByNameAction(
0687: final String hostname) throws UnknownHostException {
0688: return new PrivilegedExceptionAction() {
0689: public Object run() throws UnknownHostException {
0690: return InetAddress.getByName(hostname);
0691: }
0692: };
0693: }
0694:
0695: /**
0696: * Return a PrivilegedExceptionAction object for new Socket().
0697: *
0698: * Requires security policy:
0699: * 'permission java.net.SocketPermission "connect";'
0700: *
0701: * @return Socket
0702: * @throws IOException
0703: */
0704: public static final PrivilegedExceptionAction newSocketAction(
0705: final InetAddress host, final int port) throws IOException {
0706: return new PrivilegedExceptionAction() {
0707: public Object run() throws IOException {
0708: return new Socket(host, port);
0709: }
0710: };
0711: }
0712:
0713: /**
0714: * Return a PrivilegedExceptionAction object for new ServerSocket().
0715: *
0716: * Requires security policy:
0717: * 'permission java.net.SocketPermission "listen";'
0718: *
0719: * @return ServerSocket
0720: * @throws IOException
0721: */
0722: public static final PrivilegedExceptionAction newServerSocketAction(
0723: final int port) throws IOException {
0724: return new PrivilegedExceptionAction() {
0725: public Object run() throws IOException {
0726: return new ServerSocket(port);
0727: }
0728: };
0729: }
0730:
0731: /**
0732: * Return a PrivilegedExceptionAction object for ServerSocket.accept().
0733: *
0734: * Requires security policy:
0735: * 'permission java.net.SocketPermission "listen";'
0736: *
0737: * @return Socket
0738: * @throws IOException
0739: */
0740: public static final PrivilegedExceptionAction acceptAction(
0741: final ServerSocket ss) throws IOException {
0742: return new PrivilegedExceptionAction() {
0743: public Object run() throws IOException {
0744: return ss.accept();
0745: }
0746: };
0747: }
0748:
0749: /**
0750: * Return a PrivilegeAction object for System.getProperties().
0751: *
0752: * Requires security policy:
0753: * 'permission java.util.PropertyPermission "read";'
0754: *
0755: * @return Properties
0756: */
0757: public static final PrivilegedAction getPropertiesAction() {
0758: return new PrivilegedAction() {
0759: public Object run() {
0760: return System.getProperties();
0761: }
0762: };
0763: }
0764:
0765: /**
0766: * Return a PrivilegeAction object for System.getProperty().
0767: *
0768: * Requires security policy:
0769: * 'permission java.util.PropertyPermission "read";'
0770: *
0771: * @return String
0772: */
0773: public static final PrivilegedAction getPropertyAction(
0774: final String name) {
0775: return new PrivilegedAction() {
0776: public Object run() {
0777: return System.getProperty(name);
0778: }
0779: };
0780: }
0781:
0782: /**
0783: * Return a PrivilegeAction object for Thread.currentThread
0784: * .getContextClassLoader().
0785: *
0786: * Requires security policy:
0787: * 'permission java.lang.RuntimePermission "getClassLoader";'
0788: *
0789: * @return ClassLoader
0790: */
0791: public static final PrivilegedAction getContextClassLoaderAction() {
0792: return new PrivilegedAction() {
0793: public Object run() {
0794: return Thread.currentThread().getContextClassLoader();
0795: }
0796: };
0797: }
0798:
0799: /**
0800: * Return a PrivilegedAction object for new Thread().
0801: *
0802: * Requires security policy:
0803: * 'permission java.lang.RuntimePermission "modifyThreadGroup";'
0804: * 'permission java.lang.RuntimePermission "modifyThread";'
0805: *
0806: * @return Thread
0807: */
0808: public static final PrivilegedAction newDaemonThreadAction(
0809: final Runnable target, final String name) {
0810: return new PrivilegedAction() {
0811: public Object run() {
0812: Thread thread = new Thread(target, name);
0813: thread.setDaemon(true);
0814: return thread;
0815: }
0816: };
0817: }
0818:
0819: /**
0820: * Return a PrivilegedExceptionAction object for url.openStream().
0821: *
0822: * Requires security policy:
0823: * 'permission java.io.FilePermission "read";'
0824: *
0825: * @return InputStream
0826: * @throws IOException
0827: */
0828: public static final PrivilegedExceptionAction openStreamAction(
0829: final URL url) throws IOException {
0830: return new PrivilegedExceptionAction() {
0831: public Object run() throws IOException {
0832: return url.openStream();
0833: }
0834: };
0835: }
0836:
0837: /**
0838: * Return a PrivilegedExceptionAction object con.getContent().
0839: *
0840: * Requires security policy:
0841: * 'permission java.io.FilePermission "read";'
0842: *
0843: * @return Object
0844: * @throws IOException
0845: */
0846: public static final PrivilegedExceptionAction getContentAction(
0847: final URLConnection con) throws IOException {
0848: return new PrivilegedExceptionAction() {
0849: public Object run() throws IOException {
0850: return con.getContent();
0851: }
0852: };
0853: }
0854:
0855: /**
0856: * Return a PrivilegedExceptionAction object for new ZipFile().
0857: *
0858: * Requires security policy:
0859: * 'permission java.io.FilePermission "read";'
0860: *
0861: * @return ZipFile
0862: * @throws IOException
0863: */
0864: public static final PrivilegedExceptionAction newZipFileAction(
0865: final File f) throws IOException {
0866: return new PrivilegedExceptionAction() {
0867: public Object run() throws IOException {
0868: return new ZipFile(f);
0869: }
0870: };
0871: }
0872:
0873: /**
0874: * Return a PrivilegeAction object for new serp.bytecode.Code().
0875: *
0876: * Requires security policy:
0877: * 'permission java.lang.RuntimePermission "getClassLoader";'
0878: *
0879: * @return serp.bytecode.Code
0880: */
0881: public static final PrivilegedAction newCodeAction() {
0882: return new PrivilegedAction() {
0883: public Object run() {
0884: return new Code();
0885: }
0886: };
0887: }
0888:
0889: /**
0890: * Return a PrivilegeAction object for new TemporaryClassLoader().
0891: *
0892: * Requires security policy:
0893: * 'permission java.lang.RuntimePermission "createClassLoader";'
0894: *
0895: * @return TemporaryClassLoader
0896: */
0897: public static final PrivilegedAction newTemporaryClassLoaderAction(
0898: final ClassLoader parent) {
0899: return new PrivilegedAction() {
0900: public Object run() {
0901: return new TemporaryClassLoader(parent);
0902: }
0903: };
0904: }
0905:
0906: /**
0907: * Return a PrivilegeAction object for new MultiClassLoader().
0908: *
0909: * Requires security policy:
0910: * 'permission java.lang.RuntimePermission "createClassLoader";'
0911: *
0912: * @return MultiClassLoader
0913: */
0914: public static final PrivilegedAction newMultiClassLoaderAction() {
0915: return new PrivilegedAction() {
0916: public Object run() {
0917: return new MultiClassLoader();
0918: }
0919: };
0920: }
0921:
0922: /**
0923: * Return a PrivilegeAction object for new BCClassLoader().
0924: *
0925: * Requires security policy:
0926: * 'permission java.lang.RuntimePermission "createClassLoader";'
0927: *
0928: * @return BCClassLoader
0929: */
0930: public static final PrivilegedAction newBCClassLoaderAction(
0931: final Project project, final ClassLoader parent) {
0932: return new PrivilegedAction() {
0933: public Object run() {
0934: return new BCClassLoader(project, parent);
0935: }
0936: };
0937: }
0938:
0939: public static final PrivilegedAction newBCClassLoaderAction(
0940: final Project project) {
0941: return new PrivilegedAction() {
0942: public Object run() {
0943: return new BCClassLoader(project);
0944: }
0945: };
0946: }
0947:
0948: /**
0949: * Return a PrivilegedExceptionAction object for BCClass.write().
0950: *
0951: * Requires security policy:
0952: * 'permission java.io.FilePermission "write";'
0953: *
0954: * @return File
0955: * @throws IOException
0956: */
0957: public static final PrivilegedExceptionAction bCClassWrite(
0958: final BCClass bc, final File f) throws IOException {
0959: return new PrivilegedExceptionAction() {
0960: public Object run() throws IOException {
0961: bc.write(f);
0962: return null;
0963: }
0964: };
0965: }
0966:
0967: /**
0968: * Return a PrivilegeAction object for BCClass.getFields().
0969: *
0970: * Requires security policy:
0971: * 'permission java.lang.RuntimePermission "getClassLoader";'
0972: *
0973: * @return BCField
0974: */
0975: public static final PrivilegedAction getBCClassFieldsAction(
0976: final BCClass bcClass, final String fieldName) {
0977: return new PrivilegedAction() {
0978: public Object run() {
0979: return bcClass.getFields(fieldName);
0980: }
0981: };
0982: }
0983:
0984: /**
0985: * Return a PrivilegeAction object for FieldInstruction.getField().
0986: *
0987: * Requires security policy:
0988: * 'permission java.lang.RuntimePermission "getClassLoader";'
0989: *
0990: * @return BCField
0991: */
0992: public static final PrivilegedAction getFieldInstructionFieldAction(
0993: final FieldInstruction instruction) {
0994: return new PrivilegedAction() {
0995: public Object run() {
0996: return instruction.getField();
0997: }
0998: };
0999: }
1000:
1001: /**
1002: * Return a PrivilegeAction object for Project.loadClass().
1003: *
1004: * Requires security policy:
1005: * 'permission java.lang.RuntimePermission "createClassLoader";'
1006: *
1007: * @return BCClass
1008: */
1009: public static final PrivilegedAction loadProjectClassAction(
1010: final Project project, final Class clazz) {
1011: return new PrivilegedAction() {
1012: public Object run() {
1013: return project.loadClass(clazz);
1014: }
1015: };
1016: }
1017:
1018: /**
1019: * Return a PrivilegeAction object for Project.loadClass().
1020: *
1021: * Requires security policy:
1022: * 'permission java.lang.RuntimePermission "getClassLoader";'
1023: *
1024: * @return BCClass
1025: */
1026: public static final PrivilegedAction loadProjectClassAction(
1027: final Project project, final String clazzName) {
1028: return new PrivilegedAction() {
1029: public Object run() {
1030: return project.loadClass(clazzName);
1031: }
1032: };
1033: }
1034: }
|