我目前正在尝试为Karaf部署XFA表单呈现解决方案。我在本地测试时没有很好的代码(没有OSGi),但是当我尝试部署到Karaf时,我遇到了各种错误。我相信这里的根本原因是thge XFAWorker jar不会被编译为OSGi包。所以我使用了Karaf wrap协议来导入捆绑包,但仍然没有成功。我已经尝试了很多东西,并会尝试在下面尽可能完整地记录它们。但是,描述我遇到的各种障碍是非常漫长的,所以简单来说,我的问题摘要是:
如何在OSGi容器中使用iText XFA Worker? (最好是Karaf / Felix)
这是我尝试过的完整悲伤故事:
首先,需要XFA Worker jar的代码片段:
private byte[] flattenXfa(byte[] sourceXfaBytes) throws RuntimeException {
// Create an output stream for the flattened doc
ByteArrayOutputStream flattened = new ByteArrayOutputStream();
try {
Document document = new Document();
PdfReader reader = new PdfReader(sourceXfaBytes);
PdfWriter writer = PdfWriter.getInstance(document, flattened);
XFAFlattener flattener = new XFAFlattener(document, writer);
flattener.flatten(reader);
document.close();
} ... various catches
}
这个方法在我的bundle Activator的start方法上调用(通过另一个类),这意味着我尝试在bundle启动时渲染(并展平)PDF。这仅仅是出于测试目的 - 已经创建了一个完全独立的项目,它实际上只有一小部分和渲染代码,以确保没有其他任何东西可以造成麻烦。
这是我的激活器的启动方法(以防它是非常重要的)
public void start(BundleContext context) {
System.out.println("Starting the bundle");
System.out.println("Rendering PDF");
Renderer rend = new Renderer();
String templatePath = "Path/to/xfa/file";
String renderPayload = "<form1><Name>MyName</Name><Surname>MySurname</Surname></form1>";
String xfaPdfPath = "Path/to/output/interactive/form";
String flatPdfPath = "Path/to/output/flat/pdf";
String licenseFilePath = "Path/to/itext/license/file";
boolean flatten = true;
rend.renderDoc(flatten, templatePath, renderPayload, xfaPdfPath, flatPdfPath, licenseFilePath);
}
如您所见,我有一个布尔标志,允许我在某些情况下跳过flattenXfa方法。
在我的Maven POM中,我使用了以下依赖项:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xfaworker</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>itext-licensekey</artifactId>
<version>1.0.4</version>
</dependency>
请注意:itextpdf和xmlworker jar可作为OSGi包使用。只有xfaworker和itext-licensekey会导致问题
如前所述,当我在OSGi之外运行代码时,它工作得很好 - 它像一个冠军一样渲染和展平PDF。
当我尝试从OSGi运行时,它会根据我的方法提出各种问题。所以,这里有一些我尝试过的事情。我将解释每个问题:
启动Karaf后,我按顺序运行以下命令,即模拟我的最终功能文件将执行的操作:
bundle:install -s mvn:com.itextpdf/itextpdf/5.5.11
bundle:install -s mvn:com.itextpdf.tool/xmlworker/5.5.11
bundle:install -s wrap:mvn:com.itextpdf.tool/xfaworker/5.5.11
bundle:install -s wrap:mvn:com.itextpdf.tool/itext-licensekey/1.0.4
bundle:install mvn:com.testing/xfa-test/0.0.1-SNAPSHOT (I specifically don't start it yet)
当我现在开始捆绑时,我遇到以下问题:
Starting the bundle
Rendering PDF
java.lang.NoClassDefFoundError: com/itextpdf/tool/xml/html/CssAppliersAware
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2370)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2154)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1542)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1415)
at org.apache.felix.framework.BundleWiringImpl.searchImports(BundleWiringImpl.java:1595)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1525)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.testing.xfa.Renderer.flattenXfa(Renderer.java:123)
at com.testing.xfa.Renderer.renderDoc(Renderer.java:52)
at com.testing.xfa.Activator.start(Activator.java:34)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:697)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2226)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2144)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
at org.apache.karaf.bundle.command.Start.executeOnBundle(Start.java:38)
at org.apache.karaf.bundle.command.BundlesCommand.doExecute(BundlesCommand.java:64)
at org.apache.karaf.bundle.command.BundlesCommand.execute(BundlesCommand.java:54)
at org.apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83)
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
at org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java:274)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.tool.xml.html.CssAppliersAware not found by wrap_mvn_com.itextpdf.tool_xfaworker_5.5.11 [61]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1574)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 35 more
Error executing command: Error executing command on bundles:
Error starting bundle 65: Activator start error in bundle xfa-test [65].
为了兴趣,如果我将flatten属性设置为false(即我只渲染交互式表单而不展平),那么即使在OSGi中它也能正常工作。但是,这种情况不使用XFA Worker jar。
从Try 1中的错误消息来看,问题似乎与捆绑连接有关。所以我想:为什么不直接将所有非OSGi内容直接嵌入我的包中作为私有包
所以我按如下方式编辑maven插件:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>${maven-bundle-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.testing.xfa.Activator</Bundle-Activator>
<Export-Package>
com.testing.xfa*;version=${project.version}
</Export-Package>
<Import-Package>
*
</Import-Package>
<Private-Package>com.itextpdf.*</Private-Package>
</instructions>
</configuration>
</plugin>
现在,当我安装并尝试启动我的软件包时,我收到以下(典型的OSGi)消息:
Error executing command: Error executing command on bundles:
Error starting bundle 66: Unable to resolve xfa-test [66](R 66.0): missing requirement [xfa-test [66](R 66.0)] osgi.wiring.package; (osgi.wiring.package=org.antlr.v4.runtime) Unresolved requirements: [[xfa-test [66](R 66.0)] osgi.wiring.package; (osgi.wiring.package=org.antlr.v4.runtime)]
这让我想到了一个试错版的兔子漏洞,即在我的私有软件包标签看起来像这样的情况下添加越来越多的私有软件包:
<Private-Package>com.itextpdf.*;org.antlr.*;org.abego.treelayout.*</Private-Package>
在这个阶段,我收到以下错误(即使我将&#34; org.apache.jcp。*&#34;添加到私有软件包列表中,它也不会消失
Error executing command: Error executing command on bundles:
Error starting bundle 69: Unable to resolve xfa-test [69](R 69.0): missing requirement [xfa-test [69](R 69.0)] osgi.wiring.package; (osgi.wiring.package=org.apache.jcp.xml.dsig.internal.dom) Unresolved requirements: [[xfa-test [69](R 69.0)] osgi.wiring.package; (osgi.wiring.package=org.apache.jcp.xml.dsig.internal.dom)]
这是另一个试错兔子洞,但我的方法是: - 将所有非osgi jar放在我的私有包标签中 - 完全删除包装的罐子(因为它们现在嵌入我的包中) - 逐个添加以下Karaf包:
bundle:install -s mvn:com.itextpdf/itextpdf/5.5.11
bundle:install -s mvn:com.itextpdf.tool/xmlworker/5.5.11
bundle:install -s mvn:org.apache.directory.studio/org.apache.commons.codec/1.8
bundle:install -s mvn:org.apache.santuario/xmlsec/2.0.2
bundle:install -s mvn:org.bouncycastle/bcprov-jdk15on/1.56
bundle:install -s mvn:org.bouncycastle/bcpkix-jdk15on/1.56
bundle:install -s mvn:org.bouncycastle/bcprov-jdk15/1.45
bundle:install -s wrap:mvn:org.apache.xmlbeans/xmlbeans/2.6.0
在添加每个捆绑包之间,我尝试自己创建并检查我得到的错误。洗涤,冲洗,重复。 一两个小时后,我得到了一个私有的Packahe标签,如下所示:
<Private-Package>com.itextpdf.*;org.antlr.*;org.abego.treelayout.*;org.bouncycastle.*;org.mozilla.*</Private-Package>
在这个阶段,Karaf不再抛出osgi布线异常,但是给了我以下NullPointerException
karaf@root()> start 87
Starting the bundle
Rendering PDF
java.lang.NullPointerException
at com.itextpdf.tool.xml.xtra.xfa.positioner.Positioner.<init>(Positioner.java:190)
at com.itextpdf.tool.xml.xtra.xfa.positioner.SubFormPositioner.<init>(SubFormPositioner.java:50)
at com.itextpdf.tool.xml.xtra.xfa.pipe.FormBuilder.buildSubForm(FormBuilder.java:215)
at com.itextpdf.tool.xml.xtra.xfa.pipe.FormBuilder.buildSubForm(FormBuilder.java:152)
at com.itextpdf.tool.xml.xtra.xfa.pipe.FormBuilder.getFormDom(FormBuilder.java:566)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:901)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:491)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:359)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:329)
at com.itextpdf.tool.xml.xtra.xfa.XFAFlattener.flatten(XFAFlattener.java:300)
at com.testing.xfa.Renderer.flattenXfa(Renderer.java:124)
at com.testing.xfa.Renderer.renderDoc(Renderer.java:52)
at com.testing.xfa.Activator.start(Activator.java:35)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:697)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2226)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2144)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
at org.apache.karaf.bundle.command.Start.executeOnBundle(Start.java:38)
at org.apache.karaf.bundle.command.BundlesCommand.doExecute(BundlesCommand.java:64)
at org.apache.karaf.bundle.command.BundlesCommand.execute(BundlesCommand.java:54)
at org.apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83)
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
at org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java:274)
at java.lang.Thread.run(Thread.java:745)
Error executing command: Error executing command on bundles:
Error starting bundle 87: Activator start error in bundle xfa-test [87].
此时我相信我做了一些非常错误的事情,所以我希望能在这里得到一些帮助。
任何指针都将不胜感激