KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > security > jacc > JPolicyConfiguration


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JPolicyConfiguration.java,v 1.4 2004/07/08 13:45:47 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.security.jacc;
28
29 import java.security.Permission JavaDoc;
30 import java.security.PermissionCollection JavaDoc;
31 import java.security.Permissions JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.security.SecurityPermission JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import javax.security.jacc.PolicyConfiguration JavaDoc;
39 import javax.security.jacc.PolicyContextException JavaDoc;
40
41 import org.objectweb.util.monolog.api.BasicLevel;
42 import org.objectweb.util.monolog.api.Logger;
43
44 import org.objectweb.jonas_lib.I18n;
45
46 import org.objectweb.common.TraceCore;
47
48 /**
49  * Defines the PolicyConfiguration implementation class of JACC
50  * @author Florent Benoit
51  */

52 public class JPolicyConfiguration implements PolicyConfiguration JavaDoc {
53
54     /**
55      * Number of states
56      */

57     private static final int NB_STATES = 3;
58
59     /**
60      * Open state for the Policy Context Life Cycle
61      * Section 3.1.1.1
62      */

63     private static final int OPEN = 0;
64
65
66     /**
67      * inService state for the Policy Context Life Cycle
68      * Section 3.1.1.1
69      */

70     private static final int IN_SERVICE = 1;
71
72     /**
73      * Deleted state for the Policy Context Life Cycle
74      * Section 3.1.1.1
75      */

76     private static final int DELETED = 2;
77
78     /**
79      * I18n
80      */

81     private static I18n i18n = I18n.getInstance(JPolicyConfiguration.class);
82
83     /**
84      * String representation of the states
85      */

86     private static String JavaDoc[] states = null;
87
88     /**
89      * Current state
90      */

91     private int state;
92
93     /**
94      * ContextID string which differentiate all instances
95      */

96     private String JavaDoc contextID = null;
97
98     /**
99      * Logger
100      */

101     private static Logger logger = null;
102
103
104     /**
105      * Excluded permissions
106      */

107     private PermissionCollection JavaDoc excludedPermissions = null;
108
109     /**
110      * Unchecked permissions
111      */

112     private PermissionCollection JavaDoc uncheckedPermissions = null;
113
114     /**
115      * Role permissions
116      */

117     private Map JavaDoc rolePermissions = null;
118
119     /**
120      * Constructor of a new PolicyConfiguration object
121      * @param contextID Identifier of this PolicyConfiguration object
122      */

123     public JPolicyConfiguration(String JavaDoc contextID) {
124         this.contextID = contextID;
125
126         // init logger
127
logger = TraceCore.sec;
128
129         // init string representation
130
if (states == null) {
131             // 3 states
132
states = new String JavaDoc[NB_STATES];
133             states[OPEN] = i18n.getMessage("JPolicyConfiguration.openState");
134             states[IN_SERVICE] = i18n.getMessage("JPolicyConfiguration.inServiceState");
135             states[DELETED] = i18n.getMessage("JPolicyConfiguration.deletedState");
136         }
137
138         // initial state is open
139
resetState();
140
141         // init permissions
142
excludedPermissions = new Permissions JavaDoc();
143         uncheckedPermissions = new Permissions JavaDoc();
144         rolePermissions = new HashMap JavaDoc();
145     }
146
147     /**
148      * Used to add a single excluded policy statement to this PolicyConfiguration.
149      * @param permission the permission to be added to the excluded policy statements.
150      * @throws SecurityException if called by an AccessControlContext that has
151      * not been granted the "setPolicy" SecurityPermission.
152      * @throws UnsupportedOperationException if the state of the policy context whose
153      * interface is this PolicyConfiguration Object is "deleted" or "inService"
154      * when this method is called.
155      * @throws PolicyContextException if the implementation throws a checked exception
156      * that has not been accounted for by the addToExcludedPolicy method
157      * signature. The exception thrown by the implementation class will be
158      * encapsulated (during construction) in the thrown PolicyContextException.
159      */

160     public void addToExcludedPolicy(Permission JavaDoc permission)
161         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
162
163         if (logger.isLoggable(BasicLevel.DEBUG)) {
164             logger.log(BasicLevel.DEBUG, "Adding permission '" + permission + "' as excluded policy.");
165         }
166
167         // Section 3.3 - Check permissions
168
checkSetPolicy();
169
170         // Open state required
171
checkCurrentStateIsInState(OPEN);
172
173         // Add permission
174
if (permission != null) {
175             excludedPermissions.add(permission);
176         }
177
178     }
179
180     /**
181      * Used to add excluded policy statements to this PolicyConfiguration.
182      * @param permissions the collection of permissions to be added to the
183      * excluded policy statements. The collection may be either
184      * a homogenous or heterogenous collection.
185      * @throws SecurityException if called by an AccessControlContext that
186      * has not been granted the "setPolicy" SecurityPermission.
187      * @throws UnsupportedOperationException if the state of the policy
188      * context whose interface is this PolicyConfiguration Object
189      * is "deleted" or "inService" when this method is called.
190      * @throws PolicyContextException if the implementation throws a
191      * checked exception that has not been accounted for by
192      * the addToExcludedPolicy method signature. The exception
193      * thrown by the implementation class will be encapsulated
194      * (during construction) in the thrown PolicyContextException.
195      */

196     public void addToExcludedPolicy(PermissionCollection JavaDoc permissions)
197         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
198
199         if (logger.isLoggable(BasicLevel.DEBUG)) {
200             logger.log(BasicLevel.DEBUG, "Adding permissions '" + permissions + "' as excluded policy.");
201         }
202
203         // Section 3.3 - Check permissions
204
checkSetPolicy();
205
206         // Open state required
207
checkCurrentStateIsInState(OPEN);
208
209         // Add permissions
210
if (permissions != null) {
211             for (Enumeration JavaDoc e = permissions.elements(); e.hasMoreElements();) {
212                 excludedPermissions.add((Permission JavaDoc) e.nextElement());
213             }
214         }
215
216     }
217
218     /**
219      * Used to add a single permission to a named role in this PolicyConfiguration.
220      * @param roleName the name of the Role to which the permission is to be added.
221      * @param permission the permission to be added to the role.
222      * @throws SecurityException if called by an AccessControlContext that has not
223      * been granted the "setPolicy" SecurityPermission.
224      * @throws UnsupportedOperationException if the state of the policy context
225      * whose interface is this PolicyConfiguration Object is "deleted"
226      * or "inService" when this method is called.
227      * @throws PolicyContextException - if the implementation throws a checked exception
228      * that has not been accounted for by the addToRole method signature.
229      * The exception thrown by the implementation class will be encapsulated
230      * (during construction) in the thrown PolicyContextException.
231      */

232     public void addToRole(String JavaDoc roleName, Permission JavaDoc permission)
233         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
234
235         if (logger.isLoggable(BasicLevel.DEBUG)) {
236             logger.log(BasicLevel.DEBUG, "Adding permission '" + permission + "' to role '" + roleName + "'.");
237         }
238
239         // Section 3.3 - Check permissions
240
checkSetPolicy();
241
242         // Open state required
243
checkCurrentStateIsInState(OPEN);
244
245         // Fail if roleName is null
246
if (roleName == null) {
247             throw new PolicyContextException JavaDoc(i18n.getMessage("JPolicyConfiguration.addToRole"));
248         }
249
250
251         // Break if permission is null
252
if (permission == null) {
253             return;
254         }
255         PermissionCollection JavaDoc permissionsOfRole = (PermissionCollection JavaDoc) rolePermissions.get(roleName);
256
257         //create permission object if no previous permission for the given role
258
if (permissionsOfRole == null) {
259             permissionsOfRole = new Permissions JavaDoc();
260         }
261         permissionsOfRole.add(permission);
262
263         // add to the list
264
rolePermissions.put(roleName, permissionsOfRole);
265
266     }
267
268     /**
269      * Used to add permissions to a named role in this PolicyConfiguration.
270      * @param roleName the name of the Role to which the permissions are to be added.
271      * @param permissions the collection of permissions to be added to the role.
272      * The collection may be either a homogenous or heterogenous collection.
273      * @throws SecurityException if called by an AccessControlContext that has
274      * not been granted the "setPolicy" SecurityPermission.
275      * @throws UnsupportedOperationException if the state of the policy context whose
276      * interface is this PolicyConfiguration Object is "deleted" or
277      * inService" when this method is called.
278      * @throws PolicyContextException - if the implementation throws a checked exception
279      * that has not been accounted for by the addToRole method signature.
280      * The exception thrown by the implementation class will be encapsulated
281      * (during construction) in the thrown PolicyContextException.
282      */

283     public void addToRole(String JavaDoc roleName, PermissionCollection JavaDoc permissions)
284         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
285
286         if (logger.isLoggable(BasicLevel.DEBUG)) {
287             logger.log(BasicLevel.DEBUG, "Adding permissions '" + permissions + "' to role '" + roleName + "'.");
288         }
289
290         // Section 3.3 - Check permissions
291
checkSetPolicy();
292
293         // Open state required
294
checkCurrentStateIsInState(OPEN);
295
296         // Fail if roleName is null
297
if (roleName == null) {
298             throw new PolicyContextException JavaDoc(i18n.getMessage("JPolicyConfiguration.addToRole"));
299         }
300
301
302         // Break if permission is null
303
if (permissions == null) {
304             return;
305         }
306         PermissionCollection JavaDoc permissionsOfRole = (PermissionCollection JavaDoc) rolePermissions.get(roleName);
307
308         //create permission object if no previous permission for the given role
309
if (permissionsOfRole == null) {
310             permissionsOfRole = new Permissions JavaDoc();
311         }
312
313         for (Enumeration JavaDoc e = permissions.elements(); e.hasMoreElements();) {
314             permissionsOfRole.add((Permission JavaDoc) e.nextElement());
315         }
316
317         // add to the list
318
rolePermissions.put(roleName, permissionsOfRole);
319
320     }
321
322     /**
323      * Used to add a single unchecked policy statement to this PolicyConfiguration.
324      * @param permission the permission to be added to the unchecked policy statements.
325      * @throws SecurityException if called by an AccessControlContext that has not
326      * been granted the "setPolicy" SecurityPermission.
327      * @throws UnsupportedOperationException if the state of the policy context whose
328      * interface is this PolicyConfiguration Object is "deleted" or
329      * "inService" when this method is called.
330      * @throws PolicyContextException if the implementation throws a checked exception
331      * that has not been accounted for by the addToUncheckedPolicy method
332      * signature. The exception thrown by the implementation class will be
333      * encapsulated (during construction) in the thrown PolicyContextException.
334      */

335     public void addToUncheckedPolicy(Permission JavaDoc permission)
336         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
337
338         if (logger.isLoggable(BasicLevel.DEBUG)) {
339             logger.log(BasicLevel.DEBUG, "Adding permission '" + permission + "' as unchecked policy.");
340         }
341
342         // Section 3.3 - Check permissions
343
checkSetPolicy();
344
345         // Open state required
346
checkCurrentStateIsInState(OPEN);
347
348         // Add permission
349
if (permission != null) {
350             uncheckedPermissions.add(permission);
351         }
352
353     }
354
355     /**
356      * Used to add unchecked policy statements to this PolicyConfiguration.
357      * @param permissions the collection of permissions to be added as
358      * unchecked policy statements. The collection may be either a
359      * homogenous or heterogenous collection.
360      * @throws SecurityException if called by an AccessControlContext that
361      * has not been granted the "setPolicy" SecurityPermission.
362      * @throws UnsupportedOperationException if the state of the policy
363      * context whose interface is this PolicyConfiguration Object
364      * is "deleted" or "inService" when this method is called.
365      * @throws PolicyContextException if the implementation throws a checked
366      * exception that has not been accounted for by the
367      * addToUncheckedPolicy method signature. The exception thrown by
368      * the implementation class will be encapsulated (during construction)
369      * in the thrown PolicyContextException.
370      */

371     public void addToUncheckedPolicy(PermissionCollection JavaDoc permissions)
372         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
373
374         if (logger.isLoggable(BasicLevel.DEBUG)) {
375             logger.log(BasicLevel.DEBUG, "Adding permissions '" + permissions + "' as unchecked policy.");
376         }
377
378         // Section 3.3 - Check permissions
379
checkSetPolicy();
380
381         // Open state required
382
checkCurrentStateIsInState(OPEN);
383
384         // Add permissions
385
if (permissions != null) {
386             for (Enumeration JavaDoc e = permissions.elements(); e.hasMoreElements();) {
387                 uncheckedPermissions.add((Permission JavaDoc) e.nextElement());
388             }
389         }
390
391     }
392
393
394     /**
395      * This method is used to set to "inService" the state of the policy
396      * context whose interface is this PolicyConfiguration Object.
397      * Only those policy contexts whose state is "inService" will be included
398      * in the policy contexts processed by the Policy.refresh method. A policy
399      * context whose state is "inService" may be returned to the "open" state
400      * by calling the getPolicyConfiguration method of the PolicyConfiguration
401      * factory with the policy context identifier of the policy context.
402      * When the state of a policy context is "inService", calling any method
403      * other than commit, delete, getContextID, or inService on its
404      * PolicyConfiguration Object will cause an UnsupportedOperationException
405      * to be thrown.
406      * @throws SecurityException if called by an AccessControlContext that has
407      * not been granted the "setPolicy" SecurityPermission.
408      * @throws UnsupportedOperationException if the state of the policy context
409      * whose interface is this PolicyConfiguration Object is "deleted"
410      * when this method is called.
411      * @throws PolicyContextException if the implementation throws a checked
412      * exception that has not been accounted for by the commit method
413      * signature. The exception thrown by the implementation class
414      * will be encapsulated (during construction) in the thrown
415      * PolicyContextException.
416      */

417     public void commit() throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
418
419         // Section 3.3 - Check permissions
420
checkSetPolicy();
421
422         // Deleted state refused
423
checkCurrentStateNotInState(DELETED);
424
425         // Now state is in service
426
state = IN_SERVICE;
427
428         // add the configuration of this object
429
JPolicyConfigurationKeeper.addConfiguration(this);
430    }
431
432     /**
433      * Causes all policy statements to be deleted from this PolicyConfiguration
434      * and sets its internal state such that calling any method, other than delete,
435      * getContextID, or inService on the PolicyConfiguration will be rejected and
436      * cause an UnsupportedOperationException to be thrown.
437      * This operation has no affect on any linked PolicyConfigurations other than
438      * removing any links involving the deleted PolicyConfiguration.
439      * @throws SecurityException if called by an AccessControlContext that has
440      * not been granted the "setPolicy" SecurityPermission.
441      * @throws PolicyContextException if the implementation throws a checked
442      * exception that has not been accounted for by the delete method
443      * signature. The exception thrown by the implementation class will
444      * be encapsulated (during construction) in the thrown
445      * PolicyContextException.
446      */

447     public void delete() throws PolicyContextException JavaDoc, SecurityException JavaDoc {
448
449         // Section 3.3 - Check permissions
450
checkSetPolicy();
451
452         // all policy statements are deleted
453
excludedPermissions = new Permissions JavaDoc();
454         uncheckedPermissions = new Permissions JavaDoc();
455         rolePermissions = new HashMap JavaDoc();
456
457         // change state to DELETED
458
state = DELETED;
459
460         // remove the configuration of this object
461
JPolicyConfigurationKeeper.removeConfiguration(this);
462
463     }
464
465     /**
466      * This method returns this object's policy context identifier.
467      * @return this object's policy context identifier.
468      * @throws SecurityException if called by an AccessControlContext
469      * that has not been granted the "setPolicy" SecurityPermission.
470      * @throws PolicyContextException if the implementation throws a checked
471      * exception that has not been accounted for by the getContextID
472      * method signature. The exception thrown by the implementation
473      * class will be encapsulated (during construction) in the
474      * thrown PolicyContextException.
475      */

476     public String JavaDoc getContextID() throws PolicyContextException JavaDoc, SecurityException JavaDoc {
477
478         // Section 3.3 - Check permissions
479
checkSetPolicy();
480
481         return contextID;
482     }
483
484     /**
485      * This method is used to determine if the policy context whose interface
486      * is this PolicyConfiguration Object is in the "inService" state.
487      * @return true if the state of the associated policy context is
488      * "inService"; false otherwise.
489      * @throws SecurityException if called by an AccessControlContext that
490      * has not been granted the "setPolicy" SecurityPermission.
491      * @throws PolicyContextException if the implementation throws a checked
492      * exception that has not been accounted for by the inService
493      * method signature. The exception thrown by the implementation
494      * class will be encapsulated (during construction) in the thrown
495      * PolicyContextException.
496      */

497     public boolean inService() throws PolicyContextException JavaDoc, SecurityException JavaDoc {
498
499         // Section 3.3 - Check permissions
500
checkSetPolicy();
501
502         return (state == IN_SERVICE);
503     }
504
505     /**
506      * Creates a relationship between this configuration and another such that
507      * they share the same principal-to-role mappings.
508      * PolicyConfigurations are linked to apply a common principal-to-role
509      * mapping to multiple seperately manageable PolicyConfigurations,
510      * as is required when an application is composed of multiple modules.
511      * Note that the policy statements which comprise a role, or comprise
512      * the excluded or unchecked policy collections in a PolicyConfiguration
513      * are unaffected by the configuration being linked to another.
514      * @param link a reference to a different PolicyConfiguration than this
515      * PolicyConfiguration. The relationship formed by this method is
516      * symetric, transitive and idempotent. If the argument
517      * PolicyConfiguration does not have a different Policy context
518      * identifier than this PolicyConfiguration no relationship is
519      * formed, and an exception, as described below, is thrown.
520      * @throws SecurityException if called by an AccessControlContext that
521      * has not been granted the "setPolicy" SecurityPermission.
522      * @throws UnsupportedOperationException if the state of the policy context
523      * whose interface is this PolicyConfiguration Object is "deleted"
524      * or "inService" when this method is called.
525      * @throws IllegalArgumentException if called with an argument
526      * PolicyConfiguration whose Policy context is equivalent
527      * to that of this PolicyConfiguration.
528      * @throws PolicyContextException if the implementation throws a checked
529      * exception that has not been accounted for by the linkConfiguration
530      * method signature. The exception thrown by the implementation
531      * class will be encapsulated (during construction) in the
532      * thrown PolicyContextException.
533
534      */

535     public void linkConfiguration(PolicyConfiguration JavaDoc link)
536         throws IllegalArgumentException JavaDoc, PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
537
538         // Section 3.3 - Check permissions
539
checkSetPolicy();
540
541         // Open state required
542
checkCurrentStateIsInState(OPEN);
543
544         // Equivalent to this PolicyConfiguration object ?
545
if (this.equals(link)) {
546             throw new IllegalArgumentException JavaDoc(i18n.getMessage("JPolicyConfiguration.linkConfiguration.equivalent", this, link));
547         }
548
549         //TODO : link objects together.
550

551     }
552
553     /**
554      * Used to remove any excluded policy statements from this PolicyConfiguration.
555      * @throws SecurityException if called by an AccessControlContext that
556      * has not been granted the "setPolicy" SecurityPermission.
557      * @throws UnsupportedOperationException if the state of the policy context
558      * whose interface is this PolicyConfiguration Object is "deleted"
559      * or "inService" when this method is called.
560      * @throws PolicyContextException if the implementation throws a checked
561      * exception that has not been accounted for by the
562      * removeExcludedPolicy method signature. The exception thrown
563      * by the implementation class will be encapsulated
564      * (during construction) in the thrown PolicyContextException.
565      */

566     public void removeExcludedPolicy()
567         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
568
569         // Section 3.3 - Check permissions
570
checkSetPolicy();
571
572         // Open state required
573
checkCurrentStateIsInState(OPEN);
574
575         // reinit
576
excludedPermissions = new Permissions JavaDoc();
577     }
578
579     /**
580      * Used to remove a role and all its permissions from this PolicyConfiguration.
581      * @param roleName the name of the Role to remove from this PolicyConfiguration.
582      * @throws SecurityException if called by an AccessControlContext that has not
583      * been granted the "setPolicy" SecurityPermission.
584      * @throws UnsupportedOperationException if the state of the policy context whose
585      * interface is this PolicyConfiguration Object is "deleted" or "inService"
586      * when this method is called.
587      * @throws PolicyContextException if the implementation throws a checked exception
588      * that has not been accounted for by the removeRole method signature.
589      * The exception thrown by the implementation class will be encapsulated
590      * (during construction) in the thrown PolicyContextException.
591      */

592     public void removeRole(String JavaDoc roleName)
593         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
594
595         // Section 3.3 - Check permissions
596
checkSetPolicy();
597
598         // Open state required
599
checkCurrentStateIsInState(OPEN);
600
601         // Remove role permissions
602
rolePermissions.remove(roleName);
603     }
604
605     /**
606      * Used to remove any unchecked policy statements from this PolicyConfiguration.
607      * @throws SecurityException if called by an AccessControlContext that has not
608      * been granted the "setPolicy" SecurityPermission.
609      * @throws UnsupportedOperationException if the state of the policy context
610      * whose interface is this PolicyConfiguration Object is "deleted" or
611      * "inService" when this method is called.
612      * @throws PolicyContextException if the implementation throws a checked
613      * exception that has not been accounted for by the removeUncheckedPolicy
614      * method signature. The exception thrown by the implementation
615      * class will be encapsulated (during construction) in the thrown
616      * PolicyContextException.
617      */

618     public void removeUncheckedPolicy()
619         throws PolicyContextException JavaDoc, SecurityException JavaDoc, UnsupportedOperationException JavaDoc {
620
621         // Section 3.3 - Check permissions
622
checkSetPolicy();
623
624         // Open state required
625
checkCurrentStateIsInState(OPEN);
626
627         // Remove unckecked policy
628
uncheckedPermissions = new Permissions JavaDoc();
629     }
630
631     /**
632      * Check if the current state is not the given state
633      * Authorized states are described in javadoc of PolicyConfiguration class
634      * @param s given state
635      * @throws UnsupportedOperationException if the state is not the given state
636      */

637     private void checkCurrentStateNotInState(int s) throws UnsupportedOperationException JavaDoc {
638         if (this.state == s) {
639             String JavaDoc err = i18n.getMessage("JPolicyConfiguration.checkCurrentStateNotInState.notValidState", states[s], states[state]);
640             throw new UnsupportedOperationException JavaDoc(err);
641         }
642     }
643
644     /**
645      * Check if the current state is in the given state
646      * Authorized states are described in javadoc of PolicyConfiguration class
647      * @param s given state
648      * @throws UnsupportedOperationException if the state is not in a valid state
649      */

650     private void checkCurrentStateIsInState(int s) throws UnsupportedOperationException JavaDoc {
651         if (this.state != s) {
652             String JavaDoc err = i18n.getMessage("JPolicyConfiguration.checkCurrentStateNotInState.notValidState", states[state], states[s]);
653             throw new UnsupportedOperationException JavaDoc(err);
654         }
655     }
656
657     /**
658      * Method which check setPolicy access
659      * Section 3.3 : all public methods must throw a SecurityException
660      * when called by an AccessControlContext that has
661      * not been granted the "setPolicy" SecurityPermission
662      * @throws SecurityException when called by an AccessControlContext that
663      * has not been granted the "setPolicy" SecurityPermission.
664      */

665     private void checkSetPolicy() throws SecurityException JavaDoc {
666         SecurityManager JavaDoc securityManager = System.getSecurityManager();
667         if (securityManager != null) {
668             securityManager.checkPermission(new SecurityPermission JavaDoc("setPolicy"));
669         }
670     }
671
672     /**
673      * Indicates whether some other object is "equal to" this one.
674      * @param obj the reference object with which to compare.
675      * @return true if this object is the same as the obj argument;
676      * false otherwise.
677      */

678     public boolean equals(Object JavaDoc obj) {
679         if (!(obj instanceof PolicyConfiguration JavaDoc)) {
680             if (logger.isLoggable(BasicLevel.ERROR)) {
681                 logger.log(BasicLevel.ERROR, i18n.getMessage("JPolicyConfiguration.equals.notInstanceOf"));
682             }
683             return false;
684         } else {
685             try {
686                 return (this.contextID == ((PolicyConfiguration JavaDoc) obj).getContextID());
687             } catch (PolicyContextException JavaDoc pce) {
688                 if (logger.isLoggable(BasicLevel.ERROR)) {
689                     logger.log(BasicLevel.ERROR, i18n.getMessage("JPolicyConfiguration.equals.canNotCheck", pce.getMessage()));
690                 }
691                 return false;
692             }
693         }
694     }
695
696     /**
697      * Gets a hash code value for the object.
698      * @return a hash code value for this object.
699      */

700      public int hashCode() {
701          return contextID.hashCode();
702      }
703
704
705     /**
706      * Reset to OPEN state (Used by PolicyConfigurationFactory)
707      */

708     protected void resetState() {
709         this.state = OPEN;
710     }
711
712
713
714     /**
715      * Gets the excluded permission
716      * @return the excluded permission
717      */

718     public PermissionCollection JavaDoc getExcludedPermissions() {
719         // Works only if state is in service
720
if (state != IN_SERVICE) {
721             return new Permissions JavaDoc();
722         } else {
723             return excludedPermissions;
724         }
725     }
726
727
728     /**
729      * Gets the excluded permission
730      * @return the excluded permission
731      */

732     public PermissionCollection JavaDoc getUncheckedPermissions() {
733         // Works only if state is in service
734
if (state != IN_SERVICE) {
735             return new Permissions JavaDoc();
736         } else {
737             return uncheckedPermissions;
738         }
739     }
740
741
742     /**
743      * Gets the permissions for a given principal
744      * @param principal given principal
745      * @return the permissions for a given principal
746      */

747     public PermissionCollection JavaDoc getPermissionsForPrincipal(Principal JavaDoc principal) {
748
749         if (logger.isLoggable(BasicLevel.DEBUG)) {
750             logger.log(BasicLevel.DEBUG, "principal = " + principal);
751         }
752
753         // Works only if state is in service and if principal is not null
754
if (principal == null || state != IN_SERVICE) {
755             return new Permissions JavaDoc();
756         }
757
758         PermissionCollection JavaDoc permissionsOfRole = (PermissionCollection JavaDoc) rolePermissions.get(principal.getName());
759
760         if (logger.isLoggable(BasicLevel.DEBUG)) {
761             logger.log(BasicLevel.DEBUG, "Permissions found = " + permissionsOfRole);
762         }
763
764         //create permission object if no previous permission for the given role
765
if (permissionsOfRole == null) {
766             permissionsOfRole = new Permissions JavaDoc();
767         }
768
769         return permissionsOfRole;
770     }
771
772 }
773
Popular Tags