1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
94
95
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134 }