KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > context > jar > JarUtils


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.virtual.plugins.context.jar;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.concurrent.CopyOnWriteArraySet JavaDoc;
28
29 /**
30  * JarUtils.
31  *
32  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
33  * @version $Revision: 1.1 $
34  */

35 public class JarUtils
36 {
37    /** The jar suffixes */
38    private static Set JavaDoc<String JavaDoc> jarSuffixes = new CopyOnWriteArraySet JavaDoc<String JavaDoc>();
39
40    // Initialise known suffixes
41
static
42    {
43       jarSuffixes.add(".zip");
44       jarSuffixes.add(".ear");
45       jarSuffixes.add(".jar");
46       jarSuffixes.add(".rar");
47       jarSuffixes.add(".war");
48       jarSuffixes.add(".sar");
49       jarSuffixes.add(".har");
50       jarSuffixes.add(".aop");
51    }
52
53    /**
54     * Sets the jar suffixes
55     *
56     * @param suffixes the suffixes
57     * @throws IllegalArgumentException for a null suffix
58     */

59    public static void setJarSuffixes(Set JavaDoc<String JavaDoc> suffixes)
60    {
61       if (suffixes == null)
62          throw new IllegalArgumentException JavaDoc("Null suffix");
63       jarSuffixes = suffixes;
64    }
65
66    /**
67     * Add a jar suffix
68     *
69     * @param suffix the suffix
70     * @return true when added
71     * @throws IllegalArgumentException for a null suffix
72     */

73    public static boolean addJarSuffix(String JavaDoc suffix)
74    {
75       if (suffix == null)
76          throw new IllegalArgumentException JavaDoc("Null suffix");
77       return jarSuffixes.add(suffix);
78    }
79
80    /**
81     * Remove a jar suffix
82     *
83     * @param suffix the suffix
84     * @return true when removed
85     * @throws IllegalArgumentException for a null suffix
86     */

87    public static boolean removeJarSuffix(String JavaDoc suffix)
88    {
89       if (suffix == null)
90          throw new IllegalArgumentException JavaDoc("Null suffix");
91       return jarSuffixes.remove(suffix);
92    }
93    
94    /**
95     * Get the lis of jar suffixes
96     *
97     * @return the list of suffixes
98     */

99    public static Set JavaDoc<String JavaDoc> getSuffixes()
100    {
101       return jarSuffixes;
102    }
103
104    /**
105     * Clear the list of suffixes
106     *
107     */

108    public static void clearSuffixes()
109    {
110       jarSuffixes.clear();
111    }
112
113    /**
114     * Utilities
115     */

116    private JarUtils()
117    {
118    }
119    
120    /**
121     * Whether this is an archive
122     *
123     * @param name the name
124     * @return true when an archive
125     * @throws IllegalArgumentException for a null name
126     */

127    public static boolean isArchive(String JavaDoc name)
128    {
129       if (name == null)
130          throw new IllegalArgumentException JavaDoc("Null name");
131       
132       int index = name.lastIndexOf('.');
133       if (index == -1)
134          return false;
135       String JavaDoc suffix = name.substring(index);
136       return jarSuffixes.contains(suffix);
137    }
138    
139    /**
140     * Create a jar url from a normal url
141     *
142     * @param url the normal url
143     * @return the jar url
144     * @throws MalformedURLException if the url is malformed
145     * @throws IllegalArgumentException for a null url
146     */

147    public static URL JavaDoc createJarURL(URL JavaDoc url) throws MalformedURLException JavaDoc
148    {
149       if (url == null)
150          throw new IllegalArgumentException JavaDoc("Null url");
151       return new URL JavaDoc("jar:" + url + "!/");
152    }
153 }
154
Popular Tags