KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > protocol > URLListerFactory


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.net.protocol;
23
24 import java.net.URL JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 public class URLListerFactory {
29    private static HashMap JavaDoc defaultClasses = new HashMap JavaDoc();
30    static {
31       defaultClasses.put("file", "org.jboss.net.protocol.file.FileURLLister");
32       defaultClasses.put("http", "org.jboss.net.protocol.http.DavURLLister");
33       defaultClasses.put("https", "org.jboss.net.protocol.http.DavURLLister");
34    }
35
36    private HashMap JavaDoc classes;
37
38    /**
39     * Create a URLLister with default listers defined for file and http
40     * protocols.
41     */

42    public URLListerFactory() {
43       classes = (HashMap JavaDoc) defaultClasses.clone();
44    }
45
46    /**
47     * Create a URL lister using the protocol from the URL
48     * @param url the url defining the protocol
49     * @return a URLLister capable of listing URLs of that protocol
50     * @throws MalformedURLException if no lister could be found for the protocol
51     */

52    public URLLister createURLLister(URL JavaDoc url) throws MalformedURLException JavaDoc {
53       return createURLLister(url.getProtocol());
54    }
55
56    /**
57     * Create a URL lister for the supplied protocol
58     * @param protocol the protocol
59     * @return a URLLister capable of listing URLs of that protocol
60     * @throws MalformedURLException if no lister could be found for the protocol
61     */

62    public URLLister createURLLister(String JavaDoc protocol) throws MalformedURLException JavaDoc {
63       try {
64          String JavaDoc className = (String JavaDoc) classes.get(protocol);
65          if (className == null) {
66             throw new MalformedURLException JavaDoc("No lister class defined for protocol "+protocol);
67          }
68
69          Class JavaDoc clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
70          return (URLLister) clazz.newInstance();
71       } catch (ClassNotFoundException JavaDoc e) {
72          throw new MalformedURLException JavaDoc(e.getMessage());
73       } catch (InstantiationException JavaDoc e) {
74          throw new MalformedURLException JavaDoc(e.getMessage());
75       } catch (IllegalAccessException JavaDoc e) {
76          throw new MalformedURLException JavaDoc(e.getMessage());
77       }
78    }
79
80    /**
81     * Register a URLLister class for a given protocol
82     * @param protocol the protocol this class will handle
83     * @param className the URLLister implementation to instanciate
84     */

85    public void registerListener(String JavaDoc protocol, String JavaDoc className) {
86       classes.put(protocol, className);
87    }
88 }
89
Popular Tags