KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > DeploymentSorter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.deployment;
23
24 import java.net.URL JavaDoc;
25 import java.util.Comparator JavaDoc;
26
27 import org.jboss.util.NullArgumentException;
28
29 /**
30  * A helper class for sorting deployments.
31  *
32  * @version <tt>$Revision: 57108 $</tt>
33  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
34  * @author Scott.Stark@jboss.org
35  */

36 public class DeploymentSorter implements Comparator JavaDoc, DefaultDeploymentSorter
37 {
38    /**
39     * The default order for sorting deployments; this has been deprecated,
40     * order is really defined in SuffixOrderHelper class and/or individualy
41     * within each subdeployer.
42     *
43     * @deprecated
44     */

45    public static final String JavaDoc[] DEFAULT_SUFFIX_ORDER = {
46        ".deployer", "-deployer.xml", ".sar", "-service.xml", ".rar", "-ds.xml",
47        ".har", ".jar", ".war", ".wsr", ".ear", ".zip", ".bsh", ".last"
48    };
49
50    protected String JavaDoc[] suffixOrder;
51
52    public DeploymentSorter(String JavaDoc[] suffixOrder)
53    {
54       if (suffixOrder == null)
55          throw new NullArgumentException("suffixOrder");
56
57       this.suffixOrder = suffixOrder;
58    }
59
60    public DeploymentSorter()
61    {
62       this(DEFAULT_SUFFIX_ORDER);
63    }
64    
65    public String JavaDoc[] getSuffixOrder()
66    {
67       return suffixOrder;
68    }
69    public void setSuffixOrder(String JavaDoc[] suffixOrder)
70    {
71       this.suffixOrder = suffixOrder;
72    }
73
74    /**
75     * Return a negative number if o1 appears lower in the the suffix order than
76     * o2.
77     * If the suffixes are indentical, then sorts based on name.
78     * This is so that deployment order of components is always identical.
79     */

80    public int compare(Object JavaDoc o1, Object JavaDoc o2)
81    {
82       URL JavaDoc u1 = (URL JavaDoc)o1;
83       URL JavaDoc u2 = (URL JavaDoc)o2;
84       int order = getExtensionIndex(u1) - getExtensionIndex(u2);
85       if (order != 0)
86           return order;
87       return u1.getFile().compareTo(u2.getFile());
88    }
89    
90    /**
91     * Return the index that matches this url
92     */

93    public int getExtensionIndex(URL JavaDoc url)
94    {
95       String JavaDoc path = url.getPath();
96       if (path.endsWith("/"))
97           path = path.substring(0, path.length() - 1);
98       int i = 0;
99       for (; i < suffixOrder.length; i++)
100       {
101           if (path.endsWith(suffixOrder[i]))
102               break;
103       }
104       return i;
105    }
106 }
107
Popular Tags