Initialize the mailet
String mappingsURL = getInitParameter("mappings");
String datasourceName = mappingsURL.substring(5);
int pos = datasourceName.indexOf("/");
String tableName = datasourceName.substring(pos + 1);
datasourceName = datasourceName.substring(0, pos);
Connection conn = null;
if (getInitParameter("source_column") == null) {
throw new MailetException("source_column not specified for JDBCAlias");
}
if (getInitParameter("target_column") == null) {
throw new MailetException("target_column not specified for JDBCAlias");
}
try {
ServiceManager componentManager = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
// Get the DataSourceSelector service
DataSourceSelector datasources = (DataSourceSelector)componentManager.lookup(DataSourceSelector.ROLE);
// Get the data-source required.
datasource = (DataSourceComponent)datasources.select(datasourceName);
conn = datasource.getConnection();
// Check if the required table exists. If not, complain.
DatabaseMetaData dbMetaData = conn.getMetaData();
// Need to ask in the case that identifiers are stored, ask the DatabaseMetaInfo.
// Try UPPER, lower, and MixedCase, to see if the table is there.
if (!(theJDBCUtil.tableExists(dbMetaData, tableName))) {
StringBuffer exceptionBuffer =
new StringBuffer(128)
.append("Could not find table '")
.append(tableName)
.append("' in datasource '")
.append(datasourceName)
.append("'");
throw new MailetException(exceptionBuffer.toString());
}
//Build the query
StringBuffer queryBuffer =
new StringBuffer(128)
.append("SELECT ")
.append(getInitParameter("target_column"))
.append(" FROM ")
.append(tableName)
.append(" WHERE ")
.append(getInitParameter("source_column"))
.append(" = ?");
query = queryBuffer.toString();
} catch (MailetException me) {
throw me;
} catch (Exception e) {
throw new MessagingException("Error initializing JDBCAlias", e);
} finally {
theJDBCUtil.closeJDBCConnection(conn);
}