FilePermission.java [plain text]
package java.io;
import java.security.Permission;
public final class FilePermission extends Permission implements Serializable
{
static final long serialVersionUID = 7930732926638008763L;
private static final String CURRENT_DIRECTORY =
System.getProperty("user.dir");
private boolean readPerm = false;
private boolean writePerm = false;
private boolean executePerm = false;
private boolean deletePerm = false;
private final String actionsString;
private void checkPerms() throws IllegalArgumentException
{
String action;
int i = actionsString.indexOf(',');
int startI = 0;
while (i != -1)
{
action = actionsString.substring(startI, i).trim().toLowerCase();
if (action.equals("read"))
readPerm = true;
else if (action.equals("write"))
writePerm = true;
else if (action.equals("execute"))
executePerm = true;
else if (action.equals("delete"))
deletePerm = true;
else
throw new IllegalArgumentException("Unknown action: " + action);
startI = i + 1;
i = actionsString.indexOf(',', startI);
}
action = actionsString.substring(startI).trim().toLowerCase();
if (action.equals("read"))
readPerm = true;
else if (action.equals("write"))
writePerm = true;
else if (action.equals("execute"))
executePerm = true;
else if (action.equals("delete"))
deletePerm = true;
else
throw new IllegalArgumentException("Unknown action: " + action);
}
public FilePermission(String pathExpression, String actionsString)
{
super(pathExpression);
if (pathExpression == null)
throw new NullPointerException("pathExpression");
if (actionsString == null)
throw new IllegalArgumentException("actionsString");
this.actionsString = actionsString;
checkPerms();
}
public String getActions()
{
return actionsString;
}
public int hashCode()
{
return getName().hashCode() ^ actionsString.hashCode();
}
public boolean equals(Object o)
{
if (! (o instanceof FilePermission))
return false;
FilePermission p = (FilePermission) o;
String f1 = getName();
String f2 = p.getName();
if (f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar)
{
if (f2.length() > 0
&& f2.charAt(f2.length() - 1) == File.separatorChar)
{
if (! f2.equals(f1))
return false;
}
else
{
if (! f2.equals(f1.substring(0, f1.length() - 1)))
return false;
}
}
else
{
if (f2.length() > 0
&& f2.charAt(f2.length() - 1) == File.separatorChar)
{
if (! f1.equals(f2.substring(0, f2.length() - 1)))
return false;
}
else
{
if (! f1.equals(f2))
return false;
}
}
return (readPerm == p.readPerm
&& writePerm == p.writePerm
&& executePerm == p.executePerm
&& deletePerm == p.deletePerm);
}
public boolean implies(Permission p)
{
FilePermission fp;
if (! (p instanceof FilePermission))
return false;
fp = (FilePermission) p;
String f1 = getName();
String f2 = fp.getName();
if (f1.charAt(0) != File.separatorChar)
f1 = CURRENT_DIRECTORY + f1;
if (f2.charAt(0) != File.separatorChar)
f2 = CURRENT_DIRECTORY + f2;
String sub1;
switch (f1.charAt(f1.length() - 1))
{
case '*':
sub1 = f1.substring(0, f1.length() - 1); if (f2.length() <= sub1.length())
{
return false;
}
else if (f2.charAt(sub1.length() - 1) == File.separatorChar)
{
if (! f2.substring(0, sub1.length()).equals(sub1))
return false;
if (f2.substring(sub1.length() + 1).indexOf(File.separatorChar)
!= -1)
return false;
}
else
{
return false;
}
break;
case '-':
sub1 = f1.substring(0, f1.length() - 2);
if (f2.length() < sub1.length())
{
return false;
}
else if (f2.length() > sub1.length()
&& f2.charAt(sub1.length()) != File.separatorChar)
return false;
else if (! f2.substring(0, sub1.length()).equals(sub1))
return false;
break;
default:
if (f2.charAt(f2.length() - 1) == File.separatorChar)
{
if (! f1.equals(f2.substring(0, f2.length() - 1)))
return false;
}
else if (!f1.equals(f2))
return false;
break;
}
if (readPerm && ! fp.readPerm)
return false;
if (writePerm && ! fp.writePerm)
return false;
if (executePerm && ! fp.executePerm)
return false;
if (deletePerm && ! fp.deletePerm)
return false;
return true;
}
}