public InputStream getInputStream() throws IOException, ProcessingException {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Opening stream for datasource " + this.datasourceName +
", table " + this.tableName + ", column " + this.columnName +
(this.condition == null ? ", no condition" : ", condition " + this.condition)
);
}
Connection cnx = null;
Statement stmt = null;
try {
cnx = getConnection();
stmt = cnx.createStatement();
StringBuffer selectBuf = new StringBuffer("SELECT ").append(this.columnName).
append(" FROM ").append(this.tableName);
if (this.condition != null) {
selectBuf.append(" WHERE ").append(this.condition);
}
String select = selectBuf.toString();
if (getLogger().isDebugEnabled()) {
getLogger().debug("Executing statement " + select);
}
ResultSet rs = stmt.executeQuery(select);
rs.next();
int colType = rs.getMetaData().getColumnType(1);
switch(colType) {
case Types.BLOB :
Blob blob = rs.getBlob(1);
return new JDBCInputStream(blob.getBinaryStream(), cnx);
//break;
case Types.CLOB :
Clob clob = rs.getClob(1);
return new JDBCInputStream(clob.getAsciiStream(), cnx);
//break;
case Types.LONGVARBINARY :
case Types.VARBINARY :
return new JDBCInputStream(rs.getBinaryStream(1), cnx);
//break;
default :
String value = rs.getString(1);
stmt.close();
rs.close();
cnx.close();
return new ByteArrayInputStream(value.getBytes());
}
} catch(SQLException sqle) {
String msg = "Cannot retrieve content from " + this.systemId;
getLogger().error(msg, sqle);
try {
if (cnx != null) {
cnx.close();
}
} catch(SQLException sqle2) {
// PITA
throw new ProcessingException("Cannot close connection", sqle2);
}
// IOException would be more adequate, but ProcessingException is cascaded...
throw new ProcessingException(msg, sqle);
}
}
Get the input stream for this source. |