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