Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,13 @@ public ResolverImpl(Logger logger, Executor executor)

public Map<Resource, List<Wire>> resolve(ResolveContext rc) throws ResolutionException
{
if (m_executor != null)
// An executor provided by the resolve context takes precedence
Optional<Executor> contextExecutor = rc.getExecutor();
if (contextExecutor.isPresent())
{
return resolve(rc, contextExecutor.get());
}
else if (m_executor != null)
{
return resolve(rc, m_executor);
}
Expand Down Expand Up @@ -667,7 +673,7 @@ public Map<Resource,List<Wire>> resolveDynamic(ResolveContext context,
"Matching candidate does not provide a package name.");
}
}
ResolveSession session = ResolveSession.createSession(context, new DumbExecutor(), host, dynamicRequirement, matches);
ResolveSession session = ResolveSession.createSession(context, context.getExecutor().orElseGet(DumbExecutor::new), host, dynamicRequirement, matches);
return doResolve(session);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Executor;

import org.osgi.annotation.versioning.ConsumerType;
import org.osgi.framework.namespace.BundleNamespace;
Expand Down Expand Up @@ -253,6 +255,35 @@ public void onCancel(Runnable callback) {
// do nothing by default
}

/**
* Returns an optional {@link Executor} that the resolver can use to run
* work in parallel during the resolve operation.
* <p>
* By default a resolve context provides no executor and the resolver
* decides how to perform the work. A resolve context that wants to control
* the threads used by the resolver (for example to limit the parallelism,
* or to run the work on the calling thread) must override this method and
* return the same executor for the duration of a resolve operation. The
* resolver is not required to call this method or to use the executor. A
* resolver that uses it must prefer it to any other executor it has for
* this resolve operation.
* <p>
* The resolver must not assume that tasks submitted to the executor run
* concurrently, or that they run on a different thread than the caller.
* The resolver must not shut down the executor and must not depend on any
* lifecycle of it. A resolver that blocks the calling thread until
* submitted tasks complete must not block threads of the executor itself,
* as the executor might be bounded.
*
* @return An {@link Optional} containing the {@link Executor} to use
* during the resolve operation, or an empty Optional if no
* executor is available. Must not be {@code null}.
* @since 1.2
*/
public Optional<Executor> getExecutor() {
return Optional.empty();
}

/**
* Returns the subset of {@link Wiring#getRequiredResourceWires(String)
* required} wires that provide wires to {@link Capability capabilities}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

/**
* Resolver Service Package Version 1.1.
* Resolver Service Package Version 1.2.
* <p>
* Bundles wishing to use this package must list the package in the
* Import-Package header of the bundle's manifest. This package has two types of
Expand All @@ -24,16 +24,16 @@
* <p>
* Example import for consumers using the API in this package:
* <p>
* {@code Import-Package: org.osgi.service.resolver; version="[1.1,2.0)"}
* {@code Import-Package: org.osgi.service.resolver; version="[1.2,2.0)"}
* <p>
* Example import for providers implementing the API in this package:
* <p>
* {@code Import-Package: org.osgi.service.resolver; version="[1.1,1.2)"}
* {@code Import-Package: org.osgi.service.resolver; version="[1.2,1.3)"}
*
* @author $Id: 260072a99b71d2c4e6cfd174fd6acc1d20768e25 $
*/

@Version("1.1.1")
@Version("1.2.0")
package org.osgi.service.resolver;

import org.osgi.annotation.versioning.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.felix.resolver.Logger;
import org.apache.felix.resolver.ResolverImpl;
Expand All @@ -43,6 +46,7 @@
import org.apache.felix.resolver.test.util.PackageRequirement;
import org.apache.felix.resolver.test.util.ResolveContextImpl;
import org.apache.felix.resolver.test.util.ResourceImpl;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.framework.Constants;
Expand All @@ -63,6 +67,37 @@

public class ResolverTest
{
@Test
public void testResolveContextExecutor() throws Exception
{
AtomicInteger tasks = new AtomicInteger();
Executor executor = new Executor()
{
public void execute(Runnable command)
{
tasks.incrementAndGet();
command.run();
}
};

Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario1(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource> emptyList())
{
@Override
public Optional<Executor> getExecutor()
{
return Optional.of(executor);
}
};

resolver.resolve(rci);

Assert.assertTrue("The executor of the resolve context was not used", tasks.get() > 0);
}

@Test
public void testScenario1() throws Exception
{
Expand Down
Loading