001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2014-2015 ForgeRock AS.
025 */
026package org.opends.server.loggers;
027
028import java.util.Map.Entry;
029import java.util.NavigableMap;
030import java.util.TreeMap;
031
032/**
033 * Provides mapping from class names to simple category names used for logging.
034 * <p>
035 * Given a classname, eg org.forgerock.opendj.server.core.SomeClass, it allows
036 * to get the corresponding simplified category name if it exists, eg "CORE". If
037 * no simplified category name exist, the classname is used as a category name.
038 */
039public class LoggingCategoryNames
040{
041  /**
042   * Contains mapping from class names (or package names) to category names. In
043   * most case, package name is sufficient to map to a category name. It is
044   * valid if several entries point to the same category name.
045   */
046  private static final NavigableMap<String, String> NAMES = new TreeMap<>();
047  static
048  {
049    // The category used for messages associated with the core server.
050    NAMES.put("org.opends.server.core", "CORE");
051    NAMES.put("org.forgerock.opendj.ldap", "CORE");
052
053    // The category used for messages associated with server extensions
054    // (e.g. extended operations, SASL mechanisms, password storage, schemes, password validators, etc.).
055    NAMES.put("org.opends.server.extensions", "EXTENSIONS");
056
057    // The category used for messages associated with
058    // connection and protocol handling (e.g., ASN.1 and LDAP).
059    NAMES.put("org.opends.server.protocol", "PROTOCOL");
060    NAMES.put("org.forgerock.opendj.io", "PROTOCOL");
061
062    // The category used for messages associated with configuration handling.
063    NAMES.put("org.opends.server.config", "CONFIG");
064
065    // The category used for messages associated with the server loggers.
066    NAMES.put("org.opends.server.loggers", "LOG");
067
068    // The category used for messages associated with the general server utilities.
069    NAMES.put("org.opends.server.util", "UTIL");
070
071    // The category used for messages associated with the server schema elements.
072    NAMES.put("org.opends.server.schema", "SCHEMA");
073    NAMES.put("org.forgerock.opendj.ldap.schema", "SCHEMA");
074
075    // The category used for messages associated with the server controls.
076    NAMES.put("org.opends.server.controls", "CONTROLS");
077    NAMES.put("org.forgerock.opendj.ldap.controls", "CONTROLS");
078
079    // The category that will be used for messages associated with plugin processing.
080    NAMES.put("org.opends.server.plugins", "PLUGIN");
081
082    // The category used for messages associated with the JE backend.
083    NAMES.put("org.opends.server.backends.jeb", "JEB");
084
085    // The category used for messages associated with the pluggable backend.
086    NAMES.put("org.opends.server.backends.pluggable", "PLUGGABLE");
087
088    // The category used for messages associated with the PDB backend.
089    NAMES.put("org.opends.server.backends.pdb", "PDB");
090
091    // The category used for messages associated with generic backends.
092    NAMES.put("org.opends.server.backends", "BACKEND");
093
094    // The category used for messages associated with tools
095    NAMES.put("org.opends.server.tools", "TOOLS");
096
097    // The category used for messages associated with upgrade tool
098    NAMES.put("org.opends.server.tools.upgrade", "UPGRADE");
099
100    // The category used for messages associated with tasks
101    NAMES.put("org.opends.server.tasks", "TASK");
102
103    // The category used for messages associated with Access Control
104    NAMES.put("org.opends.server.authorization", "ACCESS_CONTROL");
105
106    // The category used for messages associated with the administration framework.
107    NAMES.put("org.opends.server.admin", "ADMIN");
108
109    // The category used for messages associated with the Synchronization
110    NAMES.put("org.opends.server.replication", "SYNC");
111
112    // The category used for messages associated with quicksetup tools
113    NAMES.put("org.opends.quicksetup", "QUICKSETUP");
114
115    // The category used for messages associated with the tool like the offline installer and unintaller.
116    NAMES.put("org.opends.quicksetup.offline", "ADMIN_TOOL");
117    NAMES.put("org.opends.guitools.uninstaller", "ADMIN_TOOL");
118
119    // The category used for messages associated with the dsconfig
120    // administration tool.
121    NAMES.put("org.opends.admin.ads", "DSCONFIG");
122
123    // The category used for messages associated with common audit.
124    NAMES.put("org.forgerock.audit", "AUDIT");
125  }
126
127  /**
128   * Returns the simple category name corresponding to the provided class name
129   * or the class name if no mapping corresponds.
130   *
131   * @param className
132   *          The classname to retrieve the category name from.
133   * @return the simple category name, or the provided className if no matching
134   *         simple category name is found
135   */
136  public static String getCategoryName(final String className)
137  {
138    final Entry<String, String> entry = NAMES.floorEntry(className);
139    if (entry != null && className.startsWith(entry.getKey()))
140    {
141      return entry.getValue();
142    }
143    return className;
144  }
145}