Commit eb0cf237 authored by Andre Freyssinet's avatar Andre Freyssinet

Allows configuration of database name through superclass.

parent 022cd2fb
/*
* Copyright (C) 2007 - 2008 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package fr.dyade.aaa.util;
import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* The DerbyDBTransaction class implements a transactionnal storage through
* a Derby embeded database.
*
* @see Transaction
*/
public final class DerbyDBTransaction extends DBTransaction {
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String connurl = "jdbc:derby:";
protected void initDB() throws IOException {
try {
Class.forName(driver).newInstance();
Properties props = new Properties();
props.put("user", "user1");
props.put("password", "user1");
conn = DriverManager.getConnection(connurl + new File(dir, "JoramDB").getPath() + ";create=true", props);
} catch (IllegalAccessException exc) {
throw new IOException(exc.getMessage());
} catch (ClassNotFoundException exc) {
throw new IOException(exc.getMessage());
} catch (InstantiationException exc) {
throw new IOException(exc.getMessage());
} catch (SQLException sqle) {
throw new IOException(sqle.getMessage());
}
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
// We create the table.
s.execute("CREATE TABLE JoramDB (name VARCHAR(256), content LONG VARCHAR FOR BIT DATA, PRIMARY KEY(name))");
s.close();
conn.commit();
} catch (SQLException sqle) {
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "DBTransaction, init() DB already exists");
}
}
}
/*
* Copyright (C) 2007 - 2019 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package fr.dyade.aaa.util;
import java.io.File;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* The DerbyDBTransaction class implements a transactionnal storage through
* a Derby embeded database.
*
* @see Transaction
*/
public final class DerbyDBTransaction extends DBTransaction {
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String connurl = "jdbc:derby:";
protected void initDB() throws IOException {
try {
Class.forName(driver).newInstance();
Properties props = new Properties();
props.put("user", "user1");
props.put("password", "user1");
conn = DriverManager.getConnection(connurl + new File(dir, "JoramDB").getPath() + ";create=true", props);
} catch (IllegalAccessException exc) {
throw new IOException(exc.getMessage());
} catch (ClassNotFoundException exc) {
throw new IOException(exc.getMessage());
} catch (InstantiationException exc) {
throw new IOException(exc.getMessage());
} catch (SQLException sqle) {
throw new IOException(sqle.getMessage());
}
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
// We create the table.
s.execute("CREATE TABLE " + dbtable + " (name VARCHAR(256), content LONG VARCHAR FOR BIT DATA, PRIMARY KEY(name))");
s.close();
conn.commit();
} catch (SQLException sqle) {
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "DBTransaction, init() DB already exists");
}
}
}
/*
* Copyright (C) 2007 - 2018 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package fr.dyade.aaa.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;
import org.objectweb.util.monolog.api.BasicLevel;
import fr.dyade.aaa.agent.AgentServer;
/**
* The MySQLDBTransaction class implements a transactionnal storage through
* a MySQL database.
*
* @see Transaction
*/
public final class MySQLDBTransaction extends DBTransaction {
private String driver = "com.mysql.jdbc.Driver";
private String connurl = "jdbc:mysql:";
protected void initDB() throws IOException {
String configFile = AgentServer.getProperty("MySQLDBTransactionConfigFile", "MySQL.properties");
Properties prop = new Properties();
try {
prop.load(new FileInputStream(configFile));
} catch (FileNotFoundException e) {
if (logmon.isLoggable(BasicLevel.INFO)) {
logmon.log(BasicLevel.INFO, "File " + configFile + " not found, using default parameters");
}
}
String dbHost = prop.getProperty("host", "localhost");
String dbPort = prop.getProperty("port", "3306");
String dbName = prop.getProperty("database", "joramdb");
String dbUser = prop.getProperty("user", "joram");
String dbPass = prop.getProperty("password", "joram");
try {
Class.forName(driver).newInstance();
StringBuffer sb = new StringBuffer(connurl);
sb.append("//");
sb.append(dbHost);
sb.append(':');
sb.append(dbPort);
sb.append('/');
sb.append(dbName);
sb.append("?user=");
sb.append(dbUser);
sb.append("&password=");
sb.append(dbPass);
Enumeration enu = prop.keys();
while (enu.hasMoreElements()) {
String key = (String)enu.nextElement();
if (key.equals("host") || key.equals("port") || key.equals("database") || key.equals("user") || key.equals("password")) {
continue;
}
sb.append('&');
sb.append(key);
sb.append('=');
sb.append(prop.get(key));
}
conn = DriverManager.getConnection(sb.toString());
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new IOException(e.getMessage());
} catch (InstantiationException e) {
throw new IOException(e.getMessage());
} catch (IllegalAccessException e) {
throw new IOException(e.getMessage());
} catch (ClassNotFoundException e) {
throw new IOException(e.getMessage());
}
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
// We create the table.
s.execute("CREATE TABLE JoramDB (name VARCHAR(255), content LONGBLOB, PRIMARY KEY(name))");
s.close();
conn.commit();
} catch (SQLException sqle) {
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "DBTransaction, init() DB already exists");
}
}
}
/*
* Copyright (C) 2007 - 2019 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Initial developer(s): ScalAgent Distributed Technologies
* Contributor(s):
*/
package fr.dyade.aaa.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;
import org.objectweb.util.monolog.api.BasicLevel;
import fr.dyade.aaa.agent.AgentServer;
/**
* The MySQLDBTransaction class implements a transactionnal storage through
* a MySQL database.
*
* @see Transaction
*/
public final class MySQLDBTransaction extends DBTransaction {
private String driver = "com.mysql.jdbc.Driver";
private String connurl = "jdbc:mysql:";
protected void initDB() throws IOException {
String configFile = AgentServer.getProperty("MySQLDBTransactionConfigFile", "MySQL.properties");
Properties prop = new Properties();
try {
prop.load(new FileInputStream(configFile));
} catch (FileNotFoundException e) {
if (logmon.isLoggable(BasicLevel.INFO)) {
logmon.log(BasicLevel.INFO, "File " + configFile + " not found, using default parameters");
}
}
String dbHost = prop.getProperty("host", "localhost");
String dbPort = prop.getProperty("port", "3306");
String dbName = prop.getProperty("database", "joramdb");
String dbUser = prop.getProperty("user", "joram");
String dbPass = prop.getProperty("password", "joram");
try {
Class.forName(driver).newInstance();
StringBuffer sb = new StringBuffer(connurl);
sb.append("//");
sb.append(dbHost);
sb.append(':');
sb.append(dbPort);
sb.append('/');
sb.append(dbName);
sb.append("?user=");
sb.append(dbUser);
sb.append("&password=");
sb.append(dbPass);
Enumeration enu = prop.keys();
while (enu.hasMoreElements()) {
String key = (String)enu.nextElement();
if (key.equals("host") || key.equals("port") || key.equals("database") || key.equals("user") || key.equals("password")) {
continue;
}
sb.append('&');
sb.append(key);
sb.append('=');
sb.append(prop.get(key));
}
conn = DriverManager.getConnection(sb.toString());
conn.setAutoCommit(false);
} catch (SQLException e) {
throw new IOException(e.getMessage());
} catch (InstantiationException e) {
throw new IOException(e.getMessage());
} catch (IllegalAccessException e) {
throw new IOException(e.getMessage());
} catch (ClassNotFoundException e) {
throw new IOException(e.getMessage());
}
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
// We create the table.
s.execute("CREATE TABLE " + dbtable + " (name VARCHAR(255), content LONGBLOB, PRIMARY KEY(name))");
s.close();
conn.commit();
} catch (SQLException sqle) {
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "DBTransaction, init() DB already exists");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment