001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static ch.qos.logback.core.CoreConstants.FA_FILENAME_COLLISION_MAP;
017import static ch.qos.logback.core.CoreConstants.RFA_FILENAME_PATTERN_COLLISION_MAP;
018
019import java.util.List;
020import java.util.Map;
021import java.util.Map.Entry;
022import java.util.Properties;
023
024import ch.qos.logback.core.Context;
025import ch.qos.logback.core.rolling.helper.FileNamePattern;
026import ch.qos.logback.core.spi.ContextAwareBase;
027
028public class ContextUtil extends ContextAwareBase {
029
030    static final String GROOVY_RUNTIME_PACKAGE = "org.codehaus.groovy.runtime";
031    // static final String SYSTEM_LOGGER_FQCN = "java.lang.System$Logger";
032
033    public ContextUtil(Context context) {
034        setContext(context);
035    }
036
037    public void addProperties(Properties props) {
038        if (props == null) {
039            return;
040        }
041
042        for (Entry<Object, Object> e : props.entrySet()) {
043            String key = (String) e.getKey();
044            context.putProperty(key, (String) e.getValue());
045        }
046
047    }
048
049    public static Map<String, String> getFilenameCollisionMap(Context context) {
050        if (context == null)
051            return null;
052        @SuppressWarnings("unchecked")
053        Map<String, String> map = (Map<String, String>) context.getObject(FA_FILENAME_COLLISION_MAP);
054        return map;
055    }
056
057    public static Map<String, FileNamePattern> getFilenamePatternCollisionMap(Context context) {
058        if (context == null)
059            return null;
060        @SuppressWarnings("unchecked")
061        Map<String, FileNamePattern> map = (Map<String, FileNamePattern>) context
062                .getObject(RFA_FILENAME_PATTERN_COLLISION_MAP);
063        return map;
064    }
065
066    public void addGroovyPackages(List<String> frameworkPackages) {
067        addFrameworkPackage(frameworkPackages, GROOVY_RUNTIME_PACKAGE);
068    }
069
070    public void addFrameworkPackage(List<String> frameworkPackages, String packageName) {
071        if (!frameworkPackages.contains(packageName)) {
072            frameworkPackages.add(packageName);
073        }
074    }
075
076}