KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > permissions > AbsPermissionManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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  * --------------------------------------------------------------------------
22  * $Id: AbsPermissionManager.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.security.permissions;
27
28 import java.net.URL JavaDoc;
29 import java.security.Policy JavaDoc;
30
31 import javax.security.jacc.PolicyConfiguration JavaDoc;
32 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
33 import javax.security.jacc.PolicyContextException JavaDoc;
34
35 import org.objectweb.easybeans.api.PermissionManagerException;
36
37 /**
38  * Manages the permission for EasyBeans EJB3 container.
39  * @author Florent Benoit
40  */

41 public abstract class AbsPermissionManager {
42
43     /**
44      * JACC Policy configuration.
45      */

46     private PolicyConfiguration JavaDoc policyConfiguration = null;
47
48     /**
49      * Context ID (URL).
50      */

51     private URL JavaDoc contextIdURL = null;
52
53     /**
54      * Context ID.
55      */

56     private String JavaDoc contextId = null;
57
58     /**
59      * Policy to use.
60      */

61     private static Policy JavaDoc policy = null;
62
63     /**
64      * Default Constructor.
65      * @param contextIdURL context ID URL used for PolicyContext
66      * @throws PermissionManagerException if permissions can't be set
67      */

68     public AbsPermissionManager(final URL JavaDoc contextIdURL) throws PermissionManagerException {
69         this(contextIdURL, true);
70     }
71
72     /**
73      * Default Constructor.
74      * @param contextIdURL context ID URL used for PolicyContext
75      * @param remove - if true, the policy configuration will be removed.
76      * @throws PermissionManagerException if permissions can't be set
77      */

78     public AbsPermissionManager(final URL JavaDoc contextIdURL, final boolean remove) throws PermissionManagerException {
79         this.contextIdURL = contextIdURL;
80         this.contextId = contextIdURL.toString();
81
82         PolicyConfigurationFactory JavaDoc policyConfigurationFactory = null;
83         // Init JACC
84
try {
85             policyConfigurationFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
86         } catch (ClassNotFoundException JavaDoc e) {
87             throw new PermissionManagerException("Error when trying to get the PolicyConfigurationFactory object", e);
88         } catch (PolicyContextException JavaDoc e) {
89             throw new PermissionManagerException("Error when trying to get the PolicyConfigurationFactory object", e);
90         }
91         try {
92             this.policyConfiguration = policyConfigurationFactory.getPolicyConfiguration(contextId, remove);
93         } catch (PolicyContextException JavaDoc pce) {
94             throw new PermissionManagerException("Error when trying to get the PolicyConfiguration object with contextId '"
95                     + contextId + "'.'", pce);
96         }
97
98         // Policy to use
99
policy = Policy.getPolicy();
100     }
101
102     /**
103      * Delete this object.
104      * @throws PermissionManagerException if the configuration can't be deleted
105      */

106     public void delete() throws PermissionManagerException {
107
108         try {
109             policyConfiguration.delete();
110         } catch (PolicyContextException JavaDoc pce) {
111             throw new PermissionManagerException("Cannot delete policyConfiguration object", pce);
112         }
113         policyConfiguration = null;
114
115         // Policy need to be refresh
116
policy.refresh();
117     }
118
119     /**
120      * Commit the Policy Configuration.
121      * @throws PermissionManagerException if commit can't be done
122      */

123     public void commit() throws PermissionManagerException {
124         try {
125             policyConfiguration.commit();
126             policy.refresh();
127         } catch (PolicyContextException JavaDoc pce) {
128             throw new PermissionManagerException("Cannot commit configuration", pce);
129         }
130     }
131
132     /**
133      * @return Returns the policy.
134      */

135     protected static Policy JavaDoc getPolicy() {
136         return policy;
137     }
138
139     /**
140      * @return Returns the contextId.
141      */

142     protected String JavaDoc getContextId() {
143         return contextId;
144     }
145
146     /**
147      * @param contextId The contextId to set.
148      */

149     protected void setContextId(final String JavaDoc contextId) {
150         this.contextId = contextId;
151     }
152
153     /**
154      * @return Returns the policyConfiguration.
155      */

156     protected PolicyConfiguration JavaDoc getPolicyConfiguration() {
157         return policyConfiguration;
158     }
159
160     /**
161      * @param policyConfiguration The policyConfiguration to set.
162      */

163     protected void setPolicyConfiguration(final PolicyConfiguration JavaDoc policyConfiguration) {
164         this.policyConfiguration = policyConfiguration;
165     }
166
167     /**
168      * @return Returns the contextId URL.
169      */

170     protected URL JavaDoc getContextIdURL() {
171         return contextIdURL;
172     }
173 }
174
Popular Tags