KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > osgi > service > wireadmin > WirePermission


1 /*
2  * $Header: /cvshome/build/org.osgi.service.wireadmin/src/org/osgi/service/wireadmin/WirePermission.java,v 1.13 2006/07/12 21:22:14 hargrave Exp $
3  *
4  * Copyright (c) OSGi Alliance (2002, 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 package org.osgi.service.wireadmin;
19
20 import java.io.IOException JavaDoc;
21 import java.security.*;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 /**
26  * Permission for the scope of a <code>Wire</code> object. When a
27  * <code>Envelope</code> object is used for communication with the <code>poll</code>
28  * or <code>update</code> method, and the scope is set, then the <code>Wire</code>
29  * object must verify that the Consumer service has
30  * <code>WirePermission[name,CONSUME]</code> and the Producer service has
31  * <code>WirePermission[name,PRODUCE]</code> for all names in the scope.
32  * <p>
33  * The names are compared with the normal rules for permission names. This means
34  * that they may end with a "*" to indicate wildcards. E.g. Door.* indicates all
35  * scope names starting with the string "Door". The last period is required due
36  * to the implementations of the <code>BasicPermission</code> class.
37  *
38  * @version $Revision: 1.13 $
39  */

40 final public class WirePermission extends BasicPermission {
41     static final long serialVersionUID = -5583709391516569321L;
42     /**
43      * The action string for the <code>PRODUCE</code> action: value is "produce".
44      */

45     public static final String JavaDoc PRODUCE = "produce";
46     /**
47      * The action string for the <code>CONSUME</code> action: value is "consume".
48      */

49     public static final String JavaDoc CONSUME = "consume";
50     private final static int ACTION_PRODUCE = 0x00000001;
51     private final static int ACTION_CONSUME = 0x00000002;
52     private final static int ACTION_ALL = ACTION_PRODUCE
53                                                         | ACTION_CONSUME;
54     private final static int ACTION_NONE = 0;
55     /**
56      * The actions mask.
57      */

58     private transient int action_mask = ACTION_NONE;
59     /**
60      * The actions in canonical form.
61      *
62      * @serial
63      */

64     private String JavaDoc actions = null;
65
66     /**
67      * Create a new WirePermission with the given name (may be wildcard) and
68      * actions.
69      * @param name Wire name.
70      * @param actions <code>produce</code>, <code>consume</code>
71      * (canonical order).
72      */

73     public WirePermission(String JavaDoc name, String JavaDoc actions) {
74         this(name, getMask(actions));
75     }
76
77     /**
78      * Package private constructor used by WirePermissionCollection.
79      *
80      * @param name class name
81      * @param mask action mask
82      */

83     WirePermission(String JavaDoc name, int mask) {
84         super(name);
85         init(mask);
86     }
87
88     /**
89      * Called by constructors and when deserialized.
90      *
91      * @param mask action mask
92      */

93     private void init(int mask) {
94         if ((mask == ACTION_NONE) || ((mask & ACTION_ALL) != mask)) {
95             throw new IllegalArgumentException JavaDoc("invalid action string");
96         }
97         action_mask = mask;
98     }
99
100     /**
101      * Parse action string into action mask.
102      *
103      * @param actions Action string.
104      * @return action mask.
105      */

106     private static int getMask(String JavaDoc actions) {
107         boolean seencomma = false;
108         int mask = ACTION_NONE;
109         if (actions == null) {
110             return mask;
111         }
112         char[] a = actions.toCharArray();
113         int i = a.length - 1;
114         if (i < 0)
115             return mask;
116         while (i != -1) {
117             char c;
118             // skip whitespace
119
while ((i != -1)
120                     && ((c = a[i]) == ' ' || c == '\r' || c == '\n'
121                             || c == '\f' || c == '\t'))
122                 i--;
123             // check for the known strings
124
int matchlen;
125             if (i >= 6 && (a[i - 6] == 'p' || a[i - 6] == 'P')
126                     && (a[i - 5] == 'r' || a[i - 5] == 'R')
127                     && (a[i - 4] == 'o' || a[i - 4] == 'O')
128                     && (a[i - 3] == 'd' || a[i - 3] == 'D')
129                     && (a[i - 2] == 'u' || a[i - 2] == 'U')
130                     && (a[i - 1] == 'c' || a[i - 1] == 'C')
131                     && (a[i] == 'e' || a[i] == 'E')) {
132                 matchlen = 7;
133                 mask |= ACTION_PRODUCE;
134             }
135             else
136                 if (i >= 6 && (a[i - 6] == 'c' || a[i - 6] == 'C')
137                         && (a[i - 5] == 'o' || a[i - 5] == 'O')
138                         && (a[i - 4] == 'n' || a[i - 4] == 'N')
139                         && (a[i - 3] == 's' || a[i - 3] == 'S')
140                         && (a[i - 2] == 'u' || a[i - 2] == 'U')
141                         && (a[i - 1] == 'm' || a[i - 1] == 'M')
142                         && (a[i] == 'e' || a[i] == 'E')) {
143                     matchlen = 7;
144                     mask |= ACTION_CONSUME;
145                 }
146                 else {
147                     // parse error
148
throw new IllegalArgumentException JavaDoc("invalid permission: "
149                             + actions);
150                 }
151             // make sure we didn't just match the tail of a word
152
// like "ackbarfregister". Also, skip to the comma.
153
seencomma = false;
154             while (i >= matchlen && !seencomma) {
155                 switch (a[i - matchlen]) {
156                     case ',' :
157                         seencomma = true;
158                     /* FALLTHROUGH */
159                     case ' ' :
160                     case '\r' :
161                     case '\n' :
162                     case '\f' :
163                     case '\t' :
164                         break;
165                     default :
166                         throw new IllegalArgumentException JavaDoc(
167                                 "invalid permission: " + actions);
168                 }
169                 i--;
170             }
171             // point i at the location of the comma minus one (or -1).
172
i -= matchlen;
173         }
174         if (seencomma) {
175             throw new IllegalArgumentException JavaDoc("invalid permission: " + actions);
176         }
177         return mask;
178     }
179
180     /**
181      * Checks if this <code>WirePermission</code> object <code>implies</code> the
182      * specified permission.
183      * <P>
184      * More specifically, this method returns <code>true</code> if:
185      * <p>
186      * <ul>
187      * <li><i>p </i> is an instanceof the <code>WirePermission</code> class,
188      * <li><i>p </i>'s actions are a proper subset of this object's actions,
189      * and
190      * <li><i>p </i>'s name is implied by this object's name. For example,
191      * <code>java.*</code> implies <code>java.home</code>.
192      * </ul>
193      *
194      * @param p The permission to check against.
195      *
196      * @return <code>true</code> if the specified permission is implied by this
197      * object; <code>false</code> otherwise.
198      */

199     public boolean implies(Permission p) {
200         if (p instanceof WirePermission) {
201             WirePermission target = (WirePermission) p;
202             return ((action_mask & target.action_mask) == target.action_mask)
203                     && super.implies(p);
204         }
205         return false;
206     }
207
208     /**
209      * Returns the canonical string representation of the actions. Always
210      * returns present actions in the following order: <code>produce</code>,
211      * <code>consume</code>.
212      *
213      * @return The canonical string representation of the actions.
214      */

215     public String JavaDoc getActions() {
216         if (actions == null) {
217             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
218             boolean comma = false;
219             if ((action_mask & ACTION_PRODUCE) == ACTION_PRODUCE) {
220                 sb.append(PRODUCE);
221                 comma = true;
222             }
223             if ((action_mask & ACTION_CONSUME) == ACTION_CONSUME) {
224                 if (comma)
225                     sb.append(',');
226                 sb.append(CONSUME);
227             }
228             actions = sb.toString();
229         }
230         return actions;
231     }
232
233     /**
234      * Returns a new <code>PermissionCollection</code> object for storing
235      * <code>WirePermission</code> objects.
236      *
237      * @return A new <code>PermissionCollection</code> object suitable for storing
238      * <code>WirePermission</code> objects.
239      */

240     public PermissionCollection newPermissionCollection() {
241         return new WirePermissionCollection();
242     }
243
244     /**
245      * Determines the equalty of two <code>WirePermission</code> objects.
246      *
247      * Checks that specified object has the same name and actions as this
248      * <code>WirePermission</code> object.
249      *
250      * @param obj The object to test for equality.
251      * @return true if <code>obj</code> is a <code>WirePermission</code>, and has
252      * the same name and actions as this <code>WirePermission</code>
253      * object; <code>false</code> otherwise.
254      */

255     public boolean equals(Object JavaDoc obj) {
256         if (obj == this) {
257             return true;
258         }
259         if (!(obj instanceof WirePermission)) {
260             return false;
261         }
262         WirePermission p = (WirePermission) obj;
263         return (action_mask == p.action_mask) && getName().equals(p.getName());
264     }
265
266     /**
267      * Returns the hash code value for this object.
268      *
269      * @return Hash code value for this object.
270      */

271     public int hashCode() {
272         return getName().hashCode() ^ getActions().hashCode();
273     }
274
275     /**
276      * Returns the current action mask. Used by the WirePermissionCollection
277      * object.
278      *
279      * @return The actions mask.
280      */

281     int getMask() {
282         return action_mask;
283     }
284
285     /**
286      * Returns a string describing this <code>WirePermission</code>. The
287      * convention is to specify the class name, the permission name, and the
288      * actions in the following format:
289      * '(org.osgi.service.wireadmin.WirePermission &quot;name&quot;
290      * &quot;actions&quot;)'.
291      *
292      * @return information about this <code>Permission</code> object.
293      */

294     public String JavaDoc toString() {
295         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
296         sb.append('(');
297         sb.append(getClass().getName());
298         sb.append(" \"");
299         sb.append(getName());
300         sb.append("\" \"");
301         sb.append(getActions());
302         sb.append("\")");
303         return sb.toString();
304     }
305
306     /**
307      * WriteObject is called to save the state of the ServicePermission to a
308      * stream. The actions are serialized, and the superclass takes care of the
309      * name.
310      */

311     private synchronized void writeObject(java.io.ObjectOutputStream JavaDoc s)
312             throws IOException JavaDoc {
313         // Write out the actions. The superclass takes care of the name
314
// call getActions to make sure actions field is initialized
315
if (actions == null)
316             getActions();
317         s.defaultWriteObject();
318     }
319
320     /**
321      * readObject is called to restore the state of the ServicePermission from a
322      * stream.
323      */

324     private synchronized void readObject(java.io.ObjectInputStream JavaDoc s)
325             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
326         // Read in the action, then initialize the rest
327
s.defaultReadObject();
328         init(getMask(actions));
329     }
330 }
331 /**
332  * A <code>WirePermissionCollection</code> stores a set of <code>WirePermission</code>
333  * permissions.
334  */

335
336 final class WirePermissionCollection extends PermissionCollection {
337     static final long serialVersionUID = 2617521094909826016L;
338     /**
339      * Table of permissions.
340      *
341      * @serial
342      */

343     private Hashtable JavaDoc permissions;
344     /**
345      * Boolean saying if "*" is in the collection.
346      *
347      * @serial
348      */

349     private boolean all_allowed;
350
351     /**
352      * Creates an empty WirePermissionCollection object.
353      *
354      */

355     public WirePermissionCollection() {
356         permissions = new Hashtable JavaDoc();
357         all_allowed = false;
358     }
359
360     /**
361      * Adds a permission to this PermissionCollection.
362      *
363      * @param permission The Permission object to add.
364      *
365      * @throws IllegalArgumentException If the permission is not a
366      * WirePermission object.
367      *
368      * @throws SecurityException If this PermissionCollection has been marked
369      * read-only.
370      */

371     public void add(Permission permission) {
372         if (!(permission instanceof WirePermission))
373             throw new IllegalArgumentException JavaDoc("invalid permission: "
374                     + permission);
375         if (isReadOnly())
376             throw new SecurityException JavaDoc("attempt to add a Permission to a "
377                     + "readonly PermissionCollection");
378         WirePermission p = (WirePermission) permission;
379         String JavaDoc name = p.getName();
380         WirePermission existing = (WirePermission) permissions.get(name);
381         if (existing != null) {
382             int oldMask = existing.getMask();
383             int newMask = p.getMask();
384             if (oldMask != newMask) {
385                 permissions.put(name, new WirePermission(name, oldMask
386                         | newMask));
387             }
388         }
389         else {
390             permissions.put(name, permission);
391         }
392         if (!all_allowed) {
393             if (name.equals("*"))
394                 all_allowed = true;
395         }
396     }
397
398     /**
399      * Determines if a set of permissions implies the permissions expressed in
400      * <code>permission</code>.
401      *
402      * @param permission The Permission object to compare.
403      *
404      * @return <code>true</code> if <code>permission</code> is a proper subset of a
405      * permission in the set; <code>false</code> otherwise.
406      */

407     public boolean implies(Permission permission) {
408         if (!(permission instanceof WirePermission))
409             return false;
410         WirePermission p = (WirePermission) permission;
411         WirePermission x;
412         int desired = p.getMask();
413         int effective = 0;
414         // short circuit if the "*" Permission was added
415
if (all_allowed) {
416             x = (WirePermission) permissions.get("*");
417             if (x != null) {
418                 effective |= x.getMask();
419                 if ((effective & desired) == desired)
420                     return true;
421             }
422         }
423         // strategy:
424
// Check for full match first. Then work our way up the
425
// name looking for matches on a.b.*
426
String JavaDoc name = p.getName();
427         x = (WirePermission) permissions.get(name);
428         if (x != null) {
429             // we have a direct hit!
430
effective |= x.getMask();
431             if ((effective & desired) == desired)
432                 return true;
433         }
434         // work our way up the tree...
435
int last, offset;
436         offset = name.length() - 1;
437         while ((last = name.lastIndexOf(".", offset)) != -1) {
438             name = name.substring(0, last + 1) + "*";
439             x = (WirePermission) permissions.get(name);
440             if (x != null) {
441                 effective |= x.getMask();
442                 if ((effective & desired) == desired)
443                     return (true);
444             }
445             offset = last - 1;
446         }
447         // we don't have to check for "*" as it was already checked
448
// at the top (all_allowed), so we just return false
449
return false;
450     }
451
452     /**
453      * Returns an enumeration of all the Permission objects in the container.
454      *
455      * @return Enumeration of all the Permission objects.
456      */

457     public Enumeration JavaDoc elements() {
458         return permissions.elements();
459     }
460 }
461
Popular Tags