KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > BasicPermissionCollection


1 /*
2  *
3  * Copyright 1999-2002 Opensugar S.A. All Rights Reserved.
4  *
5  * This software is the proprietary information of Opensugar S.A.
6  * Use is subject to license terms.
7  *
8  */

9
10 /*
11  *
12  * Dummy implementation of parts of the java.security package, for JVMs that do
13  * not support permissions.
14  *
15  * The package java.security is exported by the system bundle.
16  *
17  * When classes are loaded by the OSGi framework, they are first searched for using
18  * the system class loader. If they are found using the system class loader, they are
19  * not searched for in the shared packages. Therefore, the dummy implementation classes
20  * will never be loaded if the JVM does support permissions.
21  * If the JVM does not support permissions, then classes in the java.security package
22  * will not be found when searched for using the system class loader; they will then
23  * be searched for in the shared packages and will be found in the packages exported by
24  * the system bundle.
25  * (Note: the package sharing mechanism of the cube has been written with a special case
26  * for classes in the java.security package, so that bundles may load classes in this
27  * package from shared packages, even the bundle has no import clause for java.security
28  * in its manifest. In other words, java.security is implicitly imported by all bundles.)
29  *
30  *
31  */

32
33 package java.security;
34
35 import java.io.Serializable JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Hashtable JavaDoc;
38
39 final class BasicPermissionCollection extends PermissionCollection JavaDoc implements Serializable JavaDoc {
40
41    private Hashtable JavaDoc permissions;
42    private boolean all_allowed;
43
44    public BasicPermissionCollection() {
45       permissions = new Hashtable JavaDoc( 11 );
46       all_allowed = false;
47    }
48
49    public void add(Permission JavaDoc permission) {
50       if( !( permission instanceof BasicPermission JavaDoc ) ) {
51          throw new IllegalArgumentException JavaDoc( "invalid permission: " + permission );
52       }
53       BasicPermission JavaDoc basicpermission = (BasicPermission JavaDoc)permission;
54       permissions.put( basicpermission.getName(), permission );
55       if( !all_allowed && basicpermission.getName().equals( "*" ) ) {
56          all_allowed = true;
57       }
58    }
59
60    public Enumeration JavaDoc elements() {
61       return permissions.elements();
62    }
63
64    public boolean implies(Permission JavaDoc permission) {
65       if( !( permission instanceof BasicPermission JavaDoc ) ) {
66          return false;
67       }
68       BasicPermission JavaDoc basicpermission = (BasicPermission JavaDoc)permission;
69       if( all_allowed ) {
70          return true;
71       }
72       String JavaDoc s = basicpermission.getName();
73       Permission JavaDoc permission1 = (Permission JavaDoc)permissions.get( s );
74       if ( permission1 != null ) {
75          return permission1.implies( permission );
76       }
77       int i;
78       for( int j = s.length() - 1; ( i = s.lastIndexOf( ".", j ) ) != -1; j = i - 1 ) {
79          s = s.substring( 0, i + 1 ) + "*";
80          Permission JavaDoc permission2 = (Permission JavaDoc)permissions.get( s );
81          if( permission2 != null ) {
82             return permission2.implies( permission );
83          }
84       }
85
86       return false;
87    }
88
89 }
90
Popular Tags