1 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 ; 26 27 public class SQLAuthenticator extends AbstractAuthenticator { 28 String data = null; 29 ConnectionPool conPool = null; 30 ThreadGroup threadGroup; 31 32 public SQLAuthenticator () { 33 } 34 35 public void init(Properties allProps, String additionalPrefix) { 36 super.init (allProps, additionalPrefix); 37 Properties 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; threadGroup = new ThreadGroup ("[SqlRunnerGroup " + dbProps.url + "(" + dbProps.table + ")"); 44 } catch (Exception e) { 45 Server.debug (this, "error parsing db-config: ", e, Server.MSG_STATE, Server.LVL_MAJOR); 46 } 47 } 48 49 50 53 public void shutdown () throws Exception { 54 conPool.shutdown (); 55 } 56 57 public User loginUser(String username, String password, String cookie, IRequest request) throws Exception { 58 SqlRunner runner = new SqlRunner (username, password, cookie, request, conPool); 59 Thread t = startThread(runner); 60 try { 61 t.join(); 62 } catch (InterruptedException 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 username, String password, IRequest request) throws Exception { 72 SqlRunner runner = new SqlRunner (u, username, password, request, conPool); 73 Thread t = startThread(runner); 74 try { 75 t.join(); 76 } catch (InterruptedException 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 { 86 if (u==null || u.isUnregistered) 87 return; 88 SqlRunner runner = new SqlRunner (u, conPool); 89 startThread(runner); 90 } 91 92 private Thread startThread(SqlRunner sr) throws Exception { 93 if (threadGroup.activeCount() >= conPool.size()) 94 throw new Exception ("All connections are currently used"); 95 Thread t = new Thread (threadGroup, sr); 96 t.start(); 97 return t; 98 } 99 } | Popular Tags |