Search in sources :

Example 1 with InvalidAdaptableException

use of org.apache.sling.models.factory.InvalidAdaptableException in project sling by apache.

the class ModelAdapterFactory method internalCreateModel.

@SuppressWarnings("unchecked")
private <ModelType> Result<ModelType> internalCreateModel(final Object adaptable, final Class<ModelType> requestedType) {
    Result<ModelType> result;
    ThreadInvocationCounter threadInvocationCounter = invocationCountThreadLocal.get();
    if (threadInvocationCounter.isMaximumReached()) {
        String msg = String.format("Adapting %s to %s failed, too much recursive invocations (>=%s).", adaptable, requestedType, threadInvocationCounter.maxRecursionDepth);
        return new Result<ModelType>(new ModelClassException(msg));
    }
    threadInvocationCounter.increase();
    try {
        // check if a different implementation class was registered for this adapter type
        ModelClass<ModelType> modelClass = getImplementationTypeForAdapterType(requestedType, adaptable);
        if (!modelClass.hasModelAnnotation()) {
            String msg = String.format("Provided Adapter class does not have a Model annotation: %s", modelClass.getType());
            return new Result<ModelType>(new ModelClassException(msg));
        }
        boolean isAdaptable = false;
        Model modelAnnotation = modelClass.getModelAnnotation();
        if (modelAnnotation.cache()) {
            Map<Class, Object> adaptableCache = adapterCache.get(adaptable);
            if (adaptableCache != null) {
                ModelType cachedObject = (ModelType) adaptableCache.get(requestedType);
                if (cachedObject != null) {
                    return new Result<ModelType>(cachedObject);
                }
            }
        }
        Class<?>[] declaredAdaptable = modelAnnotation.adaptables();
        for (Class<?> clazz : declaredAdaptable) {
            if (clazz.isInstance(adaptable)) {
                isAdaptable = true;
            }
        }
        if (!isAdaptable) {
            String msg = String.format("Adaptables (%s) are not acceptable for the model class: %s", StringUtils.join(declaredAdaptable), modelClass.getType());
            return new Result<ModelType>(new InvalidAdaptableException(msg));
        } else {
            RuntimeException t = validateModel(adaptable, modelClass.getType(), modelAnnotation);
            if (t != null) {
                return new Result<ModelType>(t);
            }
            if (modelClass.getType().isInterface()) {
                Result<InvocationHandler> handlerResult = createInvocationHandler(adaptable, modelClass);
                if (handlerResult.wasSuccessful()) {
                    ModelType model = (ModelType) Proxy.newProxyInstance(modelClass.getType().getClassLoader(), new Class<?>[] { modelClass.getType() }, handlerResult.getValue());
                    if (modelAnnotation.cache()) {
                        Map<Class, Object> adaptableCache = adapterCache.get(adaptable);
                        if (adaptableCache == null) {
                            adaptableCache = new ConcurrentHashMap<Class, Object>(INNER_CACHE_INITIAL_CAPACITY);
                            adapterCache.put(adaptable, adaptableCache);
                        }
                        adaptableCache.put(requestedType, model);
                    }
                    result = new Result<ModelType>(model);
                } else {
                    return new Result<ModelType>(handlerResult.getThrowable());
                }
            } else {
                try {
                    result = createObject(adaptable, modelClass);
                    if (result.wasSuccessful() && modelAnnotation.cache()) {
                        Map<Class, Object> adaptableCache = adapterCache.get(adaptable);
                        if (adaptableCache == null) {
                            adaptableCache = new ConcurrentHashMap<Class, Object>(INNER_CACHE_INITIAL_CAPACITY);
                            adapterCache.put(adaptable, adaptableCache);
                        }
                        adaptableCache.put(requestedType, result.getValue());
                    }
                } catch (Exception e) {
                    String msg = String.format("Unable to create model %s", modelClass.getType());
                    return new Result<ModelType>(new ModelClassException(msg, e));
                }
            }
        }
        return result;
    } finally {
        threadInvocationCounter.decrease();
    }
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) MissingElementException(org.apache.sling.models.factory.MissingElementException) InvalidAdaptableException(org.apache.sling.models.factory.InvalidAdaptableException) ValidationException(org.apache.sling.models.factory.ValidationException) ExportException(org.apache.sling.models.factory.ExportException) PostConstructException(org.apache.sling.models.factory.PostConstructException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModelClassException(org.apache.sling.models.factory.ModelClassException) MissingExporterException(org.apache.sling.models.factory.MissingExporterException) MissingElementsException(org.apache.sling.models.factory.MissingElementsException) InvalidModelException(org.apache.sling.models.factory.InvalidModelException) ModelClassException(org.apache.sling.models.factory.ModelClassException) Model(org.apache.sling.models.annotations.Model) ModelClass(org.apache.sling.models.impl.model.ModelClass) InvalidAdaptableException(org.apache.sling.models.factory.InvalidAdaptableException)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Model (org.apache.sling.models.annotations.Model)1 ExportException (org.apache.sling.models.factory.ExportException)1 InvalidAdaptableException (org.apache.sling.models.factory.InvalidAdaptableException)1 InvalidModelException (org.apache.sling.models.factory.InvalidModelException)1 MissingElementException (org.apache.sling.models.factory.MissingElementException)1 MissingElementsException (org.apache.sling.models.factory.MissingElementsException)1 MissingExporterException (org.apache.sling.models.factory.MissingExporterException)1 ModelClassException (org.apache.sling.models.factory.ModelClassException)1 PostConstructException (org.apache.sling.models.factory.PostConstructException)1 ValidationException (org.apache.sling.models.factory.ValidationException)1 ModelClass (org.apache.sling.models.impl.model.ModelClass)1