1 19 20 package com.sslexplorer.core; 21 22 import java.util.Calendar ; 23 import java.util.Collection ; 24 import java.util.TreeMap ; 25 26 import javax.servlet.ServletException ; 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 43 public class UserDatabaseManager { 44 45 final static Log log = LogFactory.getLog(UserDatabaseManager.class); 46 47 private static UserDatabaseManager instance; 48 49 protected TreeMap <String , UserDatabaseDefinition> userDatabases; 50 protected Realm defaultRealm; 51 protected UserDatabase defaultUserDatabase; 52 53 56 public static final String DEFAULT_REALM_NAME = "Default"; 57 58 61 public static final String DEFAULT_REALM_DESCRIPTION = "Default SSL-Explorer Realm"; 62 63 66 private UserDatabaseManager() { 67 super(); 68 userDatabases = new TreeMap <String , 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 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 e) { 87 log.error("Failed to close userdatabase.", e); 88 } 89 } 90 91 public void close(String realmName) throws Exception { 92 UserDatabase udbInstance = getUserDatabase(realmName); 93 if(udbInstance != null) { 94 udbInstance.close(); 95 } 96 else { 97 throw new Exception ("No user database with name " + realmName + "."); 98 } 99 } 100 101 105 public UserDatabase getDefaultUserDatabase() { 106 try { 107 return getUserDatabase(DEFAULT_REALM_NAME); 108 } catch (Exception 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 { 116 if(!realm.equals(defaultRealm)) 117 throw new Exception ("Invalid realm"); 118 return defaultUserDatabase; 119 } 120 121 public UserDatabase getUserDatabase(String realmName) throws Exception { 122 if(!realmName.equals(DEFAULT_REALM_NAME)) 123 throw new Exception ("Invalid realm " + realmName); 124 return defaultUserDatabase; 125 } 126 127 132 public UserDatabase getUserDatabase(int realmId) throws Exception { 133 134 if(realmId != 1) 135 throw new Exception ("Invalid realm ID " + realmId); 136 137 return defaultUserDatabase; 138 } 139 140 public void initialize(boolean isSetupMode) throws ServletException { 141 String 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 ("Unable to initialise default user database = '" + type + "'."); 149 } 150 } catch (Exception e) { 151 log.error("Unable to initialise default user database.", e); 152 if(!isSetupMode) { 154 throw new ServletException ("Unable to initialise default user database.", e); 155 } 156 } 157 } 158 159 163 private void createDefaultUserDatabase(String type) throws Exception { 164 UserDatabaseDefinition udd = userDatabases.get(type); 165 if (udd == null) { 166 throw new Exception ("No user database of type " + type + " registered."); 167 } else { 168 Calendar now = Calendar.getInstance(); 169 Class 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 type, String realmName, String realmDescription, boolean open) throws Exception { 177 UserDatabaseDefinition udd = userDatabases.get(type); 178 if (udd == null) { 179 throw new Exception ("No user database of type " + type + " registered."); 180 } else { 181 Calendar now = Calendar.getInstance(); 182 183 Class 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 <UserDatabaseDefinition> getUserDatabaseDefinitions() { 199 return userDatabases.values(); 200 } 201 202 207 public Realm getRealm(int realmId) throws Exception { 208 if (defaultRealm.getResourceId() == realmId) { 209 return defaultRealm; 210 } 211 throw new Exception ("No realm exists for the id " + realmId); 212 } 213 214 219 public Realm getRealm(String realmName) throws Exception { 220 if (defaultRealm != null && defaultRealm.getResourceName().equals(realmName)) { 221 return defaultRealm; 222 } 223 throw new Exception ("No realm exists for the name " + realmName); 224 } 225 226 232 public Realm getDefaultRealm() throws Exception { 233 return getRealm(DEFAULT_REALM_NAME); 234 } 235 236 242 public int getDefaultRealmID() throws Exception { 243 return getDefaultRealm().getResourceId(); 244 } 245 } 246 | Popular Tags |