Source Code Cross Referenced for FilePermission.java in  » 6.0-JDK-Modules » j2me » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)FilePermission.java	1.76 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package java.io;
029:
030:        import java.security.*;
031:        import java.util.Enumeration;
032:        import java.util.List;
033:        import java.util.ArrayList;
034:        import java.util.StringTokenizer;
035:        import java.util.Vector;
036:        import java.util.Collections;
037:        import java.io.ObjectStreamField;
038:        import java.io.ObjectOutputStream;
039:        import java.io.ObjectInputStream;
040:        import java.io.IOException;
041:        import sun.security.util.SecurityConstants;
042:
043:        /**
044:         * This class represents access to a file or directory.  A FilePermission consists
045:         * of a pathname and a set of actions valid for that pathname.
046:         * <P>
047:         * Pathname is the pathname of the file or directory granted the specified
048:         * actions. A pathname that ends in "/*" (where "/" is
049:         * the file separator character, <code>File.separatorChar</code>) indicates
050:         * all the files and directories contained in that directory. A pathname
051:         * that ends with "/-" indicates (recursively) all files
052:         * and subdirectories contained in that directory. A pathname consisting of
053:         * the special token "&lt;&lt;ALL FILES&gt;&gt;" matches <b>any</b> file.
054:         * <P>
055:         * Note: A pathname consisting of a single "*" indicates all the files
056:         * in the current directory, while a pathname consisting of a single "-" 
057:         * indicates all the files in the current directory and
058:         * (recursively) all files and subdirectories contained in the current 
059:         * directory.
060:         * <P>
061:         * The actions to be granted are passed to the constructor in a string containing 
062:         * a list of one or more comma-separated keywords. The possible keywords are
063:         * "read", "write", "execute", and "delete". Their meaning is defined as follows:
064:         * <P>
065:         * <DL> 
066:         *    <DT> read <DD> read permission
067:         *    <DT> write <DD> write permission
068:         *    <DT> execute 
069:         *    <DD> execute permission. Allows <code>Runtime.exec</code> to
070:         *         be called. Corresponds to <code>SecurityManager.checkExec</code>.
071:         *    <DT> delete
072:         *    <DD> delete permission. Allows <code>File.delete</code> to
073:         *         be called. Corresponds to <code>SecurityManager.checkDelete</code>.
074:         * </DL>
075:         * <P>
076:         * The actions string is converted to lowercase before processing.
077:         * <P>
078:         * Be careful when granting FilePermissions. Think about the implications 
079:         * of granting read and especially write access to various files and 
080:         * directories. The "&lt;&lt;ALL FILES>>" permission with write action is 
081:         * especially dangerous. This grants permission to write to the entire 
082:         * file system. One thing this effectively allows is replacement of the 
083:         * system binary, including the JVM runtime environment.
084:         * 
085:         * <p>Please note: Code can always read a file from the same
086:         * directory it's in (or a subdirectory of that directory); it does not
087:         * need explicit permission to do so.
088:         * 
089:         * @see java.security.Permission
090:         * @see java.security.Permissions
091:         * @see java.security.PermissionCollection
092:         *
093:         * @version 1.67 00/02/02
094:         *
095:         * @since 1.2
096:         *
097:         * @serial exclude
098:         */
099:
100:        public final class FilePermission extends Permission implements 
101:                Serializable {
102:
103:            /**
104:             * Execute action.
105:             */
106:            private final static int EXECUTE = 0x1;
107:            /**
108:             * Write action.
109:             */
110:            private final static int WRITE = 0x2;
111:            /**
112:             * Read action.
113:             */
114:            private final static int READ = 0x4;
115:            /**
116:             * Delete action.
117:             */
118:            private final static int DELETE = 0x8;
119:
120:            /**
121:             * All actions (read,write,execute,delete)
122:             */
123:            private final static int ALL = READ | WRITE | EXECUTE | DELETE;
124:            /**
125:             * No actions.
126:             */
127:            private final static int NONE = 0x0;
128:
129:            // the actions mask
130:            private transient int mask;
131:
132:            // does path indicate a directory? (wildcard or recursive)
133:            private transient boolean directory;
134:
135:            // is it a recursive directory specification?
136:            private transient boolean recursive;
137:
138:            /**
139:             * the actions string. 
140:             *
141:             * @serial
142:             */
143:            private String actions; // Left null as long as possible, then
144:            // created and re-used in the getAction function.
145:
146:            // canonicalized dir path. In the case of
147:            // directories, it is the name "/blah/*" or "/blah/-" without
148:            // the last character (the "*" or "-").
149:
150:            private transient String cpath;
151:
152:            // static Strings used by init(int mask)
153:            private static final char RECURSIVE_CHAR = '-';
154:            private static final char WILD_CHAR = '*';
155:
156:            /*
157:             public String toString()
158:             {
159:             StringBuffer sb = new StringBuffer();
160:             sb.append("***\n");
161:             sb.append("cpath = "+cpath+"\n");
162:             sb.append("mask = "+mask+"\n");
163:             sb.append("actions = "+getActions()+"\n");
164:             sb.append("directory = "+directory+"\n");
165:             sb.append("recursive = "+recursive+"\n");
166:             sb.append("***\n");
167:             return sb.toString();
168:             }
169:             */
170:
171:            private static final long serialVersionUID = 7930732926638008763L;
172:
173:            /**
174:             * initialize a FilePermission object. Common to all constructors.
175:             * Also called during de-serialization.
176:             *
177:             * @param mask the actions mask to use.
178:             *
179:             */
180:            private void init(int mask) {
181:
182:                if ((mask & ALL) != mask)
183:                    throw new IllegalArgumentException("invalid actions mask");
184:
185:                if (mask == NONE)
186:                    throw new IllegalArgumentException("invalid actions mask");
187:
188:                if ((cpath = getName()) == null)
189:                    throw new NullPointerException("name can't be null");
190:
191:                this .mask = mask;
192:
193:                if (cpath.equals("<<ALL FILES>>")) {
194:                    directory = true;
195:                    recursive = true;
196:                    cpath = "";
197:                    return;
198:                }
199:
200:                int len = cpath.length();
201:                char last = ((len > 0) ? cpath.charAt(len - 1) : 0);
202:
203:                if (last == RECURSIVE_CHAR
204:                        && (len == 1 || cpath.charAt(len - 2) == File.separatorChar)) {
205:                    directory = true;
206:                    recursive = true;
207:                    cpath = cpath.substring(0, --len);
208:                } else if (last == WILD_CHAR
209:                        && (len == 1 || cpath.charAt(len - 2) == File.separatorChar)) {
210:                    directory = true;
211:                    //recursive = false;
212:                    cpath = cpath.substring(0, --len);
213:                } else {
214:                    // overkill since they are initialized to false, but 
215:                    // commented out here to remind us...
216:                    //directory = false;
217:                    //recursive = false;
218:                }
219:
220:                if (len == 0) {
221:                    cpath = (String) java.security.AccessController
222:                            .doPrivileged(new sun.security.action.GetPropertyAction(
223:                                    "user.dir"));
224:                }
225:
226:                // store only the canonical cpath if possible
227:
228:                // need a doPrivileged block as getCanonicalPath
229:                // might attempt to access user.dir to turn a relative
230:                // path into an absolute path.
231:                cpath = (String) AccessController
232:                        .doPrivileged(new java.security.PrivilegedAction() {
233:                            public Object run() {
234:                                try {
235:                                    File file = new File(cpath);
236:                                    String canonical_path = file
237:                                            .getCanonicalPath();
238:                                    int ln;
239:                                    if (directory
240:                                            && ((ln = canonical_path.length()) == 0 || canonical_path
241:                                                    .charAt(ln - 1) != File.separatorChar)) {
242:                                        return canonical_path + File.separator;
243:                                    } else {
244:                                        return canonical_path;
245:                                    }
246:                                } catch (IOException ioe) {
247:                                    // ignore if we can't canonicalize path?
248:                                }
249:                                return cpath;
250:                            }
251:                        });
252:
253:                // At this point the path should be absolute. die if it isn't?
254:            }
255:
256:            /**
257:             * Creates a new FilePermission object with the specified actions.
258:             * <i>path</i> is the pathname of a
259:             * file or directory, and <i>actions</i> contains a comma-separated list of the
260:             * desired actions granted on the file or directory. Possible actions are
261:             * "read", "write", "execute", and "delete". 
262:             * 
263:             * <p>A pathname that ends in "/*" (where "/" is
264:             * the file separator character, <code>File.separatorChar</code>) indicates
265:             * a directory and all the files contained in that directory. A pathname
266:             * that ends with "/-" indicates a directory and (recursively) all files
267:             * and subdirectories contained in that directory. The special pathname
268:             * "&lt;&lt;ALL FILES&gt;&gt;" matches all files.
269:             * 
270:             * <p>A pathname consisting of a single "*" indicates all the files
271:             * in the current directory, while a pathname consisting of a single "-" 
272:             * indicates all the files in the current directory and
273:             * (recursively) all files and subdirectories contained in the current 
274:             * directory.
275:             * 
276:             * @param path the pathname of the file/directory.
277:             * @param actions the action string.
278:             */
279:
280:            public FilePermission(String path, String actions) {
281:                super (path);
282:                init(getMask(actions));
283:            }
284:
285:            /**
286:             * Creates a new FilePermission object using an action mask.
287:             * More efficient than the FilePermission(String, String) constructor.
288:             * Can be used from within
289:             * code that needs to create a FilePermission object to pass into the 
290:             * <code>implies</code> method. 
291:             *
292:             * @param path the pathname of the file/directory.
293:             * @param mask the action mask to use.
294:             */
295:
296:            // package private for use by the FilePermissionCollection add method
297:            FilePermission(String path, int mask) {
298:                super (path);
299:                init(mask);
300:            }
301:
302:            /**
303:             * Checks if this FilePermission object "implies" the specified permission.
304:             * <P>
305:             * More specifically, this method returns true if:<p>
306:             * <ul>
307:             * <li> <i>p</i> is an instanceof FilePermission,<p>
308:             * <li> <i>p</i>'s actions are a proper subset of this
309:             * object's actions, and <p>
310:             * <li> <i>p</i>'s pathname is implied by this object's
311:             *      pathname. For example, "/tmp/*" implies "/tmp/foo", since
312:             *      "/tmp/*" encompasses the "/tmp" directory and all files in that
313:             *      directory, including the one named "foo".
314:             * </ul>
315:             * @param p the permission to check against.
316:             *
317:             * @return true if the specified permission is implied by this object,
318:             * false if not.  
319:             */
320:            public boolean implies(Permission p) {
321:                if (!(p instanceof  FilePermission))
322:                    return false;
323:
324:                FilePermission that = (FilePermission) p;
325:
326:                // we get the effective mask. i.e., the "and" of this and that.
327:                // They must be equal to that.mask for implies to return true.
328:
329:                return ((this .mask & that.mask) == that.mask)
330:                        && impliesIgnoreMask(that);
331:            }
332:
333:            /**
334:             * Checks if the Permission's actions are a proper subset of the
335:             * this object's actions. Returns the effective mask iff the
336:             * this FilePermission's path also implies that FilePermission's path.
337:             * 
338:             * @param that the FilePermission to check against.
339:             * @param exact return immediatly if the masks are not equal
340:             * @return the effective mask
341:             */
342:            boolean impliesIgnoreMask(FilePermission that) {
343:                if (this .directory) {
344:                    if (this .recursive) {
345:                        // make sure that.path is longer then path so
346:                        // something like /foo/- does not imply /foo
347:                        if (that.directory) {
348:                            return (that.cpath.length() >= this .cpath.length())
349:                                    && that.cpath.startsWith(this .cpath);
350:                        } else {
351:                            return ((that.cpath.length() > this .cpath.length()) && that.cpath
352:                                    .startsWith(this .cpath));
353:                        }
354:                    } else {
355:                        if (that.directory) {
356:                            // if the permission passed in is a directory
357:                            // specification, make sure that a non-recursive
358:                            // permission (i.e., this object) can't imply a recursive
359:                            // permission.
360:                            if (that.recursive)
361:                                return false;
362:                            else
363:                                return (this .cpath.equals(that.cpath));
364:                        } else {
365:                            int last = that.cpath
366:                                    .lastIndexOf(File.separatorChar);
367:                            if (last == -1)
368:                                return false;
369:                            else {
370:                                // this.cpath.equals(that.cpath.substring(0, last+1));
371:                                // Use regionMatches to avoid creating new string
372:
373:                                return (this .cpath.length() == (last + 1))
374:                                        && this .cpath.regionMatches(0,
375:                                                that.cpath, 0, last + 1);
376:                            }
377:                        }
378:                    }
379:                } else {
380:                    return (this .cpath.equals(that.cpath));
381:                }
382:            }
383:
384:            /**
385:             * Checks two FilePermission objects for equality. Checks that <i>obj</i> is
386:             * a FilePermission, and has the same pathname and actions as this object.
387:             * <P>
388:             * @param obj the object we are testing for equality with this object.
389:             * @return true if obj is a FilePermission, and has the same pathname and
390:             * actions as this FilePermission object.
391:             */
392:            public boolean equals(Object obj) {
393:                if (obj == this )
394:                    return true;
395:
396:                if (!(obj instanceof  FilePermission))
397:                    return false;
398:
399:                FilePermission that = (FilePermission) obj;
400:
401:                return (this .mask == that.mask)
402:                        && this .cpath.equals(that.cpath)
403:                        && (this .directory == that.directory)
404:                        && (this .recursive == that.recursive);
405:            }
406:
407:            /**
408:             * Returns the hash code value for this object.
409:             * 
410:             * @return a hash code value for this object.
411:             */
412:
413:            public int hashCode() {
414:                return this .cpath.hashCode();
415:            }
416:
417:            /**
418:             * Converts an actions String to an actions mask.
419:             *
420:             * @param action the action string.
421:             * @return the actions mask.
422:             */
423:            private static int getMask(String actions) {
424:
425:                int mask = NONE;
426:
427:                // Null action valid?
428:                if (actions == null) {
429:                    return mask;
430:                }
431:                // Check against use of constants (used heavily within the JDK)
432:                if (actions == SecurityConstants.FILE_READ_ACTION) {
433:                    return READ;
434:                } else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
435:                    return WRITE;
436:                } else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
437:                    return EXECUTE;
438:                } else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
439:                    return DELETE;
440:                }
441:
442:                char[] a = actions.toCharArray();
443:
444:                int i = a.length - 1;
445:                if (i < 0)
446:                    return mask;
447:
448:                while (i != -1) {
449:                    char c;
450:
451:                    // skip whitespace
452:                    while ((i != -1)
453:                            && ((c = a[i]) == ' ' || c == '\r' || c == '\n'
454:                                    || c == '\f' || c == '\t'))
455:                        i--;
456:
457:                    // check for the known strings
458:                    int matchlen;
459:
460:                    if (i >= 3 && (a[i - 3] == 'r' || a[i - 3] == 'R')
461:                            && (a[i - 2] == 'e' || a[i - 2] == 'E')
462:                            && (a[i - 1] == 'a' || a[i - 1] == 'A')
463:                            && (a[i] == 'd' || a[i] == 'D')) {
464:                        matchlen = 4;
465:                        mask |= READ;
466:
467:                    } else if (i >= 4 && (a[i - 4] == 'w' || a[i - 4] == 'W')
468:                            && (a[i - 3] == 'r' || a[i - 3] == 'R')
469:                            && (a[i - 2] == 'i' || a[i - 2] == 'I')
470:                            && (a[i - 1] == 't' || a[i - 1] == 'T')
471:                            && (a[i] == 'e' || a[i] == 'E')) {
472:                        matchlen = 5;
473:                        mask |= WRITE;
474:
475:                    } else if (i >= 6 && (a[i - 6] == 'e' || a[i - 6] == 'E')
476:                            && (a[i - 5] == 'x' || a[i - 5] == 'X')
477:                            && (a[i - 4] == 'e' || a[i - 4] == 'E')
478:                            && (a[i - 3] == 'c' || a[i - 3] == 'C')
479:                            && (a[i - 2] == 'u' || a[i - 2] == 'U')
480:                            && (a[i - 1] == 't' || a[i - 1] == 'T')
481:                            && (a[i] == 'e' || a[i] == 'E')) {
482:                        matchlen = 7;
483:                        mask |= EXECUTE;
484:
485:                    } else if (i >= 5 && (a[i - 5] == 'd' || a[i - 5] == 'D')
486:                            && (a[i - 4] == 'e' || a[i - 4] == 'E')
487:                            && (a[i - 3] == 'l' || a[i - 3] == 'L')
488:                            && (a[i - 2] == 'e' || a[i - 2] == 'E')
489:                            && (a[i - 1] == 't' || a[i - 1] == 'T')
490:                            && (a[i] == 'e' || a[i] == 'E')) {
491:                        matchlen = 6;
492:                        mask |= DELETE;
493:
494:                    } else {
495:                        // parse error
496:                        throw new IllegalArgumentException(
497:                                "invalid permission: " + actions);
498:                    }
499:
500:                    // make sure we didn't just match the tail of a word
501:                    // like "ackbarfaccept".  Also, skip to the comma.
502:                    boolean seencomma = false;
503:                    while (i >= matchlen && !seencomma) {
504:                        switch (a[i - matchlen]) {
505:                        case ',':
506:                            seencomma = true;
507:                            /*FALLTHROUGH*/
508:                        case ' ':
509:                        case '\r':
510:                        case '\n':
511:                        case '\f':
512:                        case '\t':
513:                            break;
514:                        default:
515:                            throw new IllegalArgumentException(
516:                                    "invalid permission: " + actions);
517:                        }
518:                        i--;
519:                    }
520:
521:                    // point i at the location of the comma minus one (or -1).
522:                    i -= matchlen;
523:                }
524:
525:                return mask;
526:            }
527:
528:            /**
529:             * Return the current action mask. Used by the FilePermissionCollection.
530:             *
531:             * @return the actions mask.
532:             */
533:
534:            int getMask() {
535:                return mask;
536:            }
537:
538:            /**
539:             * Return the canonical string representation of the actions.
540:             * Always returns present actions in the following order: 
541:             * read, write, execute, delete.
542:             *
543:             * @return the canonical string representation of the actions.
544:             */
545:            private static String getActions(int mask) {
546:                StringBuffer sb = new StringBuffer();
547:                boolean comma = false;
548:
549:                if ((mask & READ) == READ) {
550:                    comma = true;
551:                    sb.append("read");
552:                }
553:
554:                if ((mask & WRITE) == WRITE) {
555:                    if (comma)
556:                        sb.append(',');
557:                    else
558:                        comma = true;
559:                    sb.append("write");
560:                }
561:
562:                if ((mask & EXECUTE) == EXECUTE) {
563:                    if (comma)
564:                        sb.append(',');
565:                    else
566:                        comma = true;
567:                    sb.append("execute");
568:                }
569:
570:                if ((mask & DELETE) == DELETE) {
571:                    if (comma)
572:                        sb.append(',');
573:                    else
574:                        comma = true;
575:                    sb.append("delete");
576:                }
577:
578:                return sb.toString();
579:            }
580:
581:            /**
582:             * Returns the "canonical string representation" of the actions.
583:             * That is, this method always returns present actions in the following order: 
584:             * read, write, execute, delete. For example, if this FilePermission object
585:             * allows both write and read actions, a call to <code>getActions</code>
586:             * will return the string "read,write".
587:             *
588:             * @return the canonical string representation of the actions.
589:             */
590:            public String getActions() {
591:                if (actions == null)
592:                    actions = getActions(this .mask);
593:
594:                return actions;
595:            }
596:
597:            /**
598:             * Returns a new PermissionCollection object for storing FilePermission 
599:             * objects.
600:             * <p>
601:             * FilePermission objects must be stored in a manner that allows them 
602:             * to be inserted into the collection in any order, but that also enables the 
603:             * PermissionCollection <code>implies</code>
604:             * method to be implemented in an efficient (and consistent) manner.
605:             * 
606:             * <p>For example, if you have two FilePermissions:
607:             * <OL>
608:             * <LI>  <code>"/tmp/-", "read"</code>
609:             * <LI>  <code>"/tmp/scratch/foo", "write"</code>
610:             * </OL>
611:             * 
612:             * <p>and you are calling the <code>implies</code> method with the FilePermission: 
613:             * 
614:             * <pre>
615:             *   "/tmp/scratch/foo", "read,write", 
616:             * </pre>
617:             * 
618:             * then the <code>implies</code> function must
619:             * take into account both the "/tmp/-" and "/tmp/scratch/foo"
620:             * permissions, so the effective permission is "read,write",
621:             * and <code>implies</code> returns true. The "implies" semantics for 
622:             * FilePermissions are handled properly by the PermissionCollection object
623:             * returned by this <code>newPermissionCollection</code> method.
624:             *
625:             * @return a new PermissionCollection object suitable for storing 
626:             * FilePermissions.
627:             */
628:
629:            public PermissionCollection newPermissionCollection() {
630:                return new FilePermissionCollection();
631:            }
632:
633:            /**
634:             * WriteObject is called to save the state of the FilePermission 
635:             * to a stream. The actions are serialized, and the superclass
636:             * takes care of the name.
637:             */
638:            private void writeObject(ObjectOutputStream s) throws IOException {
639:                // Write out the actions. The superclass takes care of the name
640:                // call getActions to make sure actions field is initialized
641:                if (actions == null)
642:                    getActions();
643:                s.defaultWriteObject();
644:            }
645:
646:            /**
647:             * readObject is called to restore the state of the FilePermission from
648:             * a stream.
649:             */
650:            private void readObject(ObjectInputStream s) throws IOException,
651:                    ClassNotFoundException {
652:                // Read in the actions, then restore everything else by calling init.
653:                s.defaultReadObject();
654:                init(getMask(actions));
655:            }
656:        }
657:
658:        /**
659:         * A FilePermissionCollection stores a set of FilePermission permissions. 
660:         * FilePermission objects
661:         * must be stored in a manner that allows them to be inserted in any
662:         * order, but enable the implies function to evaluate the implies
663:         * method.
664:         * For example, if you have two FilePermissions:
665:         * <OL>
666:         * <LI> "/tmp/-", "read"
667:         * <LI> "/tmp/scratch/foo", "write"
668:         * </OL>
669:         * And you are calling the implies function with the FilePermission: 
670:         * "/tmp/scratch/foo", "read,write", then the implies function must
671:         * take into account both the /tmp/- and /tmp/scratch/foo
672:         * permissions, so the effective permission is "read,write".
673:         * 
674:         * @see java.security.Permission
675:         * @see java.security.Permissions
676:         * @see java.security.PermissionCollection
677:         *
678:         * @version 1.67 02/02/00
679:         *
680:         *
681:         * @serial include
682:         *
683:         */
684:
685:        final class FilePermissionCollection extends PermissionCollection
686:                implements  Serializable {
687:
688:            // Not serialized; see serialization section at end of class
689:            private transient List perms;
690:
691:            /**
692:             * Create an empty FilePermissions object.
693:             *
694:             */
695:
696:            public FilePermissionCollection() {
697:                perms = new ArrayList();
698:            }
699:
700:            /**
701:             * Adds a permission to the FilePermissions. The key for the hash is
702:             * permission.path.
703:             *
704:             * @param permission the Permission object to add.
705:             *
706:             * @exception IllegalArgumentException - if the permission is not a
707:             *                                       FilePermission 
708:             *
709:             * @exception SecurityException - if this FilePermissionCollection object
710:             *                                has been marked readonly
711:             */
712:
713:            public void add(Permission permission) {
714:                if (!(permission instanceof  FilePermission))
715:                    throw new IllegalArgumentException("invalid permission: "
716:                            + permission);
717:                if (isReadOnly())
718:                    throw new SecurityException(
719:                            "attempt to add a Permission to a readonly PermissionCollection");
720:
721:                // No need to synchronize because all adds are done sequentially
722:                // before any implies() calls
723:                perms.add(permission);
724:            }
725:
726:            /**
727:             * Check and see if this set of permissions implies the permissions 
728:             * expressed in "permission".
729:             *
730:             * @param p the Permission object to compare
731:             *
732:             * @return true if "permission" is a proper subset of a permission in 
733:             * the set, false if not.
734:             */
735:
736:            public boolean implies(Permission permission) {
737:                if (!(permission instanceof  FilePermission))
738:                    return false;
739:
740:                FilePermission fp = (FilePermission) permission;
741:
742:                int desired = fp.getMask();
743:                int effective = 0;
744:                int needed = desired;
745:
746:                int len = perms.size();
747:                for (int i = 0; i < len; i++) {
748:                    FilePermission x = (FilePermission) perms.get(i);
749:                    if (((needed & x.getMask()) != 0)
750:                            && x.impliesIgnoreMask(fp)) {
751:                        effective |= x.getMask();
752:                        if ((effective & desired) == desired)
753:                            return true;
754:                        needed = (desired ^ effective);
755:                    }
756:                }
757:                return false;
758:            }
759:
760:            /**
761:             * Returns an enumeration of all the FilePermission objects in the 
762:             * container.
763:             *
764:             * @return an enumeration of all the FilePermission objects.
765:             */
766:
767:            public Enumeration elements() {
768:                // Convert Iterator into Enumeration
769:                return Collections.enumeration(perms);
770:            }
771:
772:            private static final long serialVersionUID = 2202956749081564585L;
773:
774:            // Need to maintain serialization interoperability with earlier releases,
775:            // which had the serializable field:
776:            //    private Vector permissions;
777:
778:            /**
779:             * @serialField permissions java.util.Vector
780:             *     A list of FilePermission objects.
781:             */
782:            private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
783:                    "permissions", Vector.class), };
784:
785:            /**
786:             * @serialData "permissions" field (a Vector containing the FilePermissions).
787:             */
788:            /*
789:             * Writes the contents of the perms field out as a Vector for
790:             * serialization compatibility with earlier releases.
791:             */
792:            private void writeObject(ObjectOutputStream out) throws IOException {
793:                // Don't call out.defaultWriteObject()
794:
795:                // Write out Vector
796:                Vector permissions = new Vector(perms.size());
797:                permissions.addAll(perms);
798:
799:                ObjectOutputStream.PutField pfields = out.putFields();
800:                pfields.put("permissions", permissions);
801:                out.writeFields();
802:            }
803:
804:            /*
805:             * Reads in a Vector of FilePermissions and saves them in the perms field.
806:             */
807:            private void readObject(ObjectInputStream in) throws IOException,
808:                    ClassNotFoundException {
809:                // Don't call defaultReadObject()
810:
811:                // Read in serialized fields
812:                ObjectInputStream.GetField gfields = in.readFields();
813:
814:                // Get the one we want
815:                Vector permissions = (Vector) gfields.get("permissions", null);
816:                perms = new ArrayList(permissions.size());
817:                perms.addAll(permissions);
818:            }
819:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.