001: /**
002: * Copyright (C) 2005 KMFDM
003: * Created: 16.01.2005 (22:40:14)
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */package freecs.auth.sqlConnectionPool;
019:
020: import freecs.Server;
021: import freecs.core.CanceledRequestException;
022: import freecs.core.ConnectionBuffer;
023: import freecs.core.User;
024: import freecs.interfaces.IRequest;
025: import freecs.util.HashUtils;
026:
027: /**
028: * @author KMFDM
029: *
030: */
031: public class SqlRunner implements Runnable {
032: private final String username, password, cookie;
033: private final IRequest request;
034: private final User u;
035: private final short method;
036: private final ConnectionPool connPool;
037: public Exception catchedException = null;
038: public User result = null;
039:
040: public SqlRunner(String username, String password, String cookie,
041: IRequest request, ConnectionPool connPool) {
042: // handle login
043: this .u = null;
044: this .username = username;
045: this .password = password;
046: this .cookie = cookie;
047: this .request = request;
048: this .connPool = connPool;
049: this .method = 1;
050: }
051:
052: public SqlRunner(User u, String username, String password,
053: IRequest request, ConnectionPool connPool) {
054: this .u = u;
055: this .cookie = null;
056: this .username = username;
057: this .password = password;
058: this .request = request;
059: this .connPool = connPool;
060: this .method = 2;
061: }
062:
063: public SqlRunner(User u, ConnectionPool connPool) {
064: this .username = null;
065: this .password = null;
066: this .request = null;
067: this .cookie = null;
068: this .u = u;
069: this .connPool = connPool;
070: this .method = 3;
071: }
072:
073: public void run() {
074: try {
075: switch (method) {
076: case 1:
077: this .result = loginUser(username, password, cookie,
078: request);
079: break;
080: case 2:
081: this .result = loginUser(u, username, password, request);
082: break;
083: case 3:
084: logoutUser(u);
085: }
086: } catch (Exception e) {
087: catchedException = e;
088: }
089: }
090:
091: public void logoutUser(User u) throws Exception {
092: PoolElement el = null;
093: boolean success = false;
094: Exception le = null;
095: for (int i = 0; i < connPool.size() + 1; i++) {
096: try {
097: el = connPool.getPoolElement(3, null);
098: el.logoutUser(u);
099: 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,
107: Server.MSG_ERROR, Server.LVL_MAJOR);
108: catchedException = ee;
109: }
110: }
111: }
112:
113: public User loginUser(String username, String password,
114: String cookie, IRequest request) throws Exception {
115: if (Server.srv.MD5_PASSWORDS)
116: password = HashUtils.encodeMD5(password);
117: PoolElement el = null;
118: User u = null;
119: boolean success = false;
120: Exception le = null;
121: ConnectionBuffer cb = request.getConnectionBuffer();
122: Thread curr = Thread.currentThread();
123: for (int i = 0; i < connPool.size() + 1; i++) {
124: if (curr.isInterrupted() || !cb.isValid())
125: throw new CanceledRequestException(
126: "ConnectionBuffer has been invalidated");
127: try {
128: // retrieve PoolElement from ConnectionPool trying 3-times
129: el = connPool.getPoolElement(3, cb);
130: u = el.loginUser(username, password, cookie);
131: el.release();
132: success = true;
133: break;
134: } catch (Exception ee) {
135: // cleanup on exception
136: if (el != null) {
137: el.cleanup();
138: }
139: Server.debug(this , "Exception during loginUser", ee,
140: Server.MSG_ERROR, Server.LVL_MAJOR);
141: le = ee;
142: }
143: }
144: if (!success) {
145: if (le != null)
146: throw le;
147: throw new Exception("UnknownException occured");
148: }
149: return u;
150: }
151:
152: public User loginUser(User u, String username, String password,
153: IRequest request) throws Exception {
154: PoolElement el = null;
155: boolean success = false;
156: Exception le = null;
157: ConnectionBuffer cb = request.getConnectionBuffer();
158: Thread curr = Thread.currentThread();
159: for (int i = 0; i < connPool.size() + 1; i++) {
160: if (curr.isInterrupted())
161: throw new CanceledRequestException(
162: "ConnectionBuffer has been invalidated");
163: try {
164: // retrieve PoolElement from ConnectionPool trying 3-times
165: el = connPool.getPoolElement(3, cb);
166: u = el.loginUser(u, password);
167: el.release();
168: success = true;
169: break;
170: } catch (Exception ee) {
171: // cleanup on exception
172: if (el != null) {
173: el.cleanup();
174: }
175: Server.debug(this , "SQL-Exception", ee,
176: Server.MSG_ERROR, Server.LVL_MAJOR);
177: le = ee;
178: }
179: }
180: if (!success) {
181: if (le != null)
182: throw le;
183: throw new Exception("UnknownException occured");
184: }
185: return u;
186: }
187:
188: public String toString() {
189: return "[SqlRunner]";
190: }
191: }
|