gt:                     nodeId = rs.getInt(1);
context: /*******************************************************************************
   * This file is part of OpenNMS(R).
   *
   * Copyright (C) 2006-2012 The OpenNMS Group, Inc.
   * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
   *
   * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
   *
   * OpenNMS(R) 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 3 of the License,
   * or (at your option) any later version.
   *
   * OpenNMS(R) 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 OpenNMS(R).  If not, see:
   *      http://www.gnu.org/licenses/
   *
   * For more information contact:
   *     OpenNMS(R) Licensing <license@opennms.org>
   *     http://www.opennms.org/
   *     http://www.opennms.com/
   *******************************************************************************/
  
  package org.opennms.netmgt.threshd;
  
  /**
   * @author mjamison
   *
   * TODO To change the template for this generated type comment go to
   * Window - Preferences - Java - Code Style - Code Templates
   */
  
  import java.io.File;
  import java.lang.reflect.UndeclaredThrowableException;
  import java.net.InetAddress;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.Collection;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import org.opennms.core.db.DataSourceFactory;
  import org.opennms.core.utils.DBUtils;
  import org.opennms.core.utils.InetAddressUtils;
  import org.opennms.core.utils.ParameterMap;
  import org.opennms.netmgt.config.ThresholdingConfigFactory;
  import org.opennms.netmgt.config.threshd.Basethresholddef;
  import org.opennms.netmgt.dao.support.RrdFileConstants;
  import org.opennms.netmgt.model.events.EventProxy;
  import org.opennms.netmgt.model.events.EventProxyException;
  import org.opennms.netmgt.poller.NetworkInterface;
  import org.opennms.netmgt.rrd.RrdException;
  import org.opennms.netmgt.rrd.RrdUtils;
  import org.opennms.netmgt.utils.IfLabel;
  import org.opennms.netmgt.xml.event.Event;
  import org.opennms.netmgt.xml.event.Events;
  import org.opennms.netmgt.xml.event.Log;
  import org.opennms.netmgt.xml.event.Parm;
  import org.opennms.netmgt.xml.event.Value;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * <P>
   * The JMXThresholder class ...
   * </P>
   *
   * @author <A HREF="mailto:mike@opennms.org">Mike Jamison </A>
   * @author <A HREF="http://www.opennms.org/">OpenNMS </A>
   * @deprecated No longer used - see ThresholdingVisitor
   */
  public abstract class JMXThresholder implements ServiceThresholder {
      
      
      private static final Logger LOG = LoggerFactory.getLogger(JMXThresholder.class);
      /**
       * SQL statement to retrieve interface's 'ipinterface' table information.
       */
      private static final String SQL_GET_NODEID = "SELECT nodeid FROM ipinterface WHERE ipAddr=? AND ismanaged!='D'";
  
      /**
       * Default thresholding interval (in milliseconds).
       * 
       */
      private static final int DEFAULT_INTERVAL = 300000; // 300s or 5m
      
      /**
       * Default age before which a data point is considered "out of date"
       */
      
      private static final int DEFAULT_RANGE = 0; // 300s or 5m
      
  
      /**
       * Interface attribute key used to store the interface's node id
       */
      static final String NODE_ID_KEY = "org.opennms.netmgt.collectd.JMXThresholder.NodeId";
  
      /**
       * Interface attribute key used to store the interface's node id
       */
      static final String RRD_REPOSITORY_KEY = "org.opennms.netmgt.collectd.JMXThresholder.RrdRepository";
  
      /**
       * Interface attribute key used to store a map of node level ThresholdEntity
       * objects keyed by datasource name.
       */
      static final String NODE_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.JMXThresholder.NodeThresholdMap";
  
      /**
       * Interface attribute key used to store a map of interface level
       * ThresholdEntity objects keyed by datasource name.
       */
      static final String BASE_IF_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.JMXThresholder.IfThresholdMap";
  
      /**
       * We must maintain a map of interface level ThresholdEntity objects on a
       * per interface basis in order to maintain separate exceeded counts and the
       * like for each of a node's interfaces. This interface attribute key used
       * to store a map of interface level ThresholdEntity object maps keyed by
       * ifLabel. So it wil refer to a map of maps indexed by ifLabel.
       */
      static final String ALL_IF_THRESHOLD_MAP_KEY = "org.opennms.netmgt.collectd.JMXThresholder.AllIfThresholdMap";
  
      private String serviceName = null;
      
      private boolean useFriendlyName = false;
      
      /**
       * <p>Setter for the field <code>serviceName</code>.</p>
       *
       * @param name a {@link java.lang.String} object.
       */
      public void setServiceName(String name) {
          serviceName = name;
      }
  
      /**
       * <P>
       * Returns the name of the service that the plug-in collects ("SNMP").
       * </P>
       *
       * @return The service that the plug-in collects.
       */
      public String serviceName() {
          return serviceName.toUpperCase();
      }
  
      /**
       * {@inheritDoc}
       *
       * <P>
       * Initialize the service thresholder.
       * </P>
       * @exception RuntimeException
       *                Thrown if an unrecoverable error occurs that prevents the
       *                plug-in from functioning.
       */
      @Override
      public void initialize(Map<?,?> parameters) {
      }
  
      /**
       * <p>reinitialize</p>
       */
      @Override
      public void reinitialize() {
          //Nothing to do 
      }
      /**
       * Responsible for freeing up any resources held by the thresholder.
       */
      @Override
      public void release() {
          // Nothing to release...
      }
  
      /**
       * {@inheritDoc}
       *
       * Responsible for performing all necessary initialization for the specified
       * interface in preparation for thresholding.
       */
      @Override
      public void initialize(ThresholdNetworkInterface iface, Map<?,?> parameters) {
          // Get interface address from NetworkInterface
          if (iface.getType() != NetworkInterface.TYPE_INET) {
              throw new RuntimeException("Unsupported interface type, only TYPE_INET currently supported");
          }
  
          InetAddress ipAddr = (InetAddress) iface.getAddress();
  
          // Retrieve the name of the thresholding group associated
          // with this interface.
          String groupName = ParameterMap.getKeyedString(parameters, "thresholding-group", serviceName);
  
          // Get the threshold group's RRD repository path
          String repository = null;
          try {
              repository = ThresholdingConfigFactory.getInstance().getRrdRepository(groupName);
          } catch (IllegalArgumentException e) {
              throw new RuntimeException("Thresholding group '" + groupName + "' does not exist.");
          }
  
          // Add RRD repository as an attribute of the interface for retrieval
          // by the check() method.
          iface.setAttribute(RRD_REPOSITORY_KEY, repository);
  
          // Retrieve the collection of Threshold objects associated with
          // the defined thresholding group and build two maps, one consisting
          // of node level ThresholdEntity objects and another consisting of
          // interface level ThresholdEntity objects both keyed by datasource
          // name.
          //
          // Each ThresholdEntity can wrap one high Threshold and one low
          // Threshold castor-generated object for a single datasource.
          // If more than one high or more than one low threshold is defined
          // for a single datasource a warning messages is generated. Only
          // the first threshold in such a scenario will be used for thresholding.
          //
          Map<String, ThresholdEntity> nodeMap   = new HashMap<String, ThresholdEntity>();
          Map<String, ThresholdEntity> baseIfMap = new HashMap<String, ThresholdEntity>();
          try {
              for (Basethresholddef thresh : ThresholdingConfigFactory.getInstance().getThresholds(groupName)) {
                  // See if map entry already exists for this datasource
                  // If not, create a new one.
                  boolean newEntity = false;
                  ThresholdEntity thresholdEntity = null;
                  try {
                      BaseThresholdDefConfigWrapper wrapper=BaseThresholdDefConfigWrapper.getConfigWrapper(thresh);
                      if (wrapper.getDsType().equals("node")) {
                          thresholdEntity = nodeMap.get(wrapper.getDatasourceExpression());
                      } else if (wrapper.getDsType().equals("if")) {
                          thresholdEntity = baseIfMap.get(wrapper.getDatasourceExpression());
                      }
      
                      // Found entry?
                      if (thresholdEntity == null) {
                          // Nope, create a new one
                          newEntity = true;
                          thresholdEntity = new ThresholdEntity();
                      }
      
                      try {
                          thresholdEntity.addThreshold(wrapper);
                      } catch (IllegalStateException e) {
                          LOG.warn("Encountered duplicate {} for datasource {}", thresh.getType(), wrapper.getDatasourceExpression(), e);
                      }
   
                      // Add new entity to the map
                      if (newEntity) {
                          if (thresh.getDsType().equals("node")) {
                              nodeMap.put(wrapper.getDatasourceExpression(), thresholdEntity);
                          } else if (thresh.getDsType().equals("if")) {
                              baseIfMap.put(wrapper.getDatasourceExpression(), thresholdEntity);
                          }
                      }
                  } catch (ThresholdExpressionException e) {
                      LOG.warn("Could not parse threshold expression", e);
                  }
  
              }
          } catch (IllegalArgumentException e) {
              throw new RuntimeException("Thresholding group '" + groupName + "' does not exist.");
          }
  
          // Add node and interface thresholding maps as attributes of the
          // interface for retrieval by the check() method.
          iface.setAttribute(NODE_THRESHOLD_MAP_KEY,    nodeMap);
          iface.setAttribute(BASE_IF_THRESHOLD_MAP_KEY, baseIfMap);
  
          // Now create an empty map which will hold interface level
          // ThresholdEntity objects for each of the node's interfaces.
          // This map will be keyed by the interface's iflabel and will
          // contain as a value a map of ThresholdEntity objects keyed
          // by datasource name.
          iface.setAttribute(ALL_IF_THRESHOLD_MAP_KEY, new HashMap<String,Map<String,ThresholdEntity>>());
  
          final DBUtils d = new DBUtils(getClass());
          // Get database connection in order to retrieve the nodeid and
          // ifIndex from the database for this interface.
          Connection dbConn = null;
          try {
              dbConn = DataSourceFactory.getInstance().getConnection();
              d.watch(dbConn);
          } catch (SQLException e) {
              LOG.error("initialize: Failed getting connection to the database", e);
              throw new UndeclaredThrowableException(e);
          }
  
          int nodeId = -1;
  
          // All database calls wrapped in try/finally block so we make
          // certain that the connection will be closed when we are
          // finished.
          try {
              // Prepare & execute the SQL statement to get the 'nodeid',
              // 'ifIndex' and 'isSnmpPrimary' fields from the ipInterface table.
              PreparedStatement stmt = null;
              final String hostAddress = InetAddressUtils.str(ipAddr);
  			try {
                  stmt = dbConn.prepareStatement(SQL_GET_NODEID);
                  d.watch(stmt);
                  stmt.setString(1, hostAddress); // interface address
                  ResultSet rs = stmt.executeQuery();
                  d.watch(rs);
                  if (rs.next()) {
  