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  package org.apache.logging.log4j;
18  
19  import java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  
23  /**
24   * Applications create Markers by using the Marker Manager. All Markers created by this Manager are
25   * immutable.
26   */
27  public final class MarkerManager {
28  
29      private static ConcurrentMap<String, Marker> markerMap = new ConcurrentHashMap<String, Marker>();
30  
31      private MarkerManager() {
32      }
33  
34      /**
35       * Retrieve a Marker or create a Marker that has no parent.
36       * @param name The name of the Marker.
37       * @return The Marker with the specified name.
38       */
39      public static Marker getMarker(String name) {
40          markerMap.putIfAbsent(name, new Log4JMarker(name));
41          return markerMap.get(name);
42      }
43  
44      /**
45       * Retrieves or creates a Marker with the specified parent. The parent must have been previously created.
46       * @param name The name of the Marker.
47       * @param parent The name of the parent Marker.
48       * @return The Marker with the specified name.
49       * @throws IllegalArgumentException if the parent Marker does not exist.
50       */
51      public static Marker getMarker(String name, String parent) {
52          Marker parentMarker = markerMap.get(parent);
53          if (parentMarker == null) {
54              throw new IllegalArgumentException("Parent Marker " + parent + " has not been defined");
55          }
56          return getMarker(name, parentMarker);
57      }
58  
59      /**
60       * Retrieves or creates a Marker with the specified parent.
61       * @param name The name of the Marker.
62       * @param parent The parent Marker.
63       * @return The Marker with the specified name.
64       */
65      public static Marker getMarker(String name, Marker parent) {
66          markerMap.putIfAbsent(name, new Log4JMarker(name, parent));
67          return markerMap.get(name);
68      }
69  
70      /**
71       * The actual Marker implementation.
72       */
73      private static class Log4JMarker implements Marker {
74  
75          private static final long serialVersionUID = 100L;
76  
77          private final String name;
78          private final Marker parent;
79  
80          public Log4JMarker(String name) {
81              this.name = name;
82              this.parent = null;
83          }
84  
85          public Log4JMarker(String name, Marker parent) {
86              this.name = name;
87              this.parent = parent;
88          }
89  
90          public String getName() {
91              return this.name;
92          }
93  
94          public Marker getParent() {
95              return this.parent;
96          }
97  
98          public boolean isInstanceOf(Marker m) {
99              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 }