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    public org.apache.logging.log4j.Marker getLog4jMarker() {
048        return marker;
049    }
050
051    @Override
052    public void add(final Marker marker) {
053        final Marker m = factory.getMarker(marker.getName());
054        this.marker.addParents(((Log4jMarker)m).getLog4jMarker());
055    }
056
057    @Override
058    public boolean remove(final Marker marker) {
059        return this.marker.remove(MarkerManager.getMarker(marker.getName()));
060    }
061
062    @Override
063    public String getName() {
064        return marker.getName();
065    }
066
067    @Override
068    public boolean hasReferences() {
069        return marker.hasParents();
070    }
071
072    @Override
073    public boolean hasChildren() {
074        return marker.hasParents();
075    }
076
077    @Override
078    @SuppressWarnings("rawtypes")
079    public Iterator iterator() {
080        final List<Marker> parents = new ArrayList<Marker>();
081        for (final org.apache.logging.log4j.Marker m : this.marker.getParents()) {
082            parents.add(factory.getMarker(m.getName()));
083        }
084        return parents.iterator();
085    }
086
087    @Override
088    public boolean contains(final org.slf4j.Marker marker) {
089        return this.marker.isInstanceOf(marker.getName());
090    }
091
092    @Override
093    public boolean contains(final String s) {
094        return this.marker.isInstanceOf(s);
095    }
096}