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