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 */ 017package org.apache.logging.slf4j; 018 019import java.util.ArrayList; 020import java.util.Iterator; 021import java.util.List; 022import java.util.Objects; 023 024import org.apache.logging.log4j.MarkerManager; 025import org.slf4j.IMarkerFactory; 026import org.slf4j.Marker; 027import org.slf4j.impl.StaticMarkerBinder; 028 029/** 030 * Log4j/SLF4J {@link org.slf4j.Marker} type bridge. 031 */ 032public class Log4jMarker implements Marker { 033 034 public static final long serialVersionUID = 1590472L; 035 036 private final IMarkerFactory factory = StaticMarkerBinder.SINGLETON.getMarkerFactory(); 037 038 private final org.apache.logging.log4j.Marker marker; 039 040 /** 041 * Constructs a Log4jMarker using an existing Log4j {@link org.apache.logging.log4j.Marker}. 042 * @param marker The Log4j Marker upon which to base this Marker. 043 */ 044 public Log4jMarker(final org.apache.logging.log4j.Marker marker) { 045 this.marker = marker; 046 } 047 048 @Override 049 public void add(final Marker marker) { 050 if (marker == null) { 051 throw new IllegalArgumentException(); 052 } 053 final Marker m = factory.getMarker(marker.getName()); 054 this.marker.addParents(((Log4jMarker)m).getLog4jMarker()); 055 } 056 057 @Override 058 public boolean contains(final org.slf4j.Marker marker) { 059 if (marker == null) { 060 throw new IllegalArgumentException(); 061 } 062 return this.marker.isInstanceOf(marker.getName()); 063 } 064 065 @Override 066 public boolean contains(final String s) { 067 return s != null ? this.marker.isInstanceOf(s) : false; 068 } 069 070 @Override 071 public boolean equals(final Object obj) { 072 if (this == obj) { 073 return true; 074 } 075 if (obj == null) { 076 return false; 077 } 078 if (!(obj instanceof Log4jMarker)) { 079 return false; 080 } 081 final Log4jMarker other = (Log4jMarker) obj; 082 if (!Objects.equals(marker, other.marker)) { 083 return false; 084 } 085 return true; 086 } 087 088 public org.apache.logging.log4j.Marker getLog4jMarker() { 089 return marker; 090 } 091 092 @Override 093 public String getName() { 094 return marker.getName(); 095 } 096 097 @Override 098 public boolean hasChildren() { 099 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}