KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > osgi > service > cm > ConfigurationPermission


1 /*
2  * $Header: /cvshome/build/org.osgi.service.cm/src/org/osgi/service/cm/ConfigurationPermission.java,v 1.22 2006/07/08 00:42:00 hargrave Exp $
3  *
4  * Copyright (c) OSGi Alliance (2004, 2006). All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.osgi.service.cm;
20
21 import java.security.*;
22 import java.util.Enumeration JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 /**
26  * Indicates a bundle's authority to configure bundles.
27  *
28  * This permission has only a single action: CONFIGURE.
29  *
30  * @version $Revision: 1.22 $
31  * @since 1.2
32  */

33
34 public final class ConfigurationPermission extends BasicPermission {
35     static final long serialVersionUID = 5716868734811965383L;
36     /**
37      * The action string <code>configure</code>.
38      */

39     public final static String JavaDoc CONFIGURE = "configure";
40
41     /**
42      * Create a new ConfigurationPermission.
43      *
44      * @param name Name must be &quot;*&quot;.
45      * @param actions <code>configure</code> (canonical order).
46      */

47
48     public ConfigurationPermission(String JavaDoc name, String JavaDoc actions) {
49         super(name);
50         if (!name.equals("*")) {
51             throw new IllegalArgumentException JavaDoc("name must be *");
52         }
53         actions = actions.trim();
54         if (actions.equalsIgnoreCase(CONFIGURE)||actions.equals("*"))
55             return;
56         
57         throw new IllegalArgumentException JavaDoc("actions must be " + CONFIGURE);
58     }
59
60     /**
61      * Determines if a <code>ConfigurationPermission</code> object "implies"
62      * the specified permission.
63      *
64      * @param p The target permission to check.
65      * @return <code>true</code> if the specified permission is implied by
66      * this object; <code>false</code> otherwise.
67      */

68
69     public boolean implies(Permission p) {
70         return p instanceof ConfigurationPermission;
71     }
72
73     /**
74      * Determines the equality of two <code>ConfigurationPermission</code>
75      * objects.
76      * <p>
77      * Two <code>ConfigurationPermission</code> objects are equal.
78      *
79      * @param obj The object being compared for equality with this object.
80      * @return <code>true</code> if <code>obj</code> is equivalent to this
81      * <code>ConfigurationPermission</code>; <code>false</code>
82      * otherwise.
83      */

84     public boolean equals(Object JavaDoc obj) {
85         return obj instanceof ConfigurationPermission;
86     }
87
88     /**
89      * Returns the hash code value for this object.
90      *
91      * @return Hash code value for this object.
92      */

93
94     public int hashCode() {
95         return getName().hashCode() ^ getActions().hashCode();
96     }
97
98     /**
99      * Returns the canonical string representation of the
100      * <code>ConfigurationPermission</code> actions.
101      *
102      * <p>
103      * Always returns present <code>ConfigurationPermission</code> actions in
104      * the following order: <code>CONFIGURE</code>
105      *
106      * @return Canonical string representation of the
107      * <code>ConfigurationPermission</code> actions.
108      */

109     public String JavaDoc getActions() {
110         return CONFIGURE;
111     }
112
113     /**
114      * Returns a new <code>PermissionCollection</code> object suitable for
115      * storing <code>ConfigurationPermission</code>s.
116      *
117      * @return A new <code>PermissionCollection</code> object.
118      */

119     public PermissionCollection newPermissionCollection() {
120         return new ConfigurationPermissionCollection();
121     }
122 }
123
124 /**
125  * Stores a set of <code>ConfigurationPermission</code> permissions.
126  *
127  * @see java.security.Permission
128  * @see java.security.Permissions
129  * @see java.security.PermissionCollection
130  */

131 final class ConfigurationPermissionCollection extends PermissionCollection {
132     static final long serialVersionUID = -6917638867081695839L;
133     /**
134      * True if collection is non-empty.
135      *
136      * @serial
137      */

138     private boolean hasElement;
139
140     /**
141      * Creates an empty <tt>ConfigurationPermissionCollection</tt> object.
142      *
143      */

144     public ConfigurationPermissionCollection() {
145         hasElement = false;
146     }
147
148     /**
149      * Adds the specified permission to the
150      * <tt>ConfigurationPermissionCollection</tt>. The key for the hash is
151      * the interface name of the service.
152      *
153      * @param permission The <tt>Permission</tt> object to add.
154      *
155      * @exception IllegalArgumentException If the permission is not an
156      * <tt>ConfigurationPermission</tt>.
157      *
158      * @exception SecurityException If this ConfigurationPermissionCollection
159      * object has been marked read-only.
160      */

161
162     public void add(Permission permission) {
163         if (!(permission instanceof ConfigurationPermission)) {
164             throw new IllegalArgumentException JavaDoc("invalid permission: "
165                     + permission);
166         }
167
168         if (isReadOnly())
169             throw new SecurityException JavaDoc("attempt to add a Permission to a "
170                     + "readonly PermissionCollection");
171
172         hasElement = true;
173     }
174
175     /**
176      * Determines if the specified set of permissions implies the permissions
177      * expressed in the parameter <tt>permission</tt>.
178      *
179      * @param p The Permission object to compare.
180      *
181      * @return true if permission is a proper subset of a permission in the set;
182      * false otherwise.
183      */

184
185     public boolean implies(Permission p) {
186         return hasElement && (p instanceof ConfigurationPermission);
187     }
188
189     /**
190      * Returns an enumeration of an <tt>ConfigurationPermission</tt> object.
191      *
192      * @return Enumeration of an <tt>ConfigurationPermission</tt> object.
193      */

194
195     public Enumeration JavaDoc elements() {
196         final boolean nonEmpty = hasElement;
197         return new Enumeration JavaDoc() {
198             private boolean more = nonEmpty;
199
200             public boolean hasMoreElements() {
201                 return more;
202             }
203
204             public Object JavaDoc nextElement() {
205                 if (more) {
206                     more = false;
207
208                     return new ConfigurationPermission("*",
209                             ConfigurationPermission.CONFIGURE);
210                 }
211                 else {
212                     throw new NoSuchElementException JavaDoc();
213                 }
214             }
215         };
216     }
217
218 }
219
Popular Tags