Bug 535963 - [log] ExtendedLogEntry.getContext should use event as
context

Change-Id: Iefb2734f9f04a90811fda9bfb38446f5bd486380
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/equinox/log/test/LogReaderServiceTest.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/equinox/log/test/LogReaderServiceTest.java
index 2cf3301..f68ee39 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/equinox/log/test/LogReaderServiceTest.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/equinox/log/test/LogReaderServiceTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2014 IBM Corporation and others All rights reserved. This
+ * Copyright (c) 2007, 2018 IBM Corporation and others All rights reserved. This
  * program and the accompanying materials are made available under the terms of
  * the Eclipse Public License v1.0 which accompanies this distribution, and is
  * available at http://www.eclipse.org/legal/epl-v10.html
@@ -18,6 +18,7 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
+import org.eclipse.equinox.log.ExtendedLogEntry;
 import org.eclipse.equinox.log.SynchronousLogListener;
 import org.eclipse.osgi.container.Module;
 import org.eclipse.osgi.container.ModuleContainerAdaptor.ContainerEvent;
@@ -26,8 +27,11 @@
 import org.eclipse.osgi.tests.OSGiTestsActivator;
 import org.eclipse.osgi.tests.bundles.AbstractBundleTests;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.log.LogEntry;
@@ -151,7 +155,11 @@
 			testBundle.start();
 			listener.waitForLogEntry();
 		}
-		assertTrue(listener.getEntryX().getLevel() == LogService.LOG_INFO);
+
+		ExtendedLogEntry entry = listener.getEntryX();
+		assertTrue(entry.getLevel() == LogService.LOG_INFO);
+		assertEquals("Wrong level.", LogLevel.INFO, entry.getLogLevel());
+		assertTrue("Wrong context: " + entry.getContext(), entry.getContext() instanceof BundleEvent);
 	}
 
 	public void testLogBundleEventSynchronous() throws Exception {
@@ -179,7 +187,10 @@
 			OSGiTestsActivator.getContext().registerService(Object.class.getName(), new Object(), null);
 			listener.waitForLogEntry();
 		}
-		assertTrue(listener.getEntryX().getLevel() == LogService.LOG_INFO);
+		ExtendedLogEntry entry = listener.getEntryX();
+		assertTrue(entry.getLevel() == LogService.LOG_INFO);
+		assertEquals("Wrong level.", LogLevel.INFO, entry.getLogLevel());
+		assertTrue("Wrong context: " + entry.getContext(), entry.getContext() instanceof ServiceEvent);
 	}
 
 	public void testLogServiceEventDebug() throws Exception {
@@ -191,18 +202,34 @@
 			registration.setProperties(new Hashtable());
 			listener.waitForLogEntry();
 		}
-		assertTrue(listener.getEntryX().getLevel() == LogService.LOG_DEBUG);
+		ExtendedLogEntry entry = listener.getEntryX();
+		assertTrue(entry.getLevel() == LogService.LOG_DEBUG);
+		assertEquals("Wrong level.", LogLevel.DEBUG, entry.getLogLevel());
+		assertTrue("Wrong context: " + entry.getContext(), entry.getContext() instanceof ServiceEvent);
 	}
 
 	public void testLogFrameworkEvent() throws Exception {
 		Bundle testBundle = installer.installBundle("test.logging.a"); //$NON-NLS-1$
-		TestListener listener = new TestListener(testBundle);
+		final AtomicReference<LogEntry> logEntry = new AtomicReference<>();
+		final CountDownLatch countDown = new CountDownLatch(1);
+		LogListener listener = new LogListener() {
+			@Override
+			public void logged(LogEntry entry) {
+				if ("Events.Framework".equals(entry.getLoggerName())) {
+					logEntry.set(entry);
+					countDown.countDown();
+				}
+			}
+		};
 		reader.addLogListener(listener);
-		synchronized (listener) {
-			installer.refreshPackages(new Bundle[] {testBundle});
-			listener.waitForLogEntry();
-		}
-		assertTrue(listener.getEntryX().getLevel() == LogService.LOG_INFO);
+		installer.refreshPackages(new Bundle[] {testBundle});
+
+		countDown.await(5, TimeUnit.SECONDS);
+
+		ExtendedLogEntry entry = (ExtendedLogEntry) logEntry.get();
+		assertTrue(entry.getLevel() == LogService.LOG_INFO);
+		assertEquals("Wrong level.", LogLevel.INFO, entry.getLogLevel());
+		assertTrue("Wrong context: " + entry.getContext(), entry.getContext() instanceof FrameworkEvent);
 	}
 
 	public void testLogFrameworkEventType() throws Exception {
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogEntryImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogEntryImpl.java
index 86eac4c..b686c58 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogEntryImpl.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogEntryImpl.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2016 Cognos Incorporated, IBM Corporation and others
+ * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation and others
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
@@ -26,6 +26,7 @@
 	private final int level;
 	private final LogLevel logLevelEnum;
 	private final String message;
+	private final ServiceReference<?> ref;
 	private final Throwable throwable;
 	private final Object contextObject;
 	private final long time;
@@ -55,7 +56,7 @@
 		return threadId.longValue();
 	}
 
-	public ExtendedLogEntryImpl(Bundle bundle, String loggerName, StackTraceElement stackTraceElement, Object contextObject, LogLevel logLevelEnum, int level, String message, Throwable throwable) {
+	public ExtendedLogEntryImpl(Bundle bundle, String loggerName, StackTraceElement stackTraceElement, Object contextObject, LogLevel logLevelEnum, int level, String message, ServiceReference<?> ref, Throwable throwable) {
 		this.time = System.currentTimeMillis();
 		this.loggerName = loggerName;
 		this.bundle = bundle;
@@ -63,6 +64,7 @@
 		this.logLevelEnum = logLevelEnum;
 		this.message = message;
 		this.throwable = throwable;
+		this.ref = ref;
 		this.contextObject = contextObject;
 
 		Thread currentThread = Thread.currentThread();
@@ -110,10 +112,7 @@
 	}
 
 	public ServiceReference<?> getServiceReference() {
-		if (contextObject != null && contextObject instanceof ServiceReference)
-			return (ServiceReference<?>) contextObject;
-
-		return null;
+		return ref;
 	}
 
 	public long getTime() {
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java
index 823ac24..a54dd3d 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogReaderServiceFactory.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2017 Cognos Incorporated, IBM Corporation and others
+ * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation and others
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
@@ -29,6 +29,7 @@
 import org.eclipse.osgi.internal.log.OrderedExecutor.OrderedTaskQueue;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.log.LogEntry;
 import org.osgi.service.log.LogLevel;
@@ -209,21 +210,21 @@
 		return count;
 	}
 
-	void log(final Bundle bundle, final String name, final StackTraceElement stackTraceElement, final Object context, final LogLevel logLevelEnum, final int level, final String message, final Throwable exception) {
+	void log(final Bundle bundle, final String name, final StackTraceElement stackTraceElement, final Object context, final LogLevel logLevelEnum, final int level, final String message, final ServiceReference<?> ref, final Throwable exception) {
 		if (System.getSecurityManager() != null) {
 			AccessController.doPrivileged(new PrivilegedAction<Void>() {
 				public Void run() {
-					logPrivileged(bundle, name, stackTraceElement, context, logLevelEnum, level, message, exception);
+					logPrivileged(bundle, name, stackTraceElement, context, logLevelEnum, level, message, ref, exception);
 					return null;
 				}
 			});
 		} else {
-			logPrivileged(bundle, name, stackTraceElement, context, logLevelEnum, level, message, exception);
+			logPrivileged(bundle, name, stackTraceElement, context, logLevelEnum, level, message, ref, exception);
 		}
 	}
 
-	void logPrivileged(Bundle bundle, String name, StackTraceElement stackTraceElement, Object context, LogLevel logLevelEnum, int level, String message, Throwable exception) {
-		LogEntry logEntry = new ExtendedLogEntryImpl(bundle, name, stackTraceElement, context, logLevelEnum, level, message, exception);
+	void logPrivileged(Bundle bundle, String name, StackTraceElement stackTraceElement, Object context, LogLevel logLevelEnum, int level, String message, ServiceReference<?> ref, Throwable exception) {
+		LogEntry logEntry = new ExtendedLogEntryImpl(bundle, name, stackTraceElement, context, logLevelEnum, level, message, ref, exception);
 		storeEntry(logEntry);
 		ArrayMap<LogListener, Object[]> listenersCopy;
 		listenersLock.readLock().lock();
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogServiceFactory.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogServiceFactory.java
index e209943..fa49093 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogServiceFactory.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/ExtendedLogServiceFactory.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2017 Cognos Incorporated, IBM Corporation and others
+ * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation and others
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
@@ -9,12 +9,19 @@
 
 import java.security.AccessController;
 import java.security.Permission;
-import java.util.*;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.eclipse.equinox.log.ExtendedLogService;
 import org.eclipse.equinox.log.LogPermission;
 import org.eclipse.osgi.framework.util.SecureAction;
-import org.osgi.framework.*;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.log.LogLevel;
 import org.osgi.service.log.Logger;
 import org.osgi.service.log.admin.LoggerAdmin;
@@ -80,8 +87,8 @@
 		return logReaderServiceFactory.isLoggable(bundle, name, level);
 	}
 
-	void log(Bundle bundle, String name, StackTraceElement stackTraceElement, Object context, LogLevel logLevelEnum, int level, String message, Throwable exception) {
-		logReaderServiceFactory.log(bundle, name, stackTraceElement, context, logLevelEnum, level, message, exception);
+	void log(Bundle bundle, String name, StackTraceElement stackTraceElement, Object context, LogLevel logLevelEnum, int level, String message, ServiceReference<?> ref, Throwable exception) {
+		logReaderServiceFactory.log(bundle, name, stackTraceElement, context, logLevelEnum, level, message, ref, exception);
 	}
 
 	void checkLogPermission() throws SecurityException {
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LogServiceManager.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LogServiceManager.java
index 96fc535..c08d9f7 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LogServiceManager.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LogServiceManager.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2017 Cognos Incorporated, IBM Corporation and others
+ * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation and others
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
@@ -127,7 +127,7 @@
 		Bundle bundle = event.getBundle();
 		if (logReaderServiceFactory.isLoggable(bundle, LOGGER_BUNDLE_EVENT, LogService.LOG_INFO)) {
 			LoggerImpl logger = (LoggerImpl) systemBundleLog.getLogger(LOGGER_BUNDLE_EVENT);
-			logger.log(bundle, null, null, LogService.LOG_INFO, getBundleEventTypeName(event.getType()), null);
+			logger.log(bundle, event, null, LogService.LOG_INFO, getBundleEventTypeName(event.getType()), null, null);
 		}
 	}
 
@@ -143,7 +143,7 @@
 		int logType = (eventType == ServiceEvent.MODIFIED) ? LogService.LOG_DEBUG : LogService.LOG_INFO;
 		if (logReaderServiceFactory.isLoggable(bundle, LOGGER_SERVICE_EVENT, logType)) {
 			LoggerImpl logger = (LoggerImpl) systemBundleLog.getLogger(LOGGER_SERVICE_EVENT);
-			logger.log(bundle, reference, null, logType, getServiceEventTypeName(eventType), null);
+			logger.log(bundle, event, null, logType, getServiceEventTypeName(eventType), reference, null);
 		}
 	}
 
@@ -151,10 +151,10 @@
 	 * FrameworkListener.frameworkEvent method.
 	 *
 	 */
+	@SuppressWarnings("deprecation")
 	public void frameworkEvent(FrameworkEvent event) {
 		Bundle bundle = event.getBundle();
 		int eventType = event.getType();
-		@SuppressWarnings("deprecation")
 		int logType;
 		switch (eventType) {
 			case FrameworkEvent.ERROR :
@@ -170,7 +170,7 @@
 
 		if (logReaderServiceFactory.isLoggable(bundle, LOGGER_FRAMEWORK_EVENT, logType)) {
 			LoggerImpl logger = (LoggerImpl) systemBundleLog.getLogger(LOGGER_FRAMEWORK_EVENT);
-			logger.log(bundle, null, null, logType, getFrameworkEventTypeName(eventType), event.getThrowable());
+			logger.log(bundle, event, null, logType, getFrameworkEventTypeName(eventType), null, event.getThrowable());
 		}
 	}
 
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LoggerImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LoggerImpl.java
index 90a2ee3..ec1678d 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LoggerImpl.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/log/LoggerImpl.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2012 Cognos Incorporated, IBM Corporation and others
+ * Copyright (c) 2006, 2018 Cognos Incorporated, IBM Corporation and others
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
@@ -12,7 +12,9 @@
 import org.eclipse.equinox.log.Logger;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
-import org.osgi.service.log.*;
+import org.osgi.service.log.LogLevel;
+import org.osgi.service.log.LogService;
+import org.osgi.service.log.LoggerConsumer;
 import org.osgi.service.log.admin.LoggerContext;
 
 public class LoggerImpl implements Logger {
@@ -47,12 +49,12 @@
 
 	@SuppressWarnings("rawtypes")
 	public void log(ServiceReference sr, int level, String message) {
-		log(sr, level, message, null);
+		log(sr, null, level, message, sr, null);
 	}
 
 	@SuppressWarnings("rawtypes")
 	public void log(ServiceReference sr, int level, String message, Throwable exception) {
-		log(sr, null, level, message, exception);
+		log(sr, null, level, message, sr, exception);
 	}
 
 	public void log(Object context, int level, String message) {
@@ -60,19 +62,19 @@
 	}
 
 	public void log(Object context, int level, String message, Throwable exception) {
-		log(context, null, level, message, exception);
+		log(context, null, level, message, null, exception);
 	}
 
-	private void log(Object context, LogLevel logLevelEnum, int level, String message, Throwable exception) {
-		log(logServiceImpl.getBundle(), context, logLevelEnum, level, message, exception);
+	private void log(Object context, LogLevel logLevelEnum, int level, String message, ServiceReference<?> ref, Throwable exception) {
+		log(logServiceImpl.getBundle(), context, logLevelEnum, level, message, ref, exception);
 	}
 
-	void log(Bundle entryBundle, Object context, LogLevel logLevelEnum, int level, String message, Throwable exception) {
+	void log(Bundle entryBundle, Object context, LogLevel logLevelEnum, int level, String message, ServiceReference<?> ref, Throwable exception) {
 		if (logLevelEnum == null) {
 			logLevelEnum = getLogLevel(level);
 		}
 		if (enabledLevel.implies(logLevelEnum)) {
-			logServiceImpl.getFactory().log(entryBundle, name, getLocation(), context, logLevelEnum, level, message, exception);
+			logServiceImpl.getFactory().log(entryBundle, name, getLocation(), context, logLevelEnum, level, message, ref, exception);
 		}
 	}
 
@@ -281,7 +283,7 @@
 		StackTraceElement location = getLocation();
 		Arguments processedArguments = new Arguments(arguments);
 		String message = processedArguments.isEmpty() ? format : formatMessage(format, processedArguments);
-		logServiceImpl.getFactory().log(logServiceImpl.getBundle(), name, location, processedArguments.serviceReference(), level, level.ordinal(), message.toString(), processedArguments.throwable());
+		logServiceImpl.getFactory().log(logServiceImpl.getBundle(), name, location, processedArguments.serviceReference(), level, level.ordinal(), message.toString(), processedArguments.serviceReference(), processedArguments.throwable());
 	}
 
 	private StackTraceElement getLocation() {