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