Fixes #3697 - Review JNDI logging.

Deprecated NamingUtil.__log and replaced its
usages with the static logger-per-class idiom.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2019-06-06 15:36:37 +02:00
parent 900a036664
commit 0bc88ec286
9 changed files with 95 additions and 73 deletions

View File

@ -21,8 +21,8 @@ package org.eclipse.jetty.jndi;
import java.io.IOException;
import java.util.Collections;
import java.util.Hashtable;
import java.util.WeakHashMap;
import java.util.Map;
import java.util.WeakHashMap;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameParser;
@ -32,6 +32,7 @@ import javax.naming.spi.ObjectFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -59,7 +60,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class ContextFactory implements ObjectFactory
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(ContextFactory.class);
/**
* Map of classloaders to contexts.
@ -104,8 +105,8 @@ public class ContextFactory implements ObjectFactory
Context ctx = (Context)__threadContext.get();
if (ctx != null)
{
if(__log.isDebugEnabled())
__log.debug("Using the Context that is bound on the thread");
if(LOG.isDebugEnabled())
LOG.debug("Using the Context that is bound on the thread");
return ctx;
}
@ -114,8 +115,8 @@ public class ContextFactory implements ObjectFactory
ClassLoader loader = (ClassLoader)__threadClassLoader.get();
if (loader != null)
{
if (__log.isDebugEnabled())
__log.debug("Using threadlocal classloader");
if (LOG.isDebugEnabled())
LOG.debug("Using threadlocal classloader");
synchronized(__contextMap)
{
ctx = getContextForClassLoader(loader);
@ -123,8 +124,8 @@ public class ContextFactory implements ObjectFactory
{
ctx = newNamingContext(obj, loader, env, name, nameCtx);
__contextMap.put (loader, ctx);
if(__log.isDebugEnabled())
__log.debug("Made context {} for classloader {}",name.get(0),loader);
if(LOG.isDebugEnabled())
LOG.debug("Made context {} for classloader {}",name.get(0),loader);
}
return ctx;
}
@ -135,8 +136,8 @@ public class ContextFactory implements ObjectFactory
loader = tccl;
if (loader != null)
{
if (__log.isDebugEnabled())
__log.debug("Trying thread context classloader");
if (LOG.isDebugEnabled())
LOG.debug("Trying thread context classloader");
synchronized(__contextMap)
{
while (ctx == null && loader != null)
@ -150,8 +151,8 @@ public class ContextFactory implements ObjectFactory
{
ctx = newNamingContext(obj, tccl, env, name, nameCtx);
__contextMap.put (tccl, ctx);
if(__log.isDebugEnabled())
__log.debug("Made context {} for classloader {}", name.get(0), tccl);
if(LOG.isDebugEnabled())
LOG.debug("Made context {} for classloader {}", name.get(0), tccl);
}
return ctx;
}
@ -162,8 +163,8 @@ public class ContextFactory implements ObjectFactory
//classloader associated with the current context
if (ContextHandler.getCurrentContext() != null)
{
if (__log.isDebugEnabled() && loader != null)
__log.debug("Trying classloader of current org.eclipse.jetty.server.handler.ContextHandler");
if (LOG.isDebugEnabled() && loader != null)
LOG.debug("Trying classloader of current org.eclipse.jetty.server.handler.ContextHandler");
synchronized(__contextMap)
{
loader = ContextHandler.getCurrentContext().getContextHandler().getClassLoader();
@ -173,8 +174,8 @@ public class ContextFactory implements ObjectFactory
{
ctx = newNamingContext(obj, loader, env, name, nameCtx);
__contextMap.put (loader, ctx);
if(__log.isDebugEnabled())
__log.debug("Made context {} for classloader {} ", name.get(0), loader);
if(LOG.isDebugEnabled())
LOG.debug("Made context {} for classloader {} ", name.get(0), loader);
}
return ctx;

View File

@ -27,6 +27,7 @@ import javax.naming.NameParser;
import javax.naming.NamingException;
import org.eclipse.jetty.jndi.local.localContextRoot;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -42,7 +43,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class InitialContextFactory implements javax.naming.spi.InitialContextFactory
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(InitialContextFactory.class);
public static class DefaultParser implements NameParser
{
@ -76,10 +77,12 @@ public class InitialContextFactory implements javax.naming.spi.InitialContextFac
@Override
public Context getInitialContext(Hashtable env)
{
__log.debug("InitialContextFactory.getInitialContext()");
if(LOG.isDebugEnabled())
LOG.debug("InitialContextFactory.getInitialContext()");
Context ctx = new localContextRoot(env);
if(__log.isDebugEnabled())__log.debug("Created initial context delegate for local namespace:"+ctx);
if(LOG.isDebugEnabled())
LOG.debug("Created initial context delegate for local namespace:"+ctx);
return ctx;
}

View File

@ -44,6 +44,7 @@ import javax.naming.Referenceable;
import javax.naming.spi.NamingManager;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
@ -57,7 +58,7 @@ import org.eclipse.jetty.util.log.Logger;
@SuppressWarnings("unchecked")
public class NamingContext implements Context, Dumpable
{
private final static Logger __log=NamingUtil.__log;
private static final Logger LOG = Log.getLogger(NamingContext.class);
private final static List<Binding> __empty = Collections.emptyList();
public static final String DEEP_BINDING = "org.eclipse.jetty.jndi.deepBinding";
public static final String LOCK_PROPERTY = "org.eclipse.jetty.jndi.lock";
@ -137,8 +138,8 @@ public class NamingContext implements Context, Dumpable
_parent = parent;
_parser = parser;
_bindings = bindings==null ? new ConcurrentHashMap<>() : bindings;
if(__log.isDebugEnabled())
__log.debug("new {}", this);
if(LOG.isDebugEnabled())
LOG.debug("new {}", this);
}
/**
@ -223,7 +224,7 @@ public class NamingContext implements Context, Dumpable
}
catch (Exception e)
{
__log.warn("",e);
LOG.warn("",e);
throw new NamingException (e.getMessage());
}
}
@ -294,8 +295,8 @@ public class NamingContext implements Context, Dumpable
}
else
{
if(__log.isDebugEnabled())
__log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
if(LOG.isDebugEnabled())
LOG.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
//walk down the subcontext hierarchy
//need to ignore trailing empty "" name components
@ -448,12 +449,14 @@ public class NamingContext implements Context, Dumpable
public Object lookup(Name name)
throws NamingException
{
if(__log.isDebugEnabled())__log.debug("Looking up name=\""+name+"\"");
if(LOG.isDebugEnabled())
LOG.debug("Looking up name=\""+name+"\"");
Name cname = toCanonicalName(name);
if ((cname == null) || (cname.size() == 0))
{
__log.debug("Null or empty name, returning shallowCopy of this context");
if(LOG.isDebugEnabled())
LOG.debug("Null or empty name, returning shallowCopy of this context");
return shallowCopy();
}
@ -496,7 +499,7 @@ public class NamingContext implements Context, Dumpable
}
catch (Exception e)
{
__log.warn("",e);
LOG.warn("",e);
throw new NamingException (e.getMessage());
}
}
@ -583,7 +586,8 @@ public class NamingContext implements Context, Dumpable
public NamingEnumeration list(Name name)
throws NamingException
{
if(__log.isDebugEnabled())__log.debug("list() on Context="+getName()+" for name="+name);
if(LOG.isDebugEnabled())
LOG.debug("list() on Context="+getName()+" for name="+name);
Name cname = toCanonicalName(name);
if (cname == null)
@ -1011,8 +1015,8 @@ public class NamingContext implements Context, Dumpable
break;
}
if(__log.isDebugEnabled())
__log.debug("Adding binding with key="+key+" obj="+obj+" for context="+_name+" as "+binding);
if(LOG.isDebugEnabled())
LOG.debug("Adding binding with key="+key+" obj="+obj+" for context="+_name+" as "+binding);
if (binding!=null)
{
@ -1058,8 +1062,8 @@ public class NamingContext implements Context, Dumpable
public void removeBinding (Name name)
{
String key = name.toString();
if (__log.isDebugEnabled())
__log.debug("Removing binding with key="+key);
if (LOG.isDebugEnabled())
LOG.debug("Removing binding with key="+key);
Binding binding = _bindings.remove(key);
if (binding!=null)
{

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.jndi;
import java.util.HashMap;
import java.util.Map;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.Name;
@ -29,6 +28,7 @@ import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -37,7 +37,12 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class NamingUtil
{
public final static Logger __log=org.eclipse.jetty.util.log.Log.getLogger("jndi");
/**
* @deprecated no replacement, use a logger-per-class idiom instead.
*/
@Deprecated
public final static Logger __log = Log.getLogger("jndi");
private static final Logger LOG = Log.getLogger(NamingUtil.class);
/* ------------------------------------------------------------ */
/**
@ -67,20 +72,20 @@ public class NamingUtil
try
{
subCtx = (Context)subCtx.lookup (name.get(i));
if(__log.isDebugEnabled())
__log.debug("Subcontext "+name.get(i)+" already exists");
if(LOG.isDebugEnabled())
LOG.debug("Subcontext "+name.get(i)+" already exists");
}
catch (NameNotFoundException e)
{
subCtx = subCtx.createSubcontext(name.get(i));
if(__log.isDebugEnabled())
__log.debug("Subcontext "+name.get(i)+" created");
if(LOG.isDebugEnabled())
LOG.debug("Subcontext "+name.get(i)+" created");
}
}
subCtx.rebind (name.get(name.size() - 1), obj);
if(__log.isDebugEnabled())
__log.debug("Bound object to "+name.get(name.size() - 1));
if(LOG.isDebugEnabled())
LOG.debug("Bound object to "+name.get(name.size() - 1));
return subCtx;
}

View File

@ -18,9 +18,7 @@
package org.eclipse.jetty.jndi.java;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameParser;
@ -31,7 +29,7 @@ import javax.naming.StringRefAddr;
import org.eclipse.jetty.jndi.ContextFactory;
import org.eclipse.jetty.jndi.NamingContext;
import org.eclipse.jetty.jndi.NamingUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -46,7 +44,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class javaRootURLContext implements Context
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(javaRootURLContext.class);
public static final String URL_PREFIX = "java:";
@ -76,7 +74,7 @@ public class javaRootURLContext implements Context
}
catch (Exception e)
{
__log.warn(e);
LOG.warn(e);
}
}
@ -319,7 +317,8 @@ public class javaRootURLContext implements Context
{
String head = name.get(0);
if(__log.isDebugEnabled())__log.debug("Head element of name is: "+head);
if(LOG.isDebugEnabled())
LOG.debug("Head element of name is: "+head);
if (head.startsWith(URL_PREFIX))
{
@ -328,7 +327,8 @@ public class javaRootURLContext implements Context
if (head.length() > 0)
name.add(0, head);
if(__log.isDebugEnabled())__log.debug("name modified to "+name.toString());
if(LOG.isDebugEnabled())
LOG.debug("name modified to "+name.toString());
}
}

View File

@ -38,7 +38,7 @@ import javax.naming.Referenceable;
import javax.naming.spi.NamingManager;
import org.eclipse.jetty.jndi.NamingContext;
import org.eclipse.jetty.jndi.NamingUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
@ -53,7 +53,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class localContextRoot implements Context
{
private final static Logger __log=NamingUtil.__log;
private static final Logger LOG = Log.getLogger(localContextRoot.class);
protected final static NamingContext __root = new NamingRoot();
private final Hashtable<String,Object> _env;
@ -171,7 +171,7 @@ public class localContextRoot implements Context
}
catch (Exception e)
{
__log.warn(e);
LOG.warn(e);
throw new NamingException (e.getMessage());
}
}
@ -298,7 +298,7 @@ public class localContextRoot implements Context
}
catch (Exception e)
{
__log.warn("",e);
LOG.warn("",e);
throw new NamingException (e.getMessage());
}
}
@ -333,7 +333,8 @@ public class localContextRoot implements Context
@Override
public Object lookup(Name name) throws NamingException
{
if(__log.isDebugEnabled())__log.debug("Looking up name=\""+name+"\"");
if(LOG.isDebugEnabled())
LOG.debug("Looking up name=\""+name+"\"");
Name cname = __root.toCanonicalName(name);
if ((cname == null) || cname.isEmpty())
@ -450,8 +451,8 @@ public class localContextRoot implements Context
}
else
{
if(__log.isDebugEnabled())
__log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
if(LOG.isDebugEnabled())
LOG.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
getContext(cname).bind (cname.getSuffix(1), obj);
}
@ -492,8 +493,8 @@ public class localContextRoot implements Context
else
{
//walk down the subcontext hierarchy
if(__log.isDebugEnabled())
__log.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
if(LOG.isDebugEnabled())
LOG.debug("Checking for existing binding for name="+cname+" for first element of name="+cname.get(0));
getContext(cname).rebind (cname.getSuffix(1), obj);
}

View File

@ -26,6 +26,7 @@ import javax.naming.NameParser;
import javax.naming.NamingException;
import org.eclipse.jetty.jndi.NamingUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
@ -41,7 +42,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public abstract class NamingEntry
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(NamingEntry.class);
public static final String __contextName = "__"; //all NamingEntries stored in context called "__"
protected final Object _scope;
protected final String _jndiName; //the name representing the object associated with the NamingEntry
@ -104,7 +105,8 @@ public abstract class NamingEntry
// TODO - check on the whole overriding/non-overriding thing
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp/env");
__log.debug("Binding java:comp/env/"+localName+" to "+_objectNameString);
if(LOG.isDebugEnabled())
LOG.debug("Binding java:comp/env/"+localName+" to "+_objectNameString);
NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
}
@ -117,12 +119,13 @@ public abstract class NamingEntry
{
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp/env");
__log.debug("Unbinding java:comp/env/"+getJndiName());
if(LOG.isDebugEnabled())
LOG.debug("Unbinding java:comp/env/"+getJndiName());
env.unbind(getJndiName());
}
catch (NamingException e)
{
__log.warn(e);
LOG.warn(e);
}
}
@ -141,7 +144,7 @@ public abstract class NamingEntry
}
catch (NamingException e)
{
__log.warn(e);
LOG.warn(e);
}
}
@ -191,7 +194,8 @@ public abstract class NamingEntry
protected void save (Object object)
throws NamingException
{
__log.debug("SAVE {} in {}",this,_scope);
if(LOG.isDebugEnabled())
LOG.debug("SAVE {} in {}",this,_scope);
InitialContext ic = new InitialContext();
NameParser parser = ic.getNameParser("");
Name prefix = NamingEntryUtil.getNameForScope(_scope);

View File

@ -21,7 +21,6 @@ package org.eclipse.jetty.plus.jndi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
@ -31,13 +30,13 @@ import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import org.eclipse.jetty.jndi.NamingUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class NamingEntryUtil
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(NamingEntryUtil.class);
/**
* Link a name in a webapp's java:/comp/evn namespace to a pre-existing
@ -176,7 +175,7 @@ public class NamingEntryUtil
}
catch (NamingException e)
{
__log.warn(e);
LOG.warn(e);
return null;
}
}
@ -227,7 +226,8 @@ public class NamingEntryUtil
}
catch (NameNotFoundException e)
{
__log.debug("No entries of type "+clazz.getName()+" in context="+context);
if(LOG.isDebugEnabled())
LOG.debug("No entries of type "+clazz.getName()+" in context="+context);
}
return list;

View File

@ -26,6 +26,7 @@ import javax.naming.NamingException;
import javax.transaction.UserTransaction;
import org.eclipse.jetty.jndi.NamingUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
@ -35,7 +36,7 @@ import org.eclipse.jetty.util.log.Logger;
*/
public class Transaction extends NamingEntry
{
private static Logger __log = NamingUtil.__log;
private static final Logger LOG = Log.getLogger(Transaction.class);
public static final String USER_TRANSACTION = "UserTransaction";
@ -74,7 +75,8 @@ public class Transaction extends NamingEntry
{
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp/env");
__log.debug("Binding java:comp/env"+getJndiName()+" to "+_objectNameString);
if(LOG.isDebugEnabled())
LOG.debug("Binding java:comp/env"+getJndiName()+" to "+_objectNameString);
NamingUtil.bind(env, localName, new LinkRef(_objectNameString));
}
@ -88,7 +90,8 @@ public class Transaction extends NamingEntry
//ignore the name, it is always bound to java:comp
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp");
__log.debug("Binding java:comp/"+getJndiName()+" to "+_objectNameString);
if(LOG.isDebugEnabled())
LOG.debug("Binding java:comp/"+getJndiName()+" to "+_objectNameString);
NamingUtil.bind(env, getJndiName(), new LinkRef(_objectNameString));
}
@ -102,12 +105,13 @@ public class Transaction extends NamingEntry
{
InitialContext ic = new InitialContext();
Context env = (Context)ic.lookup("java:comp");
__log.debug("Unbinding java:comp/"+getJndiName());
if(LOG.isDebugEnabled())
LOG.debug("Unbinding java:comp/"+getJndiName());
env.unbind(getJndiName());
}
catch (NamingException e)
{
__log.warn(e);
LOG.warn(e);
}
}
}