Modified framework to accept a Logger instance so that host applications
can do custom logging until the log service arrives. (FELIX-428)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@605724 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index 254b5e6..689c1a4 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -112,6 +112,20 @@
private Thread m_shutdownThread = null;
/**
+ * Creates a new Felix framework instance with a default logger.
+ *
+ * @param configMutableMap An map for obtaining configuration properties,
+ * may be <tt>null</tt>.
+ * @param activatorList A list of System Bundle activators.
+ *
+ * @see #Felix(Logger, Map, List)
+ */
+ public Felix(Map configMutableMap, List activatorList)
+ {
+ this(null, configMutableMap, activatorList);
+ }
+
+ /**
* <p>
* This method creates the framework instance; instances of the framework
* are not active until they are started. The constructor accepts a
@@ -192,11 +206,13 @@
* class documentation for more information.
* </p>
*
+ * @param logger The logger for use by the framework or <code>null</code>
+ * use the default logger.
* @param configMutableMap A map for obtaining configuration properties,
* may be <tt>null</tt>.
* @param activatorList A list of System Bundle activators.
**/
- public Felix(Map configMutableMap, List activatorList)
+ public Felix(Logger logger, Map configMutableMap, List activatorList)
{
// Initialize member variables.
m_configMutableMap = (configMutableMap == null)
@@ -208,8 +224,18 @@
// logger needs the system bundle's context for tracking log
// services, it is created now because it is needed before
// the system bundle is created. The system bundle's context
- // will be set below after the system bundle is created.
- m_logger = new Logger((String) m_configMutableMap.get(FelixConstants.LOG_LEVEL_PROP));
+ // will be set below after the system bundle is created.\
+ m_logger = (logger == null) ? new Logger() : logger;
+ try
+ {
+ m_logger.setLogLevel(
+ Integer.parseInt(
+ (String) m_configMutableMap.get(FelixConstants.LOG_LEVEL_PROP)));
+ }
+ catch (NumberFormatException ex)
+ {
+ // Ignore and just use the default logging level.
+ }
// Initialize framework properties.
initializeFrameworkProperties();
diff --git a/framework/src/main/java/org/apache/felix/framework/Logger.java b/framework/src/main/java/org/apache/felix/framework/Logger.java
index 31ae57d..b67a04d 100644
--- a/framework/src/main/java/org/apache/felix/framework/Logger.java
+++ b/framework/src/main/java/org/apache/felix/framework/Logger.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -52,26 +52,22 @@
private BundleContext m_context = null;
private final static int LOGGER_OBJECT_IDX = 0;
- private final static int LOGGER_METHOD_IDX = 1;
+ private final static int LOGGER_METHOD_IDX = 1;
private ServiceReference m_logRef = null;
private Object[] m_logger = null;
- public Logger(int logLevel)
+ public Logger()
{
- m_logLevel = logLevel;
}
- public Logger(String s)
+ public synchronized void setLogLevel(int i)
{
- s = (s == null) ? "1" : s;
- try
- {
- m_logLevel = Integer.parseInt(s);
- }
- catch (NumberFormatException ex)
- {
- // Default to 1.
- }
+ m_logLevel = i;
+ }
+
+ public synchronized int getLogLevel()
+ {
+ return m_logLevel;
}
protected void setSystemBundleContext(BundleContext context)
@@ -80,24 +76,57 @@
startListeningForLogService();
}
- public void log(int level, String msg)
+ public final void log(int level, String msg)
{
_log(null, level, msg, null);
}
- public void log(int level, String msg, Throwable ex)
+ public final void log(int level, String msg, Throwable throwable)
{
- _log(null, level, msg, ex);
+ _log(null, level, msg, throwable);
}
- public void log(ServiceReference sr, int level, String msg)
+ public final void log(ServiceReference sr, int level, String msg)
{
_log(sr, level, msg, null);
}
- public void log(ServiceReference sr, int level, String msg, Throwable ex)
+ public final void log(ServiceReference sr, int level, String msg, Throwable throwable)
{
- _log(sr, level, msg, ex);
+ _log(sr, level, msg, throwable);
+ }
+
+ protected void doLog(ServiceReference sr, int level, String msg, Throwable throwable)
+ {
+ String s = (sr == null) ? null : "SvcRef " + sr;
+ s = (s == null) ? msg : s + " " + msg;
+ s = (throwable == null) ? s : s + " (" + throwable + ")";
+ switch (level)
+ {
+ case LOG_DEBUG:
+ System.out.println("DEBUG: " + s);
+ break;
+ case LOG_ERROR:
+ System.out.println("ERROR: " + s);
+ if (throwable != null)
+ {
+ if ((throwable instanceof BundleException) &&
+ (((BundleException) throwable).getNestedException() != null))
+ {
+ throwable = ((BundleException) throwable).getNestedException();
+ }
+ throwable.printStackTrace();
+ }
+ break;
+ case LOG_INFO:
+ System.out.println("INFO: " + s);
+ break;
+ case LOG_WARNING:
+ System.out.println("WARNING: " + s);
+ break;
+ default:
+ System.out.println("UNKNOWN[" + level + "]: " + s);
+ }
}
private void _log(ServiceReference sr, int level, String msg, Throwable throwable)
@@ -108,41 +137,15 @@
if (m_logLevel >= level)
{
+ // Use the log service if available.
if (logger != null)
{
_logReflectively(logger, sr, level, msg, throwable);
}
+ // Otherwise, default logging action.
else
{
- String s = (sr == null) ? null : "SvcRef " + sr;
- s = (s == null) ? msg : s + " " + msg;
- s = (throwable == null) ? s : s + " (" + throwable + ")";
- switch (level)
- {
- case LOG_DEBUG:
- System.out.println("DEBUG: " + s);
- break;
- case LOG_ERROR:
- System.out.println("ERROR: " + s);
- if (throwable != null)
- {
- if ((throwable instanceof BundleException) &&
- (((BundleException) throwable).getNestedException() != null))
- {
- throwable = ((BundleException) throwable).getNestedException();
- }
- throwable.printStackTrace();
- }
- break;
- case LOG_INFO:
- System.out.println("INFO: " + s);
- break;
- case LOG_WARNING:
- System.out.println("WARNING: " + s);
- break;
- default:
- System.out.println("UNKNOWN[" + level + "]: " + s);
- }
+ doLog(sr, level, msg, throwable);
}
}
}
@@ -207,7 +210,7 @@
* If a higher ranking log service is registered, then this will switch
* to the higher ranking log service.
**/
- public synchronized void serviceChanged(ServiceEvent event)
+ public final synchronized void serviceChanged(ServiceEvent event)
{
// If no logger is in use, then grab this one.
if ((event.getType() == ServiceEvent.REGISTERED) && (m_logRef == null))
@@ -228,7 +231,7 @@
m_logRef = ref;
setLogger(m_context.getService(m_logRef));
}
-
+
}
// If the current logger is going away, release it and try to
// find another one.