You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juddi.apache.org by sv...@apache.org on 2003/11/11 14:49:59 UTC

cvs commit: ws-juddi/src/java/org/apache/juddi/monitor/jdbc JDBCMonitor.java MonitorDAO.java MonitorTable.java

sviens      2003/11/11 05:49:59

  Added:       src/java/org/apache/juddi/monitor/jdbc JDBCMonitor.java
                        MonitorDAO.java MonitorTable.java
  Log:
  Moved from jUDDI CVS at SourceForge
  
  Revision  Changes    Path
  1.1                  ws-juddi/src/java/org/apache/juddi/monitor/jdbc/JDBCMonitor.java
  
  Index: JDBCMonitor.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "jUDDI" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.juddi.monitor.jdbc;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.juddi.monitor.MonitorAdapter;
  
  /**
   * @author Anou Mana (anou_mana@users.sourceforge.net)
   */
  public class JDBCMonitor extends MonitorAdapter
  {
    // private reference to the jUDDI logger
    private static Log log = LogFactory.getLog(JDBCMonitor.class);
  
    public JDBCMonitor()
    {
      super();
    }
  
    public void log()
    {
      try
      {
        // Get the Data Access Object and
        // insert the data to the table.
  
        new MonitorDAO().insert(data);
      }
      catch(Exception exp)
      {
        log.error(exp);
      }
    }
  }
  
  
  
  1.1                  ws-juddi/src/java/org/apache/juddi/monitor/jdbc/MonitorDAO.java
  
  Index: MonitorDAO.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "jUDDI" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.juddi.monitor.jdbc;
  
  import java.sql.Connection;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.juddi.datatype.RegistryObject;
  import org.apache.juddi.error.RegistryException;
  import org.apache.juddi.monitor.MonitorData;
  import org.apache.juddi.util.jdbc.ConnectionManager;
  
  /**
   * @author Anou Mana (anou_mana@users.sourceforge.net)
   */
  public class MonitorDAO
  {
    // private reference to the jUDDI logger
    private static Log log = LogFactory.getLog(MonitorDAO.class);
  
  
    public MonitorDAO()
    {
      super();
    }
  
    public void init()
    {
    }
  
    /**
     *
     */
    public RegistryObject insert(RegistryObject regObject)
      throws RegistryException
    {
      Connection connection = null;
      MonitorData data = (MonitorData)regObject;
  
      try
      {
        // grab a JDBC connection
        connection = ConnectionManager.aquireConnection();
        if(connection != null)
        {
          MonitorTable.insert(data,connection);
          connection.commit();
        }
  
        return regObject;
      }
      catch(Exception ex)
      {
        // rollback for *any* exception
        try { connection.rollback(); }
        catch(Exception e) { log.error(ex); }
  
        // write to the log
        log.error(ex);
  
        // prep RegistryFault to throw
        if (ex instanceof RegistryException)
          throw (RegistryException)ex;
        else
          throw new RegistryException(ex);
      }
      finally
      {
        // close the JDBC connection
        try {
          if (connection != null)
            connection.close();
        }
        catch(Exception ex) { log.error(ex); }
      }
    }
  
  }
  
  
  
  1.1                  ws-juddi/src/java/org/apache/juddi/monitor/jdbc/MonitorTable.java
  
  Index: MonitorTable.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "jUDDI" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.juddi.monitor.jdbc;
  
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.Timestamp;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.juddi.monitor.MonitorData;
  import org.apache.juddi.registry.Registry;
  import org.apache.juddi.util.Config;
  import org.apache.juddi.util.jdbc.ConnectionManager;
  import org.apache.juddi.util.jdbc.Transaction;
  import org.apache.juddi.uuidgen.UUIDGen;
  import org.apache.juddi.uuidgen.UUIDGenFactory;
  
  /**
   * @author Anou Mana (anou_mana@users.sourceforge.net)
   */
  class MonitorTable
  {
    // private reference to the jUDDI logger
    private static Log log = LogFactory.getLog(MonitorTable.class);
  
    static String insertSQL = null;
    static String selectSQL = null;
    static String deleteSQL = null;
  
    static
    {
      // buffer used to build SQL statements
      StringBuffer sql = null;
  
      // build insertSQL
      sql = new StringBuffer(150);
      sql.append("INSERT INTO MONITOR (");
      sql.append("REMOTE_HOST,");
      sql.append("REQUEST_URI,");
      sql.append("CALLED_FUNCTION,");
      sql.append("AUTH_TOKEN,");
      sql.append("UDDI_VERSION,");
      sql.append("LOG_TIME,");
      sql.append("FAULT) ");
      sql.append("VALUES (?,?,?,?,?,?,?)");
      insertSQL = sql.toString();
  
      // build selectSQL
      sql = new StringBuffer(200);
      sql.append("SELECT ");
      sql.append("LOG_TIME,");
      sql.append("REMOTE_HOST,");
      sql.append("REQUEST_URI,");
      sql.append("CALLED_FUNCTION,");
      sql.append("AUTH_TOKEN,");
      sql.append("UDDI_VERSION,");
      sql.append("FAULT) ");
      sql.append("FROM MONITOR ");
      sql.append("WHERE FAULT=? ");
      selectSQL = sql.toString();
  
      // build deleteSQL
      sql = new StringBuffer(100);
      sql.append("DELETE FROM MONITOR ");
      sql.append("WHERE FAULT=? ");
      deleteSQL = sql.toString();
    }
  
    /**
     * Insert new row into the MONITOR table.
     *
     * @param Monitor
     * @param connection JDBC connection
     * @throws java.sql.SQLException
     */
    public static void insert(MonitorData data,Connection connection)
      throws java.sql.SQLException
    {
      if (data == null)
        return; // everything is valid but no elements to insert
  
      PreparedStatement statement = null;
      Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
  
      try
      {
        statement = connection.prepareStatement(insertSQL);
        statement.setString(1,data.getRemoteHost());
        statement.setString(2,data.getRequestURI());
        statement.setString(3,data.getCalledFunction());
        statement.setString(4,data.getAuthToken());
        statement.setString(5,data.getVersion());
        statement.setTimestamp(6,timeStamp);
        statement.setString(7,data.getFault());
  
          log.debug("insert into MONITOR table:\n\n\t" + insertSQL +
            "\n\t REMOTE_HOST=" + data.getRemoteHost() +
            "\n\t REQUEST_URI=" + data.getRequestURI() +
            "\n\t CALLED_FUNCTION=" + data.getCalledFunction() +
            "\n\t AUTH_TOKEN=" + data.getAuthToken()+
            "\n\t UDDI_VERSION=" + data.getVersion() +
            "\n\t LOG_TIME=" + timeStamp.getTime() +
            "\n\t FAULT=" + data.getFault() + "\n");
  
          statement.executeUpdate();
      }
      finally
      {
        try {
          statement.close();
        }
        catch (Exception e) { /* ignored */ }
      }
    }
  
    /***************************************************************************/
    /***************************** TEST DRIVER *********************************/
    /***************************************************************************/
  
  
    public static void main(String[] args)
      throws Exception
    {
      // make sure we're using a DBCP DataSource and
      // not trying to use JNDI to aquire one.
      Config.setStringProperty("juddi.useConnectionPool","true");
  
      Connection conn = null;
      try {
        conn = ConnectionManager.aquireConnection();
        test(conn);
      }
      finally {
        if (conn != null)
          conn.close();
      }
    }
  
    public static void test(Connection connection)
      throws Exception
    {
      UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
      Transaction txn = new Transaction();
  
      if (connection != null)
      {
        try
        {
          MonitorData data = new MonitorData("123.234.345.456", "testFunction");
  
          data.setAuthToken("");
          data.setUddiVersion(Registry.UDDI_V2_GENERIC);
          data.setFault("None");
  
          // begin a new transaction
          txn.begin(connection);
  
          // insert a new monitor log
          MonitorTable.insert(data,connection);
  
          // commit the transaction
          txn.commit();
        }
        catch(Exception ex)
        {
          try { txn.rollback(); }
          catch(java.sql.SQLException sqlex) { sqlex.printStackTrace(); }
          throw ex;
        }
      }
    }
  }