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.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 &lt;psmith@apache.org&gt;
    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  //    TODO setup all the base URLs in the default.properties and configure in ApplicationPreferenceModel
    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              // TODO: handle exception
    70          }
    71
    72          helpLocator.installClassloaderLocator(this.getClass().getClassLoader());
    73  //      helpLocator.installLocator(new URL());
    74      }
    75
    76      /**
    77       * The current Help URL that should be displayed, and is
    78       * a PropertyChangeListener supported property.
    79       *
    80       * This method ALWAYS fires property change events
    81       * even if the value is the same (the oldvalue
    82       * of the event will be null)
    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 }