带有Axis 1和Tomcat的自定义XML编码器

时间:2011-07-28 23:19:13

标签: java tomcat jboss axis

this页面上的文档表明我可以通过提供包含此路径的文件来覆盖Axis的默认XML编码器:

myapp.war/META-INF/services/org.apache.axis.components.encoding.XMLEncoder

此文件的文件内容为:

com.myapp.CustomEncoder

com.myapp.Customer位于myapp.war / WEB-INF / classes / com / myapp / CustomEncoder.class。

请注意,myapp.war实际上在我部署时会作为完整目录展开,但我认为这不应该有所作为。

在任何情况下,这似乎都不适用于Apache Tomcat 7.0,但奇怪的是它可以在JBoss 4.2.3上运行。或者,我尝试通过将其添加到Tomcat的启动脚本来设置系统属性:

-Dorg.apache.axis.components.encoding.XMLEncoder=com.myapp.CustomEncoder

仍然没有看到我的CustomEncoder代码被调用,而且只在Tomcat上调用。为什么这个应用于JBoss而不是Tomcat? Tomcat上的类路径是否有所不同?我怎样才能做到这一点?

感谢。

----更新----

由于提到过,似乎加载自定义提供程序的源代码似乎存在于org.apache.axis.components.encoding.XMLEncoderFactory中:

public static final String ENCODING_UTF_8 = "UTF-8";
public static final String ENCODING_UTF_16 = "UTF-16";
public static final String DEFAULT_ENCODING = ENCODING_UTF_8;

private static Map encoderMap = new HashMap();
private static final String PLUGABLE_PROVIDER_FILENAME = "org.apache.axis.components.encoding.XMLEncoder";

static {
    encoderMap.put(ENCODING_UTF_8, new UTF8Encoder());
    encoderMap.put(ENCODING_UTF_16, new UTF16Encoder());
    encoderMap.put(ENCODING_UTF_8.toLowerCase(), new UTF8Encoder());
    encoderMap.put(ENCODING_UTF_16.toLowerCase(), new UTF16Encoder());
    try {
        loadPluggableEncoders();
    } catch (Throwable t){
        String msg=t + JavaUtils.LS + JavaUtils.stackToString(t);
        log.info(Messages.getMessage("exception01",msg));
    }
}

...

/**
 Look for file META-INF/services/org.apache.axis.components.encoding.XMLEncoder
 in all the JARS, get the classes listed in those files and add them to 
 encoders list if they are valid encoders. 

 Here is how the scheme would work.

 A company providing a new encoder will jar up their encoder related
 classes in a JAR file. The following file containing the name of the new 
 encoder class is also made part of this JAR file. 

 META-INF/services/org.apache.axis.components.encoding.XMLEncoder

 By making this JAR part of the webapp, the new encoder will be 
 automatically discovered. 
 */
private static void loadPluggableEncoders() {
    ClassLoader clzLoader = XMLEncoder.class.getClassLoader();
    ClassLoaders loaders = new ClassLoaders();
    loaders.put(clzLoader);
    DiscoverServiceNames dsn = new DiscoverServiceNames(loaders);
    ResourceNameIterator iter = dsn.findResourceNames(PLUGABLE_PROVIDER_FILENAME);
    while (iter.hasNext()) {
        String className = iter.nextResourceName();
        try {
            Object o = Class.forName(className).newInstance();
            if (o instanceof XMLEncoder) {
                XMLEncoder encoder = (XMLEncoder) o;
                encoderMap.put(encoder.getEncoding(), encoder);
                encoderMap.put(encoder.getEncoding().toLowerCase(), encoder);
            }
        } catch (Exception e) {
            String msg = e + JavaUtils.LS + JavaUtils.stackToString(e);
            log.info(Messages.getMessage("exception01", msg));
            continue;
        }
    }
}

为了好玩,我尝试将META-INF / services / org.apache.axis.components.encoding.XMLEncoder打包在一个单独的jar中并将其存储在myapp.war / WEB-INF / lib中;仍然没有。

我不明白。

1 个答案:

答案 0 :(得分:0)

让新项目说编码器包含你的自定义编码器然后使它成为jar。在meta -inf下创建服务文件夹下的jar并在其下创建文件org.apache.axis.components.encoding.XMLEncoder并在此文件中提及你的类编码器的名称(完全限定)。将此jar放在类路径中。