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