001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.deployment;
023:
024: import java.net.URL;
025: import java.util.Comparator;
026:
027: import org.jboss.util.NullArgumentException;
028:
029: /**
030: * A helper class for sorting deployments.
031: *
032: * @version <tt>$Revision: 57205 $</tt>
033: * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
034: * @author Scott.Stark@jboss.org
035: */
036: public class DeploymentSorter implements Comparator,
037: DefaultDeploymentSorter {
038: /**
039: * The default order for sorting deployments; this has been deprecated,
040: * order is really defined in SuffixOrderHelper class and/or individualy
041: * within each subdeployer.
042: *
043: * @deprecated
044: */
045: public static final String[] DEFAULT_SUFFIX_ORDER = { ".deployer",
046: "-deployer.xml", ".sar", "-service.xml", ".rar", "-ds.xml",
047: ".har", ".jar", ".war", ".wsr", ".ear", ".zip", ".bsh",
048: ".last" };
049:
050: protected String[] suffixOrder;
051:
052: public DeploymentSorter(String[] suffixOrder) {
053: if (suffixOrder == null)
054: throw new NullArgumentException("suffixOrder");
055:
056: this .suffixOrder = suffixOrder;
057: }
058:
059: public DeploymentSorter() {
060: this (DEFAULT_SUFFIX_ORDER);
061: }
062:
063: public String[] getSuffixOrder() {
064: return suffixOrder;
065: }
066:
067: public void setSuffixOrder(String[] suffixOrder) {
068: this .suffixOrder = suffixOrder;
069: }
070:
071: /**
072: * Return a negative number if o1 appears lower in the the suffix order than
073: * o2.
074: * If the suffixes are indentical, then sorts based on name.
075: * This is so that deployment order of components is always identical.
076: */
077: public int compare(Object o1, Object o2) {
078: URL u1 = (URL) o1;
079: URL u2 = (URL) o2;
080: int order = getExtensionIndex(u1) - getExtensionIndex(u2);
081: if (order != 0)
082: return order;
083: return u1.getFile().compareTo(u2.getFile());
084: }
085:
086: /**
087: * Return the index that matches this url
088: */
089: public int getExtensionIndex(URL url) {
090: String path = url.getPath();
091: if (path.endsWith("/"))
092: path = path.substring(0, path.length() - 1);
093: int i = 0;
094: for (; i < suffixOrder.length; i++) {
095: if (path.endsWith(suffixOrder[i]))
096: break;
097: }
098: return i;
099: }
100: }
|