BasicMarkerFactory.java revision 7ba0605dc97fb81bde8311510d27b3ccba170008
17ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu/**
27ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu * Copyright (c) 2004-2011 QOS.ch
388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * All rights reserved.
47ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu *
588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * Permission is hereby granted, free  of charge, to any person obtaining
688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * a  copy  of this  software  and  associated  documentation files  (the
788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * "Software"), to  deal in  the Software without  restriction, including
888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * without limitation  the rights to  use, copy, modify,  merge, publish,
988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * distribute,  sublicense, and/or sell  copies of  the Software,  and to
1088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * permit persons to whom the Software  is furnished to do so, subject to
1188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * the following conditions:
127ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu *
1388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * The  above  copyright  notice  and  this permission  notice  shall  be
1488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * included in all copies or substantial portions of the Software.
157ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu *
1688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
1788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
1888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
1988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
2088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
2288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
237ba0605dc97fb81bde8311510d27b3ccba170008Ceki Gulcu *
2488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu */
2588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcupackage org.slf4j.helpers;
2688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
2788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcuimport java.util.HashMap;
2888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcuimport java.util.Map;
2988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
3088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcuimport org.slf4j.IMarkerFactory;
3188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcuimport org.slf4j.Marker;
3288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
3388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu/**
3488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * An almost trivial implementation of the {@link IMarkerFactory}
3588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * interface which creates {@link BasicMarker} instances.
3688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu *
379f10490a05f7344f4b3ef657e8991f5d51934e2fCeki Gulcu * <p>Simple logging systems can conform to the SLF4J API by binding
3888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * {@link org.slf4j.MarkerFactory} with an instance of this class.
3988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu *
4088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu * @author Ceki G&uuml;lc&uuml;
4188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu */
4288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcupublic class BasicMarkerFactory implements IMarkerFactory {
4388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
4488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  Map markerMap = new HashMap();
4588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
4688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  /**
4788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * Regular users should <em>not</em> create
4888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * <code>BasicMarkerFactory</code> instances. <code>Marker</code>
4988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * instances can be obtained using the static {@link
5088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * org.slf4j.MarkerFactory#getMarker} method.
5188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   */
5288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  public BasicMarkerFactory() {
5388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  }
5488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
5588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  /**
5688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * Manufacture a {@link BasicMarker} instance by name. If the instance has been
5788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * created earlier, return the previously created instance.
5888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   *
5988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * @param name the name of the marker to be created
6088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * @return a Marker instance
6188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   */
6288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  public synchronized Marker getMarker(String name) {
6388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    if (name == null) {
6488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu      throw new IllegalArgumentException("Marker name cannot be null");
6588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    }
6688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
6788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    Marker marker = (Marker) markerMap.get(name);
6888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    if (marker == null) {
6988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu      marker = new BasicMarker(name);
7088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu      markerMap.put(name, marker);
7188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    }
7288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    return marker;
7388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  }
7488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
7588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  /**
7688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   * Does the name marked already exist?
7788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu   */
7888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  public synchronized boolean exists(String name) {
7988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    if (name == null) {
8088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu      return false;
8188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    }
8288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    return markerMap.containsKey(name);
8388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  }
8488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
8588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  public boolean detachMarker(String name) {
8688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    if(name == null) {
8788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu      return false;
8888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    }
8988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    return (markerMap.remove(name) != null);
9088c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  }
9188c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
9288c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
9388c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  public Marker getDetachedMarker(String name) {
9488c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu    return  new BasicMarker(name);
9588c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu  }
9688c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
9788c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
9888c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu
9988c4c456766193e012eb890e2208473d99b91f83Ceki Gulcu}
100