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.log4j.rewrite;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.StringTokenizer;
024
025import org.apache.log4j.Logger;
026import org.apache.log4j.spi.LoggingEvent;
027
028/**
029 * This policy rewrites events by adding
030 * a user-specified list of properties to the event.
031 * Existing properties are not modified.
032 *
033 * The combination of the RewriteAppender and this policy
034 * performs the same actions as the PropertyFilter from log4j 1.3.
035 */
036
037public class PropertyRewritePolicy implements RewritePolicy {
038    private Map properties = Collections.EMPTY_MAP;
039    public PropertyRewritePolicy() {
040    }
041
042    /**
043     * Set a string representing the property name/value pairs.
044     * 
045     * Form: propname1=propvalue1,propname2=propvalue2
046     * 
047     * @param props
048     */
049    public void setProperties(String props) {
050        Map hashTable = new HashMap();
051        StringTokenizer pairs = new StringTokenizer(props, ",");
052        while (pairs.hasMoreTokens()) {
053            StringTokenizer entry = new StringTokenizer(pairs.nextToken(), "=");
054            hashTable.put(entry.nextElement().toString().trim(), entry.nextElement().toString().trim());
055        }
056        synchronized(this) {
057            properties = hashTable;
058        }
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    public LoggingEvent rewrite(final LoggingEvent source) {
065        if (!properties.isEmpty()) {
066            Map rewriteProps = new HashMap(source.getProperties());
067            for(Iterator iter = properties.entrySet().iterator();
068                    iter.hasNext();
069                    ) {
070                Map.Entry entry = (Map.Entry) iter.next();
071                if (!rewriteProps.containsKey(entry.getKey())) {
072                    rewriteProps.put(entry.getKey(), entry.getValue());
073                }
074            }
075
076            return new LoggingEvent(
077                    source.getFQNOfLoggerClass(),
078                    source.getLogger() != null ? source.getLogger(): Logger.getLogger(source.getLoggerName()), 
079                    source.getTimeStamp(),
080                    source.getLevel(),
081                    source.getMessage(),
082                    source.getThreadName(),
083                    source.getThrowableInformation(),
084                    source.getNDC(),
085                    source.getLocationInformation(),
086                    rewriteProps);
087        }
088        return source;
089    }
090
091
092
093}