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.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22 import java.beans.PropertyChangeSupport;
23 import java.io.File;
24 import java.net.URL;
25
26 import org.apache.log4j.LogManager;
27 import org.apache.log4j.Logger;
28 import org.apache.log4j.chainsaw.ChainsawConstants;
29
30
31 /***
32 * Singleton help manager where objects can register to display
33 * Help for something, an independant viewer can register to
34 * be notified when the requested Help URL changes and can display
35 * it appropriately. This class effectively decouples the help requester
36 * from the help implementation (if any!)
37 *
38 * @author Paul Smith <psmith@apache.org>
39 *
40 */
41 public final class HelpManager {
42
43 private static final HelpManager instance = new HelpManager();
44 private HelpLocator helpLocator = new HelpLocator();
45 private URL helpURL;
46 private final PropertyChangeSupport propertySupport =
47 new PropertyChangeSupport(this);
48 private final Logger logger = LogManager.getLogger(HelpManager.class);
49
50 private HelpManager() {
51
52
53
54 try {
55
56 if (System.getProperty("log4j.chainsaw.localDocs") != null) {
57 logger.info("Adding HelpLocator for localDocs property=" +
58 System.getProperty("log4j.chainsaw.localDocs"));
59 helpLocator.installLocator(new URL(
60 System.getProperty("log4j.chainsaw.localDocs")));
61 }else if(new File("docs/api").exists()) {
62 File dir = new File("docs/api");
63 logger.info("Detected Local JavaDocs at " + dir.toString());
64 helpLocator.installLocator(dir.toURI().toURL());
65 } else {
66 logger.warn("Could not find any local JavaDocs, you might want to consider running 'ant javadoc'. The release version will be able to access Javadocs from the Apache website.");
67 }
68 } catch (Exception e) {
69
70 }
71
72 helpLocator.installClassloaderLocator(this.getClass().getClassLoader());
73
74 }
75
76 /***
77 * @return URL
78 */
79 public final URL getHelpURL() {
80
81 return helpURL;
82 }
83
84 /***
85 * The current Help URL that should be displayed, and is
86 * a PropertyChangeListener supported property.
87 *
88 * This method ALWAYS fires property change events
89 * even if the value is the same (the oldvalue
90 * of the event will be null)
91 * @param helpURL
92 */
93 public final void setHelpURL(URL helpURL) {
94 this.helpURL = helpURL;
95 firePropertyChange("helpURL", null, this.helpURL);
96 }
97
98 /***
99 * @param listener
100 */
101 public void addPropertyChangeListener(PropertyChangeListener listener) {
102 propertySupport.addPropertyChangeListener(listener);
103 }
104
105 /***
106 * @param propertyName
107 * @param listener
108 */
109 public synchronized void addPropertyChangeListener(String propertyName,
110 PropertyChangeListener listener) {
111 propertySupport.addPropertyChangeListener(propertyName, listener);
112 }
113
114 /***
115 * @param evt
116 */
117 public void firePropertyChange(PropertyChangeEvent evt) {
118 propertySupport.firePropertyChange(evt);
119 }
120
121 /***
122 * @param propertyName
123 * @param oldValue
124 * @param newValue
125 */
126 public void firePropertyChange(String propertyName, boolean oldValue,
127 boolean newValue) {
128 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
129 }
130
131 /***
132 * @param propertyName
133 * @param oldValue
134 * @param newValue
135 */
136 public void firePropertyChange(String propertyName, int oldValue,
137 int newValue) {
138 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
139 }
140
141 /***
142 * @param propertyName
143 * @param oldValue
144 * @param newValue
145 */
146 public void firePropertyChange(String propertyName, Object oldValue,
147 Object newValue) {
148 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
149 }
150
151 /***
152 * @param listener
153 */
154 public synchronized void removePropertyChangeListener(
155 PropertyChangeListener listener) {
156 propertySupport.removePropertyChangeListener(listener);
157 }
158
159 /***
160 * @param propertyName
161 * @param listener
162 */
163 public synchronized void removePropertyChangeListener(String propertyName,
164 PropertyChangeListener listener) {
165 propertySupport.removePropertyChangeListener(propertyName, listener);
166 }
167
168 /***
169 *
170 */
171 public static HelpManager getInstance() {
172
173 return instance;
174 }
175
176 /***
177 * Given a class, and that it belongs within the org.apache.log4j project,
178 * sets the URL to the JavaDoc for that class.
179 *
180 * @param c
181 */
182 public void showHelpForClass(Class c) {
183
184 URL url = getHelpForClass(c);
185 setHelpURL(url);
186 }
187
188 /***
189 * Determines the most appropriate Help resource for a particular class
190 * or returns ChainsawConstants.URL_PAGE_NOT_FOUND if there is no resource located.
191 *
192 * @return URL
193 */
194 public URL getHelpForClass(Class c) {
195
196 String name = c.getName();
197 name = name.replace('.', '/') + ".html";
198
199 URL url = helpLocator.findResource(name);
200 logger.debug("located help resource for '" + name + "' at " +
201 ((url == null) ? "" : url.toExternalForm()));
202
203 return (url != null) ? url : ChainsawConstants.URL_PAGE_NOT_FOUND;
204
205 }
206 }