1 package net.sourceforge.backpedal.impl.db;
2
3 import net.sourceforge.backpedal.api.db.ConnectionFactory;
4
5 import java.sql.Connection;
6 import java.sql.Driver;
7 import java.sql.SQLException;
8 import java.util.Properties;
9
10 import net.mikehogan.veggie.exceptions.VeggieSystemException;
11
12 public class ConnectionFactoryImpl implements ConnectionFactory {
13 private String driverName = null;
14 private String connectUrl = null;
15 private final Properties properties;
16
17 public ConnectionFactoryImpl() {
18 properties = new Properties();
19 }
20
21 private boolean configured() {
22 return driverName != null && connectUrl != null;
23 }
24
25 public void setDriverName(String driverName) {
26 this.driverName = driverName;
27 }
28
29 public void setConnectUrl(String connectUrl) {
30 this.connectUrl = connectUrl;
31 }
32
33 public void addProperty(final String s) {
34 if (s != null && !"".equals(s)) {
35 final int indexOfEquals = s.indexOf("=");
36 final String name = s.substring(0, indexOfEquals);
37 final String value = s.substring(indexOfEquals + 1);
38 properties.setProperty(name, value);
39 }
40 }
41
42 public Connection getConnection() throws SQLException {
43 if (!configured()) {
44 throw new VeggieSystemException("Connection factory is not configured");
45 }
46 Connection result = null;
47 try {
48 result = loadConnection();
49 } catch (Exception e) {
50 throw new SQLException(e.getMessage());
51 }
52 return result;
53 }
54
55 private Connection loadConnection() throws Exception {
56 final Class c = Class.forName(driverName);
57 final Driver driver = (Driver) c.newInstance();
58 return driver.connect(connectUrl, properties);
59 }
60 }
This page was automatically generated by Maven