/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt 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.deployment;

import java.util.Comparator;

import java.net.URL;

import org.jboss.util.NullArgumentException;

/**
 * A helper class for sorting deployments.
 *
 * @version <tt>$Revision: 1.5.2.7 $</tt>
 * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
 */
public class DeploymentSorter implements Comparator
{
   /** The default order for sorting deployments. */
   public static final String[] DEFAULT_SUFFIX_ORDER = {
       "aop", "aop.xml", "sar", "rar", "ds.xml", "service.xml", "har", "jar", "war", "wsr", "ear", "zip", "last" 
   };

   protected String[] suffixOrder;

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

      this.suffixOrder = suffixOrder;
   }

   public DeploymentSorter()
   {
      this(DEFAULT_SUFFIX_ORDER);
   }
   
   /**
    * 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;
   }
}
