All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zeroturnaround.javarebel.integration.generic.RestrictedResource Maven / Gradle / Ivy

The newest version!
package org.zeroturnaround.javarebel.integration.generic;

import java.net.URL;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.zeroturnaround.javarebel.Resource;

/**
 * @author Rein Raudjärv
 * 
 * @see RestrictedClassResourceSource
 */
public class RestrictedResource implements Resource {
  
  public static Resource newInstance(AccessControlContext acc, Resource target) {
    if (acc == null || target == null)
      return null;
    
    return new RestrictedResource(acc, target);
  }
  
  private final AccessControlContext acc;
  private final Resource target;
  
  private RestrictedResource(AccessControlContext acc, Resource target) {
    this.acc = acc;
    this.target = target;
  }
  
  public Resource getTarget() {
    return target;
  }

  public byte[] getBytes() {
    return (byte[]) AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
        return target.getBytes();
      }
    }, acc);
  }

  public long lastModified() {
    Long obj = (Long) AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
        return new Long(target.lastModified());
      }
    }, acc);
    return obj.longValue();
  }

  public URL toURL() {
    return (URL) AccessController.doPrivileged(new PrivilegedAction() {
      public Object run() {
        return target.toURL();
      }
    }, acc);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy