| author | Dmitry Sklyut | 2010-07-21 11:56:52 (EDT) |
|---|---|---|
| committer | Glyn Normington | 2010-07-21 11:56:52 (EDT) |
| commit | 40de9793325529b222e4091510796481a8bed77f (patch) (side-by-side diff) | |
| tree | 30f997a6da4d04f3da44412ded5e6f8ac8920034 | |
| parent | 1a678d1f06564dd36583d2d4f4bba2377c098394 (diff) | |
| download | org.eclipse.virgo.snaps-40de9793325529b222e4091510796481a8bed77f.zip org.eclipse.virgo.snaps-40de9793325529b222e4091510796481a8bed77f.tar.gz org.eclipse.virgo.snaps-40de9793325529b222e4091510796481a8bed77f.tar.bz2 | |
bug 320505: prevent snap binding to wrong bundle
3 files changed, 103 insertions, 3 deletions
diff --git a/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/HostSelector.java b/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/HostSelector.java index d7f8f58..caf96c8 100644 --- a/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/HostSelector.java +++ b/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/HostSelector.java @@ -28,6 +28,10 @@ final class HostSelector { this.hostDefinition = hostDefinition; this.moduleScope = moduleScope; } + + SnapHostDefinition getHostDefinition() { + return this.hostDefinition; + } ServiceReference selectHost(ServiceReference[] candidates) { ServiceReference bestSoFar = null; diff --git a/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitor.java b/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitor.java index dc5e911..f9cfbf0 100644 --- a/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitor.java +++ b/org.eclipse.virgo.snaps.core/src/main/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitor.java @@ -146,11 +146,18 @@ final class SnapFactoryMonitor implements ServiceTrackerCustomizer { ServletContext servletContext = (ServletContext) this.context.getService(hostReference); if (servletContext != null) { synchronized (this.hostStateMonitor) { - this.hostReference = hostReference; + + //Bug 320505 + ServiceReference matchedHost = this.hostSelector.selectHost(new ServiceReference[] { hostReference }); + if (matchedHost == null) { + logger.info("Host {} did not match {} ", hostReference.getBundle().getSymbolicName(), + this.hostSelector.getHostDefinition().toString()); + return; + } } + Bundle hostBundle = hostReference.getBundle(); - SnapLifecycleState newState = SnapLifecycleState.INIT_FAILED; Snap snap = this.factory.createSnap(new Host(hostBundle, servletContext, new RequestRouter(this.snapRegistry, servletContext))); @@ -172,7 +179,6 @@ final class SnapFactoryMonitor implements ServiceTrackerCustomizer { } } } - } } diff --git a/org.eclipse.virgo.snaps.core/src/test/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitorTests.java b/org.eclipse.virgo.snaps.core/src/test/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitorTests.java index 64b8d0e..c35a373 100644 --- a/org.eclipse.virgo.snaps.core/src/test/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitorTests.java +++ b/org.eclipse.virgo.snaps.core/src/test/java/org/eclipse/virgo/snaps/core/internal/SnapFactoryMonitorTests.java @@ -22,6 +22,10 @@ import static org.junit.Assert.fail; import java.io.File; import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import javax.servlet.ServletContext; @@ -43,6 +47,9 @@ import org.eclipse.virgo.medic.test.eventlog.MockEventLogger; public class SnapFactoryMonitorTests extends AbstractEquinoxLaunchingTests { private SnapFactoryMonitor binder; + + private static final CountDownLatch latch = new CountDownLatch(1); + private static final Executor executor = Executors.newFixedThreadPool(2); @Before public void setupBinder() { @@ -189,4 +196,87 @@ public class SnapFactoryMonitorTests extends AbstractEquinoxLaunchingTests { assertTrue("File: " + path + " does not exist", f.exists()); return getBundleContext().installBundle("file:" + f.getAbsolutePath()); } + + + /** + * Test binding of snap to a host if snap started prior to the host and if there are many hosts out there vs. just the target host (singular) + * @throws Exception + */ + @Test + public void testManyDifferentHosts() throws Exception { + final Bundle host = installBundle("travel_1"); + final Bundle host2 = installBundle("travel_2"); + final Bundle host3 = installBundle("travel_3"); + final Bundle targetHost = installBundle("clinic_1"); + + // these will be picked up by "already published" logic + host2.start(); + host3.start(); + + publishContextForBundle(host2); + publishContextForBundle(host3); + + Snap slice = createMock(Snap.class); + slice.init(); + expect(slice.getSnapProperties()).andReturn(new Properties()); + expect(slice.getContextPath()).andReturn("/hotels").anyTimes(); + + final SnapFactory factory = createMock(SnapFactory.class); + expect(factory.createSnap(isA(Host.class))).andReturn(slice); + replay(factory, slice); + + // public snap factory bound to "clinic" host + executor.execute(new Runnable() { + public void run() { + publishFactory(factory, "clinic", "[1.0, 3.0)"); + } + }); + + + // need to wait for sec or so to let all services propagate. + try { + latch.await(1, TimeUnit.SECONDS); + } catch (Exception ex) { + // ignore + } + + // spin a thread so it does not block + // start host 1 + executor.execute(new Runnable() { + + public void run() { + try { + host.start(); + } catch (BundleException e) { + throw new RuntimeException(e); + } + publishContextForBundle(host); + } + }); + + + // need to wait for few sec to let all services propagate. + try { + latch.await(1, TimeUnit.SECONDS); + } catch (Exception ex) { + // ignore + } + + // start host 2 (the actual one we are interested in) + executor.execute(new Runnable() { + + public void run() { + try { + targetHost.start(); + } catch (BundleException e) { + throw new RuntimeException(e); + } + publishContextForBundle(targetHost); + } + }); + + + assertSnapPublished("/hotels", targetHost); + verify(factory, slice); + } } |

