001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.mysql;
007:
008: import org.apache.commons.dbcp.BasicDataSource;
009: import org.openrdf.sail.SailException;
010: import org.openrdf.sail.rdbms.RdbmsStore;
011: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
012:
013: /**
014: * A convenient way to initialise a MySql RDF store.
015: *
016: * @author James Leigh
017: *
018: */
019: public class MySqlStore extends RdbmsStore {
020: private String name = genName();
021: private String serverName = "localhost";
022: private String databaseName;
023: private int portNumber;
024: private String user;
025: private String password;
026:
027: public MySqlStore() {
028: super ();
029: }
030:
031: public MySqlStore(String databaseName) {
032: setDatabaseName(databaseName);
033: }
034:
035: public String getName() {
036: return name;
037: }
038:
039: public void setName(String name) {
040: this .name = name;
041: }
042:
043: public String getServerName() {
044: return serverName;
045: }
046:
047: public void setServerName(String serverName) {
048: this .serverName = serverName;
049: }
050:
051: public String getDatabaseName() {
052: return databaseName;
053: }
054:
055: public void setDatabaseName(String databaseName) {
056: this .databaseName = databaseName;
057: }
058:
059: public int getPortNumber() {
060: return portNumber;
061: }
062:
063: public void setPortNumber(int portNumber) {
064: this .portNumber = portNumber;
065: }
066:
067: public String getUser() {
068: return user;
069: }
070:
071: public void setUser(String user) {
072: this .user = user;
073: }
074:
075: public String getPassword() {
076: return password;
077: }
078:
079: public void setPassword(String password) {
080: this .password = password;
081: }
082:
083: @Override
084: public void initialize() throws SailException {
085: try {
086: Class.forName("com.mysql.jdbc.Driver");
087: } catch (ClassNotFoundException e) {
088: throw new RdbmsException(e.toString(), e);
089: }
090: StringBuilder url = new StringBuilder();
091: url.append("jdbc:mysql:");
092: if (serverName != null) {
093: url.append("//").append(serverName);
094: if (portNumber > 0) {
095: url.append(":").append(portNumber);
096: }
097: url.append("/");
098: }
099: url.append(databaseName);
100: url.append("?useUnicode=yes&characterEncoding=UTF-8");
101: BasicDataSource ds = new BasicDataSource();
102: ds.setUrl(url.toString());
103: if (user != null) {
104: ds.setUsername(user);
105: } else {
106: ds.setUsername(System.getProperty("user.name"));
107: }
108: if (password != null) {
109: ds.setPassword(password);
110: }
111: MySqlConnectionFactory factory = new MySqlConnectionFactory();
112: factory.setSail(this );
113: factory.setDataSource(ds);
114: setConnectionFactory(factory);
115: super .initialize();
116: }
117:
118: private String genName() {
119: String hex = Integer.toHexString(System.identityHashCode(this ));
120: return getClass().getSimpleName() + "#" + hex;
121: }
122: }
|