001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.security.jacc;
023:
024: import java.security.Permission;
025: import java.security.PermissionCollection;
026: import javax.security.jacc.PolicyConfiguration;
027: import javax.security.jacc.PolicyContextException;
028:
029: import org.jboss.util.state.StateMachine;
030: import org.jboss.util.state.IllegalTransitionException;
031: import org.jboss.util.state.State;
032: import org.jboss.logging.Logger;
033:
034: /** The JACC PolicyConfiguration implementation. This class associates a
035: * context id with the permission ops it passes along to the global
036: * DelegatingPolicy instance.
037: *
038: * @author Scott.Stark@jboss.org
039: * @version $Revision: 57203 $
040: */
041: public class JBossPolicyConfiguration implements PolicyConfiguration {
042: private static Logger log = Logger
043: .getLogger(JBossPolicyConfiguration.class);
044: /** The JACC context id associated with the policy */
045: private String contextID;
046: /** The Policy impl which handles the JACC permissions */
047: private DelegatingPolicy policy;
048: /** A state machine whihc enforces the state behavior of this config */
049: private StateMachine configStateMachine;
050: /** A trace level logging flag set when the policy is created */
051: private boolean trace;
052:
053: protected JBossPolicyConfiguration(String contextID,
054: DelegatingPolicy policy, StateMachine configStateMachine)
055: throws PolicyContextException {
056: this .contextID = contextID;
057: this .policy = policy;
058: this .configStateMachine = configStateMachine;
059:
060: if (contextID == null)
061: throw new IllegalArgumentException(
062: "contextID cannot be null");
063: if (policy == null)
064: throw new IllegalArgumentException("policy cannot be null");
065: if (configStateMachine == null)
066: throw new IllegalArgumentException(
067: "configStateMachine cannot be null");
068:
069: validateState("getPolicyConfiguration");
070: trace = log.isTraceEnabled();
071: if (trace)
072: log.trace("ctor, contextID=" + contextID);
073: }
074:
075: void initPolicyConfiguration(boolean remove)
076: throws PolicyContextException {
077: validateState("getPolicyConfiguration");
078: policy.initPolicyConfiguration(contextID, remove);
079: }
080:
081: public void addToExcludedPolicy(Permission permission)
082: throws PolicyContextException {
083: if (trace)
084: log.trace("addToExcludedPolicy, p=" + permission);
085: validateState("addToExcludedPolicy");
086: policy.addToExcludedPolicy(contextID, permission);
087: }
088:
089: public void addToExcludedPolicy(PermissionCollection permissions)
090: throws PolicyContextException {
091: if (trace)
092: log.trace("addToExcludedPolicy, pc=" + permissions);
093: validateState("addToExcludedPolicy");
094: policy.addToExcludedPolicy(contextID, permissions);
095: }
096:
097: public void addToRole(String roleName, Permission permission)
098: throws PolicyContextException {
099: if (trace)
100: log.trace("addToRole, roleName=" + roleName + ", p="
101: + permission);
102: validateState("addToRole");
103: policy.addToRole(contextID, roleName, permission);
104: }
105:
106: public void addToRole(String roleName,
107: PermissionCollection permissions)
108: throws PolicyContextException {
109: if (trace)
110: log.trace("addToRole, roleName=" + roleName + ", pc="
111: + permissions);
112: validateState("addToRole");
113: policy.addToRole(contextID, roleName, permissions);
114: }
115:
116: public void addToUncheckedPolicy(Permission permission)
117: throws PolicyContextException {
118: if (trace)
119: log.trace("addToUncheckedPolicy, p=" + permission);
120: validateState("addToUncheckedPolicy");
121: policy.addToUncheckedPolicy(contextID, permission);
122: }
123:
124: public void addToUncheckedPolicy(PermissionCollection permissions)
125: throws PolicyContextException {
126: if (trace)
127: log.trace("addToUncheckedPolicy, pc=" + permissions);
128: validateState("addToUncheckedPolicy");
129: policy.addToUncheckedPolicy(contextID, permissions);
130: }
131:
132: public void commit() throws PolicyContextException {
133: if (trace)
134: log.trace("commit");
135: validateState("commit");
136: policy.commit(contextID);
137: }
138:
139: public void delete() throws PolicyContextException {
140: if (trace)
141: log.trace("delete");
142: validateState("delete");
143: policy.delete(contextID);
144: }
145:
146: public String getContextID() throws PolicyContextException {
147: validateState("getContextID");
148: return contextID;
149: }
150:
151: public boolean inService() throws PolicyContextException {
152: validateState("inService");
153: State state = configStateMachine.getCurrentState();
154: boolean inService = state.getName().equals("inService");
155: return inService;
156: }
157:
158: public void linkConfiguration(PolicyConfiguration link)
159: throws PolicyContextException {
160: if (trace)
161: log.trace("linkConfiguration, linkTo: "
162: + link.getContextID());
163: validateState("linkConfiguration");
164: policy.linkConfiguration(contextID, link);
165: }
166:
167: public void removeExcludedPolicy() throws PolicyContextException {
168: if (trace)
169: log.trace("removeExcludedPolicy");
170: validateState("removeExcludedPolicy");
171: policy.removeExcludedPolicy(contextID);
172: }
173:
174: public void removeRole(String roleName)
175: throws PolicyContextException {
176: if (trace)
177: log.trace("removeRole: " + roleName);
178: validateState("removeRole");
179: policy.removeRole(contextID, roleName);
180: }
181:
182: public void removeUncheckedPolicy() throws PolicyContextException {
183: if (trace)
184: log.trace("removeUncheckedPolicy");
185: validateState("removeUncheckedPolicy");
186: policy.removeUncheckedPolicy(contextID);
187: }
188:
189: protected void validateState(String action)
190: throws PolicyContextException {
191: try {
192: configStateMachine.nextState(action);
193: } catch (IllegalTransitionException e) {
194: log.debug("validateState failure", e);
195: throw new PolicyContextException("Operation not allowed", e);
196: }
197: }
198: }
|