001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: AbsPermissionManager.java 8321 2006-05-09 14:18:10Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.security;
026:
027: import java.security.Policy;
028:
029: import javax.security.jacc.PolicyConfiguration;
030: import javax.security.jacc.PolicyConfigurationFactory;
031: import javax.security.jacc.PolicyContextException;
032:
033: import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping;
034:
035: /**
036: * Defines an abstract PermissionManager class which will manage JACC
037: * permissions for an ejbjar, webapp, etc.
038: * @author Florent Benoit
039: */
040: public abstract class AbsPermissionManager {
041:
042: /**
043: * JACC Policy configuration
044: */
045: private PolicyConfiguration policyConfiguration = null;
046:
047: /**
048: * Context ID
049: */
050: private String contextId = null;
051:
052: /**
053: * Policy to use
054: */
055: private static Policy policy = null;
056:
057: /**
058: * Default Constructor
059: * @param contextId context ID used for PolicyContext
060: * @throws PermissionManagerException if permissions can't be set
061: */
062: public AbsPermissionManager(String contextId)
063: throws PermissionManagerException {
064: this (contextId, true);
065: }
066:
067: /**
068: * Default Constructor
069: * @param contextId context ID used for PolicyContext
070: * @param remove - if true, the policy configuration will be removed.
071: * @throws PermissionManagerException if permissions can't be set
072: */
073: public AbsPermissionManager(String contextId, boolean remove)
074: throws PermissionManagerException {
075: this .contextId = contextId;
076:
077: PolicyConfigurationFactory policyConfigurationFactory = null;
078: // Init JACC
079: try {
080: policyConfigurationFactory = PolicyConfigurationFactory
081: .getPolicyConfigurationFactory();
082: } catch (Exception e) {
083: throw new PermissionManagerException(
084: "Error when trying to get the PolicyConfigurationFactory object : '"
085: + e.getMessage() + "'.");
086: }
087: try {
088: this .policyConfiguration = policyConfigurationFactory
089: .getPolicyConfiguration(contextId, remove);
090: } catch (PolicyContextException pce) {
091: throw new PermissionManagerException(
092: "Error when trying to get the PolicyConfiguration object with contextId '"
093: + contextId + "' : " + pce.getMessage());
094: }
095:
096: // Policy to use
097: policy = Policy.getPolicy();
098: }
099:
100: /**
101: * Delete this object
102: * @throws PermissionManagerException if the configuration can't be deleted
103: */
104: public void delete() throws PermissionManagerException {
105: resetDeploymentDesc();
106:
107: try {
108: policyConfiguration.delete();
109: } catch (PolicyContextException pce) {
110: throw new PermissionManagerException(
111: "Can't delete policyConfiguration object", pce);
112: }
113: policyConfiguration = null;
114:
115: // Also delete user-to-role mapping
116: JPolicyUserRoleMapping.removeUserToRoleMapping(contextId);
117:
118: // Policy need to be refresh
119: policy.refresh();
120: }
121:
122: /**
123: * Commit the Policy Configuration
124: * @throws PermissionManagerException if commit can't be done
125: */
126: public void commit() throws PermissionManagerException {
127: try {
128: policyConfiguration.commit();
129: policy.refresh();
130: } catch (PolicyContextException pce) {
131: throw new PermissionManagerException(
132: "Can't commit configuration", pce);
133: }
134: }
135:
136: /**
137: * Reset Deployment Descriptor
138: */
139: protected abstract void resetDeploymentDesc();
140:
141: /**
142: * @return Returns the policy.
143: */
144: protected static Policy getPolicy() {
145: return policy;
146: }
147:
148: /**
149: * @param policy The policy to set.
150: */
151: protected static void setPolicy(Policy policy) {
152: AbsPermissionManager.policy = policy;
153: }
154:
155: /**
156: * @return Returns the contextId.
157: */
158: protected String getContextId() {
159: return contextId;
160: }
161:
162: /**
163: * @param contextId The contextId to set.
164: */
165: protected void setContextId(String contextId) {
166: this .contextId = contextId;
167: }
168:
169: /**
170: * @return Returns the policyConfiguration.
171: */
172: protected PolicyConfiguration getPolicyConfiguration() {
173: return policyConfiguration;
174: }
175:
176: /**
177: * @param policyConfiguration The policyConfiguration to set.
178: */
179: protected void setPolicyConfiguration(
180: PolicyConfiguration policyConfiguration) {
181: this.policyConfiguration = policyConfiguration;
182: }
183:
184: }
|