|
Java example source code file (FilePermission.java)
The FilePermission.java Java example source code/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.io; import java.security.*; import java.util.Enumeration; import java.util.List; import java.util.ArrayList; import java.util.Vector; import java.util.Collections; import sun.security.util.SecurityConstants; /** * This class represents access to a file or directory. A FilePermission consists * of a pathname and a set of actions valid for that pathname. * <P> * Pathname is the pathname of the file or directory granted the specified * actions. A pathname that ends in "/*" (where "/" is * the file separator character, <code>File.separatorChar) indicates * all the files and directories contained in that directory. A pathname * that ends with "/-" indicates (recursively) all files * and subdirectories contained in that directory. A pathname consisting of * the special token "<<ALL FILES>>" matches <b>any file. * <P> * Note: A pathname consisting of a single "*" indicates all the files * in the current directory, while a pathname consisting of a single "-" * indicates all the files in the current directory and * (recursively) all files and subdirectories contained in the current * directory. * <P> * The actions to be granted are passed to the constructor in a string containing * a list of one or more comma-separated keywords. The possible keywords are * "read", "write", "execute", "delete", and "readlink". Their meaning is * defined as follows: * * <DL> * <DT> read Runtime.exec to
* be called. Corresponds to <code>SecurityManager.checkExec.
* <DT> delete
* <DD> delete permission. Allows File.delete to
* be called. Corresponds to <code>SecurityManager.checkDelete.
* <DT> readlink
* <DD> read link permission. Allows the target of a
* <a href="../nio/file/package-summary.html#links">symbolic link
* to be read by invoking the {@link java.nio.file.Files#readSymbolicLink
* readSymbolicLink } method.
* </DL>
* <P>
* The actions string is converted to lowercase before processing.
* <P>
* Be careful when granting FilePermissions. Think about the implications
* of granting read and especially write access to various files and
* directories. The "<<ALL FILES>>" permission with write action is
* especially dangerous. This grants permission to write to the entire
* file system. One thing this effectively allows is replacement of the
* system binary, including the JVM runtime environment.
*
* <p>Please note: Code can always read a file from the same
* directory it's in (or a subdirectory of that directory); it does not
* need explicit permission to do so.
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
* @since 1.2
*
* @serial exclude
*/
public final class FilePermission extends Permission implements Serializable {
/**
* Execute action.
*/
private final static int EXECUTE = 0x1;
/**
* Write action.
*/
private final static int WRITE = 0x2;
/**
* Read action.
*/
private final static int READ = 0x4;
/**
* Delete action.
*/
private final static int DELETE = 0x8;
/**
* Read link action.
*/
private final static int READLINK = 0x10;
/**
* All actions (read,write,execute,delete,readlink)
*/
private final static int ALL = READ|WRITE|EXECUTE|DELETE|READLINK;
/**
* No actions.
*/
private final static int NONE = 0x0;
// the actions mask
private transient int mask;
// does path indicate a directory? (wildcard or recursive)
private transient boolean directory;
// is it a recursive directory specification?
private transient boolean recursive;
/**
* the actions string.
*
* @serial
*/
private String actions; // Left null as long as possible, then
// created and re-used in the getAction function.
// canonicalized dir path. In the case of
// directories, it is the name "/blah/*" or "/blah/-" without
// the last character (the "*" or "-").
private transient String cpath;
// static Strings used by init(int mask)
private static final char RECURSIVE_CHAR = '-';
private static final char WILD_CHAR = '*';
/*
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("***\n");
sb.append("cpath = "+cpath+"\n");
sb.append("mask = "+mask+"\n");
sb.append("actions = "+getActions()+"\n");
sb.append("directory = "+directory+"\n");
sb.append("recursive = "+recursive+"\n");
sb.append("***\n");
return sb.toString();
}
*/
private static final long serialVersionUID = 7930732926638008763L;
/**
* initialize a FilePermission object. Common to all constructors.
* Also called during de-serialization.
*
* @param mask the actions mask to use.
*
*/
private void init(int mask) {
if ((mask & ALL) != mask)
throw new IllegalArgumentException("invalid actions mask");
if (mask == NONE)
throw new IllegalArgumentException("invalid actions mask");
if ((cpath = getName()) == null)
throw new NullPointerException("name can't be null");
this.mask = mask;
if (cpath.equals("<"/tmp/-", "read"
* <LI> "/tmp/scratch/foo", "write"
* </OL>
*
* <p>and you are calling the implies method with the FilePermission:
*
* <pre>
* "/tmp/scratch/foo", "read,write",
* </pre>
*
* then the <code>implies function must
* take into account both the "/tmp/-" and "/tmp/scratch/foo"
* permissions, so the effective permission is "read,write",
* and <code>implies returns true. The "implies" semantics for
* FilePermissions are handled properly by the PermissionCollection object
* returned by this <code>newPermissionCollection method.
*
* @return a new PermissionCollection object suitable for storing
* FilePermissions.
*/
public PermissionCollection newPermissionCollection() {
return new FilePermissionCollection();
}
/**
* WriteObject is called to save the state of the FilePermission
* to a stream. The actions are serialized, and the superclass
* takes care of the name.
*/
private void writeObject(ObjectOutputStream s)
throws IOException
{
// Write out the actions. The superclass takes care of the name
// call getActions to make sure actions field is initialized
if (actions == null)
getActions();
s.defaultWriteObject();
}
/**
* readObject is called to restore the state of the FilePermission from
* a stream.
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException
{
// Read in the actions, then restore everything else by calling init.
s.defaultReadObject();
init(getMask(actions));
}
}
/**
* A FilePermissionCollection stores a set of FilePermission permissions.
* FilePermission objects
* must be stored in a manner that allows them to be inserted in any
* order, but enable the implies function to evaluate the implies
* method.
* For example, if you have two FilePermissions:
* <OL>
* <LI> "/tmp/-", "read"
* <LI> "/tmp/scratch/foo", "write"
* </OL>
* And you are calling the implies function with the FilePermission:
* "/tmp/scratch/foo", "read,write", then the implies function must
* take into account both the /tmp/- and /tmp/scratch/foo
* permissions, so the effective permission is "read,write".
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
*
* @serial include
*
*/
final class FilePermissionCollection extends PermissionCollection
implements Serializable
{
// Not serialized; see serialization section at end of class
private transient List<Permission> perms;
/**
* Create an empty FilePermissionCollection object.
*/
public FilePermissionCollection() {
perms = new ArrayList<>();
}
/**
* Adds a permission to the FilePermissionCollection. The key for the hash is
* permission.path.
*
* @param permission the Permission object to add.
*
* @exception IllegalArgumentException - if the permission is not a
* FilePermission
*
* @exception SecurityException - if this FilePermissionCollection object
* has been marked readonly
*/
public void add(Permission permission) {
if (! (permission instanceof FilePermission))
throw new IllegalArgumentException("invalid permission: "+
permission);
if (isReadOnly())
throw new SecurityException(
"attempt to add a Permission to a readonly PermissionCollection");
synchronized (this) {
perms.add(permission);
}
}
/**
* Check and see if this set of permissions implies the permissions
* expressed in "permission".
*
* @param permission the Permission object to compare
*
* @return true if "permission" is a proper subset of a permission in
* the set, false if not.
*/
public boolean implies(Permission permission) {
if (! (permission instanceof FilePermission))
return false;
FilePermission fp = (FilePermission) permission;
int desired = fp.getMask();
int effective = 0;
int needed = desired;
synchronized (this) {
int len = perms.size();
for (int i = 0; i < len; i++) {
FilePermission x = (FilePermission) perms.get(i);
if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(fp)) {
effective |= x.getMask();
if ((effective & desired) == desired)
return true;
needed = (desired ^ effective);
}
}
}
return false;
}
/**
* Returns an enumeration of all the FilePermission objects in the
* container.
*
* @return an enumeration of all the FilePermission objects.
*/
public Enumeration<Permission> elements() {
// Convert Iterator into Enumeration
synchronized (this) {
return Collections.enumeration(perms);
}
}
private static final long serialVersionUID = 2202956749081564585L;
// Need to maintain serialization interoperability with earlier releases,
// which had the serializable field:
// private Vector permissions;
/**
* @serialField permissions java.util.Vector
* A list of FilePermission objects.
*/
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("permissions", Vector.class),
};
/**
* @serialData "permissions" field (a Vector containing the FilePermissions).
*/
/*
* Writes the contents of the perms field out as a Vector for
* serialization compatibility with earlier releases.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
// Don't call out.defaultWriteObject()
// Write out Vector
Vector<Permission> permissions = new Vector<>(perms.size());
synchronized (this) {
permissions.addAll(perms);
}
ObjectOutputStream.PutField pfields = out.putFields();
pfields.put("permissions", permissions);
out.writeFields();
}
/*
* Reads in a Vector of FilePermissions and saves them in the perms field.
*/
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get the one we want
@SuppressWarnings("unchecked")
Vector<Permission> permissions = (VectorOther Java examples (source code examples)Here is a short list of links related to this Java FilePermission.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.