当使用JBOSS EAP 7和Apache FOP时,我在实例化一个新的Fop对象时随机(?)得到一个异常。重新启动后,代码可能会起作用。我怀疑有类加载问题。 在独立情况下(Eclipse),一切正常。有人建议FOP实现已在不同的安全上下文中加载。击败我意味着什么。
以下是例外: 由以下原因引起:java.lang.ClassCastException:org.apache.fop.render.afp.extensions.AFPExtensionHandlerFactory无法转换为org.apache.fop.util.ContentHandlerFactory
示例代码:
import java.io.File;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Force classloader to use FOP apache implementations first (experimental)
*/
@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class InitBean {
private static final Logger logger = LogManager.getLogger(InitBean.class.getName());
public InitBean() {
super();
}
@PostConstruct
public void initClassloader() {
try {
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, new NullOutputStream());
logger.debug("Created FOP handler {}", fop.getDefaultHandler().getClass().getName());
}
catch (Exception e) {
logger.error(e.getMessage());
}
}
}
库:Apache fop 2.2; xalan-2.7.2; docx4j-export-fo-6.0.1; xmlgraphics-commons-2.2; xercesImpl-2.11.0; docx4j-6.0.1; serializer-2.7.2
如何解决此问题?