Repository: ignite Updated Branches: refs/heads/ignite-1270 4dea7bc48 -> 43d099439
IGNITE-1270 Make callback methods protected & add a test for them. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/43d09943 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/43d09943 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/43d09943 Branch: refs/heads/ignite-1270 Commit: 43d099439c00ec6e3073c5c41a2f714566de7012 Parents: 4dea7bc Author: Raul Kripalani <ra...@apache.org> Authored: Thu Dec 3 14:09:55 2015 +0000 Committer: Raul Kripalani <ra...@apache.org> Committed: Thu Dec 3 14:09:55 2015 +0000 ---------------------------------------------------------------------- .../IgniteAbstractOsgiContextActivator.java | 40 +++++++--- .../ignite/osgi/IgniteOsgiServiceTest.java | 58 ++++++++++++-- .../activators/BasicIgniteTestActivator.java | 30 +++++++ .../ignite/osgi/activators/TestOsgiFlags.java | 53 +++++++++++++ .../osgi/activators/TestOsgiFlagsImpl.java | 83 ++++++++++++++++++++ 5 files changed, 244 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/43d09943/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java ---------------------------------------------------------------------- diff --git a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java index c83c40d..ac76f6e 100644 --- a/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java +++ b/modules/osgi/src/main/java/org/apache/ignite/osgi/IgniteAbstractOsgiContextActivator.java @@ -29,6 +29,7 @@ import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.osgi.classloaders.BundleDelegatingClassLoader; import org.apache.ignite.osgi.classloaders.ContainerSweepClassLoader; import org.apache.ignite.osgi.classloaders.OsgiClassLoadingStrategyType; +import org.jetbrains.annotations.Nullable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -100,10 +101,19 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva cfg.setClassLoader(clsLdr); - preStart(ctx); + onBeforeStart(ctx); // Start Ignite. - ignite = Ignition.start(cfg); + try { + ignite = Ignition.start(cfg); + } + catch (Throwable t) { + U.error(log, "Failed to start Ignite via OSGi Activator [errMsg=" + t.getMessage() + ']', t); + + onAfterStart(ctx, t); + + return; + } log = ignite.log(); @@ -115,23 +125,25 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva // Export Ignite as a service. exportOsgiService(ignite); - postStart(ctx); + onAfterStart(ctx, null); } /** * Stops Ignite when the bundle is stopping. * - * @param context Bundle context. + * @param ctx Bundle context. * @throws Exception If failed. */ - @Override public final void stop(BundleContext context) throws Exception { - preStop(context); + @Override public final void stop(BundleContext ctx) throws Exception { + onBeforeStop(ctx); try { ignite.close(); } - catch (Exception e) { - U.error(log, "Failed to stop Ignite via OSGi Activator [errMsg=" + e.getMessage() + ']', e); + catch (Throwable t) { + U.error(log, "Failed to stop Ignite via OSGi Activator [errMsg=" + t.getMessage() + ']', t); + + onAfterStop(ctx, t); return; } @@ -141,7 +153,7 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva IgniteOsgiUtils.classloaders().remove(ignite); - postStop(context); + onAfterStop(ctx, null); } /** @@ -151,7 +163,7 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva * * @param ctx The {@link BundleContext}. */ - private void preStart(BundleContext ctx) { + protected void onBeforeStart(BundleContext ctx) { // No-op. } @@ -161,8 +173,9 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva * The default implementation is empty. Override it to introduce custom logic. * * @param ctx The {@link BundleContext}. + * @param t Throwable in case an error occurred when starting. {@code null} otherwise. */ - private void postStart(BundleContext ctx) { + protected void onAfterStart(BundleContext ctx, @Nullable Throwable t) { // No-op. } @@ -173,7 +186,7 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva * * @param ctx The {@link BundleContext}. */ - private void preStop(BundleContext ctx) { + protected void onBeforeStop(BundleContext ctx) { // No-op. } @@ -183,8 +196,9 @@ public abstract class IgniteAbstractOsgiContextActivator implements BundleActiva * The default implementation is empty. Override it to introduce custom logic. * * @param ctx The {@link BundleContext}. + * @param t Throwable in case an error occurred when stopping. {@code null} otherwise. */ - private void postStop(BundleContext ctx) { + protected void onAfterStop(BundleContext ctx, @Nullable Throwable t) { // No-op. } http://git-wip-us.apache.org/repos/asf/ignite/blob/43d09943/modules/osgi/src/test/java/org/apache/ignite/osgi/IgniteOsgiServiceTest.java ---------------------------------------------------------------------- diff --git a/modules/osgi/src/test/java/org/apache/ignite/osgi/IgniteOsgiServiceTest.java b/modules/osgi/src/test/java/org/apache/ignite/osgi/IgniteOsgiServiceTest.java index 2cb8838..9a2e92d 100644 --- a/modules/osgi/src/test/java/org/apache/ignite/osgi/IgniteOsgiServiceTest.java +++ b/modules/osgi/src/test/java/org/apache/ignite/osgi/IgniteOsgiServiceTest.java @@ -23,25 +23,32 @@ import java.util.List; import javax.inject.Inject; import org.apache.ignite.Ignite; import org.apache.ignite.osgi.activators.BasicIgniteTestActivator; +import org.apache.ignite.osgi.activators.TestOsgiFlags; +import org.apache.ignite.osgi.activators.TestOsgiFlagsImpl; import org.junit.Test; import org.junit.runner.RunWith; import org.ops4j.pax.exam.Configuration; import org.ops4j.pax.exam.CoreOptions; import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.ProbeBuilder; +import org.ops4j.pax.exam.TestProbeBuilder; import org.ops4j.pax.exam.junit.PaxExam; import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; import org.ops4j.pax.exam.spi.reactors.PerMethod; import org.ops4j.pax.exam.util.Filter; +import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.ops4j.pax.exam.CoreOptions.streamBundle; import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle; import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd; /** - * Pax Exam test class to check whether the Ignite service is exposed properly. + * Pax Exam test class to check whether the Ignite service is exposed properly and whether lifecycle callbacks + * are invoked. */ @RunWith(PaxExam.class) @ExamReactorStrategy(PerMethod.class) @@ -50,6 +57,9 @@ public class IgniteOsgiServiceTest extends AbstractIgniteKarafTest { @Inject @Filter("(ignite.name=testGrid)") private Ignite ignite; + @Inject + private BundleContext bundleCtx; + /** * @return Config. */ @@ -60,22 +70,56 @@ public class IgniteOsgiServiceTest extends AbstractIgniteKarafTest { // Add bundles we require. options.add( streamBundle(bundle() - .add(BasicIgniteTestActivator.class) - .set(Constants.BUNDLE_SYMBOLICNAME, BasicIgniteTestActivator.class.getSimpleName()) - .set(Constants.BUNDLE_ACTIVATOR, BasicIgniteTestActivator.class.getName()) - .set(Constants.DYNAMICIMPORT_PACKAGE, "*") - .build(withBnd()))); + .add(BasicIgniteTestActivator.class) + .add(TestOsgiFlags.class) + .add(TestOsgiFlagsImpl.class) + .set(Constants.BUNDLE_SYMBOLICNAME, BasicIgniteTestActivator.class.getSimpleName()) + .set(Constants.BUNDLE_ACTIVATOR, BasicIgniteTestActivator.class.getName()) + .set(Constants.EXPORT_PACKAGE, "org.apache.ignite.osgi.activators") + .set(Constants.DYNAMICIMPORT_PACKAGE, "*") + .build(withBnd()))); + + // Uncomment this if you'd like to debug inside the container. + // options.add(KarafDistributionOption.debugConfiguration()); return CoreOptions.options(options.toArray(new Option[0])); } /** + * Builds the probe. + * + * @param probe The probe builder. + * @return The probe builder. + */ + @ProbeBuilder + public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) { + probe.setHeader(Constants.IMPORT_PACKAGE, "*,org.apache.ignite.osgi.activators"); + + return probe; + } + + /** * @throws Exception If failed. */ @Test - public void testServiceExposed() throws Exception { + public void testServiceExposedAndCallbacksInvoked() throws Exception { assertNotNull(ignite); assertEquals("testGrid", ignite.name()); + + TestOsgiFlags flags = (TestOsgiFlags) bundleCtx.getService( + bundleCtx.getAllServiceReferences(TestOsgiFlags.class.getName(), null)[0]); + + assertNotNull(flags); + assertEquals(Boolean.TRUE, flags.getOnBeforeStartInvoked()); + assertEquals(Boolean.TRUE, flags.getOnAfterStartInvoked()); + + // The bundle is still not stopped, therefore these callbacks cannot be tested. + assertNull(flags.getOnBeforeStopInvoked()); + assertNull(flags.getOnAfterStopInvoked()); + + // No exceptions. + assertNull(flags.getOnAfterStartThrowable()); + assertNull(flags.getOnAfterStopThrowable()); } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/43d09943/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java ---------------------------------------------------------------------- diff --git a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java index a74ee87..c414092 100644 --- a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java +++ b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/BasicIgniteTestActivator.java @@ -17,14 +17,19 @@ package org.apache.ignite.osgi.activators; +import java.util.Hashtable; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.osgi.IgniteAbstractOsgiContextActivator; import org.apache.ignite.osgi.classloaders.OsgiClassLoadingStrategyType; +import org.jetbrains.annotations.Nullable; +import org.osgi.framework.BundleContext; /** * Basic Ignite Activator for testing. */ public class BasicIgniteTestActivator extends IgniteAbstractOsgiContextActivator { + /** Flags to report our state to a watcher. */ + private TestOsgiFlagsImpl flags = new TestOsgiFlagsImpl(); /** * @return Ignite config. @@ -43,4 +48,29 @@ public class BasicIgniteTestActivator extends IgniteAbstractOsgiContextActivator @Override public OsgiClassLoadingStrategyType classLoadingStrategy() { return OsgiClassLoadingStrategyType.BUNDLE_DELEGATING; } + + /** {@inheritDoc} */ + @Override protected void onBeforeStart(BundleContext ctx) { + flags.onBeforeStartInvoked = Boolean.TRUE; + + // Export the flags as an OSGi service. + ctx.registerService(TestOsgiFlags.class, flags, new Hashtable<String, Object>()); + } + + /** {@inheritDoc} */ + @Override protected void onAfterStart(BundleContext ctx, @Nullable Throwable t) { + flags.onAfterStartInvoked = Boolean.TRUE; + flags.onAfterStartThrowable = t; + } + + /** {@inheritDoc} */ + @Override protected void onBeforeStop(BundleContext ctx) { + flags.onBeforeStopInvoked = Boolean.TRUE; + } + + /** {@inheritDoc} */ + @Override protected void onAfterStop(BundleContext ctx, @Nullable Throwable t) { + flags.onAfterStopInvoked = Boolean.TRUE; + flags.onAfterStopThrowable = t; + } } http://git-wip-us.apache.org/repos/asf/ignite/blob/43d09943/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlags.java ---------------------------------------------------------------------- diff --git a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlags.java b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlags.java new file mode 100644 index 0000000..09a2d29 --- /dev/null +++ b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlags.java @@ -0,0 +1,53 @@ +/* + * 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.ignite.osgi.activators; + +/** + * Interface to export the flags in OSGi. + */ +public interface TestOsgiFlags { + /** + * @return The flag. + */ + Boolean getOnBeforeStartInvoked(); + + /** + * @return The flag. + */ + Boolean getOnAfterStartInvoked(); + + /** + * @return The flag. + */ + Throwable getOnAfterStartThrowable(); + + /** + * @return The flag. + */ + Boolean getOnBeforeStopInvoked(); + + /** + * @return The flag. + */ + Boolean getOnAfterStopInvoked(); + + /** + * @return The flag. + */ + Throwable getOnAfterStopThrowable(); +} http://git-wip-us.apache.org/repos/asf/ignite/blob/43d09943/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlagsImpl.java ---------------------------------------------------------------------- diff --git a/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlagsImpl.java b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlagsImpl.java new file mode 100644 index 0000000..ccec2df --- /dev/null +++ b/modules/osgi/src/test/java/org/apache/ignite/osgi/activators/TestOsgiFlagsImpl.java @@ -0,0 +1,83 @@ +/* + * 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.ignite.osgi.activators; + +/** + * Data transfer object representing flags we want to watch from the OSGi tests. + */ +public class TestOsgiFlagsImpl implements TestOsgiFlags { + /** onBeforeStartInvoked flag. */ + public Boolean onBeforeStartInvoked; + + /** onAfterStartInvoked flag. */ + public Boolean onAfterStartInvoked; + + /** onAfterStartThrowable flag. */ + public Throwable onAfterStartThrowable; + + /** onBeforeStartInvoked flag. */ + public Boolean onBeforeStopInvoked; + + /** onAfterStopInvoked flag. */ + public Boolean onAfterStopInvoked; + + /** onAfterStopThrowable flag. */ + public Throwable onAfterStopThrowable; + + /** + * @return The flag. + */ + @Override public Boolean getOnBeforeStartInvoked() { + return onBeforeStartInvoked; + } + + /** + * @return The flag. + */ + @Override public Boolean getOnAfterStartInvoked() { + return onAfterStartInvoked; + } + + /** + * @return The flag. + */ + @Override public Throwable getOnAfterStartThrowable() { + return onAfterStartThrowable; + } + + /** + * @return The flag. + */ + @Override public Boolean getOnBeforeStopInvoked() { + return onBeforeStopInvoked; + } + + /** + * @return The flag. + */ + @Override public Boolean getOnAfterStopInvoked() { + return onAfterStopInvoked; + } + + /** + * @return The flag. + */ + @Override public Throwable getOnAfterStopThrowable() { + return onAfterStopThrowable; + } +}