01: /**
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 1999 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19: * USA
20: *
21: * Initial developer(s): Guillaume SAUTHIER
22: * --------------------------------------------------------------------------
23: * $Id: Locator.java 4622 2004-04-19 13:49:54Z sauthieg $
24: * --------------------------------------------------------------------------
25: */package org.objectweb.jonas_lib.loader.locator;
26:
27: import java.util.List;
28: import java.net.URL;
29: import java.io.IOException;
30:
31: /**
32: * A <code>Locator</code> is used to hide System specific
33: * when looking for a file.
34: *
35: * @author Guillaume Sauthier
36: */
37: public abstract class Locator {
38:
39: /**
40: * Returns true when file was found.
41: *
42: * @param path the path to the file to look up
43: *
44: * @return true when file was found, otherwise false.
45: */
46: public abstract boolean hasFile(String path);
47:
48: /**
49: * Returns true when directory was found.
50: *
51: * @param path the path to the directory to look up
52: *
53: * @return true when directory was found, otherwise false.
54: */
55: public abstract boolean hasDirectory(String path);
56:
57: /**
58: * Returns a list of filename stored in path.
59: *
60: * @param path the path to the directory where looking for files
61: *
62: * @return a list of filename stored in path.
63: */
64: public abstract List listContent(String path);
65:
66: /**
67: * Return a new Locator in function of the URL type.
68: * an URL pointing to a jar file will return a <code>JarFileLocator</code>
69: * and an URL pointing to a directory file will return a <code>DirFileLocator</code>.
70: *
71: * @param url the base URL
72: *
73: * @return a new Locator in function of the URL type.
74: *
75: * @throws IOException when cannot find a specialized locator for the given URL.
76: */
77: public static Locator getLocator(URL url) throws IOException {
78:
79: String path = url.getPath();
80: if (path.matches(".*\\..ar")) {
81: // jar detected if path ends with .?ar (*.jar, *.war, *.ear)
82: return new JarFileLocator(url);
83: } else if (path.endsWith("/")) {
84: // directory detected if url ends with a separator /
85: return new DirLocator(url);
86: } else {
87: String err = "Unsupported URL '" + url + "' support "
88: + "only jar archive and directory";
89: throw new IOException(err);
90: }
91: }
92: }
|