在AEM 6.2中创建自定义路径浏览器谓词



我正在尝试为路径浏览器实现自定义OSGI服务谓词。如果有人知道这个代码出了什么问题:)下面有一个例外。也许是@Component或依赖的问题

<path jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/foundation/form/pathbrowser"
fieldDescription="List item link" 
fieldLabel="List Item link"
name="./path"
predicate="predicate"
rootPath="/content">
</path>

谓词实现:

import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import com.day.cq.commons.predicate.AbstractResourcePredicate;
import com.day.cq.wcm.api.Page;
@Component(label = "Content-page Predicate", description = "This predicate is used to restricted to allow selection of pages that have template content-page")
@Service(value = Predicate.class)
@Properties({
@Property(label = "Predicate Name", name = "predicate.name", value = "predicate", propertyPrivate = true) })
public class ContentPagePredicate extends AbstractResourcePredicate {
private static final String CQ_TEMPLATE_CONTENT = "/conf/xxx-lab/settings/wcm/templates/content-page";
@Override
public boolean evaluate(Resource resource) {
if (null != resource) {
if (!resource.getResourceType().equals("cq:Page")) {
return false;
}
Page page = resource.adaptTo(Page.class);
return page.getTemplate().getName().equals(CQ_TEMPLATE_CONTENT);
}
return false;
}
}

Maven构建输出:

[ERROR] Failed to execute goal org.apache.felix:maven-scr-plugin:1.20.0:scr (generate-scr-scrdescriptor) on project SomethingDemo.core: Execution generate-scr-scrdescriptor of goal org.apache.felix:maven-scr-plugin:1.20.0:scr failed: An API incompatibility was encountered while executing org.apache.felix:maven-scr-plugin:1.20.0:scr: java.lang.VerifyError: Constructor must call super() or this() before return
[ERROR] Exception Details:
[ERROR] Location:
[ERROR] com/day/cq/commons/predicate/AbstractNodePredicate.<init>()V @1: return
[ERROR] Reason:
[ERROR] Error exists in the bytecode
[ERROR] Bytecode:
[ERROR] 0x0000000: 2ab1 

当您从AEM API扩展一个用SCR注释(用于生成OSGi捆绑描述符)注释的类,同时在您使用的Uber Jar中进行模糊处理时,可能会发生您看到的错误。

你可以在Adobe的公共Maven存储库中找到一个不复杂的Uber Jar,用于你正在使用的AEM版本。

如果你代表客户或合作伙伴,你也应该能够从帮助网站下载一个https://daycare.day.com/home/products/uberjar.html

如果您的项目使用的存储库已经有了未被混淆的Jar,那么它应该像更改依赖关系一样简单。

例如,在一个使用带有模糊类的AEM 6.2 Uber Jar的项目中

<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<version>6.2.0</version>
<scope>provided</scope>
<classifier>obfuscated-apis</classifier>
</dependency>

只需更改分类器即可获得一个未被混淆的版本:

<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<version>6.2.0</version>
<scope>provided</scope>
<classifier>apis</classifier>
</dependency>

看看这个Github问题,就一个非常相似的问题进行更广泛的讨论。

你可能也会发现这个Adobe帮助论坛线程很有趣,尽管它属于测试版。

只需尝试实现org.apache.commons.collections.Predicate

另外:resource.getResourceType().equals("cq:Page")永远不会是true,因为cq:Page是资源的jcr:pimaryTypepage.getTemplate()不适用于发布:

public booean evaluate(Resource resource) {
if (null == resource) return false;
final ValueMap map = resource.getValueMap();
return "cq:Page".equals(map.get("jcr:primaryType", "")
&& CQ_TEMPLATE_CONTENT.equals(map.get("cq:template", "")
}

最新更新