/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ha.framework.server.util;

import java.util.ArrayList;
import java.util.Hashtable;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Context;

import org.jboss.system.ServiceMBeanSupport;
import org.jboss.logging.Logger;
import  org.jboss.ha.framework.server.util.TopologyMonitorService.AddressPort;

/** A utility MBean that can be used as the trigger target of the
 * TopologyMonitorService to probe the state of JNDI on the cluster nodes.
 *
 * @author Scott.Stark@jboss.org
 * @version $Revision: 57188 $
 */
public class PingJndi extends ServiceMBeanSupport
   implements PingJndiMBean
{
   private String urlPrefix;
   private String urlSuffix;
   private String urlPattern;
   private String[] lookupNames;

   /** Get the names of JNDI bindings that should be queried on each host
    * @return the array of target names to test
    * @jmx:managed-attribute
    */
   public String[] getLookupNames()
   {
      return lookupNames;
   }
   /** Set the names of JNDI bindings that should be queried on each host
    * @param names
    * @jmx:managed-attribute
    */
   public void setLookupNames(String[] names)
   {
      this.lookupNames = names;
   }

   /** Get the Context.PROVIDER_URL regular expression.
    * @return the regular expression containing the host, for example
    * 'jnp://(host):1099/'
    * @jmx:managed-attribute
    */
   public String getProviderURLPattern()
   {
      return urlPattern;
   }

   /** Set the regular expression containing the hostname/IP address of
    * the JNDI provider. This expression is used to build the JNDI
    * Context.PROVIDER_URL for each node in the cluster. The expression
    * should contain a "(host)" component that will be replaced with the
    * cluster node hostname.
    *
    * @param regex the regular expression containing the host, for example
    * 'jnp://(host):1099/'
    * @jmx:managed-attribute
    */
   public void setProviderURLPattern(String regex)
   {
      this.urlPattern = regex;
      this.urlPrefix = regex;
      this.urlSuffix = "";
      String hostExp = "{host}";
      int hostIndex = regex.indexOf(hostExp);
      if( hostIndex >= 0 )
      {
         urlPrefix = regex.substring(0, hostIndex);
         int endIndex = hostIndex + hostExp.length();
         urlSuffix = regex.substring(endIndex);
      }
   }

   /** The TopologyMonitorService trigger callback operation.
    *
    * @param removed ArrayList<AddressPort> of nodes that were removed
    * @param added ArrayList<AddressPort> of nodes that were added
    * @param members ArrayList<AddressPort> of nodes currently in the cluster
    * @param logCategoryName the log4j category name used by the
    * TopologyMonitorService. This is used for logging to integrate with
    * the TopologyMonitorService output.
    */
   public void membershipChanged(ArrayList removed, ArrayList added,
      ArrayList members, String logCategoryName)
   {
      log.debug("membershipChanged");
      Logger tmsLog = Logger.getLogger(logCategoryName);
      Hashtable localEnv = null;
      try
      {
         InitialContext localCtx = new InitialContext();
         localEnv = localCtx.getEnvironment();
      }
      catch(NamingException e)
      {
         tmsLog.error("Failed to obtain InitialContext env", e);
         return;
      }

      tmsLog.info("Checking removed hosts JNDI binding");
      doLookups(localEnv, tmsLog, removed);
      tmsLog.info("Checking added hosts JNDI binding");
      doLookups(localEnv, tmsLog, added);
      tmsLog.info("Checking members hosts JNDI binding");
      doLookups(localEnv, tmsLog, members);
   }

   private void doLookups(Hashtable localEnv, Logger tmsLog, ArrayList nodes)
   {
      for(int n = 0; n < nodes.size(); n ++)
      {
         AddressPort addrInfo = (AddressPort) nodes.get(n);
         String providerURL = urlPrefix + addrInfo.getHostName() + urlSuffix;
         Hashtable env = new Hashtable(localEnv);
         env.put(Context.PROVIDER_URL, providerURL);
         tmsLog.info("Checking names on: "+addrInfo);
         try
         {
            InitialContext ctx = new InitialContext(env);
            for(int s = 0; s < lookupNames.length; s ++)
            {
               String name = lookupNames[s];
               Object value = ctx.lookup(name);
               tmsLog.info("lookup("+name+"): "+value);
            }
         }
         catch(Exception e)
         {
            tmsLog.error("Failed lookups on: "+addrInfo, e);
         }
      }
   }
}
