KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > auth > sqlConnectionPool > SqlRunner


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

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 /**
30  * @author KMFDM
31  *
32  */

33 public class SqlRunner implements Runnable JavaDoc {
34     private final String JavaDoc 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 JavaDoc catchedException=null;
40     public User result=null;
41     
42     public SqlRunner (String JavaDoc username, String JavaDoc password, String JavaDoc cookie, IRequest request, ConnectionPool connPool) {
43         // handle login
44
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 JavaDoc username, String JavaDoc 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 JavaDoc e) {
87             catchedException = e;
88         }
89     }
90
91     public void logoutUser(User u) throws Exception JavaDoc {
92         PoolElement el = null;
93         boolean success = false;
94         Exception JavaDoc 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 JavaDoc 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 JavaDoc username, String JavaDoc password, String JavaDoc cookie, IRequest request) throws Exception JavaDoc {
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 JavaDoc le = null;
119         ConnectionBuffer cb = request.getConnectionBuffer();
120         Thread JavaDoc 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                 // retrieve PoolElement from ConnectionPool trying 3-times
126
el = connPool.getPoolElement(3, cb);
127                 u = el.loginUser(username, password, cookie);
128                 el.release();
129                 success=true;
130                 break;
131             } catch (Exception JavaDoc ee) {
132                 // cleanup on exception
133
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 JavaDoc ("UnknownException occured");
144         }
145         return u;
146     }
147
148     public User loginUser(User u, String JavaDoc username, String JavaDoc password, IRequest request) throws Exception JavaDoc {
149         PoolElement el=null;
150         boolean success = false;
151         Exception JavaDoc le = null;
152         ConnectionBuffer cb = request.getConnectionBuffer();
153         Thread JavaDoc 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                 // retrieve PoolElement from ConnectionPool trying 3-times
159
el = connPool.getPoolElement(3, cb);
160                 u = el.loginUser(u, password);
161                 el.release();
162                 success=true;
163                 break;
164             } catch (Exception JavaDoc ee) {
165                 // cleanup on exception
166
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 JavaDoc ("UnknownException occured");
177         }
178         return u;
179     }
180     
181     public String JavaDoc toString() {
182         return "[SqlRunner]";
183     }
184 }
185
Popular Tags