KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import com.sslexplorer.core.CoreServlet;
26 import com.sslexplorer.core.CoreUtil;
27 import com.sslexplorer.realms.Realm;
28
29 /**
30  * Default implementation of a {@link com.sslexplorer.security.UserDatabase}.
31  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
32  */

33 public abstract class DefaultUserDatabase implements UserDatabase {
34     static Log log = LogFactory.getLog(DefaultUserDatabase.class);
35
36     // Private instance variables
37
protected boolean open;
38     protected String JavaDoc description;
39     protected boolean supportsAccountCreation;
40     protected boolean supportsPasswordChange;
41     protected Realm realm;
42
43     /**
44      * Constructor.
45      * @param description description
46      * @param supportsAccountCreation true if concrete user database supports account creation
47      * @param supportsPasswordChange true if concrete user database supports password changing
48      */

49     public DefaultUserDatabase(String JavaDoc description, boolean supportsAccountCreation, boolean supportsPasswordChange) {
50         this.description = description;
51         this.supportsAccountCreation = supportsAccountCreation;
52         this.supportsPasswordChange = supportsPasswordChange;
53     }
54
55     /*
56      * (non-Javadoc)
57      * @see com.sslexplorer.security.UserDatabase#supportsPasswordChange()
58      */

59     public boolean supportsPasswordChange() {
60         return supportsPasswordChange;
61     }
62
63     /*
64      * (non-Javadoc)
65      * @see com.sslexplorer.security.UserDatabase#supportsAccountCreation()
66      */

67     public boolean supportsAccountCreation() {
68         return supportsAccountCreation;
69     }
70
71     /*
72      * (non-Javadoc)
73      * @see com.sslexplorer.security.UserDatabase#getDatabaseDescription()
74      */

75     public String JavaDoc getDatabaseDescription() {
76         return description;
77     }
78
79     public void open(CoreServlet controllingServlet, Realm realm) throws Exception JavaDoc {
80         if (realm == null) {
81             throw new IllegalArgumentException JavaDoc("No realm supplied.");
82         }
83         this.open = true;
84         log.info("Opening user database " + getClass().getName() + " for realm " + realm.getResourceName());
85         this.realm = realm;
86     }
87
88     public void open(CoreServlet controllingServlet) throws Exception JavaDoc {
89         throw new Exception JavaDoc("User databases must be opened with the realm.");
90     }
91
92     /*
93      * (non-Javadoc)
94      * @see com.sslexplorer.security.UserDatabase#changePassword(java.lang.String, java.lang.String, java.lang.String, boolean)
95      */

96     public void changePassword(String JavaDoc username, String JavaDoc oldPassword, String JavaDoc password, boolean forcePasswordChangeAtLogon)
97                     throws UserDatabaseException, InvalidLoginCredentialsException {
98         assertSupportsPasswordChange();
99         throw new InvalidLoginCredentialsException(
100                         "User database is not read-only, but the changePassword() method has not been implemented");
101     }
102
103     /*
104      * (non-Javadoc)
105      * @see com.sslexplorer.security.UserDatabase#setPassword(java.lang.String, java.lang.String, boolean, com.sslexplorer.security.User, java.lang.String)
106      */

107     public void setPassword(String JavaDoc username, String JavaDoc password, boolean forcePasswordChangeAtLogon, User adminUser,
108                             String JavaDoc adminPassword) throws UserDatabaseException, InvalidLoginCredentialsException {
109         assertSupportsPasswordChange();
110         throw new InvalidLoginCredentialsException("");
111
112     }
113
114     /*
115      * (non-Javadoc)
116      * @see com.sslexplorer.security.UserDatabase#createAccount(java.lang.String, java.lang.String, java.lang.String, java.lang.String com.sslexplorer.security.Role[])
117      */

118     public User createAccount(String JavaDoc username, String JavaDoc password, String JavaDoc email, String JavaDoc fullname, Role[] roles) throws Exception JavaDoc {
119         assertSupportsAccountCreation();
120         throw new Exception JavaDoc("User database is not read-only, but the createAccount() method has not been implemented");
121     }
122
123     /*
124      * (non-Javadoc)
125      * @see com.sslexplorer.security.UserDatabase#getUsersInRole(com.sslexplorer.security.Role)
126      */

127     public User[] getUsersInRole(Role role) throws Exception JavaDoc {
128         return CoreUtil.getUsersInRole(role, this);
129     }
130
131     /*
132      * (non-Javadoc)
133      * @see com.sslexplorer.security.UserDatabase#updateAccount(com.sslexplorer.security.User, java.lang.String, java.lang.String, com.sslexplorer.security.Role[])
134      */

135     public void updateAccount(User user, String JavaDoc email, String JavaDoc fullname, Role[] roles) throws Exception JavaDoc {
136         assertSupportsAccountCreation();
137         throw new Exception JavaDoc("User database is not read-only, but the updateAccount() method has not been implemented");
138     }
139
140     /*
141      * (non-Javadoc)
142      * @see com.sslexplorer.security.UserDatabase#deleteAccount(com.sslexplorer.security.User)
143      */

144     public void deleteAccount(User user) throws Exception JavaDoc, UserNotFoundException {
145         assertSupportsAccountCreation();
146         throw new Exception JavaDoc("User database is not read-only, but the deleteAccount() method has not been implemented");
147     }
148
149     /*
150      * (non-Javadoc)
151      * @see com.sslexplorer.security.UserDatabase#createRole(java.lang.String)
152      */

153     public Role createRole(String JavaDoc rolename) throws Exception JavaDoc {
154         assertSupportsAccountCreation();
155         throw new Exception JavaDoc("User database is not read-only, but the createRole() method has not been implemented");
156     }
157
158     /*
159      * (non-Javadoc)
160      * @see com.sslexplorer.security.UserDatabase#deleteRole(java.lang.String)
161      */

162     public void deleteRole(String JavaDoc rolename) throws Exception JavaDoc {
163         assertSupportsAccountCreation();
164         throw new Exception JavaDoc("User database is not read-only, but the deleteRole() method has not been implemented");
165     }
166     
167     protected void assertSupportsPasswordChange() throws InvalidLoginCredentialsException {
168         if (!supportsPasswordChange()) {
169             throw new InvalidLoginCredentialsException("Database doesn't support password change.");
170         }
171     }
172     
173     protected void assertSupportsAccountCreation() throws Exception JavaDoc {
174         if (!supportsAccountCreation()) {
175             throw new Exception JavaDoc("User database is read-only");
176         }
177     }
178
179     /*
180      * (non-Javadoc)
181      * @see com.sslexplorer.core.Database#cleanup()
182      */

183     public void cleanup() throws Exception JavaDoc {
184     }
185
186     /*
187      * (non-Javadoc)
188      * @see com.sslexplorer.security.UserDatabase#getRealm()
189      */

190     public Realm getRealm() {
191         return realm;
192     }
193
194     /*
195      * (non-Javadoc)
196      * @see com.sslexplorer.security.UserDatabase#close()
197      */

198     public void close() throws Exception JavaDoc {
199         this.open = false;
200         log.info("Closing user database " + getClass().getName() + " for realm " + realm.getResourceName());
201     }
202
203     /*
204      * (non-Javadoc)
205      * @see com.sslexplorer.security.UserDatabase#isOpen()
206      */

207     public boolean isOpen() {
208         return open;
209     }
210 }
Popular Tags