KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > UserDatabaseManager


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.core;
21
22 import java.util.Calendar JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.jaxen.expr.DefaultRelativeLocationPath;
31
32 import com.sslexplorer.boot.ContextHolder;
33 import com.sslexplorer.properties.Property;
34 import com.sslexplorer.properties.impl.realms.RealmKey;
35 import com.sslexplorer.realms.DefaultRealm;
36 import com.sslexplorer.realms.Realm;
37 import com.sslexplorer.security.UserDatabase;
38 import com.sslexplorer.security.UserDatabaseDefinition;
39
40 /**
41  * @author Brett Smith <brett@3sp.com>
42  */

43 public class UserDatabaseManager {
44     
45     final static Log log = LogFactory.getLog(UserDatabaseManager.class);
46     
47     private static UserDatabaseManager instance;
48     
49     protected TreeMap JavaDoc<String JavaDoc, UserDatabaseDefinition> userDatabases;
50     protected Realm defaultRealm;
51     protected UserDatabase defaultUserDatabase;
52
53     /**
54      * Constant name for the default/initial realm.
55      */

56     public static final String JavaDoc DEFAULT_REALM_NAME = "Default";
57
58     /**
59      * Constant description for the default/initial realm.
60      */

61     public static final String JavaDoc DEFAULT_REALM_DESCRIPTION = "Default SSL-Explorer Realm";
62     
63     /**
64      *
65      */

66     private UserDatabaseManager() {
67         super();
68         userDatabases = new TreeMap JavaDoc<String JavaDoc, UserDatabaseDefinition>();
69     }
70     
71     public void registerDatabase(UserDatabaseDefinition userDatabaseDefinition) {
72         if (log.isInfoEnabled())
73             log.info("Registering user database " + userDatabaseDefinition.getName() + " with class " + userDatabaseDefinition.getUserDatabaseClass().getName());
74         userDatabases.put(userDatabaseDefinition.getName(), userDatabaseDefinition);
75     }
76     
77     public UserDatabaseDefinition getUserDatabaseDefinition(String JavaDoc name) {
78         return userDatabases.get(name);
79     }
80     
81     public void closeAll() {
82         if (log.isInfoEnabled())
83             log.info("Closing all user databases");
84         try {
85             getUserDatabase(defaultRealm).close();
86         } catch (Exception JavaDoc e) {
87             log.error("Failed to close userdatabase.", e);
88         }
89     }
90     
91     public void close(String JavaDoc realmName) throws Exception JavaDoc {
92         UserDatabase udbInstance = getUserDatabase(realmName);
93         if(udbInstance != null) {
94             udbInstance.close();
95         }
96         else {
97             throw new Exception JavaDoc("No user database with name " + realmName + ".");
98         }
99     }
100     
101         /**
102      * Gets the initial user database.
103      * @return UserDatabase
104      */

105     public UserDatabase getDefaultUserDatabase() {
106         try {
107             return getUserDatabase(DEFAULT_REALM_NAME);
108         } catch (Exception JavaDoc e) {
109             log.error("Failed to retrieve the default realm.", e);
110             return null;
111         }
112     }
113     
114     
115     public UserDatabase getUserDatabase(Realm realm) throws Exception JavaDoc {
116         if(!realm.equals(defaultRealm))
117             throw new Exception JavaDoc("Invalid realm");
118         return defaultUserDatabase;
119     }
120     
121     public UserDatabase getUserDatabase(String JavaDoc realmName) throws Exception JavaDoc {
122         if(!realmName.equals(DEFAULT_REALM_NAME))
123             throw new Exception JavaDoc("Invalid realm " + realmName);
124         return defaultUserDatabase;
125     }
126     
127    /**
128      * @param realmId
129      * @return UserDatabase
130      * @throws Exception
131      */

132     public UserDatabase getUserDatabase(int realmId) throws Exception JavaDoc {
133         
134         if(realmId != 1)
135             throw new Exception JavaDoc("Invalid realm ID " + realmId);
136         
137         return defaultUserDatabase;
138     }
139     
140     public void initialize(boolean isSetupMode) throws ServletException JavaDoc {
141         String JavaDoc type = Property.getProperty(new RealmKey("security.userDatabase", 1));
142         try {
143             if(userDatabases.containsKey(type)) {
144                 createDefaultUserDatabase(type);
145             } else if (isSetupMode) {
146                 createDefaultUserDatabase("builtIn");
147             } else {
148                 throw new ServletException JavaDoc("Unable to initialise default user database = '" + type + "'.");
149             }
150         } catch (Exception JavaDoc e) {
151             log.error("Unable to initialise default user database.", e);
152             // if we can't open the database in setup mode we still have to continue
153
if(!isSetupMode) {
154                 throw new ServletException JavaDoc("Unable to initialise default user database.", e);
155             }
156         }
157     }
158     
159    /**
160      * @param type
161      * @throws Exception
162      */

163     private void createDefaultUserDatabase(String JavaDoc type) throws Exception JavaDoc {
164         UserDatabaseDefinition udd = userDatabases.get(type);
165         if (udd == null) {
166             throw new Exception JavaDoc("No user database of type " + type + " registered.");
167         } else {
168             Calendar JavaDoc now = Calendar.getInstance();
169             Class JavaDoc clazz = udd.getUserDatabaseClass();
170             defaultUserDatabase = (UserDatabase) clazz.newInstance();
171             defaultRealm = new DefaultRealm(type, 1, DEFAULT_REALM_NAME, DEFAULT_REALM_DESCRIPTION, now, now);
172             defaultUserDatabase.open(CoreServlet.getServlet(), defaultRealm);
173         }
174     }
175     
176     public UserDatabase createUserDatabase(String JavaDoc type, String JavaDoc realmName, String JavaDoc realmDescription, boolean open) throws Exception JavaDoc {
177         UserDatabaseDefinition udd = userDatabases.get(type);
178         if (udd == null) {
179             throw new Exception JavaDoc("No user database of type " + type + " registered.");
180         } else {
181             Calendar JavaDoc now = Calendar.getInstance();
182             
183             Class JavaDoc clazz = udd.getUserDatabaseClass();
184             UserDatabase udb = (UserDatabase) clazz.newInstance();
185             Realm realm = new DefaultRealm(type, 1, DEFAULT_REALM_NAME, DEFAULT_REALM_DESCRIPTION, now, now);
186             if(!udb.isOpen() && open)
187                 udb.open(CoreServlet.getServlet(), realm);
188             return udb;
189         }
190     }
191     public static UserDatabaseManager getInstance() {
192         if(instance == null) {
193             instance = new UserDatabaseManager();
194         }
195         return instance;
196     }
197
198     public Collection JavaDoc<UserDatabaseDefinition> getUserDatabaseDefinitions() {
199         return userDatabases.values();
200     }
201     
202     /**
203      * @param realmId
204      * @return Realm
205      * @throws Exception
206      */

207     public Realm getRealm(int realmId) throws Exception JavaDoc {
208         if (defaultRealm.getResourceId() == realmId) {
209             return defaultRealm;
210         }
211         throw new Exception JavaDoc("No realm exists for the id " + realmId);
212     }
213
214     /**
215      * @param realmName
216      * @return Realm
217      * @throws Exception
218      */

219     public Realm getRealm(String JavaDoc realmName) throws Exception JavaDoc {
220         if (defaultRealm != null && defaultRealm.getResourceName().equals(realmName)) {
221             return defaultRealm;
222         }
223         throw new Exception JavaDoc("No realm exists for the name " + realmName);
224     }
225
226     /**
227      * Convenience method to get the default realm.
228      *
229      * @return default realm
230      * @throws Exception if realm cannot be found
231      */

232     public Realm getDefaultRealm() throws Exception JavaDoc {
233         return getRealm(DEFAULT_REALM_NAME);
234     }
235
236     /**
237      * Convenience method to get the default realm ID
238      *
239      * @return default realm id
240      * @throws Exception if realm cannot be found
241      */

242     public int getDefaultRealmID() throws Exception JavaDoc {
243         return getDefaultRealm().getResourceId();
244     }
245 }
246
Popular Tags