View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.chainsaw.help;
19  
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.net.URLClassLoader;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.log4j.LogManager;
29  import org.apache.log4j.Logger;
30  import org.apache.log4j.chainsaw.messages.MessageCenter;
31  
32  /***
33   * A helper class that assists the HelpManager by serving as a collection of
34   * Class loaders based on URL roots.
35   * 
36   * @author Paul Smith
37   *         <psmith@apache.org>
38   */
39  class HelpLocator {
40    private List classLoaders = new ArrayList();
41    private static Logger logger = LogManager.getLogger(HelpLocator.class);
42    HelpLocator() {
43    }
44  
45    /***
46     * Adds a ClassLoader to be used as a help resource locator
47     */
48    void installClassloaderLocator(ClassLoader cl) {
49      classLoaders.add(cl);
50    }
51    /***
52     * Adds a new locator to the current set of locators by using the passed in
53     * URL as a base to locate help resources. The URL SHOULD end in a '/'
54     * character.
55     */
56    void installLocator(URL url) {
57      try {
58        classLoaders.add(new HelpResourceLoader(url));
59      } catch (Exception e) {
60        MessageCenter.getInstance().getLogger().error(
61          "Failed to setup the resoure loaders for the Help Subsystem");
62      }
63    }
64  
65    /***
66     * Locates a help resource by using the internal resource locator collection.
67     * 
68     * @return URL of the located resource, or null if it cannot be located.
69     */
70    URL findResource(String name) {
71      URL url = null;
72  
73      for (Iterator iter = classLoaders.iterator(); iter.hasNext();) {
74        ClassLoader loader = (ClassLoader) iter.next();
75        url = loader.getResource(name);
76  
77        if (url != null) {
78          break;
79        }
80      }
81  
82      return url;
83    }
84  
85    private static class HelpResourceLoader extends ClassLoader {
86      private URL root;
87  
88      private HelpResourceLoader(URL root) {
89        this.root = root;
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see java.lang.ClassLoader#findResource(java.lang.String)
96       */
97      protected URL findResource(String name) {
98        URL url = super.findResource(name);
99  
100       if (url != null) {
101         return url;
102       }
103 
104       try {
105         URL resourceURL = new URL(root, name);
106         URL[] urlArray = new URL[] { root, resourceURL };
107         logger.debug("Looking for Help resource at:" + resourceURL.toExternalForm());
108         logger.debug("urlArray=" + Arrays.asList(urlArray));
109         return new URLClassLoader(
110           urlArray).findResource(
111           name);
112       } catch (MalformedURLException e) {
113         e.printStackTrace();
114       }
115 
116       return null;
117     }
118   }
119 
120   //  public static void main(String[] args) throws Exception {
121   //    HelpLocator locator = new HelpLocator();
122   //    locator.installLocator(new File(".").toURL());
123   //    locator.installLocator(new
124   // URL("http://java.sun.com/j2se/1.4.2/docs/api/"));
125   //    String[] resources =
126   //      new String[] { "build.properties", "java/lang/ClassLoader.html", };
127   //
128   //    for (int i = 0; i < resources.length; i++) {
129   //      String resource = resources[i];
130   //      URL url = locator.findResource(resource);
131   //      System.out.println("resource=" + resource + ", url=" + url);
132   //    }
133   //  }
134 }