KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jguard > core > authentication > configuration > ConfigurationHelper


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.core.authentication.configuration;
29
30 import java.util.ArrayList 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 javax.security.auth.login.AppConfigurationEntry JavaDoc;
38 import javax.security.auth.login.Configuration JavaDoc;
39
40 import net.sf.jguard.core.CoreConstants;
41
42
43 /**
44  * utility class to deal with the {@link javax.security.auth.login.Configuration} class.
45  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
46  */

47 public class ConfigurationHelper {
48      
49      private static String JavaDoc scope;
50      
51      
52      private static boolean configurationInstalled = false;
53      private static Logger JavaDoc logger = Logger.getLogger(ConfigurationHelper.class.getName());
54      private final static String JavaDoc COM_SUN_SECURITY_AUTH_LOGIN_CONFIG_FILE = "com.sun.security.auth.login.ConfigFile";
55
56     /**
57      * add the required AppConfigurationEntry when no none is configured for the webapp.
58      * @param applicationName
59      * @param authenticationSettings
60      * @param debug
61      */

62     public static void addConfigurationEntryForWebapp(JGuardConfiguration configuration,String JavaDoc applicationName,Map JavaDoc authenticationSettings,boolean debug){
63         List JavaDoc webappEntries = buildAppConfigurationEntries(applicationName,authenticationSettings,debug);
64         configuration.addConfigEntriesForApplication(applicationName,webappEntries);
65     }
66
67
68     /**
69      * install JGuardConfiguration.
70      * @param includeOldConfiguration include informations contained in the replaced configuration instance
71      * @param includeConfigFromJavaParam
72      * @throws RuntimeException
73      */

74     public static void installConfiguration(boolean includeOldConfiguration,boolean includeConfigFromJavaParam) throws RuntimeException JavaDoc {
75         if(!configurationInstalled){
76             includeConfigFromJavaParam = false;
77         }
78             JGuardConfiguration jGuardConf = installWithOldConfig(includeOldConfiguration);
79         
80         if(includeConfigFromJavaParam){
81             //TODO build a Configuration object to include from the configuration file
82
try {
83                 Class JavaDoc defaultConfigClass = Class.forName(ConfigurationHelper.COM_SUN_SECURITY_AUTH_LOGIN_CONFIG_FILE);
84                 Configuration JavaDoc defaultConfiguration = (Configuration JavaDoc) defaultConfigClass.newInstance();
85                 jGuardConf.includeConfiguration(defaultConfiguration);
86             } catch (ClassNotFoundException JavaDoc e) {
87                 logger.log(Level.SEVERE,ConfigurationHelper.COM_SUN_SECURITY_AUTH_LOGIN_CONFIG_FILE+" class cannot be found "+e.getMessage());
88             } catch (InstantiationException JavaDoc e) {
89                 logger.log(Level.SEVERE,ConfigurationHelper.COM_SUN_SECURITY_AUTH_LOGIN_CONFIG_FILE+" class cannot be instantiated "+e.getMessage());
90             } catch (IllegalAccessException JavaDoc e) {
91                 logger.log(Level.SEVERE,ConfigurationHelper.COM_SUN_SECURITY_AUTH_LOGIN_CONFIG_FILE+" class cannot be accessed "+e.getMessage());
92             }
93
94         }
95
96     }
97
98
99     /**
100      *
101      * @param includeOldConfiguration
102      * @return
103      * @throws RuntimeException
104      */

105     private static JGuardConfiguration installWithOldConfig(boolean includeOldConfiguration) throws RuntimeException JavaDoc {
106         JGuardConfiguration jGuardConf;
107         Configuration JavaDoc oldConfiguration = null;
108         boolean skipOldConfig = false;
109         try{
110           oldConfiguration = Configuration.getConfiguration();
111           logger.log(Level.FINE," oldConfiguration="+oldConfiguration.getClass().getName());
112           logger.log(Level.FINE," oldConfiguration="+oldConfiguration);
113
114         }catch(SecurityException JavaDoc sex){
115           skipOldConfig = true;
116           logger.log(Level.FINE," addConfigurationEntryForWebapp() - exception raised when we try to retrieve the default Configuration instance ");
117           logger.log(Level.FINE," addConfigurationEntryForWebapp() - "+ sex.getMessage());
118           logger.log(Level.FINE," jGuard will not include the old Configuration ");
119         }catch(NullPointerException JavaDoc npe){
120             skipOldConfig = true;
121             logger.log(Level.FINE,"addConfigurationEntryForWebapp() - a NullPointerException has been raised when the default configuration :no configuration is defined ");
122             logger.log(Level.FINE,"addConfigurationEntryForWebapp() - "+ npe.getMessage());
123         }
124
125         //we override the current Configuration to JGuardConfiguration
126
//only if the current Configuration is not a JGuardConfiguration instance
127
if(oldConfiguration== null ||
128                 (!oldConfiguration.getClass().getName().equals(JGuardConfiguration.class.getName()))){
129               jGuardConf = new JGuardConfiguration();
130               Configuration.setConfiguration(jGuardConf);
131               logger.log(Level.INFO," JGuardConfiguration is set ");
132         //it is a JGuardConfiguration but with a class not loaded with this classloader
133
}else if(!oldConfiguration.getClass().equals(JGuardConfiguration.class)
134                  && oldConfiguration.getClass().getName().equals(JGuardConfiguration.class.getName())){
135             logger.log(Level.SEVERE," jGuard_jvm must be placed under the shared libraries directory or on the jvm side, not in the WEB-INF/lib directory of the webapp ");
136             throw new RuntimeException JavaDoc(" jGuard_jvm must be placed under the shared libraries directory or on the jvm side, not in the WEB-INF/lib directory of the webapp ");
137         }else{
138           jGuardConf = (JGuardConfiguration) Configuration.getConfiguration();
139           logger.log(Level.FINE,"configuration="+oldConfiguration.getClass().getName());
140           logger.log(Level.FINE," JGuardConfiguration is already set ");
141           // we don't include the old Configuration
142
//because the old Configuration is kept: it's already a jGuardConfiguration
143
skipOldConfig = true;
144         }
145
146         //there is no configuration entries for the webapp
147
if(!skipOldConfig && includeOldConfiguration == true){
148             logger.log(Level.INFO," jGuard include the old Configuration ");
149            jGuardConf.includeConfiguration(oldConfiguration);
150         }
151
152         //we should only execute one time this method per webapp
153
configurationInstalled = true;
154         return jGuardConf;
155     }
156
157
158     /**
159      * build the list of AppConfigurationEntry.
160      * @param applicationName
161      * @param authSettings
162      * @param debug
163      * @return list built
164      */

165     private static List JavaDoc buildAppConfigurationEntries(String JavaDoc applicationName,Map JavaDoc authSettings,boolean debug) {
166         List JavaDoc appConfigurationEntryList = new ArrayList JavaDoc();
167
168         List JavaDoc loginModules = (List JavaDoc)authSettings.get(CoreConstants.LOGIN_MODULES);
169         Iterator JavaDoc itLoginModules = loginModules.iterator();
170         while(itLoginModules.hasNext()){
171                Map JavaDoc loginModuleMap = (Map JavaDoc)itLoginModules.next();
172                String JavaDoc loginModuleClassName = (String JavaDoc)loginModuleMap.get(CoreConstants.NAME);
173                String JavaDoc loginModuleFlag = (String JavaDoc)loginModuleMap.get(CoreConstants.FLAG);
174                AppConfigurationEntry.LoginModuleControlFlag JavaDoc controlFlag;
175                if(loginModuleFlag.equalsIgnoreCase(CoreConstants.REQUIRED)){
176                    controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
177                }else if(loginModuleFlag.equalsIgnoreCase(CoreConstants.OPTIONAL)){
178                    controlFlag = AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
179                }else if(loginModuleFlag.equalsIgnoreCase(CoreConstants.REQUISITE)){
180                    controlFlag = AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
181                }else if(loginModuleFlag.equalsIgnoreCase(CoreConstants.SUFFICIENT)){
182                    controlFlag = AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
183                }else{
184                    throw new IllegalArgumentException JavaDoc(" invalid loginModuleControlFlag ="+loginModuleFlag+" is neither OPTIONAL,REQUIRED,REQUISITE nor SUFFICIENT ");
185                }
186                Map JavaDoc loginModuleOptions = (Map JavaDoc)loginModuleMap.get(CoreConstants.LOGIN_MODULE_OPTIONS);
187                loginModuleOptions.put(CoreConstants.APPLICATION_NAME,applicationName);
188                AppConfigurationEntry JavaDoc entry = new AppConfigurationEntry JavaDoc(loginModuleClassName,controlFlag,loginModuleOptions);
189                appConfigurationEntryList.add(entry);
190         }
191         if(appConfigurationEntryList.size()==0){
192             throw new IllegalArgumentException JavaDoc(" no loginModules have been configured for the application="+applicationName);
193         }
194         return appConfigurationEntryList;
195     }
196
197
198     public static String JavaDoc getScope() {
199         return scope;
200     }
201
202
203
204 }
205
Popular Tags