Search in sources :

Example 86 with ModifiableValueMap

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();
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 87 with ModifiableValueMap

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();
    }
}
Also used : Calendar(java.util.Calendar) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ScheduleInfo(org.apache.sling.event.jobs.ScheduleInfo)

Example 88 with ModifiableValueMap

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);
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 89 with ModifiableValueMap

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();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet)

Example 90 with ModifiableValueMap

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));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6