KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > daemon > DaemonPermission


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.daemon;
18
19 import java.security.Permission JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * This class represents the permissions to control and query the status of
24  * a <code>Daemon</code>. A <code>DaemonPermission</code> consists of a
25  * target name and a list of actions associated with it.
26  * <p>
27  * In this specification version the only available target name for this
28  * permission is &quot;control&quot;, but further releases may add more target
29  * names to fine-tune the access that needs to be granted to the caller.
30  * </p>
31  * <p>
32  * Actions are defined by a string of comma-separated values, as shown in the
33  * table below. The empty string implies no permission at all, while the
34  * special &quot;*&quot; value implies all permissions for the given
35  * name:
36  * </p>
37  * <p>
38  * <table width="100%" border="1">
39  * <tr>
40  * <th>Target&quot;Name</th>
41  * <th>Action</th>
42  * <th>Description</th>
43  * </tr>
44  * <tr>
45  * <td rowspan="5">&quot;control&quot;</td>
46  * <td>&quot;start&quot;</td>
47  * <td>
48  * The permission to call the <code>start()</code> method in an instance
49  * of a <code>DaemonController</code> interface.
50  * </td>
51  * </tr>
52  * <tr>
53  * <td>&quot;stop&quot;</td>
54  * <td>
55  * The permission to call the <code>stop()</code> method in an instance
56  * of a <code>DaemonController</code> interface.
57  * </td>
58  * </tr>
59  * <tr>
60  * <td>&quot;shutdown&quot;</td>
61  * <td>
62  * The permission to call the <code>shutdown()</code> method in an instance
63  * of a <code>DaemonController</code> interface.
64  * </td>
65  * </tr>
66  * <tr>
67  * <td>&quot;reload&quot;</td>
68  * <td>
69  * The permission to call the <code>reload()</code> method in an instance
70  * of a <code>DaemonController</code> interface.
71  * </td>
72  * </tr>
73  * <tr>
74  * <td>&quot;*&quot;</td>
75  * <td>
76  * The special wildcard action implies all above-mentioned action. This is
77  * equal to construct a permission with the &quot;start, stop, shutdown,
78  * reload&quot; list of actions.
79  * </td>
80  * </tr>
81  * </table>
82  * </p>
83  *
84  * @author Pier Fumagalli
85  * @author Copyright &copy; 2000-2001 <a HREF="http://www.apache.org/">The
86  * Apache Software Foundation</a>. All rights reserved.
87  * @version 1.0 <i>(CVS $Revision: 155409 $)</i>
88  */

89 public final class DaemonPermission extends Permission JavaDoc {
90
91     /* ==================================================================== */
92     /* Constants. */
93
94     /**
95      * The target name when associated with control actions
96      * (&quot;control&quot;).
97      */

98     protected static final String JavaDoc CONTROL = "control";
99
100     /**
101      * The target type when associated with control actions.
102      */

103     protected static final int TYPE_CONTROL = 1;
104
105     /**
106      * The action name associated with the permission to call the
107      * <code>DaemonController.start()</code> method.
108      */

109     protected static final String JavaDoc CONTROL_START = "start";
110
111     /**
112      * The action name associated with the permission to call the
113      * <code>DaemonController.stop()</code> method.
114      */

115     protected static final String JavaDoc CONTROL_STOP = "stop";
116
117     /**
118      * The action name associated with the permission to call the
119      * <code>DaemonController.shutdown()</code> method.
120      */

121     protected static final String JavaDoc CONTROL_SHUTDOWN = "shutdown";
122
123     /**
124      * The action name associated with the permission to call the
125      * <code>DaemonController.reload()</code> method.
126      */

127     protected static final String JavaDoc CONTROL_RELOAD = "reload";
128
129     /**
130      * The action mask associated with the permission to call the
131      * <code>DaemonController.start()</code> method.
132      */

133     protected static final int MASK_CONTROL_START = 0x01;
134
135     /**
136      * The action mask associated with the permission to call the
137      * <code>DaemonController.stop()</code> method.
138      */

139     protected static final int MASK_CONTROL_STOP = 0x02;
140
141     /**
142      * The action mask associated with the permission to call the
143      * <code>DaemonController.shutdown()</code> method.
144      */

145     protected static final int MASK_CONTROL_SHUTDOWN = 0x04;
146
147     /**
148      * The action mask associated with the permission to call the
149      * <code>DaemonController.reload()</code> method.
150      */

151     protected static final int MASK_CONTROL_RELOAD = 0x08;
152
153     /**
154      * The &quot;wildcard&quot; action implying all actions for the given
155      * target name.
156      */

157     protected static final String JavaDoc WILDCARD = "*";
158
159     /* ==================================================================== */
160     /* Instance variables */
161
162     /** The type of this permission object. */
163     private transient int type = 0;
164     /** The permission mask associated with this permission object. */
165     private transient int mask = 0;
166     /** The String representation of this permission object. */
167     private transient String JavaDoc desc = null;
168
169     /* ==================================================================== */
170     /* Constructors */
171
172     /**
173      * Create a new <code>DaemonPermission</code> instance with a specified
174      * permission name.
175      * <p>
176      * This constructor will create a new <code>DaemonPermission</code>
177      * instance that <b>will not</b> grant any permission to the caller.
178      *
179      * @param target The target name of this permission.
180      * @exception IllegalArgumentException If the specified target name is not
181      * supported.
182      */

183     public DaemonPermission (String JavaDoc target)
184     throws IllegalArgumentException JavaDoc {
185         // Setup the target name of this permission object.
186
super(target);
187
188         // Check if the permission target name was specified
189
if (target==null)
190             throw new IllegalArgumentException JavaDoc("Null permission name");
191
192         // Check if this is a "control" permission and set up accordingly.
193
if (CONTROL.equalsIgnoreCase(target)) {
194             type=TYPE_CONTROL;
195             return;
196         }
197
198         // If we got here, we have an invalid permission name.
199
throw new IllegalArgumentException JavaDoc("Invalid permission name \""+
200                                            target+"\" specified");
201     }
202
203     /**
204      * Create a new <code>DaemonPermission</code> instance with a specified
205      * permission name and a specified list of actions.
206      * <p>
207      * </p>
208      *
209      * @param target The target name of this permission.
210      * @param actions The list of actions permitted by this permission.
211      * @exception IllegalArgumentException If the specified target name is not
212      * supported, or the specified list of actions includes an
213      * invalid value.
214      */

215     public DaemonPermission(String JavaDoc target, String JavaDoc actions)
216     throws IllegalArgumentException JavaDoc {
217         // Setup this instance's target name.
218
this(target);
219
220         // Create the appropriate mask if this is a control permission.
221
if (this.type==TYPE_CONTROL) {
222             this.mask=this.createControlMask(actions);
223             return;
224         }
225     }
226
227     /* ==================================================================== */
228     /* Public methods */
229
230     /**
231      * Return the list of actions permitted by this instance of
232      * <code>DaemonPermission</code> in its canonical form.
233      *
234      * @return The canonicalized list of actions.
235      */

236     public String JavaDoc getActions() {
237         if (this.type==TYPE_CONTROL) {
238             return(this.createControlActions(this.mask));
239         }
240         return("");
241     }
242
243     /**
244      * Return the hash code for this <code>DaemonPermission</code> instance.
245      *
246      * @return An hash code value.
247      */

248     public int hashCode() {
249         this.setupDescription();
250         return(this.desc.hashCode());
251     }
252
253     /**
254      * Check if a specified object equals <code>DaemonPermission</code>.
255      *
256      * @return <b>true</b> or <b>false</b> wether the specified object equals
257      * this <code>DaemonPermission</code> instance or not.
258      */

259     public boolean equals(Object JavaDoc object) {
260         if (object == this) return(true);
261
262         if (!(object instanceof DaemonPermission)) return false;
263
264         DaemonPermission that = (DaemonPermission)object;
265
266         if (this.type!=that.type) return(false);
267         return(this.mask==that.mask);
268     }
269
270     /**
271      * Check if this <code>DaemonPermission</code> implies another
272      * <code>Permission</code>.
273      *
274      * @return <b>true</b> or <b>false</b> wether the specified permission
275      * is implied by this <code>DaemonPermission</code> instance or
276      * not.
277      */

278     public boolean implies(Permission JavaDoc permission) {
279         if (permission == this) return(true);
280
281         if (!(permission instanceof DaemonPermission)) return false;
282
283         DaemonPermission that = (DaemonPermission)permission;
284
285         if (this.type!=that.type) return(false);
286         return((this.mask&that.mask)==that.mask);
287     }
288
289     /**
290      * Return a <code>String</code> representation of this instance.
291      *
292      * @return A <code>String</code> representing this
293      * <code>DaemonPermission</code> instance.
294      */

295     public String JavaDoc toString() {
296         this.setupDescription();
297         return(new String JavaDoc(this.desc));
298     }
299
300     /* ==================================================================== */
301     /* Private methods */
302
303     /** Create a String description for this permission instance. */
304     private void setupDescription() {
305         if (this.desc!=null) return;
306
307         StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
308         buf.append(this.getClass().getName());
309         buf.append('[');
310         switch (this.type) {
311             case (TYPE_CONTROL): {
312                 buf.append(CONTROL);
313                 break;
314             }
315             default: {
316                 buf.append("UNKNOWN");
317                 break;
318             }
319         }
320         buf.append(':');
321         buf.append(this.getActions());
322         buf.append(']');
323
324         this.desc=buf.toString();
325     }
326
327     /** Create a permission mask for a given control actions string. */
328     private int createControlMask(String JavaDoc actions)
329     throws IllegalArgumentException JavaDoc {
330         if (actions==null) return(0);
331
332         int mask=0;
333         StringTokenizer JavaDoc tok=new StringTokenizer JavaDoc(actions,",",false);
334         while (tok.hasMoreTokens()) {
335             String JavaDoc val=tok.nextToken().trim();
336
337             if (WILDCARD.equals(val)) {
338                 return(MASK_CONTROL_START|MASK_CONTROL_STOP|
339                        MASK_CONTROL_SHUTDOWN|MASK_CONTROL_RELOAD);
340             } else if (CONTROL_START.equalsIgnoreCase(val)) {
341                 mask=mask|MASK_CONTROL_START;
342             } else if (CONTROL_STOP.equalsIgnoreCase(val)) {
343                 mask=mask|MASK_CONTROL_STOP;
344             } else if (CONTROL_SHUTDOWN.equalsIgnoreCase(val)) {
345                 mask=mask|MASK_CONTROL_SHUTDOWN;
346             } else if (CONTROL_RELOAD.equalsIgnoreCase(val)) {
347                 mask=mask|MASK_CONTROL_RELOAD;
348             } else {
349                 throw new IllegalArgumentException JavaDoc("Invalid action name \""+
350                                                    val+"\" specified");
351             }
352         }
353         return(mask);
354     }
355
356     /** Create a actions list for a given control permission mask. */
357     private String JavaDoc createControlActions(int mask) {
358         StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
359         boolean sep=false;
360
361         if ((mask&MASK_CONTROL_START)==MASK_CONTROL_START) {
362             sep=true;
363             buf.append(CONTROL_START);
364         }
365
366         if ((mask&MASK_CONTROL_STOP)==MASK_CONTROL_STOP) {
367             if (sep) buf.append(",");
368             else sep=true;
369             buf.append(CONTROL_STOP);
370         }
371
372         if ((mask&MASK_CONTROL_SHUTDOWN)==MASK_CONTROL_SHUTDOWN) {
373             if (sep) buf.append(",");
374             else sep=true;
375             buf.append(CONTROL_SHUTDOWN);
376         }
377
378         if ((mask&MASK_CONTROL_RELOAD)==MASK_CONTROL_RELOAD) {
379             if (sep) buf.append(",");
380             else sep=true;
381             buf.append(CONTROL_RELOAD);
382         }
383
384         return buf.toString();
385     }
386 }
387
Popular Tags