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

Home
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
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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