KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > jacc > JBossPolicyConfiguration


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security.jacc;
8
9 import java.security.Permission JavaDoc;
10 import java.security.PermissionCollection JavaDoc;
11 import javax.security.jacc.PolicyConfiguration JavaDoc;
12 import javax.security.jacc.PolicyContextException JavaDoc;
13
14 import org.jboss.util.state.StateMachine;
15 import org.jboss.util.state.IllegalTransitionException;
16 import org.jboss.util.state.State;
17 import org.jboss.logging.Logger;
18
19 /** The JACC PolicyConfiguration implementation. This class associates a
20  * context id with the permission ops it passes along to the global
21  * DelegatingPolicy instance.
22  *
23  * @author Scott.Stark@jboss.org
24  * @version $Revision: 1.4.2.1 $
25  */

26 public class JBossPolicyConfiguration
27    implements PolicyConfiguration JavaDoc
28 {
29    private static Logger log = Logger.getLogger(JBossPolicyConfiguration.class);
30    /** The JACC context id associated with the policy */
31    private String JavaDoc contextID;
32    /** The Policy impl which handles the JACC permissions */
33    private DelegatingPolicy policy;
34    /** A state machine whihc enforces the state behavior of this config */
35    private StateMachine configStateMachine;
36    /** A trace level logging flag set when the policy is created */
37    private boolean trace;
38
39    protected JBossPolicyConfiguration(String JavaDoc contextID, DelegatingPolicy policy,
40       StateMachine configStateMachine)
41       throws PolicyContextException JavaDoc
42    {
43       this.contextID = contextID;
44       this.policy = policy;
45       this.configStateMachine = configStateMachine;
46
47       if (contextID == null)
48          throw new IllegalArgumentException JavaDoc("contextID cannot be null");
49       if (policy == null)
50          throw new IllegalArgumentException JavaDoc("policy cannot be null");
51       if (configStateMachine == null)
52          throw new IllegalArgumentException JavaDoc("configStateMachine cannot be null");
53
54       validateState("getPolicyConfiguration");
55       trace = log.isTraceEnabled();
56       if( trace )
57          log.trace("ctor, contextID="+contextID);
58    }
59
60    void initPolicyConfiguration(boolean remove)
61       throws PolicyContextException JavaDoc
62    {
63       validateState("getPolicyConfiguration");
64       policy.initPolicyConfiguration(contextID, remove);
65    }
66
67    public void addToExcludedPolicy(Permission JavaDoc permission)
68       throws PolicyContextException JavaDoc
69    {
70       if( trace )
71          log.trace("addToExcludedPolicy, p="+permission);
72       validateState("addToExcludedPolicy");
73       policy.addToExcludedPolicy(contextID, permission);
74    }
75    
76    public void addToExcludedPolicy(PermissionCollection JavaDoc permissions)
77       throws PolicyContextException JavaDoc
78    {
79       if( trace )
80          log.trace("addToExcludedPolicy, pc="+permissions);
81       validateState("addToExcludedPolicy");
82       policy.addToExcludedPolicy(contextID, permissions);
83    }
84
85    public void addToRole(String JavaDoc roleName, Permission JavaDoc permission)
86       throws PolicyContextException JavaDoc
87    {
88       if( trace )
89          log.trace("addToRole, roleName="+roleName+", p="+permission);
90       validateState("addToRole");
91       policy.addToRole(contextID, roleName, permission);
92    }
93
94    public void addToRole(String JavaDoc roleName, PermissionCollection JavaDoc permissions)
95       throws PolicyContextException JavaDoc
96    {
97       if( trace )
98          log.trace("addToRole, roleName="+roleName+", pc="+permissions);
99       validateState("addToRole");
100       policy.addToRole(contextID, roleName, permissions);
101    }
102
103    public void addToUncheckedPolicy(Permission JavaDoc permission)
104       throws PolicyContextException JavaDoc
105    {
106       if( trace )
107          log.trace("addToUncheckedPolicy, p="+permission);
108       validateState("addToUncheckedPolicy");
109       policy.addToUncheckedPolicy(contextID, permission);
110    }
111
112    public void addToUncheckedPolicy(PermissionCollection JavaDoc permissions)
113       throws PolicyContextException JavaDoc
114    {
115       if( trace )
116          log.trace("addToUncheckedPolicy, pc="+permissions);
117       validateState("addToUncheckedPolicy");
118       policy.addToUncheckedPolicy(contextID, permissions);
119    }
120
121    public void commit()
122       throws PolicyContextException JavaDoc
123    {
124       if( trace )
125          log.trace("commit");
126       validateState("commit");
127       policy.commit(contextID);
128    }
129
130    public void delete()
131       throws PolicyContextException JavaDoc
132    {
133       if( trace )
134          log.trace("delete");
135       validateState("delete");
136       policy.delete(contextID);
137    }
138
139    public String JavaDoc getContextID()
140       throws PolicyContextException JavaDoc
141    {
142       validateState("getContextID");
143       return contextID;
144    }
145
146    public boolean inService()
147       throws PolicyContextException JavaDoc
148    {
149       validateState("inService");
150       State state = configStateMachine.getCurrentState();
151       boolean inService = state.getName().equals("inService");
152       return inService;
153    }
154
155    public void linkConfiguration(PolicyConfiguration JavaDoc link)
156       throws PolicyContextException JavaDoc
157    {
158       if( trace )
159          log.trace("linkConfiguration, linkTo: "+link.getContextID());
160       validateState("linkConfiguration");
161       policy.linkConfiguration(contextID, link);
162    }
163
164    public void removeExcludedPolicy()
165       throws PolicyContextException JavaDoc
166    {
167       if( trace )
168          log.trace("removeExcludedPolicy");
169       validateState("removeExcludedPolicy");
170       policy.removeExcludedPolicy(contextID);
171    }
172
173    public void removeRole(String JavaDoc roleName)
174       throws PolicyContextException JavaDoc
175    {
176       if( trace )
177          log.trace("removeRole: "+roleName);
178       validateState("removeRole");
179       policy.removeRole(contextID, roleName);
180    }
181
182    public void removeUncheckedPolicy()
183       throws PolicyContextException JavaDoc
184    {
185       if( trace )
186          log.trace("removeUncheckedPolicy");
187       validateState("removeUncheckedPolicy");
188       policy.removeUncheckedPolicy(contextID);
189    }
190
191    protected void validateState(String JavaDoc action)
192       throws PolicyContextException JavaDoc
193    {
194       try
195       {
196          configStateMachine.nextState(action);
197       }
198       catch(IllegalTransitionException e)
199       {
200          log.debug("validateState failure", e);
201          throw new PolicyContextException JavaDoc("Operation not allowed", e);
202       }
203    }
204 }
205
Popular Tags