| 1 19 package freecs.auth.sqlConnectionPool; 20 21 import freecs.Server; 22 import freecs.core.CanceledRequestException; 23 import freecs.core.ConnectionBuffer; 24 import freecs.core.User; 25 import freecs.interfaces.IRequest; 26 import freecs.util.HashUtils; 27 28 29 33 public class SqlRunner implements Runnable { 34 private final String username, password, cookie; 35 private final IRequest request; 36 private final User u; 37 private final short method; 38 private final ConnectionPool connPool; 39 public Exception catchedException=null; 40 public User result=null; 41 42 public SqlRunner (String username, String password, String cookie, IRequest request, ConnectionPool connPool) { 43 this.u=null; 45 this.username = username; 46 this.password = password; 47 this.cookie = cookie; 48 this.request = request; 49 this.connPool = connPool; 50 this.method = 1; 51 } 52 53 public SqlRunner (User u, String username, String password, IRequest request, ConnectionPool connPool) { 54 this.u = u; 55 this.cookie=null; 56 this.username = username; 57 this.password = password; 58 this.request = request; 59 this.connPool = connPool; 60 this.method = 2; 61 } 62 63 public SqlRunner (User u, ConnectionPool connPool) { 64 this.username=null; 65 this.password=null; 66 this.request=null; 67 this.cookie=null; 68 this.u = u; 69 this.connPool = connPool; 70 this.method = 3; 71 } 72 73 74 public void run() { 75 try { 76 switch (method) { 77 case 1: 78 this.result = loginUser(username, password, cookie, request); 79 break; 80 case 2: 81 this.result = loginUser(u, username, password, request); 82 break; 83 case 3: 84 logoutUser(u); 85 } 86 } catch (Exception e) { 87 catchedException = e; 88 } 89 } 90 91 public void logoutUser(User u) throws Exception { 92 PoolElement el = null; 93 boolean success = false; 94 Exception le = null; 95 for (int i = 0; i < connPool.size()+1; i++) { 96 try { 97 el = connPool.getPoolElement(3, null); 98 el.logoutUser (u); 99 el.release(); 100 success=true; 101 break; 102 } catch (Exception ee) { 103 if (el != null) { 104 el.cleanup(); 105 } 106 Server.debug(this, "SQL-Exception", ee, Server.MSG_ERROR, Server.LVL_MAJOR); 107 catchedException = ee; 108 } 109 } 110 } 111 112 public User loginUser(String username, String password, String cookie, IRequest request) throws Exception { 113 if (Server.srv.MD5_PASSWORDS) 114 password = HashUtils.encodeMD5(password); 115 PoolElement el=null; 116 User u = null; 117 boolean success = false; 118 Exception le = null; 119 ConnectionBuffer cb = request.getConnectionBuffer(); 120 Thread curr = Thread.currentThread(); 121 for (int i = 0; i < connPool.size()+1; i++) { 122 if (curr.isInterrupted() || !cb.isValid()) 123 throw new CanceledRequestException ("ConnectionBuffer has been invalidated"); 124 try { 125 el = connPool.getPoolElement(3, cb); 127 u = el.loginUser(username, password, cookie); 128 el.release(); 129 success=true; 130 break; 131 } catch (Exception ee) { 132 if (el!=null) { 134 el.cleanup(); 135 } 136 Server.debug(this, "Exception during loginUser", ee, Server.MSG_ERROR, Server.LVL_MAJOR); 137 le = ee; 138 } 139 } 140 if (!success) { 141 if (le != null) 142 throw le; 143 throw new Exception ("UnknownException occured"); 144 } 145 return u; 146 } 147 148 public User loginUser(User u, String username, String password, IRequest request) throws Exception { 149 PoolElement el=null; 150 boolean success = false; 151 Exception le = null; 152 ConnectionBuffer cb = request.getConnectionBuffer(); 153 Thread curr = Thread.currentThread(); 154 for (int i = 0; i < connPool.size()+1; i++) { 155 if (curr.isInterrupted()) 156 throw new CanceledRequestException ("ConnectionBuffer has been invalidated"); 157 try { 158 el = connPool.getPoolElement(3, cb); 160 u = el.loginUser(u, password); 161 el.release(); 162 success=true; 163 break; 164 } catch (Exception ee) { 165 if (el!=null) { 167 el.cleanup(); 168 } 169 Server.debug(this, "SQL-Exception", ee, Server.MSG_ERROR, Server.LVL_MAJOR); 170 le = ee; 171 } 172 } 173 if (!success) { 174 if (le != null) 175 throw le; 176 throw new Exception ("UnknownException occured"); 177 } 178 return u; 179 } 180 181 public String toString() { 182 return "[SqlRunner]"; 183 } 184 } 185 | Popular Tags |