use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.
the class FactoryPreconditions method checkPreconditions.
public boolean checkPreconditions(final String unavailableName, final String unavailableServicePid) {
synchronized (this) {
final List<RequiredProvider> localRequiredProviders = this.requiredProviders;
final ResourceProviderTracker localTracker = this.tracker;
boolean canRegister = localTracker != null;
if (localRequiredProviders != null && localTracker != null) {
for (final RequiredProvider rp : localRequiredProviders) {
canRegister = false;
for (final ResourceProviderHandler h : localTracker.getResourceProviderStorage().getAllHandlers()) {
final ServiceReference ref = h.getInfo().getServiceReference();
final Object servicePid = ref.getProperty(Constants.SERVICE_PID);
if (unavailableServicePid != null && unavailableServicePid.equals(servicePid)) {
// ignore this service
continue;
}
if (unavailableName != null && unavailableName.equals(h.getInfo().getName())) {
// ignore this service
continue;
}
if (rp.name != null && rp.name.equals(h.getInfo().getName())) {
canRegister = true;
break;
} else if (rp.filter != null && rp.filter.match(ref)) {
canRegister = true;
break;
} else if (rp.pid != null && rp.pid.equals(servicePid)) {
canRegister = true;
break;
} else if (rp.pid != null && rp.pid.equals(ref.getProperty(LegacyResourceProviderWhiteboard.ORIGINAL_SERVICE_PID))) {
canRegister = true;
break;
}
}
if (!canRegister) {
break;
}
}
}
return canRegister;
}
}
use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.
the class FactoryPreconditionsTest method getResourceProviderHandlersWithNames.
private List<ResourceProviderHandler> getResourceProviderHandlersWithNames(String[] names) {
final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
for (final String n : names) {
final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
Mockito.when(info.getName()).thenReturn(n);
final ServiceReference ref = Mockito.mock(ServiceReference.class);
Mockito.when(handler.getInfo()).thenReturn(info);
Mockito.when(info.getServiceReference()).thenReturn(ref);
result.add(handler);
}
return result;
}
use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.
the class ResourceResolverControl method checkSourceAndDest.
private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
// check source
final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
if (srcNode == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
AuthenticatedResourceProvider srcProvider = null;
try {
srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (srcProvider == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
if (srcResource == null) {
throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
}
// check destination
final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
if (destNode == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
AuthenticatedResourceProvider destProvider = null;
try {
destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
} catch (LoginException e) {
// ignore
}
if (destProvider == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
final Resource destResource = destProvider.getResource(destAbsPath, null, null);
if (destResource == null) {
throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
}
// check for sub providers of src and dest
if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
return srcProvider;
}
return null;
}
use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.
the class BasicResolveContext method getParentProviderAndContext.
private Object[] getParentProviderAndContext() {
ResourceProvider<?> parentProvider = null;
ResolveContext<?> parentResolveContext = null;
if (this.parentPath != null) {
String path = this.parentPath;
while (path != null && parentProvider == null) {
final Node<ResourceProviderHandler> node = this.control.getResourceProviderStorage().getTree().getBestMatchingNode(path);
if (node != null) {
final ResourceProviderHandler handler = node.getValue();
try {
parentResolveContext = this.resolveContextManager.getOrCreateResolveContext(handler, this.control);
if (parentResolveContext != null) {
parentProvider = handler.getResourceProvider();
}
} catch (final LoginException se) {
// skip this, try next
}
if (parentProvider == null) {
parentResolveContext = null;
path = ResourceUtil.getParent(path);
}
} else {
path = null;
}
}
}
return parentProvider != null ? new Object[] { parentProvider, parentResolveContext } : null;
}
use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.
the class ResourceProviderEntryTest method testRemoveTheOnlyProvider.
@Test
public void testRemoveTheOnlyProvider() throws LoginException {
long counter = 1;
providers.clear();
for (String path : new String[] { "/foo", "/", "/foo/bar" }) {
final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
final ResourceProvider<?> p = Mockito.mock(ResourceProvider.class);
Mockito.when(p.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(path), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(resolver));
++counter;
ResourceProviderHandler h = createRPHandler(p, "rp" + counter, counter, path);
providers.add(h);
this.providersBasedResolver = null;
{
final Resource r = getResource(path);
assertEqualsResolver(resolver, r);
assertFalse(r instanceof SyntheticResource);
}
providers.remove(h);
this.providersBasedResolver = null;
{
final Resource r = getResource(path);
// If our provider is indeed gone, we should get one of the following conditions
if (r == null) {
//fine
} else if (!p.equals(r.getResourceResolver())) {
//fine
} else {
fail("Expecting inactive provider after removing it for " + path);
}
}
}
}
Aggregations