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.slf4j;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.logging.log4j.MarkerManager;
24  import org.slf4j.IMarkerFactory;
25  import org.slf4j.Marker;
26  import org.slf4j.impl.StaticMarkerBinder;
27  
28  /**
29   * Log4j/SLF4J {@link org.slf4j.Marker} type bridge.
30   */
31  public class Log4jMarker implements Marker {
32  
33      public static final long serialVersionUID = 1590472L;
34  
35      private final IMarkerFactory factory = StaticMarkerBinder.SINGLETON.getMarkerFactory();
36  
37      private final org.apache.logging.log4j.Marker marker;
38  
39      /**
40       * Constructs a Log4jMarker using an existing Log4j {@link org.apache.logging.log4j.Marker}.
41       * @param marker The Log4j Marker upon which to base this Marker.
42       */
43      public Log4jMarker(final org.apache.logging.log4j.Marker marker) {
44          this.marker = marker;
45      }
46  
47      @Override
48      public void add(final Marker marker) {
49  		if (marker == null) {
50  			throw new IllegalArgumentException();
51  		}
52          final Marker m = factory.getMarker(marker.getName());
53          this.marker.addParents(((Log4jMarker)m).getLog4jMarker());
54      }
55  
56      @Override
57  	public boolean contains(final org.slf4j.Marker marker) {
58  		if (marker == null) {
59  			throw new IllegalArgumentException();
60  		}
61  		return this.marker.isInstanceOf(marker.getName());
62  	}
63  
64      @Override
65  	public boolean contains(final String s) {
66  		return s != null ? this.marker.isInstanceOf(s) : false;
67  	}
68  
69      @Override
70  	public boolean equals(final Object obj) {
71  		if (this == obj) {
72  			return true;
73  		}
74  		if (obj == null) {
75  			return false;
76  		}
77  		if (!(obj instanceof Log4jMarker)) {
78  			return false;
79  		}
80  		final Log4jMarker other = (Log4jMarker) obj;
81  		if (marker == null) {
82  			if (other.marker != null) {
83  				return false;
84  			}
85  		} else if (!marker.equals(other.marker)) {
86  			return false;
87  		}
88  		return true;
89  	}
90  
91      public org.apache.logging.log4j.Marker getLog4jMarker() {
92          return marker;
93      }
94  
95      @Override
96      public String getName() {
97          return marker.getName();
98      }
99  
100     @Override
101     public boolean hasChildren() {
102         return marker.hasParents();
103     }
104 
105     @Override
106 	public int hashCode() {
107 		final int prime = 31;
108 		int result = 1;
109 		result = prime * result + ((marker == null) ? 0 : marker.hashCode());
110 		return result;
111 	}
112 
113     @Override
114     public boolean hasReferences() {
115         return marker.hasParents();
116     }
117 
118 	@Override
119     public Iterator<Marker> iterator() {
120         final org.apache.logging.log4j.Marker[] log4jParents = this.marker.getParents();
121         final List<Marker> parents = new ArrayList<>(log4jParents.length);
122 		for (final org.apache.logging.log4j.Marker m : log4jParents) {
123             parents.add(factory.getMarker(m.getName()));
124         }
125         return parents.iterator();
126     }
127 
128 	@Override
129 	public boolean remove(final Marker marker) {
130 		return marker != null ? this.marker.remove(MarkerManager.getMarker(marker.getName())) : false;
131 	}
132 }