Как заставить работать «Шаблон делегирования для моделей строп»? - PullRequest
2 голосов
/ 23 мая 2019

Я пытаюсь создать прокси-компонент для базового компонента AEM с пользовательской моделью слинга, которая делегирует часть функциональности обратно модели слияния основного компонента.

Я следую примеру из https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models, но это приводит к java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field.

Настройка

  • Версия AEM 6.4.4
  • core.wcm.components.content версия 2.4.0

Источники

/ apps / myproject / components / pageHeadline / .content.xml

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="cq:Component"
    jcr:title="Page Headline"
    jcr:description="Display Page Heading"
    sling:resourceSuperType="core/wcm/components/title/v2/title"
    componentGroup="My Project"/>

com.example.core.models.PageHeadline

package com.example.core.models;

import com.adobe.cq.wcm.core.components.models.Title;
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

@Model(adaptables = SlingHttpServletRequest.class, adapters = Title.class, resourceType = "myproject/components/pageHeadline")
public class PageHeadline implements Title {

    @ScriptVariable
    private Page currentPage;

    @Self @Via(type = ResourceSuperType.class)
    private Title delegate;

    @Override
    public String getText() {
        return currentPage.getTitle();
    }

    @Override
    public String getType() {
        return delegate.getType();
    }
}

Результат

org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
...
Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier com.example.core.models.Title cannot be correctly instantiated by the Use API
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:78) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:69) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.sightly.apps.core.wcm.components.title.v2.title.title_html.render(title_html.java:41)
    at org.apache.sling.scripting.sightly.java.compiler.RenderUnit.render(RenderUnit.java:48) [org.apache.sling.scripting.sightly.compiler.java:1.0.26.1_4_0]
    at org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript.eval(SightlyCompiledScript.java:61) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:386) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.0.54]
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.0.54]
    ... 269 common frames omitted
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.example.core.models.PageHeadline
    at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:765) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:448) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:314) [org.apache.sling.models.impl:1.4.10]
    at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:126) [org.apache.sling.scripting.sightly.models.provider:1.0.6]
    at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.54.1_4_0]
    ... 276 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.example.core.models.Title com.example.core.models.PageHeadline.title
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:749) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: Could not inject field due to reflection issues
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:48) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.setField(ModelAdapterFactory.java:989) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.access$200(ModelAdapterFactory.java:132) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory$SetFieldCallback.inject(ModelAdapterFactory.java:500) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:595) [org.apache.sling.models.impl:1.4.10]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:744) [org.apache.sling.models.impl:1.4.10]
        ... 280 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Can not set com.example.core.models.Title field com.example.core.models.PageHeadline.title to com.adobe.cq.wcm.core.components.internal.models.v1.TitleImpl
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
        at java.lang.reflect.Field.set(Field.java:764)
        at org.apache.sling.models.impl.model.InjectableField.set(InjectableField.java:46) [org.apache.sling.models.impl:1.4.10]
        ... 285 common frames omitted

1 Ответ

0 голосов
/ 10 июля 2019

У меня была та же проблема, когда шаблон делегирования не работал, в частности, введенный атрибут был нулевым, если я использовал @Optional, или ту же трассировку стека, что и при публикации, если @Optional был пропущен.

Проблема для меня заключалась в том, что мы используем следующий код Embed-Dependency:

<Embed-Dependency>
  *;scope=compile|runtime
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>

в maven-bundle-plugin. Проблема заключалась в том, что зависимость основных компонентов была установлена ​​как встроенная зависимость. Как только я это исправил, шаблон делегирования работал нормально.

<Embed-Dependency>
  *;scope=compile|runtime;artifactId=!core.wcm.components.core
</Embed-Dependency>
...