KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > security > authorization > PermissionFactory


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Shirley Sasson.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.kernel.security.authorization;
48
49 import org.mr.kernel.security.SecurityConstants;
50 import org.mr.kernel.security.MantaSecurityException;
51 import org.mr.kernel.security.SecurityActionTypes;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54 import org.mr.kernel.security.authorization.permissions.MantaPermissionWithParameter;
55 import org.mr.kernel.security.authorization.permissions.MantaPermission;
56
57 import java.util.*;
58 import java.lang.reflect.Constructor JavaDoc;
59
60 /**
61  * This class is a factory class for permissions. It creates an instance of
62  * MantaPermission object according to the permission type string supplied.
63  *
64  * This class is a singltone.
65  *
66  * @version 1.0
67  * @since May 9, 2006
68  * @author Shirley Sasson
69  *
70  */

71 public class PermissionFactory implements SecurityActionTypes, SecurityConstants {
72     private static PermissionFactory _instance;
73     private static Map _names = new HashMap();
74     private Log _logger;
75
76     /**
77       * Returns the single instance of PermissionFactory.
78       *
79       * @return the single instance of PermissionFactory
80       */

81     public static PermissionFactory getInstance() {
82         if (_instance == null){
83             synchronized(PermissionFactory.class){
84                 if (_instance == null)
85                     _instance = new PermissionFactory();
86             }
87         }
88         return _instance;
89     }
90
91     private PermissionFactory(){
92         // actions
93
_names.put(PERMISSION_CREATE_BROWSER_FOR_QUEUE, PERMISSION_WITHOUT_PARAMETER);
94         _names.put(PERMISSION_CREATE_BROWSER_FOR_SPECIFIC_QUEUE, SPECIFIC_QUEUE_PERMISSION);
95         _names.put(PERMISSION_CREATE_CONSUMER_FOR_QUEUE, PERMISSION_WITHOUT_PARAMETER);
96         _names.put(PERMISSION_CREATE_CONSUMER_FOR_SPECIFIC_QUEUE, SPECIFIC_QUEUE_PERMISSION);
97         _names.put(PERMISSION_CREATE_CONSUMER_FOR_TOPIC, PERMISSION_WITHOUT_PARAMETER);
98         _names.put(PERMISSION_CREATE_CONSUMER_FOR_SPECIFIC_TOPIC, SPECIFIC_TOPIC_PERMISSION);
99         _names.put(PERMISSION_CREATE_PRODUCER_FOR_QUEUE, PERMISSION_WITHOUT_PARAMETER);
100         _names.put(PERMISSION_CREATE_PRODUCER_FOR_SPECIFIC_QUEUE, SPECIFIC_QUEUE_PERMISSION);
101         _names.put(PERMISSION_CREATE_PRODUCER_FOR_TOPIC, PERMISSION_WITHOUT_PARAMETER);
102         _names.put(PERMISSION_CREATE_PRODUCER_FOR_SPECIFIC_TOPIC, SPECIFIC_TOPIC_PERMISSION);
103         _names.put(PERMISSION_SUBSCRIBE_DURABLE_ON_TOPIC, PERMISSION_WITHOUT_PARAMETER);
104         _names.put(PERMISSION_SUBSCRIBE_DURABLE_ON_SPECIFIC_TOPIC, SPECIFIC_TOPIC_PERMISSION);
105
106         // management
107
_names.put(PERMISSION_CREATE_GROUP, PERMISSION_WITHOUT_PARAMETER);
108         _names.put(PERMISSION_MODIFY_GROUP, PERMISSION_WITHOUT_PARAMETER);
109         _names.put(PERMISSION_MODIFY_SPECIFIC_GROUP, SPECIFIC_GROUP_PERMISSION);
110         _names.put(PERMISSION_DELETE_GROUP, PERMISSION_WITHOUT_PARAMETER);
111         _names.put(PERMISSION_DELETE_SPECIFIC_GROUP, SPECIFIC_GROUP_PERMISSION);
112         _names.put(PERMISSION_SET_PERMISSIONS_FOR_GROUP, PERMISSION_WITHOUT_PARAMETER);
113         _names.put(PERMISSION_SET_PERMISSIONS_FOR_SPECIFIC_GROUP, SPECIFIC_GROUP_PERMISSION);
114         _names.put(PERMISSION_READ_PERMISSIONS_FOR_GROUP, PERMISSION_WITHOUT_PARAMETER);
115         _names.put(PERMISSION_READ_PERMISSIONS_FOR_SPECIFIC_GROUP, SPECIFIC_GROUP_PERMISSION);
116         _names.put(PERMISSION_CREATE_USER, PERMISSION_WITHOUT_PARAMETER);
117         _names.put(PERMISSION_CREATE_USER_IN_SPECIFIC_GROUP, SPECIFIC_GROUP_PERMISSION);
118         _names.put(PERMISSION_MODIFY_USER, PERMISSION_WITHOUT_PARAMETER);
119         _names.put(PERMISSION_MODIFY_SPECIFIC_USER, SPECIFIC_USER_PERMISSION);
120         _names.put(PERMISSION_DELETE_USER, PERMISSION_WITHOUT_PARAMETER);
121         _names.put(PERMISSION_DELETE_SPECIFIC_USER, SPECIFIC_USER_PERMISSION);
122         _names.put(PERMISSION_SET_PERMISSIONS_FOR_USER, PERMISSION_WITHOUT_PARAMETER);
123         _names.put(PERMISSION_SET_PERMISSIONS_FOR_SPECIFIC_USER, SPECIFIC_USER_PERMISSION);
124         _names.put(PERMISSION_READ_PERMISSIONS_FOR_USER, PERMISSION_WITHOUT_PARAMETER);
125         _names.put(PERMISSION_READ_PERMISSIONS_FOR_SPECIFIC_USER, SPECIFIC_USER_PERMISSION);
126         _names.put(PERMISSION_CREATE_WHITE_LIST_ENTRY, PERMISSION_WITHOUT_PARAMETER);
127         _names.put(PERMISSION_DELETE_WHITE_LIST_ENTRY, PERMISSION_WITHOUT_PARAMETER);
128         _names.put(PERMISSION_READ_ANY, PERMISSION_WITHOUT_PARAMETER);
129         _names.put(PERMISSION_ALL, PERMISSION_WITHOUT_PARAMETER);
130     }
131
132     /**
133      * Returns a list of all permissions types available within this factory.
134      *
135      * @return a list of all permissions types available within this factory. Objects in the list are String.
136      *
137      */

138     public List getAvailablePermissions(){
139         List availablePermissions = new ArrayList();
140         Set names = _names.keySet();
141         Iterator iter = names.iterator();
142         while (iter.hasNext()){
143             availablePermissions.add(iter.next());
144         }
145         return availablePermissions;
146     }
147
148     /**
149      * Retunrs an instance of MantaPermission according to the given permission name.
150      *
151      * @param permissionName the name of the permission to instantiate
152      * @return an instance of MantaPermission
153      * @throws MantaSecurityException if an error occured
154      *
155      */

156     public MantaPermission getPermission(String JavaDoc permissionName) throws MantaSecurityException {
157         return getPermission(permissionName, null);
158     }
159
160     /**
161      * Returns an instance of MantaPermission according to the given permission name.
162      * If the MantaPermission instantiated is instance of MantaPermissionWithParameter, it also
163      * sets the given Object param to the permission instance.
164      *
165      * @param permissionName the name of the permission to instantiate
166      * @param param the parameter to set in the permission of it is of type MantaPermissionWithParameter
167      * @return an instance of MantaPermission
168      * @throws MantaSecurityException if an error occured
169      *
170      */

171     public MantaPermission getPermission(String JavaDoc permissionName, Object JavaDoc param) throws MantaSecurityException {
172         MantaPermission p;
173
174         if (!_names.containsKey(permissionName)){
175             if (getLogger().isErrorEnabled())
176                 getLogger().error("[getPermission] No MantaPermission class found with name " + permissionName);
177             throw new MantaSecurityException("No MantaPermission class found with name " + permissionName);
178         }
179
180         try {
181             String JavaDoc permissionsClassName = (String JavaDoc) _names.get(permissionName);
182             Class JavaDoc[] parameters = {String JavaDoc.class};
183             Constructor JavaDoc constructor = Class.forName(permissionsClassName).getConstructor(parameters);
184             String JavaDoc[] args = {permissionName};
185             p = (MantaPermission) constructor.newInstance(args);
186             if (p instanceof MantaPermissionWithParameter){
187                 MantaPermissionWithParameter permissionsWithParam = (MantaPermissionWithParameter) p;
188                 permissionsWithParam.setParameter(param);
189                 p = permissionsWithParam;
190             }
191             if (getLogger().isDebugEnabled())
192                 getLogger().debug("[getPermission] Class " + permissionsClassName + " was instantiated");
193
194         }
195         catch (Exception JavaDoc e){
196             if (getLogger().isErrorEnabled())
197                 getLogger().error("[getPermission] Error instantiating MantaPermission class. " + e.getMessage());
198             throw new MantaSecurityException("Error instantiating MantaPermission class. " + e.getMessage());
199         }
200         return p;
201     }
202
203     /**
204      * Returns the instance of the logger for this class
205      *
206      * @return the instance of the logger
207      */

208     public Log getLogger(){
209         if (_logger == null){
210             _logger = LogFactory.getLog(getClass().getName());
211         }
212         return _logger;
213     }
214 }
215
Popular Tags