1:
38:
39:
40: package ;
41:
42: import ;
43:
44: public final class FilePermission extends Permission implements Serializable
45: {
46: private static final long serialVersionUID = 7930732926638008763L;
47:
48: private static final String ALL_FILES = "<<ALL FILES>>";
49:
50: private boolean readPerm = false;
51: private boolean writePerm = false;
52: private boolean executePerm = false;
53: private boolean deletePerm = false;
54: private final String actionsString;
55:
56:
57: private void checkPerms() throws IllegalArgumentException
58: {
59: String action;
60: int i = actionsString.indexOf(',');
61: int startI = 0;
62: while (i != -1)
63: {
64: action = actionsString.substring(startI, i).trim().toLowerCase();
65: if (action.equals("read"))
66: readPerm = true;
67: else if (action.equals("write"))
68: writePerm = true;
69: else if (action.equals("execute"))
70: executePerm = true;
71: else if (action.equals("delete"))
72: deletePerm = true;
73: else
74: throw new IllegalArgumentException("Unknown action: " + action);
75:
76: startI = i + 1;
77: i = actionsString.indexOf(',', startI);
78: }
79:
80: action = actionsString.substring(startI).trim().toLowerCase();
81: if (action.equals("read"))
82: readPerm = true;
83: else if (action.equals("write"))
84: writePerm = true;
85: else if (action.equals("execute"))
86: executePerm = true;
87: else if (action.equals("delete"))
88: deletePerm = true;
89: else
90: throw new IllegalArgumentException("Unknown action: " + action);
91: }
92:
93:
102: public FilePermission(String pathExpression, String actionsString)
103: {
104:
105: super(pathExpression);
106: if (pathExpression == null)
107: throw new NullPointerException("pathExpression");
108: if (actionsString == null)
109: throw new IllegalArgumentException("actionsString");
110: this.actionsString = actionsString;
111: checkPerms();
112: }
113:
114:
118: public String getActions()
119: {
120: return actionsString;
121: }
122:
123:
132: public int hashCode()
133: {
134: return getName().hashCode() ^ actionsString.hashCode();
135: }
136:
137:
144: public boolean equals(Object o)
145: {
146: if (! (o instanceof FilePermission))
147: return false;
148: FilePermission p = (FilePermission) o;
149:
150: String f1 = getName();
151: String f2 = p.getName();
152:
153:
154:
155: if (f1.length() > 0 && f1.charAt(f1.length() - 1) == File.separatorChar)
156: {
157: if (f2.length() > 0
158: && f2.charAt(f2.length() - 1) == File.separatorChar)
159: {
160: if (! f2.equals(f1))
161: return false;
162: }
163: else
164: {
165: if (! f2.equals(f1.substring(0, f1.length() - 1)))
166: return false;
167: }
168: }
169: else
170: {
171: if (f2.length() > 0
172: && f2.charAt(f2.length() - 1) == File.separatorChar)
173: {
174: if (! f1.equals(f2.substring(0, f2.length() - 1)))
175: return false;
176: }
177: else
178: {
179: if (! f1.equals(f2))
180: return false;
181: }
182: }
183: return (readPerm == p.readPerm
184: && writePerm == p.writePerm
185: && executePerm == p.executePerm
186: && deletePerm == p.deletePerm);
187: }
188:
189:
201: public boolean implies(Permission p)
202: {
203: if (! (p instanceof FilePermission))
204: return false;
205:
206: String f1 = getName();
207:
208: if (f1.equals(ALL_FILES))
209: return true;
210:
211: FilePermission fp = (FilePermission) p;
212: String f2 = fp.getName();
213:
214: if (f2.equals(ALL_FILES))
215: return false;
216:
217: try
218: {
219: f1 = new File(f1).getCanonicalPath();
220: f2 = new File(f2).getCanonicalPath();
221: }
222: catch (IOException ioe)
223: {
224: return false;
225: }
226:
227: String sub1;
228:
229: switch (f1.charAt(f1.length() - 1))
230: {
231: case '*':
232: sub1 = f1.substring(0, f1.length() - 1);
233: if (f2.length() <= sub1.length())
234: {
235:
236:
237:
238:
239: return false;
240: }
241: else if (f2.charAt(sub1.length() - 1) == File.separatorChar)
242: {
243:
244: if (! f2.substring(0, sub1.length()).equals(sub1))
245: return false;
246:
247:
248: if (f2.substring(sub1.length() + 1).indexOf(File.separatorChar)
249: != -1)
250: return false;
251: }
252: else
253: {
254:
255:
256:
257: return false;
258: }
259: break;
260: case '-':
261:
262: sub1 = f1.substring(0, f1.length() - 2);
263: if (f2.length() < sub1.length())
264: {
265:
266:
267: return false;
268: }
269: else if (f2.length() > sub1.length()
270: && f2.charAt(sub1.length()) != File.separatorChar)
271: return false;
272: else if (! f2.substring(0, sub1.length()).equals(sub1))
273: return false;
274: break;
275:
276: default:
277: if (!f1.equals(f2))
278: return false;
279: break;
280: }
281:
282: if (fp.readPerm && ! readPerm)
283: return false;
284: if (fp.writePerm && ! writePerm)
285: return false;
286: if (fp.executePerm && ! executePerm)
287: return false;
288: if (fp.deletePerm && ! deletePerm)
289: return false;
290:
291: return true;
292: }
293: }