public void init(Properties props) throws RepositoryConfigurationException, VCASException
{
final String sn = props.getProperty(JDBC_SOURCE_NAME_PARAM);
if (sn == null)
{
throw new RepositoryConfigurationException(JDBC_SOURCE_NAME_PARAM + " parameter expected!");
}
try
{
dataSource = (DataSource)new InitialContext().lookup(sn);
Connection conn = null;
Statement st = null;
try
{
conn = SecurityHelper.doPrivilegedSQLExceptionAction(new PrivilegedExceptionAction<Connection>()
{
public Connection run() throws Exception
{
return dataSource.getConnection();
}
});
DatabaseMetaData dbMetaData = conn.getMetaData();
String dialect = props.getProperty(JDBC_DIALECT_PARAM);
if (dialect == null || DBConstants.DB_DIALECT_AUTO.equalsIgnoreCase(dialect))
{
dialect = DialectDetecter.detect(dbMetaData);
}
this.dialect = dialect;
// init database metadata
String tn = props.getProperty(TABLE_NAME_PARAM);
if (tn != null)
{
tableName = tn;
}
else
{
tableName = DEFAULT_TABLE_NAME;
}
// make error pattern for DB2
String pattern = String.format(DB2_PK_CONSTRAINT_DETECT_PATTERN, tableName);
DB2_PK_CONSTRAINT_DETECT = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
sqlConstraintPK = tableName + "_PK";
sqlVCASIDX = tableName + "_IDX";
if (DBConstants.DB_DIALECT_PGSQL.equalsIgnoreCase(dialect)
|| DBConstants.DB_DIALECT_INGRES.equalsIgnoreCase(dialect))
{
// use lowercase for postgres/ingres metadata.getTable(), HSQLDB wants UPPERCASE
// for other seems not matter
tableName = tableName.toUpperCase().toLowerCase();
sqlConstraintPK = sqlConstraintPK.toUpperCase().toLowerCase();
sqlVCASIDX = sqlVCASIDX.toUpperCase().toLowerCase();
}
sqlAddRecord = "INSERT INTO " + tableName + " (PROPERTY_ID, ORDER_NUM, CAS_ID) VALUES(?,?,?)";
sqlDeleteRecord = "DELETE FROM " + tableName + " WHERE PROPERTY_ID=?";
sqlDeleteValueRecord = "DELETE FROM " + tableName + " WHERE PROPERTY_ID=? AND ORDER_NUM=?";
sqlSelectRecord = "SELECT CAS_ID FROM " + tableName + " WHERE PROPERTY_ID=? AND ORDER_NUM=?";
sqlSelectRecords = "SELECT CAS_ID, ORDER_NUM FROM " + tableName + " WHERE PROPERTY_ID=? ORDER BY ORDER_NUM";
sqlSelectOwnRecords =
"SELECT P.CAS_ID, P.ORDER_NUM, S.CAS_ID as SHARED_ID " + "FROM " + tableName + " P LEFT JOIN "
+ tableName + " S ON P.PROPERTY_ID<>S.PROPERTY_ID AND P.CAS_ID=S.CAS_ID "
+ "WHERE P.PROPERTY_ID=? GROUP BY P.CAS_ID, P.ORDER_NUM, S.CAS_ID ORDER BY P.ORDER_NUM";
sqlSelectSharingProps =
"SELECT DISTINCT C.PROPERTY_ID AS PROPERTY_ID FROM " + tableName + " C, " + tableName + " P "
+ "WHERE C.CAS_ID=P.CAS_ID AND C.PROPERTY_ID<>P.PROPERTY_ID AND P.PROPERTY_ID=?";
// init database objects
ResultSet trs = dbMetaData.getTables(null, null, tableName, null);
// check if table already exists
if (!trs.next())
{
st = conn.createStatement();
// create table
st.executeUpdate("CREATE TABLE " + tableName
+ " (PROPERTY_ID VARCHAR(96) NOT NULL, ORDER_NUM INTEGER NOT NULL, CAS_ID VARCHAR(512) NOT NULL, "
+ "CONSTRAINT " + sqlConstraintPK + " PRIMARY KEY(PROPERTY_ID, ORDER_NUM))");
// create index on hash (CAS_ID)
st.executeUpdate("CREATE INDEX " + sqlVCASIDX + " ON " + tableName + "(CAS_ID, PROPERTY_ID, ORDER_NUM)");
if (LOG.isDebugEnabled())
{
LOG.debug("JDBC Value Content Address Storage initialized in database " + sn);
}
}
else if (LOG.isDebugEnabled())
{
LOG.debug("JDBC Value Content Address Storage already initialized in database " + sn);
}
}
catch (SQLException e)
{
throw new VCASException("VCAS INIT database error: " + e, e);
}
finally
{
if (st != null)
{
try
{
st.close();
}
catch (SQLException e)
{
LOG.error("Can't close the Statement: " + e);
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (SQLException e)
{
throw new VCASException("VCAS INIT database error on Connection close: " + e, e);
}
}
}
}
catch (NamingException e)
{
throw new RepositoryConfigurationException("JDBC data source is not available in JNDI with name '" + sn
+ "'. Error: " + e);
}
}