package examples.security.rdbmsrealm;
import examples.security.util.RealmProperties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
*/
public class LoadDDL
{
public static void main(String[] args)
throws Exception
{
System.out.println("Reading properties from \"" + RDBMSRealm.RDBMS_PROPS +
"\" ...");
RealmProperties props =
new RealmProperties(RDBMSRealm.class, RDBMSRealm.RDBMS_PROPS);
String driver = props.get(RDBMSDelegate.DRIVER);
System.out.println("Setting up driver \"" + driver + "\" ...");
Class.forName(driver).newInstance();
String url = props.get(RDBMSDelegate.DB_URL);
String user = props.get(RDBMSDelegate.DB_USER);
String passwd = props.get(RDBMSDelegate.DB_PASSWORD);
System.out.println("Connecting to URL \"" + url + "\" ...");
Connection conn = DriverManager.getConnection(url, user, passwd);
conn.setAutoCommit(true);
System.out.println("Executing DDL file \"" + RDBMSRealm.RDBMS_DDL +
"\" ...");
InputStream in = null;
try
{
in = new FileInputStream(RDBMSRealm.RDBMS_DDL);
}
catch (FileNotFoundException e)
{
// ignore
}
if (in == null)
{
in = LoadDDL.class.getResourceAsStream(RDBMSRealm.RDBMS_DDL);
}
if (in == null)
{
throw new FileNotFoundException("check current directory and classpath for " +
RDBMSRealm.RDBMS_DDL);
}
LineNumberReader ddl = new LineNumberReader(new InputStreamReader(in));
String line;
while ((line = ddl.readLine()) != null)
{
int pound;
if ((pound = line.indexOf('#')) != -1)
{
line = line.substring(0, pound);
}
line = line.trim();
if (line.length() == 0)
{
continue;
}
// Comment this next line out if your server requires statements
// to end with semicolons.
if (line.endsWith(";"))
{
line = line.substring(0, line.length() - 1);
}
Statement stmt = conn.createStatement();
try
{
stmt.execute(line);
}
catch (SQLException e)
{
System.err.println(RDBMSRealm.RDBMS_DDL + ":" + ddl.getLineNumber() +
": " + e.getMessage());
}
finally
{
stmt.close();
}
}
System.out.println("Closing DB connection ...");
conn.close();
System.out.println("... finished.");
}
}