Skip to content

Commit

Permalink
PAXLOGGING-35 Introduce the notion of an MDC style PaxContext into th…
Browse files Browse the repository at this point in the history
…e API. Implement the log4j and slf4j MDC context to go against this PaxContext, and ensure the context is provided to the underlying log4j delegate within the PaxLoggerImpl.
  • Loading branch information
mooseroy committed Aug 10, 2008
1 parent 48c4fd7 commit a4e3460
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
106 changes: 106 additions & 0 deletions api/src/main/java/org/apache/log4j/MDC.java
@@ -0,0 +1,106 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.log4j;

import java.util.Map;
import org.ops4j.pax.logging.OSGIPaxLoggingManager;
import org.ops4j.pax.logging.PaxContext;
import org.ops4j.pax.logging.PaxLoggingManager;
import org.osgi.framework.BundleContext;

/**
* Wrap the PaxContext with MDC api. PaxContext is derived from original MDC.
*/
public class MDC {

private static PaxContext m_context;
private static PaxContext m_defaultContext = new PaxContext();
private static PaxLoggingManager m_paxLogging;

public static void setBundleContext(BundleContext ctx) {
m_paxLogging = new OSGIPaxLoggingManager(ctx);
// We need to instruct all loggers to ensure the SimplePaxLoggingManager is replaced.
m_paxLogging.open();
}

/**
* For all the methods that operate against the context, return true if the MDC should use the PaxContext object ffrom the PaxLoggingManager,
* or if the logging manager is not set, or does not have its context available yet, use a default context local to this MDC.
* @return true if the MDC should use the PaxContext object ffrom the PaxLoggingManager,
* or if the logging manager is not set, or does not have its context available yet, use a default context local to this MDC.
*/
private static boolean setContext() {
if (m_context == null && m_paxLogging != null) {
m_context = (m_paxLogging.getPaxLoggingService() != null) ? m_paxLogging.getPaxLoggingService().getPaxContext() : null;
}
return m_context != null;
}

/**
Put a context value (the <code>o</code> parameter) as identified
with the <code>key</code> parameter into the current thread's
context map.
<p>If the current thread does not have a context map it is
created as a side effect.
*/
static public void put(String key, Object o) {
if (setContext()) {
m_context.put(key, o);
} else {
m_defaultContext.put(key, o);
}
}

/**
Get the context identified by the <code>key</code> parameter.
<p>This method has no side effects.
*/
static public Object get(String key) {
if (setContext()) {
return m_context.get(key);
} else {
return m_defaultContext.get(key);
}
}

/**
Remove the the context identified by the <code>key</code>
parameter.
*/
public static void remove(String key) {
if (setContext()) {
m_context.remove(key);
} else {
m_defaultContext.remove(key);
}
}

/**
* Get the current thread's MDC as a map.
* */
public static Map getContext() {
if (setContext()) {
return m_context.getContext();
} else {
return m_defaultContext.getContext();
}
}
}
42 changes: 42 additions & 0 deletions api/src/main/java/org/apache/log4j/helpers/ThreadLocalMap.java
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.log4j.helpers;

import java.util.Hashtable;

/**
<code>ThreadLocalMap</code> extends {@link InheritableThreadLocal}
to bequeath a copy of the hashtable of the MDC of the parent
thread.
@author Ceki G&uuml;lc&uuml;
@since 1.2
*/
final public class ThreadLocalMap extends InheritableThreadLocal {

public
final
Object childValue(Object parentValue) {
Hashtable ht = (Hashtable) parentValue;
if(ht != null) {
return ht.clone();
} else {
return null;
}
}
}
101 changes: 101 additions & 0 deletions api/src/main/java/org/ops4j/pax/logging/PaxContext.java
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Derived from org.apache.log4j.MDC
*/
package org.ops4j.pax.logging;

import java.util.Hashtable;
import java.util.Map;
import org.apache.log4j.helpers.ThreadLocalMap;

/**
The MDC class is similar to the {@link NDC} class except that it is
based on a map instead of a stack. It provides <em>mapped
diagnostic contexts</em>. A <em>Mapped Diagnostic Context</em>, or
MDC in short, is an instrument for distinguishing interleaved log
output from different sources. Log output is typically interleaved
when a server handles multiple clients near-simultaneously.
<p><b><em>The MDC is managed on a per thread basis</em></b>. A
child thread automatically inherits a <em>copy</em> of the mapped
diagnostic context of its parent.
<p>The MDC class requires JDK 1.2 or above. Under JDK 1.1 the MDC
will always return empty values but otherwise will not affect or
harm your application.
@since 1.2
@author Ceki G&uuml;lc&uuml; */
public class PaxContext {

static final int HT_SIZE = 7;

Object tlm;

public PaxContext() {
tlm = new ThreadLocalMap();
}


public void putAll(Map context){
Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();
if(ht == null) {
ht = new Hashtable(HT_SIZE);
((ThreadLocalMap)tlm).set(ht);
}
ht.putAll(context);
}

public void put(String key, Object o) {
Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();
if(ht == null) {
ht = new Hashtable(HT_SIZE);
((ThreadLocalMap)tlm).set(ht);
}
ht.put(key, o);
}

public String get(String key) {
Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();
if(ht != null && key != null) {
return (String) ht.get(key);
} else {
return null;
}
}

public void remove(String key) {
Hashtable ht = (Hashtable) ((ThreadLocalMap)tlm).get();
if(ht != null) {
ht.remove(key);
}
}


public Map getContext() {
return (Map) ((ThreadLocalMap)tlm).get();
}

public void clear() {
Map context=(Map)((ThreadLocalMap)tlm).get();
if(context!=null){
context.clear();
}
}
}

0 comments on commit a4e3460

Please sign in to comment.