JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / org / apache / log4j / rewrite / PropertyRewritePolicy.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.log4j.rewrite;
18
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.Map;
23 import java.util.StringTokenizer;
24
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.spi.LoggingEvent;
27
28 /**
29  * This policy rewrites events by adding
30  * a user-specified list of properties to the event.
31  * Existing properties are not modified.
32  *
33  * The combination of the RewriteAppender and this policy
34  * performs the same actions as the PropertyFilter from log4j 1.3.
35  */
36
37 public class PropertyRewritePolicy implements RewritePolicy {
38     private Map properties = Collections.EMPTY_MAP;
39     public PropertyRewritePolicy() {
40     }
41
42     /**
43      * Set a string representing the property name/value pairs.
44      * 
45      * Form: propname1=propvalue1,propname2=propvalue2
46      * 
47      * @param props
48      */
49     public void setProperties(String props) {
50         Map hashTable = new HashMap();
51         StringTokenizer pairs = new StringTokenizer(props, ",");
52         while (pairs.hasMoreTokens()) {
53             StringTokenizer entry = new StringTokenizer(pairs.nextToken(), "=");
54             hashTable.put(entry.nextElement().toString().trim(), entry.nextElement().toString().trim());
55         }
56         synchronized(this) {
57             properties = hashTable;
58         }
59     }
60
61     /**
62      * {@inheritDoc}
63      */
64     public LoggingEvent rewrite(final LoggingEvent source) {
65         if (!properties.isEmpty()) {
66             Map rewriteProps = new HashMap(source.getProperties());
67             for(Iterator iter = properties.entrySet().iterator();
68                     iter.hasNext();
69                     ) {
70                 Map.Entry entry = (Map.Entry) iter.next();
71                 if (!rewriteProps.containsKey(entry.getKey())) {
72                     rewriteProps.put(entry.getKey(), entry.getValue());
73                 }
74             }
75
76             return new LoggingEvent(
77                     source.getFQNOfLoggerClass(),
78                     source.getLogger() != null ? source.getLogger(): Logger.getLogger(source.getLoggerName()), 
79                     source.getTimeStamp(),
80                     source.getLevel(),
81                     source.getMessage(),
82                     source.getThreadName(),
83                     source.getThrowableInformation(),
84                     source.getNDC(),
85                     source.getLocationInformation(),
86                     rewriteProps);
87         }
88         return source;
89     }
90
91
92
93 }