package com.dbxml.db.enterprise.jsp;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: ConnectionTag.java,v 1.3 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.client.xmlrpc.dbXMLClientImpl;
import com.dbxml.util.dbXMLException;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * ConnectionTag creates a reference to a dbXMLClient.
 * <br>
 * <br>
 * Example:
 * <br>
 * <pre>   &lt;dbxml:connection name="myCon">
 *      &lt;dbxml:property name="host" value="myhost.com"/>
 *   &lt;/dbxml:connection></pre>
 */

public class ConnectionTag extends BodyTagSupport implements PropertyContainer {
   private static final String ID_PREFIX = "dbxml:";
   private static final String DEFAULT_DRIVER = dbXMLClientImpl.class.getName();

   static final String NAME = "name";
   static final String DRIVER = "driver";

   private String name;
   private String driver = DEFAULT_DRIVER;
   private Map props = new TreeMap();

   public ConnectionTag() {
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getDriver() {
      return driver;
   }

   public void setDriver(String driver) {
      this.driver = driver;
   }

   public void setProperty(String name, String value) {
      props.put(name, value);
   }

   public int doEndTag() throws JspException {
      String conID = getConnectionID();
      dbXMLClient client = (dbXMLClient)pageContext.getSession().getAttribute(conID);
      if ( client == null ) {
         try {
            client = (dbXMLClient)Class.forName(driver).newInstance();
         }
         catch ( Exception e ) {
            throw new JspException("Driver '" + driver + "' could not be instantiated");
         }

         client.getProperties().putAll(props);

         try {
            client.connect();
            pageContext.getSession().setAttribute(conID, client);
         }
         catch ( dbXMLException e ) {
            throw new JspException("Couldn't connect to dbXML Database", e);
         }
      }
      pageContext.setAttribute(name, client);

      return SKIP_BODY;
   }

   private String getConnectionID() {
      StringBuffer sb = new StringBuffer(32);
      sb.append(ID_PREFIX);

      Iterator iter = props.entrySet().iterator();
      while ( iter.hasNext() ) {
         Map.Entry entry = (Map.Entry)iter.next();
         String key = (String)entry.getKey();
         String value = (String)entry.getValue();
         sb.append(key + '=' + value);
         if ( iter.hasNext() )
            sb.append(';');
      }
      return sb.toString();
   }
}

