gt:             if (key.equals(parameter.getKey())) {
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.xmlrpcd;
  
  import java.net.MalformedURLException;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  
  import org.opennms.core.utils.LogUtils;
  import org.opennms.netmgt.EventConstants;
  import org.opennms.netmgt.capsd.CapsdDbSyncer;
  import org.opennms.netmgt.config.CapsdConfig;
  import org.opennms.netmgt.config.PollerConfig;
  import org.opennms.netmgt.config.capsd.ProtocolPlugin;
  import org.opennms.netmgt.config.poller.Downtime;
  import org.opennms.netmgt.config.poller.Filter;
  import org.opennms.netmgt.config.poller.Package;
  import org.opennms.netmgt.config.poller.Parameter;
  import org.opennms.netmgt.config.poller.Rrd;
  import org.opennms.netmgt.config.poller.Service;
  import org.opennms.netmgt.model.events.EventBuilder;
  import org.opennms.netmgt.model.events.EventIpcManager;
  
  /**
   * <p>OpenNMSProvisioner class.</p>
   *
   * @author <a href="mailto:brozow@opennms.org">Mathew Brozowski</a>
   * @author <a href="mailto:david@opennms.org">David Hustace</a>
   * @author <a href="mailto:dj@opennms.org">DJ Gregor</a>
   * @author <a href="mailto:mhuot@opennms.org">Mike Huot</a>
   */
  public class OpenNMSProvisioner implements Provisioner {
      
      private static final String JDBC_MONITOR = "org.opennms.netmgt.poller.monitors.JDBCMonitor";
      private static final String HTTPS_MONITOR = "org.opennms.netmgt.poller.monitors.HttpsMonitor";
      private static final String HTTP_MONITOR = "org.opennms.netmgt.poller.monitors.HttpMonitor";
      private static final String TCP_MONITOR = "org.opennms.netmgt.poller.monitors.TcpMonitor";
      private static final String DNS_MONITOR = "org.opennms.netmgt.poller.monitors.DnsMonitor";
      private static final String ICMP_MONITOR = "org.opennms.netmgt.poller.monitors.IcmpMonitor";
  
      private static final String JDBC_PLUGIN = "org.opennms.netmgt.capsd.plugins.JDBCPlugin";
      private static final String HTTPS_PLUGIN = "org.opennms.netmgt.capsd.plugins.HttpsPlugin";
      private static final String HTTP_PLUGIN = "org.opennms.netmgt.capsd.plugins.HttpPlugin";
      private static final String TCP_PLUGIN = "org.opennms.netmgt.capsd.plugins.TcpPlugin";
      private static final String DNS_PLUGIN = "org.opennms.netmgt.capsd.plugins.DnsPlugin";
      private static final String ICMP_PLUGIN = "org.opennms.netmgt.capsd.plugins.IcmpPlugin";
  
  
      private static class Parm {
          String m_key;
          String m_val;
          Parm(String key, String val) { m_key = key; m_val = val; }
          Parm(String key, int val) { m_key = key; m_val = ""+val; }
          String getKey() { return m_key; }
          String getVal() { return m_val; }
  
      }
  
      private CapsdConfig m_capsdConfig;
      private PollerConfig m_pollerConfig;
      private EventIpcManager m_eventManager;
      private CapsdDbSyncer m_capsdDbSyncer;
  
  
      private void checkRetries(final int retries) {
          if (retries < 0) throw new IllegalArgumentException("Illegal retries "+retries+". Must be >= 0");
      }
  
      private void checkTimeout(final int timeout) {
          if (timeout <= 0) throw new IllegalArgumentException("Illegal timeout "+timeout+". Must be > 0");
      }
      
      private void checkInterval(final int interval) {
          if (interval <= 0) throw new IllegalArgumentException("Illegal interval "+interval+". Must be > 0");
      }
      
      private void checkDowntimeInterval(final int interval) {
          checkInterval(interval);
      }
      
      private void checkDowntimeDuration(final int duration) {
          checkInterval(duration);
      }
      
      private void checkPort(final int port) {
          if (port < 1 || port > 65535) throw new IllegalArgumentException("Illegal port "+port+". Must be between 1 and 65535 (inclusive)");
      }
      
      private void checkHostname(final String hostname) {
          if (hostname == null) throw new NullPointerException("hostname must not be null");
          if (hostname.length() > 512) throw new IllegalArgumentException("Illegal hostname "+hostname+". Hostnames must not be longer than 512 characters");
      }
      
      private void checkUrl(final String url) {
          if (url == null) throw new NullPointerException("url must not be null");
          if (url.length() == 0) throw new IllegalArgumentException("Illegal url \'\'.  Must not be zero length");
          if (url.length() > 512) throw new IllegalArgumentException("Illegal url "+url+". Must be no more than 512 chars");
      }
      
      private void checkContentCheck(final String check) {
          if (check != null && check.length() > 128) throw new IllegalArgumentException("Illegal contentCheck "+check+". Must be no more than 128 chars.");
      }
      
      private void checkResponseRange(final String response) {
          if (response == null || response.equals("")) return;
          
          if (response.indexOf('-') < 0) {
          	checkResponseCode(response);
          } else {
          	final int dash = response.indexOf('-');
          	final String startCode = response.substring(0, dash).trim();
          	if ("".equals(startCode)) throw new IllegalArgumentException("Illegal Start code. Expected range format is <startCode>-<endCode>.");
          	checkResponseCode(startCode);
  
          	if (dash+1 > response.length()-1) throw new IllegalArgumentException("Illegal End code. Expected range format is <startcode>-<endcode>");
          	final String endCode = response.substring(dash+1);
          	checkResponseCode(endCode);
          }
      }
      
      private void checkResponseCode(final String response) {
          if (response == null || response.equals("")) return;
              
          final int code = Integer.parseInt(response);
          if (code < 100 || code > 599) throw new IllegalArgumentException("Illegal response code "+code+". Must be between 100 and 599");
      }
      
      private void checkUsername(final String username) {
          if (username == null) throw new NullPointerException("username is null");
          if (username.length() > 64) throw new IllegalArgumentException("Illegal username "+username+". username must be no more than than 64 characters");
      }
      
      private void checkPassword(final String pw) {
          if (pw == null) throw new NullPointerException("password is null");
          if (pw.length() > 64) throw new IllegalArgumentException("Illegal password "+pw+". password must be no more than than 64 charachters");
      }
      
      private void checkDriver(final String driver) {
          if (driver == null) throw new NullPointerException("driver is null");
          if (driver.length() == 0) throw new IllegalArgumentException("Illegal driver.  Must not be zero length");
          if (driver.length() > 128) throw new IllegalArgumentException("Illegal driver "+driver+". Driver length must be no more than than 128 characters");
              
      }
      
      private void validateSchedule(final int retries, final int timeout, final int interval, final int downTimeInterval, final int downTimeDuration) {
          checkRetries(retries);
          checkTimeout(timeout);
          checkInterval(interval);
          checkDowntimeInterval(downTimeInterval);
          checkDowntimeDuration(downTimeDuration);
      }
          
  
  
      /** {@inheritDoc} */
      public boolean addServiceICMP(final String serviceId, final int retry, final int timeout, final int interval, final int downTimeInterval, final int downTimeDuration) {
          validateSchedule(retry, timeout, interval, downTimeInterval, downTimeDuration);
          return addService(serviceId, retry, timeout, interval, downTimeInterval, downTimeDuration, ICMP_MONITOR, ICMP_PLUGIN);
      }
      
      private boolean addService(final String serviceId, final int retries, final int timeout, final int interval, final int downTimeInterval, final int downTimeDuration, final String monitor, final String plugin) {
          return addService(serviceId, retries, timeout, interval, downTimeInterval, downTimeDuration, monitor, plugin, new Parm[0]);
      }
      
      private boolean addService(final String serviceId, final int retries, final int timeout, final int interval, final int downTimeInterval, final int downTimeDuration, final String monitor, final String plugin, final Parm[] entries) {
      	final String pkgName = serviceId;
      	final Package pkg = getPackage(pkgName, interval, downTimeInterval, downTimeDuration);
  
      	final Properties parms = new Properties();
          parms.setProperty("retry", ""+retries);
          parms.setProperty("timeout", ""+timeout);
          
          for(int i = 0; i < entries.length; i++) {
              parms.setProperty(entries[i].getKey(), entries[i].getVal());
          }
  
          addServiceToPackage(pkg, serviceId, interval, parms);
  
          if (m_pollerConfig.getPackage(pkg.getName()) == null) {
              m_pollerConfig.addPackage(pkg);
          }
          
          if (m_pollerConfig.getServiceMonitor(serviceId) == null) {
              LogUtils.debugf(this, "Adding a new monitor for %s", serviceId);
              m_pollerConfig.addMonitor(serviceId, monitor);
          } else {
              LogUtils.debugf(this, "No need to add a new monitor for %s", serviceId);
          }
          
          if (m_capsdConfig.getProtocolPlugin(serviceId) == null) {
          	final ProtocolPlugin pPlugin = new ProtocolPlugin();
              pPlugin.setProtocol(serviceId);
              pPlugin.setClassName(plugin);
              pPlugin.setScan("off");
              m_capsdConfig.addProtocolPlugin(pPlugin);
          }
          
          saveConfigs();
          return true;
      }
      private void saveConfigs() {
          try {
              m_capsdConfig.save();
              syncServices();
              m_pollerConfig.save();
  
              m_eventManager.sendNow(new EventBuilder(EventConstants.SCHEDOUTAGES_CHANGED_EVENT_UEI, "OpenNMSProvisioner").getEvent());
          } catch (final Throwable e) {
              throw new RuntimeException("Error saving poller or capsd configuration: " + e, e);
          }
      }
      
      private void syncServices() {
          getCapsdDbSyncer().syncServicesTable();
      }
      
      private void addServiceToPackage(final Package pkg, final String serviceId, final int interval, final Properties parms) {
      	Service svc = m_pollerConfig.getServiceInPackage(serviceId, pkg);
          if (svc == null) {
              svc = new Service();
              svc.setName(serviceId);
              pkg.addService(svc);
          }
          svc.setInterval(interval);
          final Enumeration<?> keys = parms.propertyNames();
          while(keys.hasMoreElements()) {
              final String key = (String)keys.nextElement();
              final String value = parms.getProperty(key);
              setParameter(svc, key, value);
          }
      }
      private void setParameter(final Service svc, final String key, final String value) {
      	Parameter parm = findParamterWithKey(svc, key);
          if (parm == null) {
              parm = new Parameter();
              svc.addParameter(parm);
          }
          parm.setKey(key);
          parm.setValue(value);
      }
      
      /**
       * <p>findParamterWithKey</p>
       *
       * @param svc a {@link org.opennms.netmgt.config.poller.Service} object.
       * @param key a {@link java.lang.String} object.
       * @return a {@link org.opennms.netmgt.config.poller.Parameter} object.
       */
      public Parameter findParamterWithKey(final Service svc, final String key) {
      	final Enumeration<Parameter> e = svc.enumerateParameter();
          while(e.hasMoreElements()) {
              final Parameter parameter = (Parameter)e.nextElement();
  