KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > ext > authorization > AuthorizationHelper


1 /*
2 jGuard is a security framework based on top of jaas (java authentication and authorization security).
3 it is written for web applications, to resolve simply, access control problems.
4 version $Name: $
5 http://sourceforge.net/projects/jguard/
6
7 Copyright (C) 2004 Charles GAY
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23
24 jGuard project home page:
25 http://sourceforge.net/projects/jguard/
26
27 */

28 package net.sf.jguard.ext.authorization;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36
37 import net.sf.ehcache.CacheException;
38 import net.sf.jguard.core.CoreConstants;
39 import net.sf.jguard.core.authorization.permissions.PermissionUtils;
40 import net.sf.jguard.core.authorization.policy.MultipleAppPolicy;
41 import net.sf.jguard.ext.SecurityConstants;
42 import net.sf.jguard.ext.authorization.manager.AuthorizationManager;
43 import net.sf.jguard.ext.util.XMLUtils;
44
45 import org.dom4j.Document;
46 import org.dom4j.Element;
47
48 /**
49  * utility class used to load Authorization configuration
50  * and to create and register an {@link AuthorizationManager}.
51  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
52  */

53 public class AuthorizationHelper {
54
55     private static final Logger JavaDoc logger = Logger
56     .getLogger(AuthorizationHelper.class.getName());
57
58
59     /**
60      * create an {@link AuthorizationManager} and register it into the {@link MultipleAppPolicy}.
61      * @param authorizationOptions
62      * @param applicationName
63      * @return AuthorizationManager created and registered
64      */

65     public static AuthorizationManager initAuthorization(Map JavaDoc authorizationOptions,String JavaDoc applicationName){
66         
67
68         //permission caching section
69
if ( !("true".equals(authorizationOptions.get(SecurityConstants.AUTHORIZATION_PERMISSION_RESOLUTION_CACHING)))){
70             PermissionUtils.setCachesEnabled(false);
71         }else{
72             // by default, permission resolution caching is activated
73
try {
74                 PermissionUtils.createCaches();
75                 PermissionUtils.setCachesEnabled(true);
76             } catch (CacheException e) {
77                 logger.warning("Failed to activate permission resolution caching : " + e.getMessage());
78                 PermissionUtils.setCachesEnabled(false);
79             }
80         }
81
82         //authorizationManager creation
83
AuthorizationManager am = null;
84         try {
85             // Create new authorization manager based on settings
86
authorizationOptions.put(CoreConstants.APPLICATION_NAME,applicationName);
87             if (logger.isLoggable(Level.FINEST)) {
88                 logger.finest(" authorizationManagerOptions="+ authorizationOptions);
89             }
90             AuthorizationManagerFactory.createAuthorizationManager(authorizationOptions);
91             am = AuthorizationManagerFactory.getAuthorizationManager();
92
93         } catch (AuthorizationException e) {
94             logger.severe("AuthorizationException : "+e.getMessage());
95             throw new IllegalArgumentException JavaDoc(e.getMessage());
96         }
97         
98         
99         
100
101         return am;
102     }
103
104     /**
105      * loads the Authorization configuration file and returns settings.
106      * @param configurationLocation
107      * @param appHomePath
108      * @return map containing authorization settings
109      */

110     public static Map JavaDoc loadConfiguration(String JavaDoc configurationLocation,String JavaDoc appHomePath){
111         Map JavaDoc authorizationMap = new HashMap JavaDoc();
112         Document doc = XMLUtils.read(configurationLocation);
113
114         Element authorization = doc.getRootElement().element(SecurityConstants.AUTHORIZATION);
115         Element scope = authorization.element(SecurityConstants.SCOPE);
116         if (scope != null){
117             authorizationMap.put(SecurityConstants.SCOPE, scope.getTextTrim());
118         }
119         
120         Element permissionResolutionCaching = authorization.element(SecurityConstants.AUTHORIZATION_PERMISSION_RESOLUTION_CACHING);
121         if (permissionResolutionCaching != null){
122             authorizationMap.put(SecurityConstants.AUTHORIZATION_PERMISSION_RESOLUTION_CACHING, permissionResolutionCaching.getTextTrim());
123         }
124         authorizationMap.put(SecurityConstants.AUTHORIZATION_MANAGER,authorization.element(SecurityConstants.AUTHORIZATION_MANAGER).getTextTrim());
125
126         List JavaDoc authorizationList = authorization.element(SecurityConstants.AUTHORIZATION_MANAGER_OPTIONS).elements(SecurityConstants.OPTION);
127         Iterator JavaDoc itAuthorizationOptions = authorizationList.iterator();
128         while(itAuthorizationOptions.hasNext()){
129             Element option = (Element)itAuthorizationOptions.next();
130             String JavaDoc name = option.element(CoreConstants.NAME).getTextTrim();
131             String JavaDoc value = option.element(SecurityConstants.VALUE_MARKUP).getTextTrim();
132              if(SecurityConstants.AUTHORIZATION_XML_FILE_LOCATION.equals(name)||SecurityConstants.AUTHORIZATION_DATABASE_FILE_LOCATION.equals(name)){
133                     value=appHomePath+value;
134                 }
135             authorizationMap.put(name,value);
136         }
137         return authorizationMap;
138     }
139
140 }
141
Popular Tags