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