KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > security > AbsPermissionManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@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  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: AbsPermissionManager.java,v 1.4 2004/07/13 15:20:40 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.security;
28
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.jonas.security.jacc.JPolicyUserRoleMapping;
36
37 /**
38  * Defines an abstract PermissionManager class which will manage JACC
39  * permissions for an ejbjar, webapp, etc.
40  * @author Florent Benoit
41  */

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

47     private PolicyConfiguration JavaDoc policyConfiguration = null;
48
49     /**
50      * Context ID
51      */

52     private String JavaDoc contextId = null;
53
54     /**
55      * Policy to use
56      */

57     private static Policy JavaDoc policy = null;
58
59     /**
60      * Default Constructor
61      * @param contextId context ID used for PolicyContext
62      * @throws PermissionManagerException if permissions can't be set
63      */

64     public AbsPermissionManager(String JavaDoc contextId) throws PermissionManagerException {
65         this.contextId = contextId;
66
67         PolicyConfigurationFactory JavaDoc policyConfigurationFactory = null;
68         // Init JACC
69
try {
70             policyConfigurationFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
71         } catch (Exception JavaDoc e) {
72             throw new PermissionManagerException("Error when trying to get the PolicyConfigurationFactory object : '"
73                     + e.getMessage() + "'.");
74         }
75         try {
76             this.policyConfiguration = policyConfigurationFactory.getPolicyConfiguration(contextId, true);
77         } catch (PolicyContextException JavaDoc pce) {
78             throw new PermissionManagerException(
79                     "Error when trying to get the PolicyConfiguration object with contextId '" + contextId + "' : "
80                             + pce.getMessage());
81         }
82
83         // Policy to use
84
policy = Policy.getPolicy();
85     }
86
87     /**
88      * Delete this object
89      * @throws PermissionManagerException if the configuration can't be deleted
90      */

91     public void delete() throws PermissionManagerException {
92         resetDeploymentDesc();
93
94         try {
95             policyConfiguration.delete();
96         } catch (PolicyContextException JavaDoc pce) {
97             throw new PermissionManagerException("Can't delete policyConfiguration object", pce);
98         }
99         policyConfiguration = null;
100
101         // Also delete user-to-role mapping
102
JPolicyUserRoleMapping.removeUserToRoleMapping(contextId);
103
104         // Policy need to be refresh
105
policy.refresh();
106     }
107
108     /**
109      * Commit the Policy Configuration
110      * @throws PermissionManagerException if commit can't be done
111      */

112     public void commit() throws PermissionManagerException {
113         try {
114             policyConfiguration.commit();
115             policy.refresh();
116         } catch (PolicyContextException JavaDoc pce) {
117             throw new PermissionManagerException("Can't commit configuration", pce);
118         }
119     }
120
121     /**
122      * Reset Deployment Descriptor
123      */

124     protected abstract void resetDeploymentDesc();
125
126     /**
127      * @return Returns the policy.
128      */

129     protected static Policy JavaDoc getPolicy() {
130         return policy;
131     }
132
133     /**
134      * @param policy The policy to set.
135      */

136     protected static void setPolicy(Policy JavaDoc policy) {
137         AbsPermissionManager.policy = policy;
138     }
139
140     /**
141      * @return Returns the contextId.
142      */

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

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

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

164     protected void setPolicyConfiguration(PolicyConfiguration JavaDoc policyConfiguration) {
165         this.policyConfiguration = policyConfiguration;
166     }
167
168 }
Popular Tags