OSGi服務是Java類或服務介面,以及許多作為名稱/值配對的服務屬性。 服務屬性區分提供具有相同服務介面的服務的不同服務提供商。
OSGi服務由其服務介面在語義上定義,並作為服務對象實現。 服務的功能由服務實施的介面定義。 因此,不同的應用程式可以實現相同的服務。 服務介面允許捆綁包通過綁定介面而非實施進行交互。 應指定服務介面,並盡可能減少實作詳細資料。
一個簡單介面,帶有一個方法,用於將資料與XDP模板合併。
package com.learningaemforms.adobe.core;
import com.adobe.aemfd.docmanager.Document;
public interface MyfirstInterface {
public Document mergeDataWithXDPTemplate(Document xdpTemplate, Document xmlDocument);
}
建立名為com.learningaemforms.adobe.core.impl
的新包以保存介面的實現。
package com.learningaemforms.adobe.core.impl;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.output.api.OutputService;
import com.adobe.fd.output.api.OutputServiceException;
import com.learningaemforms.adobe.core.MyfirstInterface;
@Component(service = MyfirstInterface.class)
public class MyfirstInterfaceImpl implements MyfirstInterface {
@Reference
OutputService outputService;
private static final Logger log = LoggerFactory.getLogger(MyfirstInterfaceImpl.class);
@Override
public Document mergeDataWithXDPTemplate(Document xdpTemplate, Document xmlDocument) {
com.adobe.fd.output.api.PDFOutputOptions pdfOptions = new com.adobe.fd.output.api.PDFOutputOptions();
pdfOptions.setAcrobatVersion(com.adobe.fd.output.api.AcrobatVersion.Acrobat_11);
try {
return outputService.generatePDFOutput(xdpTemplate, xmlDocument, pdfOptions);
} catch (OutputServiceException e) {
log.error("Failed to merge data with XDP Template", e);
}
return null;
}
}
第10行上的注釋@Component(...)
將此Java類標籤為OSGi元件,並將其註冊為OSGi服務。
@Reference
注釋是OSGi聲明性服務的一部分,用於將Outputservice的引用插入到變數outputService
中。
c:\aemformsbundles\learningaemforms\core
mvn clean install -PautoInstallBundle
此套件也可在以下位置C:\AEMFormsBundles\learningaemforms\core\target
使用。 此套件也可透過Felix Web主控台部署至AEM。
您現在可以在JSP頁中使用該服務。 下列程式碼片段顯示如何存取您的服務,以及如何使用服務實作的方法
MyFirstAEMFormsService myFirstAEMFormsService = sling.getService(com.learningaemforms.adobe.core.MyFirstAEMFormsService.class);
com.adobe.aemfd.docmanager.Document generatedDocument = myFirstAEMFormsService.mergeDataWithXDPTemplate(xdp_or_pdf_template,xmlDocument);
包含JSP頁的示例包可從此處
使用套件管理器將套件匯入並安裝至AEM
使用postman進行POST呼叫,並提供輸入參數,如下方螢幕擷取畫面所示