Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvladt2011-02-17 20:03:43 +0000
committervladt2011-02-17 20:03:43 +0000
commit52d3db5c53c2d3fe6a34886bb0139ef3b2da1032 (patch)
tree2de4c521a258fb0637704bbfca281754502bce6c /org.eclipse.m2e.logback.configuration
parentea432ab47e2dddc985a348cdfe6dc654e046d102 (diff)
downloadm2e-core-52d3db5c53c2d3fe6a34886bb0139ef3b2da1032.tar.gz
m2e-core-52d3db5c53c2d3fe6a34886bb0139ef3b2da1032.tar.xz
m2e-core-52d3db5c53c2d3fe6a34886bb0139ef3b2da1032.zip
335872: Start the m2e logback bundle by default and configure logback only after the eclipse instance location is initialized
Diffstat (limited to 'org.eclipse.m2e.logback.configuration')
-rw-r--r--org.eclipse.m2e.logback.configuration/META-INF/p2.inf1
-rw-r--r--org.eclipse.m2e.logback.configuration/build.properties3
-rw-r--r--org.eclipse.m2e.logback.configuration/src/org/eclipse/m2e/logback/configuration/LogPlugin.java81
3 files changed, 56 insertions, 29 deletions
diff --git a/org.eclipse.m2e.logback.configuration/META-INF/p2.inf b/org.eclipse.m2e.logback.configuration/META-INF/p2.inf
new file mode 100644
index 00000000..fe8e606a
--- /dev/null
+++ b/org.eclipse.m2e.logback.configuration/META-INF/p2.inf
@@ -0,0 +1 @@
+instructions.configure = markStarted(started: true); \ No newline at end of file
diff --git a/org.eclipse.m2e.logback.configuration/build.properties b/org.eclipse.m2e.logback.configuration/build.properties
index e5f876ad..11399196 100644
--- a/org.eclipse.m2e.logback.configuration/build.properties
+++ b/org.eclipse.m2e.logback.configuration/build.properties
@@ -3,4 +3,5 @@ output.. = target/classes/
bin.includes = META-INF/,\
.,\
defaultLogbackConfiguration/,\
- OSGI-INF/l10n/bundle.properties
+ OSGI-INF/l10n/bundle.properties,\
+ OSGI-INF/
diff --git a/org.eclipse.m2e.logback.configuration/src/org/eclipse/m2e/logback/configuration/LogPlugin.java b/org.eclipse.m2e.logback.configuration/src/org/eclipse/m2e/logback/configuration/LogPlugin.java
index 5de2236a..375e7c2d 100644
--- a/org.eclipse.m2e.logback.configuration/src/org/eclipse/m2e/logback/configuration/LogPlugin.java
+++ b/org.eclipse.m2e.logback.configuration/src/org/eclipse/m2e/logback/configuration/LogPlugin.java
@@ -15,6 +15,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
+import java.util.Timer;
+import java.util.TimerTask;
import org.osgi.framework.BundleContext;
import org.slf4j.ILoggerFactory;
@@ -31,7 +33,7 @@ import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
-import org.eclipse.osgi.service.datalocation.Location;
+
public class LogPlugin extends Plugin {
private static final String PLUGIN_ID = "org.eclipse.m2e.logback.configuration"; //$NON-NLS-1$
@@ -39,43 +41,64 @@ public class LogPlugin extends Plugin {
// This has to match the log directory in defaultLogbackConfiguration/logback.xml
public static final String PROPERTY_LOG_DIRECTORY = "org.eclipse.m2e.log.dir"; //$NON-NLS-1$
+ private BundleContext bundleContext;
+
+ private boolean isConfigured;
+
+ private Timer timer = new Timer("logback configurator timer");
+
+ private TimerTask timerTask = new TimerTask() {
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ if(Platform.getInstanceLocation().isSet()) {
+ timer.cancel();
+ configureLogback();
+ }
+ }
+ };
+
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
+ bundleContext = context;
- configureLogger(context);
-
- LogHelper.logJavaProperties(LoggerFactory.getLogger(LogPlugin.class));
+ if(System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) != null) {
+ // The standard logback config file property is set - don't force our configuration
+ systemOut(ContextInitializer.CONFIG_FILE_PROPERTY + "=" //$NON-NLS-1$
+ + System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY));
+ } else {
+ if(!Platform.getInstanceLocation().isSet()) {
+ systemOut("The " + PLUGIN_ID + " bundle was activated before the platform instance location was initialized."); //$NON-NLS-1$ //$NON-NLS-2$
+ timer.schedule(timerTask, 0 /*delay*/, 50 /*period*/);
+ } else {
+ configureLogback();
+ }
+ }
}
private static void systemOut(String message) {
- System.out.println(PLUGIN_ID + ": " + message);
+ System.out.println(PLUGIN_ID + ": " + message); //$NON-NLS-1$
}
private static void systemErr(String message) {
- System.err.println(PLUGIN_ID + ": " + message);
+ System.err.println(PLUGIN_ID + ": " + message); //$NON-NLS-1$
}
- private void configureLogger(BundleContext context) {
- if(System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) != null) {
- systemOut(ContextInitializer.CONFIG_FILE_PROPERTY + "="
- + System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY));
+ private synchronized void configureLogback() {
+ if(isConfigured) {
+ systemOut("Logback was configured already"); //$NON-NLS-1$
return;
}
- Location instanceLocation = Platform.getInstanceLocation();
- if(!instanceLocation.isSet()) {
- new Exception("The " + PLUGIN_ID + " bundle was activated before the platform instance location was initialized.");
- return;
- }
- File stateDir = getStateLocation().toFile();
-
- File configFile = new File(stateDir, "logback." + context.getBundle().getVersion().toString() + ".xml");
- systemOut("Logback config file: " + configFile.getAbsolutePath());
try {
+ File stateDir = getStateLocation().toFile();
+
+ File configFile = new File(stateDir, "logback." + bundleContext.getBundle().getVersion().toString() + ".xml"); //$NON-NLS-1$ //$NON-NLS-2$
+ systemOut("Logback config file: " + configFile.getAbsolutePath()); //$NON-NLS-1$
+
if(!configFile.isFile()) {
// Copy the default config file to the actual config file
- InputStream is = context.getBundle().getEntry("defaultLogbackConfiguration/logback.xml").openStream();
+ InputStream is = bundleContext.getBundle().getEntry("defaultLogbackConfiguration/logback.xml").openStream(); //$NON-NLS-1$
try {
configFile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(configFile);
@@ -95,13 +118,15 @@ public class LogPlugin extends Plugin {
}
}
- if(System.getProperty(PROPERTY_LOG_DIRECTORY, "").length() <= 0) {
+ if(System.getProperty(PROPERTY_LOG_DIRECTORY, "").length() <= 0) { //$NON-NLS-1$
System.setProperty(PROPERTY_LOG_DIRECTORY, stateDir.getAbsolutePath());
}
loadConfiguration(configFile.toURL());
+
+ isConfigured = true;
} catch(Exception e) {
e.printStackTrace();
- getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, "Exception while setting up logging:" + e.getMessage(), e));
+ getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, "Exception while setting up logging:" + e.getMessage(), e)); //$NON-NLS-1$
return;
}
}
@@ -111,9 +136,7 @@ public class LogPlugin extends Plugin {
int i = 0;
while(loggerFactory instanceof SubstituteLoggerFactory && i < 100) {
// slf4j is initialization phase
- if(loggerFactory != null) {
- systemOut("SLF4J logger factory class: " + loggerFactory.getClass().getName());
- }
+ systemOut("SLF4J logger factory class: " + loggerFactory.getClass().getName()); //$NON-NLS-1$
try {
Thread.sleep(50);
} catch(InterruptedException e) {
@@ -125,15 +148,15 @@ public class LogPlugin extends Plugin {
if(!(loggerFactory instanceof LoggerContext)) {
if(loggerFactory == null) {
// Is it possible?
- systemErr("SLF4J logger factory is null");
+ systemErr("SLF4J logger factory is null"); //$NON-NLS-1$
return;
}
- systemErr("SLF4J logger factory is not an instance of LoggerContext: "
+ systemErr("SLF4J logger factory is not an instance of LoggerContext: " //$NON-NLS-1$
+ loggerFactory.getClass().getName());
return;
}
- systemOut("Initializing logback");
+ systemOut("Initializing logback"); //$NON-NLS-1$
LoggerContext lc = (LoggerContext) loggerFactory;
lc.reset();
@@ -142,5 +165,7 @@ public class LogPlugin extends Plugin {
configurator.doConfigure(configFile);
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
+
+ LogHelper.logJavaProperties(LoggerFactory.getLogger(LogPlugin.class));
}
}

Back to the top