KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileFilter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Comparator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.net.URL JavaDoc;
31
32 import org.jboss.net.protocol.URLLister;
33
34 /**
35  * <p>A simple filter for the URLDeploymentScanner. Three arrays are
36  * maintained for checking: a prefix, suffix, and match array. If the
37  * filename starts with any of the prefixes, ends with any of the
38  * suffixes, or exactly matches any of the matches, then the accepts
39  * method will return false.
40  *
41  * NOTE: the arrays *must* be sorted for the string matching to work,
42  * and suffixes use the 'reverseComparator'
43  *
44  * @author somebody@jboss.org
45  * @version $Revision: 57108 $
46  */

47 public class DeploymentFilter implements FileFilter JavaDoc, URLLister.URLFilter
48 {
49    /**
50     * Compare the strings backwards. This assists in suffix comparisons.
51     */

52    private static final Comparator JavaDoc reverseComparator = new Comparator JavaDoc()
53    {
54       public int compare(Object JavaDoc o1, Object JavaDoc o2)
55       {
56          int idx1 = ((String JavaDoc) o1).length();
57          int idx2 = ((String JavaDoc) o2).length();
58          int comp = 0;
59
60          while (comp == 0 && idx1 > 0 && idx2 > 0)
61             comp = ((String JavaDoc) o1).charAt(--idx1) - ((String JavaDoc) o2).charAt(--idx2);
62
63          return (comp == 0) ? (idx1 - idx2) : comp;
64       }
65    };
66
67    /** the default prefix list */
68    private static final String JavaDoc[] DEFAULT_PREFIXES =
69       {"#", "%", ",", ".", "_$"};
70
71    /** the default suffix list */
72    private static final String JavaDoc[] DEFAULT_SUFFIXES =
73       {"#", "$", "%", "~", ",v",
74           ".BAK", ".bak", ".old", ".orig", ".tmp", ".rej", ".sh" };
75
76    /** the default matches list */
77    private static final String JavaDoc[] DEFAULT_MATCHES =
78       {".make.state", ".nse_depinfo", "CVS", "CVS.admin", "RCS", "RCSLOG",
79           "SCCS", "TAGS", "core", "tags"};
80
81    /** The list of disallowed suffixes, sorted using reverse values */
82    private ArrayList JavaDoc suffixes;
83
84    /** The sorted list of disallowed prefixes */
85    private ArrayList JavaDoc prefixes;
86
87    /** The sorted list of disallowed values */
88    private ArrayList JavaDoc matches;
89
90    /** Use the default values for suffixes, prefixes, and matches */
91    public DeploymentFilter()
92    {
93       this(DEFAULT_MATCHES, DEFAULT_PREFIXES, DEFAULT_SUFFIXES);
94    }
95
96    /**
97     * Create using a custom set of matches, prefixes, and suffixes. If any of
98     * these arrays are null, then the corresponding default will be
99     * substituted.
100     */

101    public DeploymentFilter(String JavaDoc[] matches, String JavaDoc[] prefixes, String JavaDoc[] suffixes)
102    {
103       if( matches == null )
104          matches = DEFAULT_MATCHES;
105       Arrays.sort(matches);
106       this.matches = new ArrayList JavaDoc(Arrays.asList(matches));
107
108       if( prefixes == null )
109          prefixes = DEFAULT_PREFIXES;
110       Arrays.sort(prefixes);
111       this.prefixes = new ArrayList JavaDoc(Arrays.asList(prefixes));
112
113       if( suffixes == null )
114          suffixes = DEFAULT_SUFFIXES;
115       Arrays.sort(suffixes, reverseComparator);
116       this.suffixes = new ArrayList JavaDoc(Arrays.asList(suffixes));
117    }
118
119    public void addPrefix(String JavaDoc prefix)
120    {
121       this.prefixes.add(prefix);
122       Collections.sort(this.prefixes);
123    }
124    public void addPrefixes(String JavaDoc[] prefixes)
125    {
126       this.prefixes.add(Arrays.asList(prefixes));
127       Collections.sort(this.prefixes);
128    }
129
130    public void delPrefix(String JavaDoc prefix)
131    {
132       this.prefixes.remove(prefix);
133    }
134    public void delPrefixes(String JavaDoc[] prefixes)
135    {
136       this.prefixes.removeAll(Arrays.asList(prefixes));
137       Collections.sort(this.prefixes);
138    }
139
140    public void addSuffix(String JavaDoc suffix)
141    {
142       this.suffixes.add(suffix);
143       Collections.sort(this.suffixes, reverseComparator);
144    }
145    public void addSuffixes(String JavaDoc[] suffixes)
146    {
147       this.suffixes.add(Arrays.asList(suffixes));
148       Collections.sort(this.suffixes, reverseComparator);
149    }
150
151    public void delSuffix(String JavaDoc suffix)
152    {
153       this.suffixes.remove(suffix);
154    }
155    public void delSuffixes(String JavaDoc[] suffixes)
156    {
157       this.suffixes.removeAll(Arrays.asList(suffixes));
158       Collections.sort(this.suffixes, reverseComparator);
159    }
160
161    public String JavaDoc[] getSuffixes()
162    {
163       String JavaDoc[] tmp = new String JavaDoc[suffixes.size()];
164       suffixes.toArray(tmp);
165       return tmp;
166    }
167    public void setSuffixes(String JavaDoc[] suffixes)
168    {
169       Arrays.sort(suffixes, reverseComparator);
170       this.suffixes.clear();
171       this.suffixes.addAll(Arrays.asList(suffixes));
172    }
173
174    public String JavaDoc[] getPrefixes()
175    {
176       String JavaDoc[] tmp = new String JavaDoc[prefixes.size()];
177       prefixes.toArray(tmp);
178       return tmp;
179    }
180    public void setPrefixes(String JavaDoc[] prefixes)
181    {
182       Arrays.sort(prefixes);
183       this.prefixes.clear();
184       this.prefixes.addAll(Arrays.asList(prefixes));
185    }
186
187    public String JavaDoc[] getMatches()
188    {
189       String JavaDoc[] tmp = new String JavaDoc[matches.size()];
190       matches.toArray(tmp);
191       return tmp;
192    }
193    public void setMatches(String JavaDoc[] matches)
194    {
195       Arrays.sort(matches);
196       this.matches.clear();
197       this.matches.addAll(Arrays.asList(matches));
198    }
199
200    /**
201     * If the filename matches any string in the prefix, suffix, or matches
202     * array, return false. Perhaps a bit of overkill, but this method
203     * operates in log(n) time, where n is the size of the arrays.
204     *
205     * @param file The file to be tested
206     * @return <code>false</code> if the filename matches any of the prefixes,
207     * suffixes, or matches.
208     */

209    public boolean accept(File JavaDoc file)
210    {
211       return accept(file.getName());
212    }
213
214    public boolean accept(URL JavaDoc baseURL, String JavaDoc memberName)
215    {
216       return accept(memberName);
217    }
218
219    private boolean accept(String JavaDoc name)
220    {
221       // check exact match
222
int index = Collections.binarySearch(matches, name);
223       if (index >= 0)
224          return false;
225
226       // check prefix
227
index = Collections.binarySearch(prefixes, name);
228       if (index >= 0)
229          return false;
230       if (index < -1)
231       {
232          // The < 0 index gives the first index greater than name
233
int firstLessIndex = -2 - index;
234          String JavaDoc prefix = (String JavaDoc) prefixes.get(firstLessIndex);
235          // If name starts with an ingored prefix ignore name
236
if( name.startsWith(prefix) )
237             return false;
238       }
239
240       // check suffix
241
index = Collections.binarySearch(suffixes, name, reverseComparator);
242       if (index >= 0)
243          return false;
244       if (index < -1)
245       {
246          // The < 0 index gives the first index greater than name
247
int firstLessIndex = -2 - index;
248          String JavaDoc suffix = (String JavaDoc) suffixes.get(firstLessIndex);
249          // If name ends with an ingored suffix ignore name
250
if( name.endsWith(suffix) )
251             return false;
252       }
253
254       // everything checks out.
255
return true;
256    }
257 }
258
Popular Tags