/***************************************
 *                                     *
 *  JBoss: The OpenSource J2EE WebOS   *
 *                                     *
 *  Distributable under LGPL license.  *
 *  See terms of license at gnu.org.   *
 *                                     *
 ***************************************/

package org.jboss.deployment;

import java.net.URL;
import java.util.Comparator;

import org.jboss.util.NullArgumentException;

/**
 * A helper class for sorting deployments.
 *
 * @version <tt>$Revision: 1.15.2.3 $</tt>
 * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @author Scott.Stark@jboss.org
 */
public class DeploymentSorter implements Comparator, DefaultDeploymentSorter
{
   /**
    * The default order for sorting deployments; this has been deprecated,
    * order is really defined in SuffixOrderHelper class and/or individualy
    * within each subdeployer.
    * 
    * @deprecated
    */
   public static final String[] DEFAULT_SUFFIX_ORDER = {
       ".deployer", "-deployer.xml", ".sar", "-service.xml", ".rar", "-ds.xml",
       ".har", ".jar", ".war", ".wsr", ".ear", ".zip", ".bsh", ".last"
   };

   protected String[] suffixOrder;

   public DeploymentSorter(String[] suffixOrder)
   {
      if (suffixOrder == null)
         throw new NullArgumentException("suffixOrder");

      this.suffixOrder = suffixOrder;
   }

   public DeploymentSorter()
   {
      this(DEFAULT_SUFFIX_ORDER);
   }
   
   public String[] getSuffixOrder()
   {
      return suffixOrder;
   }
   public void setSuffixOrder(String[] suffixOrder)
   {
      this.suffixOrder = suffixOrder;
   }

   /**
    * Return a negative number if o1 appears lower in the the suffix order than
    * o2.
    * If the suffixes are indentical, then sorts based on name.
    * This is so that deployment order of components is always identical.
    */
   public int compare(Object o1, Object o2) 
   {
      URL u1 = (URL)o1;
      URL u2 = (URL)o2;
      int order = getExtensionIndex(u1) - getExtensionIndex(u2);
      if (order != 0)
          return order;
      return u1.getFile().compareTo(u2.getFile());
   }
   
   /**
    * Return the index that matches this url
    */
   public int getExtensionIndex(URL url)
   {
      String path = url.getPath();
      if (path.endsWith("/")) 
          path = path.substring(0, path.length() - 1);
      int i = 0;
      for (; i < suffixOrder.length; i++)
      {
          if (path.endsWith(suffixOrder[i]))
              break;
      }
      return i;
   }
}