From: Marcus <the...@us...> - 2004-03-24 16:07:53
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/plugin/database In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25598 Added Files: DatabasePlugin.java Log Message: initial import --- NEW FILE: DatabasePlugin.java --- /* * juNK - a file search system for smb shares * * Copyright 2004 by * Marcus Proest (theevilflow at users dot sf dot net) * Uwe van Heesch (tyron_e at users dot sf dot net) * * This file is part of junk (java useful net kollektor). * * junk 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. * * junk 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 junk; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /* * DatabasePlugin.java * * Created on 23. März 2004, 20:28 */ package junk.plugin.database; import junk.plugin.PluginTools; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import javax.servlet.ServletException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig; /** * plugin to provide a database connection instead of using DBCP * @author Marcus Proest */ public class DatabasePlugin implements PlugIn, ConnectionProvider { /** * context saving key */ public static final String KEY_DATABASE_CON = "database-provider"; /** the actionservlet of the struts application */ private ActionServlet actionServlet = null; /** * plugin configuration */ private HashMap conf = null; /** log writer */ private Log log = LogFactory.getLog(DatabasePlugin.class); /** * database password */ private String pass = null; /** * database url */ private String url = null; /** * database user */ private String user = null; /** Creates a new instance of DatabasePlugin */ public DatabasePlugin() { } /** * return a valid database connection * @throws SQLException yupp, life's bad * @return a valid database connection */ public Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, pass); } /** * destroy plugin */ public void destroy() { log.info("DatabasePlugin just left the building"); } /** * initialize plugin * @param a ActionServlet of the struts application * @param m the ModuleConfig of the struts application * @throws ServletException ServletException */ public void init(ActionServlet a, ModuleConfig m) throws ServletException { this.actionServlet = a; this.conf = PluginTools.getPluginConfig(DatabasePlugin.class, m); if (this.conf == null) { log.fatal("No database configuration found."); } String driver = (String) this.conf.get("driverClass"); this.url = (String) this.conf.get("url"); this.user = (String) this.conf.get("user"); this.pass = (String) this.conf.get("password"); if ((driver == null) || (url == null) || (user == null) || (pass == null)) { log.fatal("Bad database configuration:" + "[" + driver + "][" + url + "][" + user + "][(pass null?)" + (pass == null) + "]", new IllegalArgumentException()); } try { Class.forName(driver).newInstance(); Connection con = DriverManager.getConnection(url, user, pass); con.close(); } catch (SQLException e1) { log.fatal("Error while connecting to data source.", e1); } catch (ClassNotFoundException e2) { log.fatal("Could not find JDBC driver.", e2); } catch (InstantiationException e3) { log.fatal("Could not instantiate JDBC driver.", e3); } catch (IllegalAccessException e4) { log.fatal("Illegal access while instatiating JDBC driver.", e4); } this.actionServlet.getServletContext().setAttribute(DatabasePlugin.KEY_DATABASE_CON, (ConnectionProvider) this); log.info("DatabasePlugin loaded ( [" + driver + "][" + url + "][" + user + "] )"); } } |