Id package troika sso import java sql Connection import java sql Prepa

 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
/**
* $ Id: $
*/
package troika.sso;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import org.apache.commons.dbcp.BasicDataSource;
import org.joda.time.DateTime;
import troika.Application;
/**
* XXX javadocme.
* @author Vladimir Meremyanin
*/
public class SSOEngineImpl implements SSOEngine {
private BasicDataSource datasource;
public boolean checkSsoId(final String ssoId) {
try {
final Connection connection = getConnection();
try {
return hasRecord(connection, ssoId);
} finally {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private boolean hasRecord(final Connection connection, final String ssoId) throws Exception {
final PreparedStatement ps = connection.prepareStatement("SELECT count(*) FROM XT_SSO_LOGIN WHERE M_ID = ? AND XM_LAST_ACCESS > ?");
try {
// set sso-id
ps.setString(1, ssoId);
// set session timeout
final DateTime sessionExpiration = new DateTime().minusMinutes(30);
ps.setTimestamp(2, new Timestamp(sessionExpiration.getMillis()));
// execute
final ResultSet rs = ps.executeQuery();
try {
final boolean result;
if (rs != null && rs.next()) {
result = rs.getInt(1) > 0; // XXX == 1 ?
} else {
result = false;
}
return result;
} finally {
rs.close();
}
} finally {
ps.close();
}
}
// connections
/**
* @return
* @throws SQLException
*/
private Connection getConnection() throws Exception {
if (datasource == null) {
final String dbDriver = Application.sSSODbDriver;
final String dbUrl = Application.sSSODbUrl;
final String dbUser = Application.sSSODbUser;
final String dbPassword = Application.sSSODbPassword;
datasource = new BasicDataSource();
datasource.setDriverClassName(dbDriver);
datasource.setUrl(dbUrl);
datasource.setUsername(dbUser);
datasource.setPassword(dbPassword);
datasource.addConnectionProperty("removeAbandoned", "true");
datasource.addConnectionProperty("removeAbandonedTimeout", "333");
}
final Connection connection = datasource.getConnection();
return connection;
}
}