--- /dev/null
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mir.producer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.sql.*;
+
+
+import mir.log.LoggerWrapper;
+import mir.util.ExceptionFunctions;
+import mir.util.ParameterExpander;
+
+public class ExternalDbProducerNode extends ProducerNodeDecorator {
+ private String key;
+ private String driver;
+ private String host;
+ private String port;
+ private String database;
+ private String username;
+ private String password;
+ private String query;
+
+
+ public ExternalDbProducerNode(String aKey, String aDriver, String aHost, String aPort, String aDatabase, String aUsername, String aPassword,String aQuery,ProducerNode aSubNode) {
+ super(aSubNode);
+ key = aKey;
+ driver = aDriver;
+ host = aHost;
+ port = aPort;
+ database = aDatabase;
+ username = aUsername;
+ password = aPassword;
+ query =aQuery;
+
+ }
+
+ public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {
+ try {
+ String expandedKey = ParameterExpander.expandExpression( aValueMap, key );
+ String expandedDriver = ParameterExpander.expandExpression( aValueMap, driver);
+ String expandedHost = ParameterExpander.expandExpression( aValueMap, host);
+ String expandedPort = ParameterExpander.expandExpression( aValueMap, port);
+ String expandedDatabase = ParameterExpander.expandExpression( aValueMap, database);
+ String expandedUsername = ParameterExpander.expandExpression( aValueMap, username);
+ String expandedPassword = ParameterExpander.expandExpression( aValueMap, password);
+ String expandedQuery = ParameterExpander.expandExpression( aValueMap, query);
+
+ if (expandedDriver.equals("postgresql")){
+ Class.forName("org.postgresql.Driver");
+ }
+ if (expandedDriver.equals("mysql")){
+ Class.forName("com.mysql.jdbc.Driver");
+ }
+ else {
+ throw new Exception("Unsupported DB Driver:"+expandedDriver);
+ }
+
+ Connection db = DriverManager.getConnection("jdbc:"+expandedDriver+"://"+expandedHost
+ +":"+expandedPort+"/"+expandedDatabase
+ , expandedUsername, expandedPassword);
+
+ Statement st = db.createStatement();
+ ResultSet rs = st.executeQuery(expandedQuery);
+ ResultSetMetaData rsmd = rs.getMetaData();
+ int numberOfColumns= rsmd.getColumnCount();
+ ArrayList fieldNames = new ArrayList(numberOfColumns);
+ for (int i=0;i<numberOfColumns;i++){
+ fieldNames.add(rsmd.getColumnName(i+1));
+ }
+
+ while(rs.next() && !isAborted(aValueMap)) {
+ HashMap result=new HashMap();
+ Iterator fields = fieldNames.iterator();
+ while (fields.hasNext()) {
+ String field=(String) fields.next();
+ result.put(field,rs.getString(field));
+ }
+ ParameterExpander.setValueForKey(aValueMap,expandedKey,result);
+ super.produce(aValueMap, aVerb, aLogger);
+ }
+ rs.close();
+ st.close();
+ db.close();
+ }
+ catch (Throwable t) {
+ Throwable s = ExceptionFunctions.traceCauseException(t);
+ aLogger.error("Error while accessing external database: " + s.getClass().getName()+","+ s.getMessage());
+ }
+ };
+}