Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 350961
Collapse All | Expand All

(-)src/org/eclipse/osgi/tests/resource/AllTests.java (+1 lines)
Lines 17-22 Link Here
17
	public static Test suite() {
17
	public static Test suite() {
18
		TestSuite suite = new TestSuite(AllTests.class.getName());
18
		TestSuite suite = new TestSuite(AllTests.class.getName());
19
		suite.addTest(BasicTest.suite());
19
		suite.addTest(BasicTest.suite());
20
		suite.addTest(ResolverHookTests.suite());
20
		return suite;
21
		return suite;
21
	}
22
	}
22
}
23
}
(-)src/org/eclipse/osgi/tests/resource/ResolverHookTests.java (+87 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 * 
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.osgi.tests.resource;
12
13
import java.util.*;
14
import junit.framework.Test;
15
import junit.framework.TestSuite;
16
import org.osgi.framework.Bundle;
17
import org.osgi.framework.ServiceRegistration;
18
import org.osgi.framework.hooks.resolver.ResolverHook;
19
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
20
import org.osgi.framework.wiring.*;
21
22
public class ResolverHookTests extends AbstractResourceTest {
23
24
	public static Test suite() {
25
		return new TestSuite(ResolverHookTests.class);
26
	}
27
28
	public ResolverHookTests(String name) {
29
		super(name);
30
	}
31
32
	public void testSingletonIdentity() throws Exception {
33
		final RuntimeException error[] = {null};
34
		final boolean called[] = {false};
35
		ResolverHookFactory resolverHookFactory = new ResolverHookFactory() {
36
			public ResolverHook begin(Collection triggers) {
37
				return new ResolverHook() {
38
39
					public void filterSingletonCollisions(BundleCapability singleton, Collection collisionCandidates) {
40
						if (error[0] != null)
41
							return;
42
						called[0] = true;
43
						try {
44
							assertEquals("Wrong namespace", ResourceConstants.IDENTITY_NAMESPACE, singleton.getNamespace());
45
							assertEquals("Wrong singleton directive", "true", singleton.getDirectives().get(ResourceConstants.IDENTITY_SINGLETON_DIRECTIVE));
46
							String symbolicName = (String) singleton.getAttributes().get(ResourceConstants.IDENTITY_NAMESPACE);
47
							for (Iterator iCandidates = collisionCandidates.iterator(); iCandidates.hasNext();) {
48
								BundleCapability candidate = (BundleCapability) iCandidates.next();
49
								assertEquals("Wrong namespace", ResourceConstants.IDENTITY_NAMESPACE, candidate.getNamespace());
50
								assertEquals("Wrong singleton directive", "true", candidate.getDirectives().get(ResourceConstants.IDENTITY_SINGLETON_DIRECTIVE));
51
								assertEquals("Wrong symbolic name", symbolicName, (String) candidate.getAttributes().get(ResourceConstants.IDENTITY_NAMESPACE));
52
							}
53
						} catch (RuntimeException e) {
54
							error[0] = e;
55
						}
56
					}
57
58
					public void filterResolvable(Collection candidates) {
59
						// nothing
60
					}
61
62
					public void filterMatches(BundleRequirement requirement, Collection candidates) {
63
						// nothing
64
					}
65
66
					public void end() {
67
						// nothing
68
					}
69
				};
70
			}
71
		};
72
73
		ServiceRegistration hookReg = getContext().registerService(ResolverHookFactory.class, resolverHookFactory, null);
74
75
		try {
76
			Bundle tb1v1 = installer.installBundle("singleton.tb1v1");
77
			Bundle tb1v2 = installer.installBundle("singleton.tb1v2");
78
			assertFalse(((FrameworkWiring) getContext().getBundle(0).adapt(FrameworkWiring.class)).resolveBundles(Arrays.asList(new Bundle[] {tb1v1, tb1v2})));
79
			assertTrue("ResolverHook was not called", called[0]);
80
			if (error[0] != null)
81
				throw error[0];
82
		} finally {
83
			hookReg.unregister();
84
		}
85
	}
86
87
}
(-)test_files/wiringTests/bundles/singleton.tb1v1/META-INF/MANIFEST.MF (+4 lines)
Added Link Here
1
Manifest-Version: 1.0
2
Bundle-ManifestVersion: 2
3
Bundle-SymbolicName: singleton.tb1; singleton:=true
4
Bundle-Version: 1.0.0
(-)test_files/wiringTests/bundles/singleton.tb1v2/META-INF/MANIFEST.MF (+4 lines)
Added Link Here
1
Manifest-Version: 1.0
2
Bundle-ManifestVersion: 2
3
Bundle-SymbolicName: singleton.tb1; singleton:=true
4
Bundle-Version: 2.0.0
(-)resolver/src/org/eclipse/osgi/internal/module/ResolverImpl.java (-4 / +8 lines)
Lines 27-34 Link Here
27
import org.osgi.framework.Filter;
27
import org.osgi.framework.Filter;
28
import org.osgi.framework.InvalidSyntaxException;
28
import org.osgi.framework.InvalidSyntaxException;
29
import org.osgi.framework.hooks.resolver.ResolverHook;
29
import org.osgi.framework.hooks.resolver.ResolverHook;
30
import org.osgi.framework.wiring.BundleCapability;
30
import org.osgi.framework.wiring.*;
31
import org.osgi.framework.wiring.BundleRevision;
32
31
33
public class ResolverImpl implements Resolver {
32
public class ResolverImpl implements Resolver {
34
	// Debug fields
33
	// Debug fields
Lines 661-675 Link Here
661
				if (collision == singleton || !collision.getBundleDescription().isSingleton() || !collision.isResolvable())
660
				if (collision == singleton || !collision.getBundleDescription().isSingleton() || !collision.isResolvable())
662
					continue; // Ignore the bundle we are checking and non-singletons and non-resolvable
661
					continue; // Ignore the bundle we are checking and non-singletons and non-resolvable
663
				collisionCandidates.add(collision);
662
				collisionCandidates.add(collision);
664
				capabilities.add(collision.getCapability());
663
				capabilities.add(getIdentity(collision));
665
			}
664
			}
666
			if (hook != null)
665
			if (hook != null)
667
				hook.filterSingletonCollisions(singleton.getCapability(), asCapabilities(new ArrayMap<BundleCapability, ResolverBundle>(capabilities, collisionCandidates)));
666
				hook.filterSingletonCollisions(getIdentity(singleton), asCapabilities(new ArrayMap<BundleCapability, ResolverBundle>(capabilities, collisionCandidates)));
668
			result.put(singleton, collisionCandidates);
667
			result.put(singleton, collisionCandidates);
669
		}
668
		}
670
		return result;
669
		return result;
671
	}
670
	}
672
671
672
	private BundleCapability getIdentity(ResolverBundle bundle) {
673
		List<BundleCapability> identities = bundle.getBundleDescription().getDeclaredCapabilities(ResourceConstants.IDENTITY_NAMESPACE);
674
		return identities.size() == 1 ? identities.get(0) : bundle.getCapability();
675
	}
676
673
	private void resolveBundles0(ResolverBundle[] bundles, Dictionary<Object, Object>[] platformProperties) {
677
	private void resolveBundles0(ResolverBundle[] bundles, Dictionary<Object, Object>[] platformProperties) {
674
		if (developmentMode)
678
		if (developmentMode)
675
			// need to sort bundles to keep consistent order for fragment attachment (bug 174930)
679
			// need to sort bundles to keep consistent order for fragment attachment (bug 174930)

Return to bug 350961