KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > authentication > AuthenticationMgr


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43 package org.exolab.jms.authentication;
44
45 import java.security.Principal JavaDoc;
46
47 import org.exolab.jms.service.BasicService;
48 import org.exolab.jms.service.ServiceException;
49 import org.exolab.jms.net.connector.Authenticator;
50 import org.exolab.jms.net.connector.ResourceException;
51 import org.exolab.jms.common.security.BasicPrincipal;
52
53
54 /**
55  * This is the active authentication component within the JMS server.
56  *
57  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:39 $
58  * @author <a HREF="mailto:knut@lerpold">Knut Lerpold</a>
59  */

60 public class AuthenticationMgr extends BasicService implements Authenticator {
61
62     /**
63      * The user manager
64      */

65     private UserManager _users;
66
67     /**
68      * The service name of the authentication manager
69      */

70     private static final String JavaDoc AM_SERVICE_NAME = "AuthenticationManager";
71
72     /**
73      * The singleton instance of the authentication manager.
74      */

75     private static volatile AuthenticationMgr _instance;
76
77
78     /**
79      * Construct a new <code>AuthenticationManager</code>
80      *
81      * @throws ServiceException if the service cannot be initialised
82      */

83     private AuthenticationMgr() throws ServiceException {
84         super(AM_SERVICE_NAME);
85         _users = new UserManager();
86     }
87
88     /**
89      * Create and return an instance of the singleton.
90      *
91      * @return the singleton instance
92      * @throws ServiceException if the service cannot be created
93      */

94     public static AuthenticationMgr createInstance() throws ServiceException {
95         _instance = new AuthenticationMgr();
96         return _instance;
97     }
98
99     /**
100      * Return an instance to the AuthenticationMgr singleton. This method
101      * assumes that the singleton has already been created with a call to
102      * {@link #createInstance}
103      *
104      * @return the singleton instance
105      */

106     public static AuthenticationMgr instance() {
107         return _instance;
108     }
109
110     // implement BasicService.run
111
public void run() {
112         // do nothing
113
}
114
115     // override BasicService.stop
116
public void stop() throws ServiceException {
117         // destroy the user manager.
118
_users.destroy();
119
120         // clear the static reference
121
_instance = null;
122     }
123
124     /**
125      * Create a user.
126      *
127      * @param user the user to create
128      * @return <code>true</code> if the user is created
129      * otherwise <code>false</code>
130      */

131     public boolean addUser(User user) {
132         return _users.createUser(user);
133     }
134
135     /**
136      * Remove this user
137      *
138      * @param user the user to remove
139      * @return <code>true</code> if the user is removed
140      * otherwise <code>false</code>
141      */

142     public boolean removeUser(User user) {
143         return _users.deleteUser(user);
144     }
145
146     /**
147      * Gets a user.
148      *
149      * @param user the user
150      * @return User
151      */

152     public User getUser(User user) {
153         return _users.getUser(user);
154     }
155
156     /**
157      * Update a user.
158      *
159      * @param user the user to update
160      * @return <code>true</code> if the password is updated
161      * otherwise <code>false</code>
162      */

163     public boolean updateUser(User user) {
164         return _users.updateUser(user);
165     }
166
167     /**
168      * Validate the password for the specified user.
169      *
170      * @param username the user's name
171      * @param password the password to check
172      * @return <code>true</code> if the username and password exist, otherwise
173      * <code>false</code>
174      */

175     public boolean validateUser(String JavaDoc username, String JavaDoc password) {
176         return _users.validateUser(username, password);
177     }
178
179     /**
180      * Determines if a principal has permissions to connect
181      *
182      * @param principal the principal to check
183      * @return <code>true</code> if the principal has permissions to connect
184      */

185     public boolean authenticate(Principal JavaDoc principal) {
186         String JavaDoc user = null;
187         String JavaDoc password = null;
188         if (principal instanceof BasicPrincipal) {
189             BasicPrincipal basic = (BasicPrincipal) principal;
190             user = basic.getName();
191             password = basic.getPassword();
192         } else {
193             // treat everything else as unauthenticated/unknown user
194
}
195         return validateUser(user, password);
196     }
197 }
198
Popular Tags