001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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: * --------------------------------------------------------------------------
022: * $Id: AbsPermissionManager.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.security.permissions;
025:
026: import java.net.URL;
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.ow2.easybeans.api.PermissionManagerException;
034:
035: /**
036: * Manages the permission for EasyBeans EJB3 container.
037: * @author Florent Benoit
038: */
039: public abstract class AbsPermissionManager {
040:
041: /**
042: * JACC Policy configuration.
043: */
044: private PolicyConfiguration policyConfiguration = null;
045:
046: /**
047: * Context ID (URL).
048: */
049: private URL contextIdURL = null;
050:
051: /**
052: * Context ID.
053: */
054: private String contextId = null;
055:
056: /**
057: * Policy to use.
058: */
059: private static Policy policy = null;
060:
061: /**
062: * Default Constructor.
063: * @param contextIdURL context ID URL used for PolicyContext
064: * @throws PermissionManagerException if permissions can't be set
065: */
066: public AbsPermissionManager(final URL contextIdURL)
067: throws PermissionManagerException {
068: this (contextIdURL, true);
069: }
070:
071: /**
072: * Default Constructor.
073: * @param contextIdURL context ID URL used for PolicyContext
074: * @param remove - if true, the policy configuration will be removed.
075: * @throws PermissionManagerException if permissions can't be set
076: */
077: public AbsPermissionManager(final URL contextIdURL,
078: final boolean remove) throws PermissionManagerException {
079: this .contextIdURL = contextIdURL;
080: this .contextId = contextIdURL.toString();
081:
082: PolicyConfigurationFactory policyConfigurationFactory = null;
083: // Init JACC
084: try {
085: policyConfigurationFactory = PolicyConfigurationFactory
086: .getPolicyConfigurationFactory();
087: } catch (ClassNotFoundException e) {
088: throw new PermissionManagerException(
089: "Error when trying to get the PolicyConfigurationFactory object",
090: e);
091: } catch (PolicyContextException e) {
092: throw new PermissionManagerException(
093: "Error when trying to get the PolicyConfigurationFactory object",
094: e);
095: }
096: try {
097: this .policyConfiguration = policyConfigurationFactory
098: .getPolicyConfiguration(contextId, remove);
099: } catch (PolicyContextException pce) {
100: throw new PermissionManagerException(
101: "Error when trying to get the PolicyConfiguration object with contextId '"
102: + contextId + "'.'", pce);
103: }
104:
105: // Policy to use
106: policy = Policy.getPolicy();
107: }
108:
109: /**
110: * Delete this object.
111: * @throws PermissionManagerException if the configuration can't be deleted
112: */
113: public void delete() throws PermissionManagerException {
114:
115: try {
116: policyConfiguration.delete();
117: } catch (PolicyContextException pce) {
118: throw new PermissionManagerException(
119: "Cannot delete policyConfiguration object", pce);
120: }
121: policyConfiguration = null;
122:
123: // Policy need to be refresh
124: policy.refresh();
125: }
126:
127: /**
128: * Commit the Policy Configuration.
129: * @throws PermissionManagerException if commit can't be done
130: */
131: public void commit() throws PermissionManagerException {
132: try {
133: policyConfiguration.commit();
134: policy.refresh();
135: } catch (PolicyContextException pce) {
136: throw new PermissionManagerException(
137: "Cannot commit configuration", pce);
138: }
139: }
140:
141: /**
142: * @return Returns the policy.
143: */
144: protected static Policy getPolicy() {
145: return policy;
146: }
147:
148: /**
149: * @return Returns the contextId.
150: */
151: protected String getContextId() {
152: return contextId;
153: }
154:
155: /**
156: * @param contextId The contextId to set.
157: */
158: protected void setContextId(final String contextId) {
159: this .contextId = contextId;
160: }
161:
162: /**
163: * @return Returns the policyConfiguration.
164: */
165: protected PolicyConfiguration getPolicyConfiguration() {
166: return policyConfiguration;
167: }
168:
169: /**
170: * @param policyConfiguration The policyConfiguration to set.
171: */
172: protected void setPolicyConfiguration(
173: final PolicyConfiguration policyConfiguration) {
174: this .policyConfiguration = policyConfiguration;
175: }
176:
177: /**
178: * @return Returns the contextId URL.
179: */
180: protected URL getContextIdURL() {
181: return contextIdURL;
182: }
183: }
|