001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j; 018 019 import java.util.concurrent.ConcurrentHashMap; 020 import java.util.concurrent.ConcurrentMap; 021 022 023 /** 024 * Applications create Markers by using the Marker Manager. All Markers created by this Manager are 025 * immutable. 026 */ 027 public final class MarkerManager { 028 029 private static ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<String, Marker>(); 030 031 private MarkerManager() { 032 } 033 034 /** 035 * Retrieve a Marker or create a Marker that has no parent. 036 * @param name The name of the Marker. 037 * @return The Marker with the specified name. 038 */ 039 public static Marker getMarker(String name) { 040 markerMap.putIfAbsent(name, new Log4JMarker(name)); 041 return markerMap.get(name); 042 } 043 044 /** 045 * Retrieves or creates a Marker with the specified parent. The parent must have been previously created. 046 * @param name The name of the Marker. 047 * @param parent The name of the parent Marker. 048 * @return The Marker with the specified name. 049 * @throws IllegalArgumentException if the parent Marker does not exist. 050 */ 051 public static Marker getMarker(String name, String parent) { 052 Marker parentMarker = markerMap.get(parent); 053 if (parentMarker == null) { 054 throw new IllegalArgumentException("Parent Marker " + parent + " has not been defined"); 055 } 056 return getMarker(name, parentMarker); 057 } 058 059 /** 060 * Retrieves or creates a Marker with the specified parent. 061 * @param name The name of the Marker. 062 * @param parent The parent Marker. 063 * @return The Marker with the specified name. 064 */ 065 public static Marker getMarker(String name, Marker parent) { 066 markerMap.putIfAbsent(name, new Log4JMarker(name, parent)); 067 return markerMap.get(name); 068 } 069 070 /** 071 * The actual Marker implementation. 072 */ 073 private static class Log4JMarker implements Marker { 074 075 private static final long serialVersionUID = 100L; 076 077 private final String name; 078 private final Marker parent; 079 080 public Log4JMarker(String name) { 081 this.name = name; 082 this.parent = null; 083 } 084 085 public Log4JMarker(String name, Marker parent) { 086 this.name = name; 087 this.parent = parent; 088 } 089 090 public String getName() { 091 return this.name; 092 } 093 094 public Marker getParent() { 095 return this.parent; 096 } 097 098 public boolean isInstanceOf(Marker m) { 099 if (m == null) { 100 throw new IllegalArgumentException("A marker parameter is required"); 101 } 102 Marker test = this; 103 do { 104 if (test == m) { 105 return true; 106 } 107 test = test.getParent(); 108 } while (test != null); 109 return false; 110 } 111 112 public boolean isInstanceOf(String name) { 113 if (name == null) { 114 throw new IllegalArgumentException("A marker name is required"); 115 } 116 Marker toTest = this; 117 do { 118 if (name.equals(toTest.getName())) { 119 return true; 120 } 121 toTest = toTest.getParent(); 122 } while (toTest != null); 123 return false; 124 } 125 126 @Override 127 public boolean equals(Object o) { 128 if (this == o) { 129 return true; 130 } 131 if (o == null || !(o instanceof Marker)) { 132 return false; 133 } 134 135 Marker marker = (Marker) o; 136 137 if (name != null ? !name.equals(marker.getName()) : marker.getName() != null) { 138 return false; 139 } 140 141 return true; 142 } 143 144 @Override 145 public int hashCode() { 146 return name != null ? name.hashCode() : 0; 147 } 148 149 @Override 150 public String toString() { 151 StringBuilder sb = new StringBuilder(name); 152 if (parent != null) { 153 Marker m = parent; 154 sb.append("[ "); 155 boolean first = true; 156 while (m != null) { 157 if (!first) { 158 sb.append(", "); 159 } 160 sb.append(m.getName()); 161 first = false; 162 m = m.getParent(); 163 } 164 sb.append(" ]"); 165 } 166 return sb.toString(); 167 } 168 } 169 }