use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ResourceEliminateDuplicatesIteratorTest method testIterator.
@SuppressWarnings("unchecked")
@Test
public void testIterator() {
context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c");
ResourceResolver rr = context.resourceResolver();
List<ContextResource> list = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a/b"), "/conf/a/b", 0), new ContextResource(rr.getResource("/content/a"), "/conf/a", 0), new ContextResource(rr.getResource("/content/a"), null, 0), new ContextResource(rr.getResource("/content/a/b/c"), "/conf/a/b", 0));
Iterator<Resource> result = toResourceIterator(new ResourceEliminateDuplicatesIterator(list.iterator()));
assertThat(result, ResourceIteratorMatchers.paths("/content/a", "/content/a/b", "/content/a", "/content/a/b/c"));
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ResourcePathCollatingIteratorTest method testWithConfigRefAndServiceRanking.
@SuppressWarnings("unchecked")
@Test
public void testWithConfigRefAndServiceRanking() {
context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
ResourceResolver rr = context.resourceResolver();
List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 500));
List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 100));
Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
ContextResource item1 = result.next();
ContextResource item2 = result.next();
assertFalse(result.hasNext());
assertEquals("/content/a", item1.getResource().getPath());
assertEquals("/conf/z", item1.getConfigRef());
assertEquals("/content/a", item2.getResource().getPath());
assertEquals("/conf/a", item2.getConfigRef());
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class ResourcePathCollatingIteratorTest method testWithConfigRef.
@SuppressWarnings("unchecked")
@Test
public void testWithConfigRef() {
context.build().resource("/content/a").resource("/content/a/b").resource("/content/a/b/c").resource("/content/a/b/c/d");
ResourceResolver rr = context.resourceResolver();
List<ContextResource> list1 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/z", 0));
List<ContextResource> list2 = ImmutableList.of(new ContextResource(rr.getResource("/content/a"), "/conf/a", 0));
Iterator<ContextResource> result = new ResourcePathCollatingIterator(ImmutableList.of(list1.iterator(), list2.iterator()));
ContextResource item1 = result.next();
ContextResource item2 = result.next();
assertFalse(result.hasNext());
assertEquals("/content/a", item1.getResource().getPath());
assertEquals("/conf/a", item1.getConfigRef());
assertEquals("/content/a", item2.getResource().getPath());
assertEquals("/conf/z", item2.getConfigRef());
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class SyncTokenService method storeMySyncToken.
private boolean storeMySyncToken(String syncTokenId) {
logger.trace("storeMySyncToken: start");
if (slingId == null) {
logger.info("storeMySyncToken: not yet activated (slingId is null)");
return false;
}
ResourceResolver resourceResolver = null;
try {
resourceResolver = getResourceResolver();
final Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
ModifiableValueMap syncTokens = resource.adaptTo(ModifiableValueMap.class);
boolean updateToken = false;
if (!syncTokens.containsKey(slingId)) {
updateToken = true;
} else {
Object existingToken = syncTokens.get(slingId);
if (existingToken == null || !existingToken.equals(syncTokenId)) {
updateToken = true;
}
}
if (updateToken) {
syncTokens.put(slingId, syncTokenId);
resourceResolver.commit();
logger.info("storeMySyncToken: stored syncToken of slingId=" + slingId + " as=" + syncTokenId);
} else {
logger.info("storeMySyncToken: syncToken was left unchanged for slingId=" + slingId + " at=" + syncTokenId);
}
return true;
} catch (LoginException e) {
logger.error("storeMySyncToken: could not login for storing my syncToken: " + e, e);
return false;
} catch (PersistenceException e) {
logger.error("storeMySyncToken: got PersistenceException while storing my syncToken: " + e, e);
return false;
} finally {
logger.trace("storeMySyncToken: end");
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class VotingHandler method handleEvent.
/**
* handle repository changes and react to ongoing votings
*/
@Override
public void handleEvent(final Event event) {
if (!activated) {
return;
}
String resourcePath = (String) event.getProperty("path");
String ongoingVotingsPath = config.getOngoingVotingsPath();
if (resourcePath == null) {
// not of my business
return;
}
if (!resourcePath.startsWith(ongoingVotingsPath)) {
// not of my business
return;
}
ResourceResolver resourceResolver = null;
try {
resourceResolver = resolverFactory.getServiceResourceResolver(null);
} catch (LoginException e) {
logger.error("handleEvent: could not log in administratively: " + e, e);
return;
}
try {
if (logger.isDebugEnabled()) {
logger.debug("handleEvent: path = " + resourcePath + ", event = " + event);
}
analyzeVotings(resourceResolver);
} catch (PersistenceException e) {
logger.error("handleEvent: got a PersistenceException during votings analysis: " + e, e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
Aggregations