Search in sources :

Example 1 with Adaptable

use of org.apache.sling.api.adapter.Adaptable in project sling by apache.

the class ModelAdapterFactory method adapt.

/**
     * Preferably adapt via the {@link ModelFactory} in case the target type is a Sling Model itself, otherwise use regular {@link Adaptable#adaptTo(Class)}.
     * @param value the object from which to adapt
     * @param type the target type
     * @param isWithinCollection
     * @return a Result either encapsulating an exception or the adapted value
     */
@CheckForNull
private Result<Object> adapt(final Object value, final Class<?> type, boolean isWithinCollection) {
    Object adaptedValue = null;
    final String messageSuffix = isWithinCollection ? " in collection" : "";
    if (isModelClass(type) && canCreateFromAdaptable(value, type)) {
        Result<?> result = internalCreateModel(value, type);
        if (result.wasSuccessful()) {
            adaptedValue = result.getValue();
        } else {
            return new Result<Object>(new ModelClassException(String.format("Could not create model from %s: %s%s", value.getClass(), result.getThrowable().getMessage(), messageSuffix), result.getThrowable()));
        }
    } else if (value instanceof Adaptable) {
        adaptedValue = ((Adaptable) value).adaptTo(type);
        if (adaptedValue == null) {
            return new Result<Object>(new ModelClassException(String.format("Could not adapt from %s to %s%s", value.getClass(), type, messageSuffix)));
        }
    }
    if (adaptedValue != null) {
        return new Result<Object>(adaptedValue);
    } else {
        return new Result<Object>(new ModelClassException(String.format("Could not adapt from %s to %s%s, because this class is not adaptable!", value.getClass(), type, messageSuffix)));
    }
}
Also used : ModelClassException(org.apache.sling.models.factory.ModelClassException) Adaptable(org.apache.sling.api.adapter.Adaptable) CheckForNull(javax.annotation.CheckForNull)

Example 2 with Adaptable

use of org.apache.sling.api.adapter.Adaptable in project sling by apache.

the class TestSlingFunctions method testAdaptTo.

@Test
public void testAdaptTo() throws ClassNotFoundException {
    log.info("testAdaptTo");
    Adaptable adaptable = SlingFunctions.getResource(resolver, TEST_PATH);
    Object adapted = SlingFunctions.adaptTo(adaptable, ValueMap.class.getCanonicalName());
    assertNotNull(adapted);
    assertTrue(adapted instanceof ValueMap);
    log.info("Tests successful!");
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Adaptable(org.apache.sling.api.adapter.Adaptable) Test(org.junit.Test)

Example 3 with Adaptable

use of org.apache.sling.api.adapter.Adaptable in project sling by apache.

the class LegacyResourceProviderWhiteboard method bindResourceProvider.

@Reference(service = ResourceProvider.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void bindResourceProvider(final ServiceReference<ResourceProvider> ref) {
    final BundleContext bundleContext = ref.getBundle().getBundleContext();
    final ResourceProvider provider = bundleContext.getService(ref);
    if (provider != null) {
        final String[] propertyNames = ref.getPropertyKeys();
        final boolean ownsRoot = toBoolean(ref.getProperty(OWNS_ROOTS), false);
        final List<ServiceRegistration> newServices = new ArrayList<>();
        for (final String path : PropertiesUtil.toStringArray(ref.getProperty(ROOTS), new String[0])) {
            if (path != null && !path.isEmpty()) {
                final Dictionary<String, Object> newProps = new Hashtable<>();
                newProps.put(PROPERTY_AUTHENTICATE, AuthType.no.toString());
                newProps.put(PROPERTY_MODIFIABLE, provider instanceof ModifyingResourceProvider);
                newProps.put(PROPERTY_ADAPTABLE, provider instanceof Adaptable);
                newProps.put(PROPERTY_ATTRIBUTABLE, provider instanceof AttributableResourceProvider);
                newProps.put(PROPERTY_REFRESHABLE, provider instanceof RefreshableResourceProvider);
                newProps.put(PROPERTY_NAME, provider.getClass().getName());
                newProps.put(PROPERTY_ROOT, normalizePath(path));
                if (ArrayUtils.contains(propertyNames, SERVICE_PID)) {
                    newProps.put(ORIGINAL_SERVICE_PID, ref.getProperty(SERVICE_PID));
                }
                if (ArrayUtils.contains(propertyNames, USE_RESOURCE_ACCESS_SECURITY)) {
                    newProps.put(PROPERTY_USE_RESOURCE_ACCESS_SECURITY, ref.getProperty(USE_RESOURCE_ACCESS_SECURITY));
                }
                if (ArrayUtils.contains(propertyNames, SERVICE_RANKING)) {
                    newProps.put(SERVICE_RANKING, ref.getProperty(SERVICE_RANKING));
                }
                String[] languages = PropertiesUtil.toStringArray(ref.getProperty(LANGUAGES), new String[0]);
                ServiceRegistration reg = bundleContext.registerService(org.apache.sling.spi.resource.provider.ResourceProvider.class.getName(), new LegacyResourceProviderAdapter(provider, languages, ownsRoot), newProps);
                newServices.add(reg);
            }
        }
        registrations.put(provider, newServices);
    }
}
Also used : RefreshableResourceProvider(org.apache.sling.api.resource.RefreshableResourceProvider) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) ModifyingResourceProvider(org.apache.sling.api.resource.ModifyingResourceProvider) RefreshableResourceProvider(org.apache.sling.api.resource.RefreshableResourceProvider) AttributableResourceProvider(org.apache.sling.api.resource.AttributableResourceProvider) ModifyingResourceProvider(org.apache.sling.api.resource.ModifyingResourceProvider) ResourceProvider(org.apache.sling.api.resource.ResourceProvider) Adaptable(org.apache.sling.api.adapter.Adaptable) AttributableResourceProvider(org.apache.sling.api.resource.AttributableResourceProvider) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference) Reference(org.osgi.service.component.annotations.Reference)

Example 4 with Adaptable

use of org.apache.sling.api.adapter.Adaptable in project sling by apache.

the class SlingRuntimeObjectModel method getProperty.

protected Object getProperty(Object target, Object propertyObj) {
    String property = toString(propertyObj);
    if (StringUtils.isEmpty(property)) {
        throw new IllegalArgumentException("Invalid property name");
    }
    if (target == null) {
        return null;
    }
    Object result = null;
    if (target instanceof Map) {
        result = getMapProperty((Map) target, property);
    }
    if (result == null && target instanceof Record) {
        result = ((Record) target).getProperty(property);
    }
    if (result == null) {
        result = getObjectProperty(target, property);
    }
    if (result == null && target instanceof Adaptable) {
        ValueMap valueMap = ((Adaptable) target).adaptTo(ValueMap.class);
        if (valueMap != null) {
            result = valueMap.get(property);
        }
    }
    return result;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Record(org.apache.sling.scripting.sightly.Record) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) Adaptable(org.apache.sling.api.adapter.Adaptable)

Aggregations

Adaptable (org.apache.sling.api.adapter.Adaptable)4 ValueMap (org.apache.sling.api.resource.ValueMap)2 ArrayList (java.util.ArrayList)1 Hashtable (java.util.Hashtable)1 Map (java.util.Map)1 CheckForNull (javax.annotation.CheckForNull)1 AttributableResourceProvider (org.apache.sling.api.resource.AttributableResourceProvider)1 ModifyingResourceProvider (org.apache.sling.api.resource.ModifyingResourceProvider)1 RefreshableResourceProvider (org.apache.sling.api.resource.RefreshableResourceProvider)1 ResourceProvider (org.apache.sling.api.resource.ResourceProvider)1 ModelClassException (org.apache.sling.models.factory.ModelClassException)1 Record (org.apache.sling.scripting.sightly.Record)1 Test (org.junit.Test)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 Reference (org.osgi.service.component.annotations.Reference)1