FELIX-4265 Provides a recorder for startup events
* Exports the o.a.f.i.extender.queue.debug package
* Register a QueueEventProxy service when JVM started with 'org.apache.felix.ipojo.extender.BootstrapQueueDebug=true' system property
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1528508 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core/pom.xml b/ipojo/runtime/core/pom.xml
index 0b2276e..654c0b5 100644
--- a/ipojo/runtime/core/pom.xml
+++ b/ipojo/runtime/core/pom.xml
@@ -161,6 +161,7 @@
version="${ipojo.package.version}";-split-package:=merge-first,
org.apache.felix.ipojo.extender.builder; version="${ipojo.package.version}",
org.apache.felix.ipojo.extender.queue; version="${ipojo.package.version}",
+ org.apache.felix.ipojo.extender.queue.debug; version="${ipojo.package.version}",
org.apache.felix.ipojo.parser; version="${ipojo.package.version}",
org.apache.felix.ipojo.util; version="${ipojo.package.version}",
org.apache.felix.ipojo.handlers.dependency; version="${ipojo.package.version}",
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
index df03d2e..ddac180 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
@@ -30,10 +30,12 @@
import org.apache.felix.ipojo.extender.internal.queue.NamingThreadFactory;
import org.apache.felix.ipojo.extender.internal.queue.PrefixedThreadFactory;
import org.apache.felix.ipojo.extender.internal.queue.SynchronousQueueService;
+import org.apache.felix.ipojo.extender.internal.queue.debug.ReplayQueueEventProxy;
import org.apache.felix.ipojo.extender.internal.queue.pref.HeaderPreferenceSelection;
import org.apache.felix.ipojo.extender.internal.queue.pref.Preference;
import org.apache.felix.ipojo.extender.internal.queue.pref.PreferenceQueueService;
import org.apache.felix.ipojo.extender.internal.queue.pref.enforce.EnforcedQueueService;
+import org.apache.felix.ipojo.extender.queue.debug.QueueEventProxy;
import org.apache.felix.ipojo.util.Logger;
import org.osgi.framework.*;
import org.osgi.util.tracker.BundleTracker;
@@ -43,6 +45,7 @@
* iPOJO main activator.
*/
public class Extender implements BundleActivator {
+ public static final String BOOTSTRAP_QUEUE_DEBUG_PROPERTY = "org.apache.felix.ipojo.extender.BootstrapQueueDebug";
/**
* Enables the iPOJO internal dispatcher.
* This internal dispatcher helps the OSGi framework to support large
@@ -130,6 +133,13 @@
// Initialize ConfigurationTracker
ConfigurationTracker.initialize();
+ // Initialize the queue event proxy if wanted
+ ReplayQueueEventProxy proxy = null;
+ if (Boolean.getBoolean(BOOTSTRAP_QUEUE_DEBUG_PROPERTY)) {
+ proxy = new ReplayQueueEventProxy();
+ context.registerService(QueueEventProxy.class, proxy, null);
+ }
+
BundleProcessor extensionBundleProcessor = new ExtensionBundleProcessor(m_logger);
BundleProcessor componentsProcessor = new ComponentsBundleProcessor(m_logger);
BundleProcessor configurationProcessor = new ConfigurationProcessor(m_logger);
@@ -139,6 +149,11 @@
new SynchronousQueueService(context),
Preference.SYNC,
m_logger);
+
+ // If required, add the event proxy
+ if (proxy != null) {
+ m_queueService.addQueueListener(proxy);
+ }
} else {
// Build a thread factory that will groups extender's thread together
ThreadFactory threadFactory = new GroupThreadFactory(new ThreadGroup("iPOJO Extender"));
@@ -156,6 +171,13 @@
extensionBundleProcessor = new QueuingActivationProcessor(extensionBundleProcessor, m_queueService);
componentsProcessor = new QueuingActivationProcessor(componentsProcessor, m_queueService);
configurationProcessor = new QueuingActivationProcessor(configurationProcessor, m_queueService);
+
+ // If required, add the event proxy to both real services
+ if (proxy != null) {
+ sync.addQueueListener(proxy);
+ async.addQueueListener(proxy);
+ }
+
}
m_queueService.start();
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxy.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxy.java
new file mode 100644
index 0000000..0e040bb
--- /dev/null
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxy.java
@@ -0,0 +1,157 @@
+/*
+ * 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.felix.ipojo.extender.internal.queue.debug;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.felix.ipojo.extender.queue.JobInfo;
+import org.apache.felix.ipojo.extender.queue.QueueListener;
+import org.apache.felix.ipojo.extender.queue.debug.QueueEventProxy;
+
+/**
+ * User: guillaume
+ * Date: 02/10/13
+ * Time: 10:51
+ */
+public class ReplayQueueEventProxy implements QueueEventProxy, QueueListener {
+ /**
+ * Store QueueListeners.
+ */
+ private final List<QueueListener> m_listeners = new ArrayList<QueueListener>();
+
+ /**
+ * Store QueueEvent to be replayed.
+ */
+ private List<QueueEvent> m_events = new CopyOnWriteArrayList<QueueEvent>();
+
+ public void addQueueListener(final QueueListener listener) {
+ m_listeners.add(listener);
+ // replay all the existing events to the new listener
+ replay(listener);
+ }
+
+ public void removeQueueListener(final QueueListener listener) {
+ m_listeners.remove(listener);
+ }
+
+ public void enlisted(final JobInfo info) {
+ EnlistedQueueEvent event = new EnlistedQueueEvent(info);
+ m_events.add(event);
+ forward(event);
+ }
+
+ public void started(final JobInfo info) {
+ StartedQueueEvent event = new StartedQueueEvent(info);
+ m_events.add(event);
+ forward(event);
+ }
+
+ public void executed(final JobInfo info, final Object result) {
+ ExecutedQueueEvent event = new ExecutedQueueEvent(info, result);
+ m_events.add(event);
+ forward(event);
+ }
+
+ public void failed(final JobInfo info, final Throwable throwable) {
+ FailedQueueEvent event = new FailedQueueEvent(info, throwable);
+ m_events.add(event);
+ forward(event);
+ }
+
+ /**
+ * Replay all stored events to the given QueueListener.
+ */
+ private void replay(final QueueListener listener) {
+ for (QueueEvent event : m_events) {
+ event.replay(listener);
+ }
+ }
+
+ /**
+ * Forward the given QueueEvent to all the registered listeners.
+ */
+ private void forward(final QueueEvent event) {
+ for (QueueListener listener : m_listeners) {
+ event.replay(listener);
+ }
+ }
+
+ /**
+ * Encapsulate event forwarding logic.
+ */
+ private interface QueueEvent {
+ void replay(QueueListener listener);
+ }
+
+ private class EnlistedQueueEvent implements QueueEvent {
+ private final JobInfo m_info;
+
+ public EnlistedQueueEvent(final JobInfo info) {
+ m_info = info;
+ }
+
+ public void replay(final QueueListener listener) {
+ listener.enlisted(m_info);
+ }
+ }
+
+ private class StartedQueueEvent implements QueueEvent {
+ private final JobInfo m_info;
+
+ public StartedQueueEvent(final JobInfo info) {
+ m_info = info;
+ }
+
+ public void replay(final QueueListener listener) {
+ listener.started(m_info);
+ }
+ }
+
+ private class ExecutedQueueEvent implements QueueEvent {
+ private final JobInfo m_info;
+ private final Object m_result;
+
+ public ExecutedQueueEvent(final JobInfo info, final Object result) {
+ m_info = info;
+ m_result = result;
+ }
+
+ public void replay(final QueueListener listener) {
+ listener.executed(m_info, m_result);
+ }
+ }
+
+ private class FailedQueueEvent implements QueueEvent {
+ private final JobInfo m_info;
+ private final Throwable m_throwable;
+
+ public FailedQueueEvent(final JobInfo info, final Throwable throwable) {
+ m_info = info;
+ m_throwable = throwable;
+ }
+
+ public void replay(final QueueListener listener) {
+ listener.failed(m_info, m_throwable);
+ }
+ }
+
+}
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/queue/debug/QueueEventProxy.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/queue/debug/QueueEventProxy.java
new file mode 100644
index 0000000..16dce7d
--- /dev/null
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/queue/debug/QueueEventProxy.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.felix.ipojo.extender.queue.debug;
+
+import org.apache.felix.ipojo.extender.queue.QueueListener;
+
+/**
+ * This service record events from a {@link org.apache.felix.ipojo.extender.queue.QueueService}.
+ * When a listener is added, all recorded events are replayed into the listener.
+ */
+public interface QueueEventProxy {
+ /**
+ * Add a {@link org.apache.felix.ipojo.extender.queue.QueueListener} that will be notified on events
+ * relative to the observed {@link org.apache.felix.ipojo.extender.queue.QueueService}.
+ * @param listener added listener
+ */
+ void addQueueListener(QueueListener listener);
+
+ /**
+ * Remove a {@link QueueListener} from the observed {@link org.apache.felix.ipojo.extender.queue.QueueService}.
+ * @param listener removed listener
+ */
+ void removeQueueListener(QueueListener listener);
+
+}
diff --git a/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxyTestCase.java b/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxyTestCase.java
new file mode 100644
index 0000000..3253ef9
--- /dev/null
+++ b/ipojo/runtime/core/src/test/java/org/apache/felix/ipojo/extender/internal/queue/debug/ReplayQueueEventProxyTestCase.java
@@ -0,0 +1,122 @@
+/*
+ * 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.felix.ipojo.extender.internal.queue.debug;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+import org.apache.felix.ipojo.extender.queue.JobInfo;
+import org.apache.felix.ipojo.extender.queue.QueueListener;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import junit.framework.TestCase;
+
+/**
+ * User: guillaume
+ * Date: 02/10/13
+ * Time: 12:01
+ */
+public class ReplayQueueEventProxyTestCase extends TestCase {
+
+ @Mock
+ private JobInfo m_info1;
+
+ @Mock
+ private JobInfo m_info2;
+
+ @Mock
+ private QueueListener m_listener;
+
+ @Override
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ public void testEventsAreReplayedOnListenerAddition() throws Exception {
+ ReplayQueueEventProxy proxy = new ReplayQueueEventProxy();
+
+ proxy.enlisted(m_info1);
+ proxy.started(m_info1);
+ proxy.enlisted(m_info2);
+ proxy.executed(m_info1, "hello");
+ proxy.started(m_info2);
+ Exception throwable = new Exception();
+ proxy.failed(m_info2, throwable);
+
+ proxy.addQueueListener(m_listener);
+
+ verify(m_listener).enlisted(m_info1);
+ verify(m_listener).started(m_info1);
+ verify(m_listener).executed(m_info1, "hello");
+ verify(m_listener).enlisted(m_info2);
+ verify(m_listener).started(m_info2);
+ verify(m_listener).failed(m_info2, throwable);
+
+ }
+
+ public void testNoEventsAreSendAfterListenerRemoval() throws Exception {
+ ReplayQueueEventProxy proxy = new ReplayQueueEventProxy();
+
+ proxy.enlisted(m_info1);
+ proxy.started(m_info1);
+
+ proxy.addQueueListener(m_listener);
+
+ proxy.enlisted(m_info2);
+
+ proxy.removeQueueListener(m_listener);
+
+ proxy.executed(m_info1, "hello");
+ proxy.started(m_info2);
+ Exception throwable = new Exception();
+ proxy.failed(m_info2, throwable);
+
+ // Ensure no methods are called after removal
+ verify(m_listener).enlisted(m_info1);
+ verify(m_listener).started(m_info1);
+ verify(m_listener).enlisted(m_info2);
+ verifyNoMoreInteractions(m_listener);
+
+ }
+
+ public void testEventsAreForwarded() throws Exception {
+ ReplayQueueEventProxy proxy = new ReplayQueueEventProxy();
+
+ proxy.enlisted(m_info1);
+ proxy.started(m_info1);
+
+ proxy.addQueueListener(m_listener);
+
+ proxy.enlisted(m_info2);
+ proxy.executed(m_info1, "hello");
+ proxy.started(m_info2);
+ Exception throwable = new Exception();
+ proxy.failed(m_info2, throwable);
+
+ verify(m_listener).enlisted(m_info1);
+ verify(m_listener).started(m_info1);
+ verify(m_listener).executed(m_info1, "hello");
+ verify(m_listener).enlisted(m_info2);
+ verify(m_listener).started(m_info2);
+ verify(m_listener).failed(m_info2, throwable);
+
+ }
+}