Search in sources :

Example 1 with GenericDescriptors

use of org.apache.jackrabbit.oak.util.GenericDescriptors in project sling by apache.

the class DescriptorHelper method setDescriptor.

public static void setDescriptor(ResourceResolverFactory factory, String key, String value) throws Exception {
    ResourceResolver resourceResolver = factory.getServiceResourceResolver(null);
    try {
        Session session = resourceResolver.adaptTo(Session.class);
        if (session == null) {
            return;
        }
        Repository repo = session.getRepository();
        //<hack>
        //            Method setDescriptorMethod = repo.getClass().
        //                    getDeclaredMethod("setDescriptor", String.class, String.class);
        //            if (setDescriptorMethod!=null) {
        //                setDescriptorMethod.setAccessible(true);
        //                setDescriptorMethod.invoke(repo, key, value);
        //            } else {
        //                fail("could not get 'setDescriptor' method");
        //            }
        Method getDescriptorsMethod = repo.getClass().getDeclaredMethod("getDescriptors");
        if (getDescriptorsMethod == null) {
            fail("could not get 'getDescriptors' method");
        } else {
            getDescriptorsMethod.setAccessible(true);
            GenericDescriptors descriptors = (GenericDescriptors) getDescriptorsMethod.invoke(repo);
            SimpleValueFactory valueFactory = new SimpleValueFactory();
            descriptors.put(key, valueFactory.createValue(value), true, true);
        }
        //</hack>
        //<verify-hack>
        assertEquals(value, repo.getDescriptor(key));
    //</verify-hack>
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : GenericDescriptors(org.apache.jackrabbit.oak.util.GenericDescriptors) Repository(javax.jcr.Repository) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Method(java.lang.reflect.Method) SimpleValueFactory(org.apache.jackrabbit.commons.SimpleValueFactory) Session(javax.jcr.Session)

Aggregations

Method (java.lang.reflect.Method)1 Repository (javax.jcr.Repository)1 Session (javax.jcr.Session)1 SimpleValueFactory (org.apache.jackrabbit.commons.SimpleValueFactory)1 GenericDescriptors (org.apache.jackrabbit.oak.util.GenericDescriptors)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1