This commit is contained in:
Robin Kupper
2011-04-26 22:15:29 +02:00
parent 2cbf332059
commit 6a8d6dbd5a
3 changed files with 51 additions and 29 deletions

View File

@@ -152,7 +152,7 @@ public class Config {
final List<String> worldTables = config.getStringList("tables", null); final List<String> worldTables = config.getStringList("tables", null);
tables = new HashMap<Integer, String>(); tables = new HashMap<Integer, String>();
if (worldNames == null || worldTables == null || worldNames.size() == 0 || worldNames.size() != worldTables.size()) if (worldNames == null || worldTables == null || worldNames.size() == 0 || worldNames.size() != worldTables.size())
throw new Exception("worldNames or worldTables not set porperly"); throw new Exception("worldNames or worldTables not set properly");
for (int i = 0; i < worldNames.size(); i++) { for (int i = 0; i < worldNames.size(); i++) {
tables.put(worldNames.get(i).hashCode(), worldTables.get(i)); tables.put(worldNames.get(i).hashCode(), worldTables.get(i));
} }

View File

@@ -41,7 +41,7 @@ public class ConnectionPool implements Driver {
public static final String URL_PREFIX = "jdbc:jdc:"; public static final String URL_PREFIX = "jdbc:jdc:";
private static final int MAJOR_VERSION = 1; private static final int MAJOR_VERSION = 1;
private static final int MINOR_VERSION = 0; private static final int MINOR_VERSION = 0;
private ConnectionService pool; private final ConnectionService pool;
public ConnectionPool(String driver, String url, String user, String password) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { public ConnectionPool(String driver, String url, String user, String password) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
DriverManager.registerDriver(this); DriverManager.registerDriver(this);
@@ -49,42 +49,48 @@ public class ConnectionPool implements Driver {
pool = new ConnectionService(url, user, password); pool = new ConnectionService(url, user, password);
} }
@Override
public Connection connect(String url, Properties props) throws SQLException { public Connection connect(String url, Properties props) throws SQLException {
if (!url.startsWith(URL_PREFIX)) if (!url.startsWith(URL_PREFIX))
return null; return null;
return pool.getConnection(); return pool.getConnection();
} }
@Override
public boolean acceptsURL(String url) { public boolean acceptsURL(String url) {
return url.startsWith(URL_PREFIX); return url.startsWith(URL_PREFIX);
} }
@Override
public int getMajorVersion() { public int getMajorVersion() {
return MAJOR_VERSION; return MAJOR_VERSION;
} }
@Override
public int getMinorVersion() { public int getMinorVersion() {
return MINOR_VERSION; return MINOR_VERSION;
} }
@Override
public DriverPropertyInfo[] getPropertyInfo(String str, Properties props) { public DriverPropertyInfo[] getPropertyInfo(String str, Properties props) {
return new DriverPropertyInfo[0]; return new DriverPropertyInfo[0];
} }
@Override
public boolean jdbcCompliant() { public boolean jdbcCompliant() {
return false; return false;
} }
private class JDCConnection implements Connection private class JDCConnection implements Connection
{ {
private Connection conn; private final Connection conn;
private boolean inuse; private boolean inuse;
private long timestamp; private long timestamp;
public JDCConnection(Connection conn) { public JDCConnection(Connection conn) {
this.conn = conn; this.conn = conn;
this.inuse = false; inuse = false;
this.timestamp = 0; timestamp = 0;
} }
public synchronized boolean lease() { public synchronized boolean lease() {
@@ -100,7 +106,7 @@ public class ConnectionPool implements Driver {
public boolean validate() { public boolean validate() {
try { try {
conn.getMetaData(); conn.getMetaData();
} catch (Exception e) { } catch (final Exception e) {
return false; return false;
} }
return true; return true;
@@ -114,6 +120,7 @@ public class ConnectionPool implements Driver {
return timestamp; return timestamp;
} }
@Override
public void close() { public void close() {
expireLease(); expireLease();
} }
@@ -122,74 +129,92 @@ public class ConnectionPool implements Driver {
inuse = false; inuse = false;
} }
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException { public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql); return conn.prepareStatement(sql);
} }
@Override
public CallableStatement prepareCall(String sql) throws SQLException { public CallableStatement prepareCall(String sql) throws SQLException {
return conn.prepareCall(sql); return conn.prepareCall(sql);
} }
@Override
public Statement createStatement() throws SQLException { public Statement createStatement() throws SQLException {
return conn.createStatement(); return conn.createStatement();
} }
@Override
public String nativeSQL(String sql) throws SQLException { public String nativeSQL(String sql) throws SQLException {
return conn.nativeSQL(sql); return conn.nativeSQL(sql);
} }
@Override
public void setAutoCommit(boolean autoCommit) throws SQLException { public void setAutoCommit(boolean autoCommit) throws SQLException {
conn.setAutoCommit(autoCommit); conn.setAutoCommit(autoCommit);
} }
@Override
public boolean getAutoCommit() throws SQLException { public boolean getAutoCommit() throws SQLException {
return conn.getAutoCommit(); return conn.getAutoCommit();
} }
@Override
public void commit() throws SQLException { public void commit() throws SQLException {
conn.commit(); conn.commit();
} }
@Override
public void rollback() throws SQLException { public void rollback() throws SQLException {
conn.rollback(); conn.rollback();
} }
@Override
public boolean isClosed() throws SQLException { public boolean isClosed() throws SQLException {
return conn.isClosed(); return conn.isClosed();
} }
@Override
public DatabaseMetaData getMetaData() throws SQLException { public DatabaseMetaData getMetaData() throws SQLException {
return conn.getMetaData(); return conn.getMetaData();
} }
@Override
public void setReadOnly(boolean readOnly) throws SQLException { public void setReadOnly(boolean readOnly) throws SQLException {
conn.setReadOnly(readOnly); conn.setReadOnly(readOnly);
} }
@Override
public boolean isReadOnly() throws SQLException { public boolean isReadOnly() throws SQLException {
return conn.isReadOnly(); return conn.isReadOnly();
} }
@Override
public void setCatalog(String catalog) throws SQLException { public void setCatalog(String catalog) throws SQLException {
conn.setCatalog(catalog); conn.setCatalog(catalog);
} }
@Override
public String getCatalog() throws SQLException { public String getCatalog() throws SQLException {
return conn.getCatalog(); return conn.getCatalog();
} }
@Override
public void setTransactionIsolation(int level) throws SQLException { public void setTransactionIsolation(int level) throws SQLException {
conn.setTransactionIsolation(level); conn.setTransactionIsolation(level);
} }
@Override
public int getTransactionIsolation() throws SQLException { public int getTransactionIsolation() throws SQLException {
return conn.getTransactionIsolation(); return conn.getTransactionIsolation();
} }
@Override
public SQLWarning getWarnings() throws SQLException { public SQLWarning getWarnings() throws SQLException {
return conn.getWarnings(); return conn.getWarnings();
} }
@Override
public void clearWarnings() throws SQLException { public void clearWarnings() throws SQLException {
conn.clearWarnings(); conn.clearWarnings();
} }
@@ -220,16 +245,13 @@ public class ConnectionPool implements Driver {
} }
@Override @Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
throws SQLException {
return conn.createStatement(resultSetType, resultSetConcurrency); return conn.createStatement(resultSetType, resultSetConcurrency);
} }
@Override @Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
int resultSetHoldability) throws SQLException { return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
return conn
.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
} }
@Override @Override
@@ -344,10 +366,10 @@ public class ConnectionPool implements Driver {
public class ConnectionService public class ConnectionService
{ {
private Vector<JDCConnection> connections; private final Vector<JDCConnection> connections;
private String url, user, password; private final String url, user, password;
final private long timeout = 60000; final private long timeout = 60000;
private ConnectionReaper reaper; private final ConnectionReaper reaper;
final private int poolsize = 10; final private int poolsize = 10;
public ConnectionService(String url, String user, String password) { public ConnectionService(String url, String user, String password) {
@@ -360,19 +382,19 @@ public class ConnectionPool implements Driver {
} }
public synchronized void reapConnections() { public synchronized void reapConnections() {
long stale = System.currentTimeMillis() - timeout; final long stale = System.currentTimeMillis() - timeout;
Enumeration<JDCConnection> connlist = connections.elements(); final Enumeration<JDCConnection> connlist = connections.elements();
while ((connlist != null) && (connlist.hasMoreElements())) { while (connlist != null && connlist.hasMoreElements()) {
JDCConnection conn = connlist.nextElement(); final JDCConnection conn = connlist.nextElement();
if ((conn.inUse()) && (stale > conn.getLastUse()) && (!conn.validate())) if (conn.inUse() && stale > conn.getLastUse() && !conn.validate())
removeConnection(conn); removeConnection(conn);
} }
} }
public synchronized void closeConnections() { public synchronized void closeConnections() {
Enumeration<JDCConnection> connlist = connections.elements(); final Enumeration<JDCConnection> connlist = connections.elements();
while ((connlist != null) && (connlist.hasMoreElements())) { while (connlist != null && connlist.hasMoreElements()) {
JDCConnection conn = connlist.nextElement(); final JDCConnection conn = connlist.nextElement();
removeConnection(conn); removeConnection(conn);
} }
} }
@@ -405,7 +427,7 @@ public class ConnectionPool implements Driver {
class ConnectionReaper extends Thread class ConnectionReaper extends Thread
{ {
private ConnectionService pool; private final ConnectionService pool;
private final long delay = 300000; private final long delay = 300000;
ConnectionReaper(ConnectionService pool) { ConnectionReaper(ConnectionService pool) {
@@ -417,8 +439,8 @@ public class ConnectionPool implements Driver {
while (true) { while (true) {
try { try {
Thread.sleep(delay); Thread.sleep(delay);
} catch (InterruptedException e) {} } catch (final InterruptedException e) {}
pool.reapConnections(); pool.reapConnections();
} }
} }
} }

View File

@@ -15,9 +15,9 @@ public class Download
if (file.exists()) if (file.exists())
file.delete(); file.delete();
file.createNewFile(); file.createNewFile();
InputStream in = u.openStream(); final InputStream in = u.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); final OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
byte[] buffer = new byte[1024]; final byte[] buffer = new byte[1024];
int len; int len;
while ((len = in.read(buffer)) >= 0) { while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len); out.write(buffer, 0, len);