use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class SetupService method setupContent.
private void setupContent(final ResourceResolver resolver) throws PersistenceException {
final Resource root = resolver.getResource(SlingshotConstants.APP_ROOT_PATH);
if (root != null) {
// fix resource type of root folder
if (!root.isResourceType(InternalConstants.RESOURCETYPE_HOME)) {
final ModifiableValueMap mvm = root.adaptTo(ModifiableValueMap.class);
mvm.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, InternalConstants.RESOURCETYPE_HOME);
resolver.commit();
}
final Resource usersResource = root.getChild("users");
for (final String userName : USERS) {
Resource homeResource = resolver.getResource(usersResource, userName);
if (homeResource == null) {
final Map<String, Object> props = new HashMap<String, Object>();
props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, User.RESOURCETYPE);
homeResource = resolver.create(usersResource, userName, props);
resolver.commit();
}
for (final String def : FOLDERS) {
final int index = def.indexOf(':');
final String name;
final String rt;
if (index == -1) {
name = def;
rt = "sling:OrderedFolder";
} else {
name = def.substring(0, index);
rt = def.substring(index + 1);
}
final Resource rsrc = resolver.getResource(homeResource, name);
if (rsrc == null) {
final Map<String, Object> props = new HashMap<String, Object>();
props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, rt);
resolver.create(homeResource, name, props);
resolver.commit();
}
}
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class ScheduledJobHandler method updateSchedule.
public void updateSchedule(final String scheduleName, final Collection<ScheduleInfo> scheduleInfo) {
final ResourceResolver resolver = configuration.createResourceResolver();
try {
final String scheduleKey = ResourceHelper.filterName(scheduleName);
final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true));
sb.append(scheduleKey);
final String path = sb.toString();
final Resource rsrc = resolver.getResource(path);
// This is an update, if we can't find the resource we ignore it
if (rsrc != null) {
final Calendar now = Calendar.getInstance();
// update holder first
synchronized (scheduledJobs) {
final Holder h = scheduledJobs.get(scheduleKey);
if (h != null) {
h.created = now;
}
}
final ModifiableValueMap mvm = rsrc.adaptTo(ModifiableValueMap.class);
mvm.put(Job.PROPERTY_JOB_CREATED, now);
final String[] infoArray = new String[scheduleInfo.size()];
int index = 0;
for (final ScheduleInfo si : scheduleInfo) {
infoArray[index] = ((ScheduleInfoImpl) si).getSerializedString();
index++;
}
mvm.put(ResourceHelper.PROPERTY_SCHEDULE_INFO, infoArray);
try {
resolver.commit();
} catch (final PersistenceException pe) {
logger.warn("Unable to update scheduled job " + scheduleName, pe);
}
}
} finally {
resolver.close();
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class RedirectResourceTest method testRedirectResource.
@Test
public void testRedirectResource() {
final String path = "/redir/path";
final String target = "/redir/target";
final int status = 999;
final RedirectResource res = new RedirectResource(null, path, target, status);
assertEquals(path, res.getPath());
assertEquals(RedirectResource.RT_SLING_REDIRECT, res.getResourceType());
final Map<?, ?> map = res.adaptTo(Map.class);
assertNotNull("Expected Map adapter", map);
assertEquals(target, map.get(RedirectResource.PROP_SLING_TARGET));
assertEquals(status, ((Integer) map.get(RedirectResource.PROP_SLING_STATUS)).intValue());
final ValueMap valueMap = res.adaptTo(ValueMap.class);
assertNotNull("Expected ValueMap adapter", valueMap);
assertEquals(target, valueMap.get(RedirectResource.PROP_SLING_TARGET));
assertEquals(status, ((Integer) valueMap.get(RedirectResource.PROP_SLING_STATUS)).intValue());
assertEquals(status, valueMap.get(RedirectResource.PROP_SLING_STATUS, Integer.class).intValue());
final ModifiableValueMap persistableValueMap = res.adaptTo(ModifiableValueMap.class);
assertNull("Unexpected ModifiableValueMap adapter", persistableValueMap);
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class IdMapService method init.
private synchronized boolean init() throws LoginException, PersistenceException {
if (initialized) {
return true;
}
slingId = settingsService.getSlingId();
ResourceResolver resourceResolver = null;
try {
resourceResolver = getResourceResolver();
DiscoveryLiteDescriptor descriptor = DiscoveryLiteDescriptor.getDescriptorFrom(resourceResolver);
long me = descriptor.getMyId();
final Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getIdMapPath());
ModifiableValueMap idmap = resource.adaptTo(ModifiableValueMap.class);
// check to see if either my slingId is already mapped to another clusterNodeId
// or when my clusterNodeId is already mapped to another slingId
// in both cases: clean that up
boolean foundMe = false;
for (String aKey : new HashSet<>(idmap.keySet())) {
Object value = idmap.get(aKey);
if (value instanceof Number) {
Number n = (Number) value;
if (n.longValue() == me) {
// let's check if the key is my slingId
if (aKey.equals(slingId)) {
// perfect
foundMe = true;
} else {
// cleanup necessary
logger.info("init: my clusterNodeId is already mapped to by another slingId, deleting entry: key=" + aKey + " mapped to " + value);
idmap.remove(aKey);
}
} else if (aKey.equals(slingId)) {
// cleanup necessary
logger.info("init: my slingId is already mapped to by another clusterNodeId, deleting entry: key=" + aKey + " mapped to " + value);
idmap.remove(aKey);
} else {
// that's just some other slingId-clusterNodeId mapping
// leave it unchanged
}
}
}
if (!foundMe) {
logger.info("init: added the following mapping: slingId=" + slingId + " to discovery-lite id=" + me);
idmap.put(slingId, me);
} else {
logger.info("init: mapping already existed, left unchanged: slingId=" + slingId + " to discovery-lite id=" + me);
}
resourceResolver.commit();
this.me = me;
initialized = true;
notifyAll();
return true;
} catch (Exception e) {
logger.info("init: init failed: " + e);
return false;
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class ResourceValidationModelProviderImplTest method testGetValidationModelsWithInvalidValidatorArguments1.
@Test(expected = IllegalStateException.class)
public void testGetValidationModelsWithInvalidValidatorArguments1() throws Exception {
// create a model with neither children nor properties
ValidationModel model1 = modelBuilder.build("sling/validation/test", libsValidatorsRoot.getPath() + "/testValidationModel1");
// create valid model first
Resource modelResource = createValidationModelResource(rr, libsValidatorsRoot.getPath(), "testValidationModel1", model1);
// and make parametrization of validator invalid afterwards
Resource validatorResource = modelResource.getChild("properties/field1/validators/validatorId");
ModifiableValueMap validatorArguments = validatorResource.adaptTo(ModifiableValueMap.class);
// value without "="
validatorArguments.put("validatorArguments", "key1");
Collection<ValidationModel> models = modelProvider.getValidationModels("sling/validation/test");
Assert.assertThat(models, Matchers.contains(model1));
}
Aggregations