KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > scanner > PrefixDeploymentSorter


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.scanner;
23
24 import java.util.Comparator JavaDoc;
25 import java.net.URL JavaDoc;
26 import org.jboss.deployment.DeploymentSorter;
27 import org.jboss.deployment.DefaultDeploymentSorter;
28
29 /**
30  * <p>This class is a comparator to sort deployment URLs based on the existence
31  * of a numeric prefix. The name portion of the URL is evaluated for any
32  * leading digits. If they exist, then they will define a numerical ordering
33  * for this comparator. If there is no leading digits, then they will
34  * compare as less than any name with leading digits. In the case of a
35  * tie, the DeploymentSorter is consulted (@see org.jboss.deployment.DeploymentSorter).
36  *
37  * <p>Ex.these names are in ascending order:
38  * test.sar, crap.ear, 001test.jar, 5test.rar, 5foo.jar, 120bar.jar
39  */

40 public class PrefixDeploymentSorter implements Comparator JavaDoc, DefaultDeploymentSorter
41 {
42     
43     /** This is used to break ties */
44     private DeploymentSorter sorter = new DeploymentSorter();
45
46    public String JavaDoc[] getSuffixOrder()
47    {
48       return sorter.getSuffixOrder();
49    }
50
51    public void setSuffixOrder(String JavaDoc[] suffixOrder)
52    {
53       sorter.setSuffixOrder(suffixOrder);
54    }
55
56     /**
57      * As described in @see java.util.Comparator. This implements the
58      * comparison technique described above.
59      */

60     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
61         int comp = getPrefixValue((URL JavaDoc)o1) - getPrefixValue((URL JavaDoc)o2);
62         
63         return comp == 0 ? sorter.compare(o1, o2) : comp;
64     }
65     
66     /**
67      * This extracts the prefix value from the name of a URL. If no prefix
68      * value exists, this returns -1
69      */

70     private int getPrefixValue(URL JavaDoc url) {
71         String JavaDoc path = url.getPath();
72         int nameEnd = path.length() - 1;
73         if (nameEnd <= 0) {
74             return 0;
75         }
76         
77         // ignore a trailing '/'
78
if (path.charAt(nameEnd) == '/') {
79             nameEnd--;
80         }
81         
82         // find the previous URL separator: '/'
83
int nameStart = path.lastIndexOf('/', nameEnd) + 1;
84         
85         // calculate where the digit-prefix ends
86
int prefixEnd = nameStart;
87         while (prefixEnd <= nameEnd && Character.isDigit(path.charAt(prefixEnd))) {
88             prefixEnd++;
89         }
90         
91         // If zero length prefix, return -1
92
if (prefixEnd == nameStart) {
93             return -1;
94         }
95         
96         // strip leading zeroes
97
while (nameStart < prefixEnd && path.charAt(nameStart) == '0') {
98             nameStart++;
99         }
100         
101         return (nameStart == prefixEnd) ? 0 : Integer.parseInt(path.substring(nameStart, prefixEnd));
102     }
103 }
104
Popular Tags