/* * File : JNDIConnectionBeanProvider.java * Created : 10-nov-2005 19:28 * By : fbusquets * * JClic - Authoring and playing system for educational activities * * Copyright (C) 2000 - 2005 Francesc Busquets & Departament * d'Educacio de la Generalitat de Catalunya * * This program 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. * * This program 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 (see the LICENSE file). */ package edu.xtec.util.db; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.util.Date; import java.util.Map; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; /** * This class is a special {@link edu.xtec.util.db.ConnectionBeanProvider} that obtains * the {@link java.sql.Connection} objects from a JNDI datasource. In order to * obtain objects of this class, the static method getConnectionBeanProvider of * ConnectionBeanProvider must be called, passing the text JNDI as * value of the dbDriver parameter, and the JNDI name of the datasource * as the value of dbServer param. * @author fbusquet * @version 1.0 */ public class JNDIConnectionBeanProvider extends ConnectionBeanProvider{ /** * Key used for the dbContext property */ public static final String DB_CONTEXT="dbContext"; /** * String "JNDI", used to indicate ConnectionBeanProvider that an * object of this class is required. */ public static final String JNDI="JNDI"; /** * Object used to obtain valid {@link java.sql.Connection} objects. */ protected DataSource ds; /** * Context where to look for the indicated JNDI resource. In Apache Tomcat, this value should be * java:comp/env. */ protected String dbContext; private int totalCBRequests; private Date started; private Date lastUse; private String logFileString; private PrintWriter log; private int debugLevel; private int totalStatements; private String lastRequest; /** Creates a new instance of JNDIConnectionBeanProvider */ public JNDIConnectionBeanProvider() { } /** * Main initialization function, called immediatelly after constructor by * getConnectionBeanProvider functions. * @param map Collection of key - value pairs that must specify the JNDI Datasource name and * context params. * @throws Exception Throwed if the DataSource can't be * instantiated. */ protected void setUp(Map map) throws Exception{ super.setUp(map); started=new Date(); dbDriver=JNDI; if(dbServer==null) throw new Exception("JNDI datasource name not specified!"); dbContext = getValue(map, DB_CONTEXT, null); debugLevel = Integer.parseInt(getValue(map, "dbDebugLevel", "2")); if(debugLevel>0){ try{ logFileString = getValue(map, "dbLogFile", null); if(logFileString!=null){ File f=new File(logFileString); if(!f.isAbsolute()){ f=new File(System.getProperty("user.home")); f=new File(f, logFileString); logFileString=f.getAbsolutePath(); } boolean logAppend = new Boolean(getValue(map, "dbLogAppend", "true")).booleanValue(); log = new PrintWriter(new FileOutputStream(logFileString, logAppend),true); } } catch(IOException ioex){ System.err.println(new Date().toString()+" - Error creating log file for JNDIConnectionProvider - "+ioex); } } Context ctx=new InitialContext(); if(dbContext!=null && dbContext.trim().length()>0) ctx=(Context)ctx.lookup(dbContext); ds=(DataSource)ctx.lookup(dbServer); if(log!=null){ log.println("-----------------------------------------"); log.println(started); log.println("Starting JNDIConnectionBeanProvider"); log.println("dbContext = " + dbContext); log.println("dbServer = " + dbServer); log.println("Context = " + ctx); log.println("Datasource = " + ds); log.println("-----------------------------------------"); } } /** Provides information about the current state of this ConnectionBeanProvider. * @return Information string, formatted in HTML. */ public String getInfo(){ StringBuffer sb=new StringBuffer(); sb.append("JNDIConnectionBeanProvider ").append(hashCode()).append("
\n"); sb.append(super.getInfo()); sb.append("started: ").append(started).append("
\n"); sb.append("dbContext: ").append(dbContext).append("
\n"); sb.append("Total requests: ").append(totalCBRequests).append("
\n"); sb.append("Total statements: ").append(totalStatements).append("
\n"); sb.append("Last use: ").append(lastUse).append("
\n"); sb.append("Last request: ").append(lastRequest).append("
\n"); return sb.toString(); } /** * Performs cleanup */ protected void destroy() { } /** This method must be called when the obtained ConnectionBean is no longer needed, * usualy inside the finally block of a try - catch statement. * @param conn The ConnectionBean object to be disposed * @return A descriptive String, useful only for debug purposes. */ public String freeConnectionBean(ConnectionBean conn) { if(conn!=null){ try{ lastRequest=conn.getLastStatement(); totalStatements+=conn.getNumStatements(); conn.closeConnection(); } catch(Exception ex){ if(log!=null) log.println(new Date().toString() + " Unable to close DB connection: "+ex); } } return ""; } /** This is the main function that all ConnectionbeanProvider objects must * implement.

* Important: You must ever call FreeConnectionBean after the use of the ConnectionBean * object. Typical inmplementation use a try - catch - finally statement block in * order to ensure that all ConnectionBean objects will be properly disposed after * use.

* Example:

*

     * ConectionBeanProvider cbp;
     * java.util.Properties prop=new Java.util.Properties();
     * // ...
     * // ... fill-up properties with values for dbDriver, dbServer, dbLogin, etc.
     * // ...
     * cbp=ConnectionBeanProvider.getConnectionBeanProvider(map);
     * ConnectionBean cb=cbp.getConnectionBean();
     * try {
     *  // ... use of the ConnectionBean object
     * } catch(Exception ex){
     *  // ... process possible exceptions done while database access
     * } finally {
     *  // Very important: free the ConnectionBean object:
     *  cbp.freeConnectionBean(cb);
     * }
     * 
* @return A ready-to-use ConnectionBean. Remember to return it by calling * FreeConnectionBean. */ public ConnectionBean getConnectionBean() { ConnectionBean result=null; try{ if(ds!=null){ Connection con=ds.getConnection(); result=new ConnectionBean(con, mapStatements); totalCBRequests++; lastUse=new Date(); } if(log!=null && debugLevel>2) log.println(new Date().toString()+" - connection request"); } catch(Exception ex){ if(log!=null) log.println(new Date().toString() + " Unable to get DB connection: "+ex.getMessage()); } return result; } }