package com.wmc.demo.teiid; import java.io.PrintWriter; import java.net.InetAddress; import java.sql.DriverManager; import java.util.Properties; import javax.sql.DataSource; import org.apache.derby.drda.NetworkServerControl; import org.apache.derby.jdbc.ClientDataSource; import org.teiid.translator.ExecutionFactory; import org.teiid.translator.jdbc.derby.DerbyExecutionFactory; /** * Configure an in-memory Derby database for demo purposes */ public class DerbyProvider implements DemoDataProvider { NetworkServerControl db = null; private final String uid; private final String pwd; private final int port; private final String dbname; /** * Create an instance of a Derby database * @param instanceName the name of the DB schema, must be unique per VM * @param uid the admin userid * @param pwd the admin password * @param port the TCP port to listen for new connections * @throws Exception */ public DerbyProvider(String instanceName, String uid, String pwd, int port) throws Exception { this.dbname = "memory:" + instanceName; this.uid = uid; this.pwd = pwd; this.port = port; db = new NetworkServerControl(InetAddress.getLocalHost(), port, uid, pwd); db.start(new PrintWriter(System.out, true)); db.logConnections(true); // Create the database Properties props = new Properties(); props.put("user", uid); props.put("password", pwd); // Note, URL format is needed to create a new database DriverManager.getConnection("jdbc:derby://" + InetAddress.getLocalHost().getHostName() + ":" + port + "/" + dbname + ";create=true", props); System.out.println("Created Derby in-memory database"); } /** * Get a JDBC DataSource for this database instance */ @Override public DataSource getDataSource(boolean trace) throws Exception { ClientDataSource dds = new ClientDataSource(); dds.setServerName(InetAddress.getLocalHost().getHostName()); dds.setUser(uid); dds.setPassword(pwd); dds.setPortNumber(port); dds.setDatabaseName(dbname); if (trace) { dds.setLogWriter(new PrintWriter(System.out)); dds.setTraceLevel(ClientDataSource.TRACE_STATEMENT_CALLS); } return dds; } /** * Get the Teiid ExecutionFactory specific to this database vendor */ @Override public ExecutionFactory getExecutionFactory() { return new DerbyExecutionFactory(); } }