Element initMode = DOMUtils.findChildByName(config, new QName(JDBC_NS, "init-mode"));
if (jndiRef != null) {
String refname = jndiRef.getTextContent().trim();
ds = _dataSources.get(refname);
if (ds == null)
throw new ExternalVariableModuleException("Data source reference \"" + refname
+ "\" not found for external variable " + evarId
+ "; make sure to register the data source with the engine!");
} else if (jndiDs != null) {
String name = jndiDs.getTextContent().trim();
Object dsCandidate;
InitialContext ctx;
try {
ctx = new InitialContext();
} catch (Exception ex) {
throw new ExternalVariableModuleException("Unable to access JNDI context for external variable " + evarId, ex);
}
try {
dsCandidate = ctx.lookup(name);
} catch (Exception ex) {
throw new ExternalVariableModuleException("Lookup of data source for " + evarId + " failed.", ex);
} finally {
try {
ctx.close();
} catch (NamingException e) {
;
;
}
}
if (dsCandidate == null)
throw new ExternalVariableModuleException("Data source \"" + name + "\" not found in JNDI!");
if (!(dsCandidate instanceof DataSource))
throw new ExternalVariableModuleException("JNDI object \"" + name + "\" does not implement javax.sql.DataSource");
ds = (DataSource) dsCandidate;
}
if (ds == null) {
throw new ExternalVariableModuleException("No valid data source configuration for JDBC external varible " + evarId);
}
Connection conn;
DatabaseMetaData metaData;
try {
conn = ds.getConnection();
metaData = conn.getMetaData();
} catch (Exception ex) {
throw new ExternalVariableModuleException("Unable to open database connection for external variable " + evarId, ex);
}
try {
DbExternalVariable dbev = new DbExternalVariable(evarId, ds);
if (initMode != null)
try {
dbev._initType = InitType.valueOf(initMode.getTextContent().trim());
} catch (Exception ex) {
throw new ExternalVariableModuleException("Invalid <init-mode> value: " + initMode.getTextContent().trim());
}
Element tableName = DOMUtils.findChildByName(config, new QName(JDBC_NS, "table"));
if (tableName == null || tableName.getTextContent().trim().equals(""))
throw new ExternalVariableModuleException("Must specify <table> for external variable " + evarId);
String table = tableName.getTextContent().trim();
String schema = null;
if (table.indexOf('.') != -1) {
schema = table.substring(0, table.indexOf('.'));
table = table.substring(table.indexOf('.') + 1);
}
if (metaData.storesLowerCaseIdentifiers()) {
table = table.toLowerCase();
if (schema != null)
schema = table.toLowerCase();
} else if (metaData.storesUpperCaseIdentifiers()) {
table = table.toUpperCase();
if (schema != null)
schema = schema.toUpperCase();
}
dbev.generatedKeys = metaData.supportsGetGeneratedKeys();
ResultSet tables = metaData.getTables(null, schema, table, null);
if (tables.next()) {
dbev.table = tables.getString("TABLE_NAME");
dbev.schema = tables.getString("TABLE_SCHEM");
} else
throw new ExternalVariableModuleException("Table \"" + table + "\" not found in database.");
tables.close();
List<Element> columns = DOMUtils.findChildrenByName(config, new QName(JDBC_NS, "column"));
for (Element col : columns) {
String name = col.getAttribute("name");
String colname = col.getAttribute("column-name");
String key = col.getAttribute("key");
String gentype = col.getAttribute("generator");
String expression = col.getAttribute("expression");
if (key == null || "".equals(key))
key = "no";
if (gentype == null || "".equals(gentype))
gentype = GenType.none.toString();
if (colname == null || "".equals(colname))
colname = name;
if (name == null || "".equals(name))
throw new ExternalVariableModuleException("External variable " + evarId
+ " <column> element must have \"name\" attribute. ");
if (metaData.storesLowerCaseIdentifiers())
colname = colname.toLowerCase();
else if (metaData.storesUpperCaseIdentifiers())
colname = colname.toUpperCase();
GenType gtype;
try {
gtype = GenType.valueOf(gentype);
} catch (Exception ex) {
throw new ExternalVariableModuleException("External variable " + evarId + " column \"" + name
+ "\" generator type \"" + gentype + "\" is unknown.");
}
if (gtype == GenType.expression && (expression == null || "".equals(expression)))
throw new ExternalVariableModuleException("External variable " + evarId + " column \"" + name
+ "\" used \"expression\" generator, but did not specify an expression");
Column c = dbev.new Column(name, colname, key.equalsIgnoreCase("yes"), gtype, expression);
ResultSet cmd = metaData.getColumns(null, dbev.schema, dbev.table, colname);
if (cmd.next()) {
c.dataType = cmd.getInt("DATA_TYPE");
c.nullok = cmd.getInt("NULLABLE") != 0;
} else
throw new ExternalVariableModuleException("External variable " + evarId + " referenced "
+ "non-existant column \"" + colname + "\"!");
dbev.addColumn(c);
}
if (dbev.numColumns() == 0)
throw new ExternalVariableModuleException("External variable " + evarId + " did not have any <column> elements!");
_vars.put(evarId, dbev);
} catch (SQLException se) {
throw new ExternalVariableModuleException("SQL Error", se);
} finally {
try {
conn.close();
} catch (SQLException e) {
}