-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSessionPoolConnector.java
executable file
·189 lines (173 loc) · 6.59 KB
/
SessionPoolConnector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package bc.connection;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Set;
public class SessionPoolConnector {
private void outError(Object information){
System.out.print("SessionPoolConnector");
System.out.print(" ERROR ");
System.out.println(information);
}
/** pool of Connection for user's [User Name, HibernateOracleConnect] (1:1) */
private HashMap <String,PoolConnect> connectionUserPool=new HashMap<String,PoolConnect>();
private UserSessionManager userSession=new UserSessionManager();
private Class<? extends PoolConnect> connectorClass;
private Integer poolSize=new Integer(10);
/** îáúåêò, êîòîðûé ñîçäàåò Pool ñîåäèíåíèé ïî óêàçàííîìó êëèåíòó, à âîçâðàùàåò Connection ïî SessionId
* @param ïåðåäàåòñÿ êëàññ, êîòîðûé èìååò êîíñòðóêòîð ñëåäóþùåãî ñîäåðæàíèÿ
* (String UserName, String Password, Integer poolSize)
*
*
* */
public SessionPoolConnector(Class<? extends PoolConnect> connectorClass, Integer poolSize){
this.connectorClass=connectorClass;
if(poolSize!=null){
this.poolSize=poolSize;
}
}
/** ïîëó÷èòü Connection íà îñíîâàíèè ââåäåííûõ äàííûõ
* @param userName èìÿ ïîëüçîâàòåëÿ
* @param password ïàðîëü
* @param sessionId óíèêàëüíûé íîìåð ñåññèè
* @return Connection
* @throws NoSuchMethodException
* @throws SecurityException
* */
public Connection getConnection(String userName,
String password,
String sessionId) {
Connection returnValue;
// ïðîâåðêà íàëè÷èÿ ñâÿçêè - "èìÿ ïîëüçîâàòåëÿ"-"POOL"
if(connectionUserPool.containsKey(userName)==true){
// Pool ñîçäàí ïî äàííîìó ïîëüçîâàòåëþ
if(connectionUserPool.get(userName).isPasswordEquals(password)){
returnValue=connectionUserPool.get(userName).getConnection();
this.userSession.put(userName, sessionId);
if(returnValue==null){
// âîçìîæíî ïðîèçîøëî èçìåíåíèå ïàðîëÿ - èëè æå âíåøíèå ôàêòîðû ïîâëèÿëè íà ñîåäèíåíèå (-ÿ)
this.removeSessionByUser(userName);
// ïîïûòàòüñÿ ñîçäàòü Pool ïî äàííîìó ïîëüçîâàòåëþ
try{
// ñîçäàòü îáúåêò è ïîëîæèòü åãî â õðàíèëèùå
Constructor<? extends PoolConnect> constructor=this.connectorClass.getConstructor(String.class,String.class,Integer.class);
PoolConnect newConnection=constructor.newInstance(userName, password, this.poolSize);
// ïîëó÷èòü Connection èç óêàçàííîãî Pool
returnValue=newConnection.getConnection();
if(returnValue==null){
throw new Exception();
}
this.connectionUserPool.put(userName, newConnection);
this.userSession.put(userName, sessionId);
}catch(Exception ex){
returnValue=null;
System.out.println("SessionPoolConnector Exception: "+ex.getMessage());
}
}
}else{
// Password is incorrect
// âîçìîæíî ïðîèçîøëî èçìåíåíèå ïàðîëÿ - èëè æå âíåøíèå ôàêòîðû ïîâëèÿëè íà ñîåäèíåíèå (-ÿ)
this.removeSessionByUser(userName);
// ïîïûòàòüñÿ ñîçäàòü Pool ïî äàííîìó ïîëüçîâàòåëþ
try{
// ñîçäàòü îáúåêò è ïîëîæèòü åãî â õðàíèëèùå
Constructor<? extends PoolConnect> constructor=this.connectorClass.getConstructor(String.class,String.class,Integer.class);
PoolConnect newConnection=constructor.newInstance(userName, password, this.poolSize);
// ïîëó÷èòü Connection èç óêàçàííîãî Pool
returnValue=newConnection.getConnection();
if(returnValue==null){
throw new Exception();
}
this.connectionUserPool.put(userName, newConnection);
this.userSession.put(userName, sessionId);
}catch(Exception ex){
returnValue=null;
System.out.println("SessionPoolConnector Exception: "+ex.getMessage());
}
}
}else{
// Pool íå ñîçäàí ïî äàííîìó îáúåêòó
try{
// ñîçäàòü îáúåêò è ïîëîæèòü åãî â õðàíèëèùå
Constructor<? extends PoolConnect> constructor=this.connectorClass.getConstructor(String.class,String.class,Integer.class);
PoolConnect newConnection=constructor.newInstance(userName, password, this.poolSize);
// ïîëó÷èòü Connection èç óêàçàííîãî Pool
returnValue=newConnection.getConnection();
if(returnValue==null){
throw new Exception();
}
this.connectionUserPool.put(userName, newConnection);
this.userSession.put(userName, sessionId);
}catch(Exception ex){
returnValue=null;
System.out.println("SessionPoolConnector Exception: "+ex.getMessage());
}
}
return returnValue;
}
/** âåðíóòü Connection â POOL */
public void closeConnection(Connection connection){
try{
connection.close();
}catch(Exception ex){
outError("closeConnection dropped:"+ex.getMessage());
}
}
/** Ïîëó÷èòü Connection èç POOL ïî óêàçàííîìó ñåññèîííîìó èäåíòèôèêàòîðó
* @param sessionId - óíèêàëüíûé íîìåð ñåññèè, ïî êîòîðîìó íóæíî ïîëó÷èòü Connection
* */
public Connection getConnection(String sessionId){
String user=this.userSession.getUser(sessionId);
if(user==null){
return null;
}else{
return this.connectionUserPool.get(user).getConnection();
}
}
public void removeSessionId(String sessionId){
String user=this.userSession.getUser(sessionId);
this.userSession.clear(sessionId);
if(this.userSession.hasSessionsByUser(user)==false){
try{
this.connectionUserPool.get(user).reset();
}catch(Exception ex){
// user:Pool is not finded
}
}
}
/** óäàëèòü ïî óêàçàííîìó ïîëüçîâàòåëþ âñå ñîåäèíåíèÿ */
public boolean removeSessionByUser(String userName){
boolean returnValue=true;
try{
this.connectionUserPool.get(userName).reset();
}catch(Exception ex){
System.err.println("SessionPoolConnector#removeSessionByUser UserName:"+userName+" close Exception:"+ex.getMessage());
returnValue=false;
}
return returnValue;
}
/** ïîëó÷èòü êîë-âî ïîëüçîâàòåëåé, ïî êîòîðûì ñîçäàí PoolConnection */
public int getUserPoolCount(){
return this.connectionUserPool.size();
}
/** ïîëó÷èòü èìåíà ïîëüçîâàòåëåé, ïî êîòîðûì ñîçäàíû ïóëû */
public Set<String> getUsersIntoPool(){
return this.connectionUserPool.keySet();
}
/** ïîëó÷èòü ïî óêàçàííîìó ïîëüçîâàòåëþ êîë-âî îòêðûòûõ ñîåäèíåíèé */
public int getConnectionCountByUser(String user){
return this.connectionUserPool.get(user).getConnectionCount();
}
public void printConnection(PrintStream out){
if(out==null){
out=System.out;
}
out.println("Connection:");
Set<String> users=getUsersIntoPool();
for(String user:users){
out.println("User:"+user+" ConnectionCount:"+getConnectionCountByUser(user));
}
out.println("-----------");
}
}