FileDocCategorySizeDatePackage
ConfigurationManager.javaAPI DocApache log4j 1.2.1516801Sat Aug 25 00:09:38 BST 2007org.apache.log4j.lf5.viewer.configure

ConfigurationManager

public class ConfigurationManager extends Object

ConfigurationManager handles the storage and retrival of the state of the CategoryExplorer

author
Richard Hurst
author
Brad Marlborough

Fields Summary
private static final String
CONFIG_FILE_NAME
private static final String
NAME
private static final String
PATH
private static final String
SELECTED
private static final String
EXPANDED
private static final String
CATEGORY
private static final String
FIRST_CATEGORY_NAME
private static final String
LEVEL
private static final String
COLORLEVEL
private static final String
RED
private static final String
GREEN
private static final String
BLUE
private static final String
COLUMN
private static final String
NDCTEXTFILTER
private LogBrokerMonitor
_monitor
private LogTable
_table
Constructors Summary
public ConfigurationManager(LogBrokerMonitor monitor, LogTable table)


  //--------------------------------------------------------------------------
  //   Constructors:
  //--------------------------------------------------------------------------
       
    super();
    _monitor = monitor;
    _table = table;
    load();
  
Methods Summary
private voidcloseConfigurationXML(java.lang.StringBuffer xml)

    xml.append("</configuration>\r\n");
  
protected voidcollapseTree()

    // collapse everything except the first category
    CategoryExplorerTree tree = _monitor.getCategoryExplorerTree();
    for (int i = tree.getRowCount() - 1; i > 0; i--) {
      tree.collapseRow(i);
    }
  
protected voiddeleteConfigurationFile()

    try {
      File f = new File(getFilename());
      if (f.exists()) {
        f.delete();
      }
    } catch (SecurityException e) {
      System.err.println("Cannot delete " + getFilename() +
          " because a security violation occured.");
    }
  
private voidexportLogLevelColorXMLElement(java.lang.String label, java.awt.Color color, java.lang.StringBuffer xml)

    xml.append("\t\t<").append(COLORLEVEL).append(" ").append(NAME);
    xml.append("=\"").append(label).append("\" ");
    xml.append(RED).append("=\"").append(color.getRed()).append("\" ");
    xml.append(GREEN).append("=\"").append(color.getGreen()).append("\" ");
    xml.append(BLUE).append("=\"").append(color.getBlue());
    xml.append("\"/>\r\n");
  
private voidexportLogLevelXMLElement(java.lang.String label, boolean selected, java.lang.StringBuffer xml)

    xml.append("\t\t<").append(LEVEL).append(" ").append(NAME);
    xml.append("=\"").append(label).append("\" ");
    xml.append(SELECTED).append("=\"").append(selected);
    xml.append("\"/>\r\n");
  
private voidexportLogTableColumnXMLElement(java.lang.String label, boolean selected, java.lang.StringBuffer xml)

    xml.append("\t\t<").append(COLUMN).append(" ").append(NAME);
    xml.append("=\"").append(label).append("\" ");
    xml.append(SELECTED).append("=\"").append(selected);
    xml.append("\"/>\r\n");
  
private voidexportXMLElement(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node, javax.swing.tree.TreePath path, java.lang.StringBuffer xml)

    CategoryExplorerTree tree = _monitor.getCategoryExplorerTree();

    xml.append("\t<").append(CATEGORY).append(" ");
    xml.append(NAME).append("=\"").append(node.getTitle()).append("\" ");
    xml.append(PATH).append("=\"").append(treePathToString(path)).append("\" ");
    xml.append(EXPANDED).append("=\"").append(tree.isExpanded(path)).append("\" ");
    xml.append(SELECTED).append("=\"").append(node.isSelected()).append("\"/>\r\n");
  
protected java.lang.StringgetFilename()

    String home = System.getProperty("user.home");
    String sep = System.getProperty("file.separator");

    return home + sep + "lf5" + sep + CONFIG_FILE_NAME;
  
protected java.lang.StringgetValue(org.w3c.dom.NamedNodeMap map, java.lang.String attr)

    Node n = map.getNamedItem(attr);
    return n.getNodeValue();
  
protected voidload()

    File file = new File(getFilename());
    if (file.exists()) {
      try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.
            newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(file);
        processRecordFilter(doc);
        processCategories(doc);
        processLogLevels(doc);
        processLogLevelColors(doc);
        processLogTableColumns(doc);
      } catch (Exception e) {
        // ignore all error and just continue as if there was no
        // configuration xml file but do report a message
        System.err.println("Unable process configuration file at " +
            getFilename() + ". Error Message=" + e.getMessage());
      }
    }

  
private voidopenConfigurationXML(java.lang.StringBuffer xml)

    xml.append("<configuration>\r\n");
  
private voidopenXMLDocument(java.lang.StringBuffer xml)

    xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n");
  
protected voidprocessCategories(org.w3c.dom.Document doc)

    CategoryExplorerTree tree = _monitor.getCategoryExplorerTree();
    CategoryExplorerModel model = tree.getExplorerModel();
    NodeList nodeList = doc.getElementsByTagName(CATEGORY);

    // determine where the starting node is
    NamedNodeMap map = nodeList.item(0).getAttributes();
    int j = (getValue(map, NAME).equalsIgnoreCase(FIRST_CATEGORY_NAME)) ? 1 : 0;
    // iterate backwards throught the nodeList so that expansion of the
    // list can occur
    for (int i = nodeList.getLength() - 1; i >= j; i--) {
      Node n = nodeList.item(i);
      map = n.getAttributes();
      CategoryNode chnode = model.addCategory(new CategoryPath(getValue(map, PATH)));
      chnode.setSelected((getValue(map, SELECTED).equalsIgnoreCase("true")) ? true : false);
      if (getValue(map, EXPANDED).equalsIgnoreCase("true")) ;
      tree.expandPath(model.getTreePathToRoot(chnode));
    }

  
private voidprocessConfigurationNode(org.apache.log4j.lf5.viewer.categoryexplorer.CategoryNode node, java.lang.StringBuffer xml)

    CategoryExplorerModel model = _monitor.getCategoryExplorerTree().getExplorerModel();

    Enumeration all = node.breadthFirstEnumeration();
    CategoryNode n = null;
    while (all.hasMoreElements()) {
      n = (CategoryNode) all.nextElement();
      exportXMLElement(n, model.getTreePathToRoot(n), xml);
    }

  
private voidprocessLogLevelColors(java.util.Map logLevelMenuItems, java.util.Map logLevelColors, java.lang.StringBuffer xml)

    xml.append("\t<loglevelcolors>\r\n");
    // iterate through the list of log levels being used (log4j, jdk1.4, custom levels)
    Iterator it = logLevelMenuItems.keySet().iterator();
    while (it.hasNext()) {
      LogLevel level = (LogLevel) it.next();
      // for each level, get the associated color from the log level color map
      Color color = (Color) logLevelColors.get(level);
      exportLogLevelColorXMLElement(level.getLabel(), color, xml);
    }

    xml.append("\t</loglevelcolors>\r\n");
  
protected voidprocessLogLevelColors(org.w3c.dom.Document doc)

    NodeList nodeList = doc.getElementsByTagName(COLORLEVEL);
    LogLevel.getLogLevelColorMap();

    for (int i = 0; i < nodeList.getLength(); i++) {
      Node n = nodeList.item(i);
      // check for backwards compatibility since this feature was added
      // in version 1.3
      if (n == null) {
        return;
      }

      NamedNodeMap map = n.getAttributes();
      String name = getValue(map, NAME);
      try {
        LogLevel level = LogLevel.valueOf(name);
        int red = Integer.parseInt(getValue(map, RED));
        int green = Integer.parseInt(getValue(map, GREEN));
        int blue = Integer.parseInt(getValue(map, BLUE));
        Color c = new Color(red, green, blue);
        if (level != null) {
          level.setLogLevelColorMap(level, c);
        }

      } catch (LogLevelFormatException e) {
        // ignore it will be on by default.
      }
    }
  
private voidprocessLogLevels(java.util.Map logLevelMenuItems, java.lang.StringBuffer xml)

    xml.append("\t<loglevels>\r\n");
    Iterator it = logLevelMenuItems.keySet().iterator();
    while (it.hasNext()) {
      LogLevel level = (LogLevel) it.next();
      JCheckBoxMenuItem item = (JCheckBoxMenuItem) logLevelMenuItems.get(level);
      exportLogLevelXMLElement(level.getLabel(), item.isSelected(), xml);
    }

    xml.append("\t</loglevels>\r\n");
  
protected voidprocessLogLevels(org.w3c.dom.Document doc)

    NodeList nodeList = doc.getElementsByTagName(LEVEL);
    Map menuItems = _monitor.getLogLevelMenuItems();

    for (int i = 0; i < nodeList.getLength(); i++) {
      Node n = nodeList.item(i);
      NamedNodeMap map = n.getAttributes();
      String name = getValue(map, NAME);
      try {
        JCheckBoxMenuItem item =
            (JCheckBoxMenuItem) menuItems.get(LogLevel.valueOf(name));
        item.setSelected(getValue(map, SELECTED).equalsIgnoreCase("true"));
      } catch (LogLevelFormatException e) {
        // ignore it will be on by default.
      }
    }
  
private voidprocessLogRecordFilter(java.lang.String text, java.lang.StringBuffer xml)

    xml.append("\t<").append(NDCTEXTFILTER).append(" ");
    xml.append(NAME).append("=\"").append(text).append("\"");
    xml.append("/>\r\n");
  
protected voidprocessLogTableColumns(org.w3c.dom.Document doc)

    NodeList nodeList = doc.getElementsByTagName(COLUMN);
    Map menuItems = _monitor.getLogTableColumnMenuItems();
    List selectedColumns = new ArrayList();
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node n = nodeList.item(i);
      // check for backwards compatibility since this feature was added
      // in version 1.3
      if (n == null) {
        return;
      }
      NamedNodeMap map = n.getAttributes();
      String name = getValue(map, NAME);
      try {
        LogTableColumn column = LogTableColumn.valueOf(name);
        JCheckBoxMenuItem item =
            (JCheckBoxMenuItem) menuItems.get(column);
        item.setSelected(getValue(map, SELECTED).equalsIgnoreCase("true"));

        if (item.isSelected()) {
          selectedColumns.add(column);
        }
      } catch (LogTableColumnFormatException e) {
        // ignore it will be on by default.
      }

      if (selectedColumns.isEmpty()) {
        _table.setDetailedView();
      } else {
        _table.setView(selectedColumns);
      }

    }
  
private voidprocessLogTableColumns(java.util.List logTableColumnMenuItems, java.lang.StringBuffer xml)

    xml.append("\t<logtablecolumns>\r\n");
    Iterator it = logTableColumnMenuItems.iterator();
    while (it.hasNext()) {
      LogTableColumn column = (LogTableColumn) it.next();
      JCheckBoxMenuItem item = _monitor.getTableColumnMenuItem(column);
      exportLogTableColumnXMLElement(column.getLabel(), item.isSelected(), xml);
    }

    xml.append("\t</logtablecolumns>\r\n");
  
protected voidprocessRecordFilter(org.w3c.dom.Document doc)

    NodeList nodeList = doc.getElementsByTagName(NDCTEXTFILTER);

    // there is only one value stored
    Node n = nodeList.item(0);
    // add check for backwards compatibility  as this feature was added in
    // version 1.2
    if (n == null) {
      return;
    }

    NamedNodeMap map = n.getAttributes();
    String text = getValue(map, NAME);

    if (text == null || text.equals("")) {
      return;
    }
    _monitor.setNDCLogRecordFilter(text);
  
public voidreset()

    deleteConfigurationFile();
    collapseTree();
    selectAllNodes();
  
public voidsave()

    CategoryExplorerModel model = _monitor.getCategoryExplorerTree().getExplorerModel();
    CategoryNode root = model.getRootCategoryNode();

    StringBuffer xml = new StringBuffer(2048);
    openXMLDocument(xml);
    openConfigurationXML(xml);
    processLogRecordFilter(_monitor.getNDCTextFilter(), xml);
    processLogLevels(_monitor.getLogLevelMenuItems(), xml);
    processLogLevelColors(_monitor.getLogLevelMenuItems(),
        LogLevel.getLogLevelColorMap(), xml);
    processLogTableColumns(LogTableColumn.getLogTableColumns(), xml);
    processConfigurationNode(root, xml);
    closeConfigurationXML(xml);
    store(xml.toString());
  
protected voidselectAllNodes()

    CategoryExplorerModel model = _monitor.getCategoryExplorerTree().getExplorerModel();
    CategoryNode root = model.getRootCategoryNode();
    Enumeration all = root.breadthFirstEnumeration();
    CategoryNode n = null;
    while (all.hasMoreElements()) {
      n = (CategoryNode) all.nextElement();
      n.setSelected(true);
    }
  
protected voidstore(java.lang.String s)


    try {
      PrintWriter writer = new PrintWriter(new FileWriter(getFilename()));
      writer.print(s);
      writer.close();
    } catch (IOException e) {
      // do something with this error.
      e.printStackTrace();
    }

  
public static java.lang.StringtreePathToString(javax.swing.tree.TreePath path)

    // count begins at one so as to not include the 'Categories' - root category
    StringBuffer sb = new StringBuffer();
    CategoryNode n = null;
    Object[] objects = path.getPath();
    for (int i = 1; i < objects.length; i++) {
      n = (CategoryNode) objects[i];
      if (i > 1) {
        sb.append(".");
      }
      sb.append(n.getTitle());
    }
    return sb.toString();