KickJava   Java API By Example, From Geeks To Geeks.

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


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
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
31 import java.security.AccessController JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 import javax.security.auth.AuthPermission JavaDoc;
43 import javax.security.auth.login.AppConfigurationEntry JavaDoc;
44 import javax.security.auth.login.Configuration JavaDoc;
45
46 /**
47  * extends the <a HREF="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/login/Configuration.html">
48  * Configuration</a>
49  * this class is used to define the authentication stack scheme per application.
50  * @author <a HREF="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
51  *
52  */

53 public final class JGuardConfiguration extends Configuration JavaDoc {
54
55     private Collection JavaDoc internalConfigs = null;
56     private Map JavaDoc appConfigurations = null;
57     private static Logger JavaDoc logger = Logger.getLogger(JGuardConfiguration.class.getName());
58     /**
59      * constructor.
60      */

61     public JGuardConfiguration() {
62         super();
63         logger.finest("##### JGuardConfiguration #####");
64
65         internalConfigs = new ArrayList JavaDoc();
66         appConfigurations = new HashMap JavaDoc();
67
68     }
69
70     /**
71      * reload the Configuration.
72      * @see javax.security.auth.login.Configuration#refresh()
73      */

74     public void refresh() {
75         AccessController.checkPermission(new AuthPermission JavaDoc("refreshLoginConfiguration"));
76
77         if(internalConfigs.size()>0){
78             Iterator JavaDoc itConfigs = internalConfigs.iterator();
79             while(itConfigs.hasNext()){
80                 Configuration JavaDoc tempConfig = (Configuration JavaDoc) itConfigs.next();
81                 tempConfig.refresh();
82             }
83         }
84
85     }
86
87     /**
88      * retrieve the AppConfigurationEntry array for the corresponding application's name.
89      * @param applicationName
90      * @return array of AppConfigurationEntry
91      * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String)
92      */

93     public AppConfigurationEntry JavaDoc[] getAppConfigurationEntry(
94             String JavaDoc applicationName) {
95         Collection JavaDoc appInternalEntries = new ArrayList JavaDoc();
96         Iterator JavaDoc itConfigs = internalConfigs.iterator();
97         while(itConfigs.hasNext()){
98             Configuration JavaDoc tempConfig = (Configuration JavaDoc) itConfigs.next();
99             if (tempConfig == null){
100                 itConfigs.remove();
101                 logger.log(Level.WARNING,"the default Configuration implementation has been removed from the JGuardConfiguration which imported it");
102             } else if( tempConfig.getAppConfigurationEntry(applicationName)!= null){
103                 appInternalEntries.addAll(Arrays.asList(tempConfig.getAppConfigurationEntry(applicationName)));
104             }
105         }
106
107         List JavaDoc jGuardAppConfigEntries = (List JavaDoc)appConfigurations.get(applicationName);
108
109        if(jGuardAppConfigEntries!=null){
110            appInternalEntries.addAll(jGuardAppConfigEntries);
111        }
112         if(appInternalEntries.size()>0){
113             return (AppConfigurationEntry JavaDoc[]) appInternalEntries.toArray(new AppConfigurationEntry JavaDoc[appInternalEntries.size()]);
114         }
115
116         return null;
117
118     }
119
120
121     /**
122      * add AppconfigurationEntries for a specified application.
123      * @param applicationName
124      * @param entries
125      * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String)
126      */

127     public void addConfigEntriesForApplication(String JavaDoc applicationName,List JavaDoc entries) {
128         if(entries == null || entries.size() == 0){
129             logger.log(Level.WARNING," entries provided are null or empty ");
130             return;
131         }
132
133         List JavaDoc applicationEntries = (List JavaDoc) appConfigurations.get(applicationName);
134         if(applicationEntries==null){
135             //this application is not yet configured
136
appConfigurations.put(applicationName,entries);
137         }
138
139         //we don't add other appConfigurationEntries if the application name already exists
140
//because when webapp stops and start (but not app server), configuration for the application
141
// will have twice loginmodules
142
}
143
144     /**
145      * add the same AppconfigurationEntries like an already configured application.
146      * @param applicationName name of the application to configure
147      * @param applicationTemplateName name of the application
148      * which will be the template to configure the first one.
149      * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String)
150      */

151     public void addConfigEntriesLikeApplication(String JavaDoc applicationName,String JavaDoc applicationTemplateName) {
152         List JavaDoc applicationEntries = (List JavaDoc) appConfigurations.get(applicationTemplateName);
153         if(applicationEntries== null){
154             logger.log(Level.SEVERE," there is no applications registered with your applicationName and password ");
155             return;
156         }
157         appConfigurations.put(applicationName,applicationEntries);
158     }
159
160     /**
161      * add the same AppconfigurationEntries like an already configured application.
162      * @param applicationName
163      * @param password
164      * @param applicationTemplateName
165      */

166     public void addConfigEntriesLikeApplication(String JavaDoc applicationName,String JavaDoc password,String JavaDoc applicationTemplateName) {
167         StringBuffer JavaDoc newApplicationName = new StringBuffer JavaDoc(applicationName)
168         .append('#').append(password);
169         addConfigEntriesLikeApplication(newApplicationName.toString(),applicationTemplateName);
170     }
171
172     /**
173      * include Configuration information.
174      * @param configuration
175      */

176     protected void includeConfiguration(Configuration JavaDoc configuration){
177         //we do not include a jGuardConfiguration to prevent infinite loop
178
if(!configuration.getClass().getName().equals(JGuardConfiguration.class.getName())
179         &&!internalConfigs.contains(configuration)){
180          internalConfigs.add(configuration);
181         }
182     }
183
184
185     /**
186      * register the application with a name and a password
187      * add AppconfigurationEntries for a specified application.
188      * @param applicationName
189      * @param password
190      * @param entries
191      * @see javax.security.auth.login.Configuration#getAppConfigurationEntry(java.lang.String)
192      */

193     public void addConfigEntriesForApplication(String JavaDoc applicationName,String JavaDoc password,List JavaDoc entries) {
194         StringBuffer JavaDoc newApplicationName = new StringBuffer JavaDoc(applicationName)
195         .append('#').append(password);
196         addConfigEntriesForApplication(newApplicationName.toString(),entries);
197     }
198
199
200 }
201
Popular Tags