KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > AuthenticationModuleManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import com.sslexplorer.core.CoreServlet;
30
31 /**
32  * Manages registered {@link com.sslexplorer.security.AuthenticationModuleDefinition}
33  * objects that are used to create {@link com.sslexplorer.security.AuthenticationModule}
34  * instances to be used in an {@link com.sslexplorer.security.AuthenticationScheme}.
35  * <p>
36  * Both the core and plugins will register definitions such as <i>Password Authentication</i>,
37  * <i>Personal Questions</i> and additional modules such as <i>Client Certifcate</i>
38  * and <i>Key</i>.
39  * <p>
40  * To register a module get an instance of this object (which will be created
41  * if it doesn't exist) using {@link #getInstance()}. Then use
42  * {@link #registerModule(String, Class, String, boolean, boolean, boolean)}
43  * to register the module.
44  *
45  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
46  */

47 public class AuthenticationModuleManager {
48
49     final static Log log = LogFactory.getLog(AuthenticationModuleManager.class);
50
51     private static AuthenticationModuleManager instance;
52     private TreeMap JavaDoc modules;
53
54     /**
55      * Private contructor to prevent instantiation
56      */

57     private AuthenticationModuleManager() {
58         super();
59         modules = new TreeMap JavaDoc();
60     }
61
62     /**
63      * Register a new module definition.
64      *
65      * @param name name of module
66      * @param moduleClass class to use to instantiate the module
67      * @param messageResourcesKey bundle that contains message resources
68      * @param primary primary (accepts username)
69      * @param secondary secondary (accepts something other than username)
70      * @param system system (used internally, not usable for web logon)
71      */

72     public void registerModule(String JavaDoc name, Class JavaDoc moduleClass, String JavaDoc messageResourcesKey, boolean primary, boolean secondary, boolean system) {
73         modules.put(name, new AuthenticationModuleDefinition(name, moduleClass, messageResourcesKey, primary, secondary, system));
74     }
75
76     /**
77      * Register a new module definition
78      *
79      * @param name
80      * @param moduleClass
81      * @param messageResourcesKey
82      * @param primary
83      * @param secondary
84      * @param system
85      * @param primaryIfSecondaryExists
86      */

87     public void registerModule(String JavaDoc name, Class JavaDoc moduleClass, String JavaDoc messageResourcesKey, boolean primary, boolean secondary, boolean system, boolean primaryIfSecondaryExists) {
88         modules.put(name, new AuthenticationModuleDefinition(name, moduleClass, messageResourcesKey, primary, secondary, system, primaryIfSecondaryExists));
89     }
90     /**
91      * De-register a module
92      *
93      * @param module module to de-register
94      */

95     public void deregisterModule(String JavaDoc module) {
96         modules.remove(module);
97     }
98
99     /**
100      * Get a definition give its module name. <code>null</code> will be
101      * returned if no such definition exists.
102      *
103      * @param module module name
104      * @return definition
105      */

106     public AuthenticationModuleDefinition getModuleDefinition(String JavaDoc module) {
107         return (AuthenticationModuleDefinition)modules.get(module);
108     }
109
110     /**
111      * Create a new instance of an {@link AuthenticationModule} that may
112      * be used by an {@link AuthenticationScheme} for a user to logon given its
113      * definition name.
114      *
115      * @param module module name
116      * @return authentication module instance
117      * @throws InstantiationException if module cannot be created
118      * @throws IllegalAccessException
119      */

120     public AuthenticationModule createModule(String JavaDoc module) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
121         AuthenticationModuleDefinition def = getModuleDefinition(module);
122         if(def == null) {
123             throw new IllegalArgumentException JavaDoc("No module named " + module);
124         }
125         return (AuthenticationModule)def.getModuleClass().newInstance();
126     }
127
128     /**
129      * Get an iterator of all registered authentication module definitions.
130      *
131      * @return iterator of all registered authentication module definitions
132      */

133     public Iterator JavaDoc authenticationModuleDefinitions() {
134         return modules.values().iterator();
135     }
136
137     /**
138      * Get an instance of the authentication module mananger.
139      *
140      * @return authentication module manager
141      */

142     public static AuthenticationModuleManager getInstance() {
143         if(instance == null) {
144             instance = new AuthenticationModuleManager();
145         }
146         return instance;
147     }
148
149     /**
150      * Get if a module has been registered given its definition name.
151      *
152      * @param module module name
153      * @return module registered
154      */

155     public boolean isRegistered(String JavaDoc module) {
156         return modules.containsKey(module);
157     }
158
159     /**
160      * Get if an authentication module is in use by any configured
161      * {@link AuthenticationScheme}.
162      *
163      * @param module
164      * @return module in use
165      */

166     public boolean isAuthenticationModuleInUse(String JavaDoc module) {
167         boolean available = true;
168         try {
169             boolean found = false;
170             List JavaDoc schemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences();
171             for(Iterator JavaDoc i = schemes.iterator(); i.hasNext() && !found; ) {
172                 AuthenticationScheme seq = (DefaultAuthenticationScheme)i.next();
173                 if(seq.hasModule(module)) {
174                     found = true;
175                 }
176             }
177             if(!found) {
178                 available = false;
179             }
180         } catch (Exception JavaDoc e) {
181             available = false;
182         }
183         return available;
184     }
185
186     /**
187      * Get the first {@link AuthenticationScheme} that is using the
188      * provided module. <code>null</code> will be returned if no scheme
189      * uses the module.
190      *
191      * @param module modue
192      * @return scheme
193      */

194     public AuthenticationScheme getSchemeForAuthenticationModuleInUse(String JavaDoc module) {
195         try {
196             List JavaDoc schemes = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences();
197             for(Iterator JavaDoc i = schemes.iterator(); i.hasNext(); ) {
198                 AuthenticationScheme seq = (DefaultAuthenticationScheme)i.next();
199                 if(seq.hasModule(module)) {
200                     return seq;
201                 }
202             }
203         } catch (Exception JavaDoc e) {
204         }
205         return null;
206     }
207
208 }
209
Popular Tags