KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > auth > SQLAuthenticator


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

18 package freecs.auth;
19
20 import freecs.core.CanceledRequestException;
21 import freecs.core.User;
22 import freecs.Server;
23 import freecs.auth.sqlConnectionPool.*;
24 import freecs.interfaces.IRequest;
25 import java.util.Properties JavaDoc;
26
27 public class SQLAuthenticator extends AbstractAuthenticator {
28     String JavaDoc data = null;
29     ConnectionPool conPool = null;
30     ThreadGroup JavaDoc threadGroup;
31
32     public SQLAuthenticator () {
33     }
34     
35     public void init(Properties JavaDoc allProps, String JavaDoc additionalPrefix) {
36         super.init (allProps, additionalPrefix);
37         Properties JavaDoc mapping = filterProperties(props, "mapping.");
38         Server.log (this, "parsing db-config", Server.MSG_STATE, Server.LVL_MINOR);
39         try {
40             DbProperties dbProps = new DbProperties(props, mapping);
41             conPool = new ConnectionPool (this, dbProps);
42             data = null; // causes the next login-attempt to update the cached column-list of columns to retrieve from the db
43
threadGroup = new ThreadGroup JavaDoc ("[SqlRunnerGroup " + dbProps.url + "(" + dbProps.table + ")");
44         } catch (Exception JavaDoc e) {
45             Server.debug (this, "error parsing db-config: ", e, Server.MSG_STATE, Server.LVL_MAJOR);
46         }
47     }
48
49
50     /**
51      * server is shutting down. Close all connections to jdbc-source
52      */

53     public void shutdown () throws Exception JavaDoc {
54        conPool.shutdown ();
55     }
56
57     public User loginUser(String JavaDoc username, String JavaDoc password, String JavaDoc cookie, IRequest request) throws Exception JavaDoc {
58         SqlRunner runner = new SqlRunner (username, password, cookie, request, conPool);
59         Thread JavaDoc t = startThread(runner);
60         try {
61             t.join();
62         } catch (InterruptedException JavaDoc ie) {
63             t.interrupt();
64             throw new CanceledRequestException("Login timed out for user " + username, true);
65         }
66         if (runner.catchedException!=null)
67             throw runner.catchedException;
68         return runner.result;
69     }
70
71     public User loginUser(User u, String JavaDoc username, String JavaDoc password, IRequest request) throws Exception JavaDoc {
72         SqlRunner runner = new SqlRunner (u, username, password, request, conPool);
73         Thread JavaDoc t = startThread(runner);
74         try {
75             t.join();
76         } catch (InterruptedException JavaDoc ie) {
77             t.interrupt();
78             throw new CanceledRequestException("Login timed out for user " + u.getName(), true);
79         }
80         if (runner.catchedException!=null)
81             throw runner.catchedException;
82         return runner.result;
83     }
84     
85     public void logoutUser(User u) throws Exception JavaDoc {
86         if (u==null || u.isUnregistered)
87             return;
88         SqlRunner runner = new SqlRunner (u, conPool);
89         startThread(runner);
90     }
91     
92     private Thread JavaDoc startThread(SqlRunner sr) throws Exception JavaDoc {
93         if (threadGroup.activeCount() >= conPool.size())
94             throw new Exception JavaDoc ("All connections are currently used");
95         Thread JavaDoc t = new Thread JavaDoc(threadGroup, sr);
96         t.start();
97         return t;
98     }
99 }
Popular Tags