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(final 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(final String name, final String parent) {
52          final 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(final String name, final 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(final String name) {
81              this.name = name;
82              this.parent = null;
83          }
84  
85          public Log4jMarker(final String name, final Marker parent) {
86              this.name = name;
87              this.parent = parent;
88          }
89  
90          @Override
91          public String getName() {
92              return this.name;
93          }
94  
95          @Override
96          public Marker getParent() {
97              return this.parent;
98          }
99  
100         @Override
101         public boolean isInstanceOf(final Marker m) {
102             if (m == null) {
103                 throw new IllegalArgumentException("A marker parameter is required");
104             }
105             Marker test = this;
106             do {
107                 if (test == m) {
108                     return true;
109                 }
110                 test = test.getParent();
111             } while (test != null);
112             return false;
113         }
114 
115         @Override
116         public boolean isInstanceOf(final String name) {
117             if (name == null) {
118                 throw new IllegalArgumentException("A marker name is required");
119             }
120             Marker toTest = this;
121             do {
122                 if (name.equals(toTest.getName())) {
123                     return true;
124                 }
125                 toTest = toTest.getParent();
126             } while (toTest != null);
127             return false;
128         }
129 
130         @Override
131         public boolean equals(final Object o) {
132             if (this == o) {
133                 return true;
134             }
135             if (o == null || !(o instanceof Marker)) {
136                 return false;
137             }
138 
139             final Marker marker = (Marker) o;
140 
141             if (name != null ? !name.equals(marker.getName()) : marker.getName() != null) {
142                 return false;
143             }
144 
145             return true;
146         }
147 
148         @Override
149         public int hashCode() {
150             return name != null ? name.hashCode() : 0;
151         }
152 
153         @Override
154         public String toString() {
155             final StringBuilder sb = new StringBuilder(name);
156             if (parent != null) {
157                 Marker m = parent;
158                 sb.append("[ ");
159                 boolean first = true;
160                 while (m != null) {
161                     if (!first) {
162                         sb.append(", ");
163                     }
164                     sb.append(m.getName());
165                     first = false;
166                     m = m.getParent();
167                 }
168                 sb.append(" ]");
169             }
170             return sb.toString();
171         }
172     }
173 }