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)));
}
}
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!");
}
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);
}
}
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;
}
Aggregations