使用 Axis 1 和 Tomcat 的自定义 XML 编码器

发布于 2024-11-26 18:49:22 字数 3420 浏览 1 评论 0原文

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 中;还是什么都没有。

我不明白。

The documentation on this page indicates that I can override Axis's default XML encoder by providing a file with this path:

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

The file contents of this file are:

com.myapp.CustomEncoder

Where com.myapp.Customer is in myapp.war/WEB-INF/classes/com/myapp/CustomEncoder.class.

Note that myapp.war is actually exploded as a full directory when I deploy but I don't think that should make a difference.

In any case, this doesn't seem to work on Apache Tomcat 7.0 but strangely enough does work on JBoss 4.2.3. Alternatively I tried setting a system property by adding this to Tomcat's startup script:

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

Still not seeing my CustomEncoder code being called, and only on Tomcat. Why should this work on JBoss and not Tomcat? Is the classpath different somehow on Tomcat? How can I make this work?

Thanks.

---- Update ----

Since it was mentioned, the source code that seems to load the custom provider appears to exist in 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;
        }
    }
}

Just for fun, I tried packaging up META-INF/services/org.apache.axis.components.encoding.XMLEncoder in a separate jar and storing it in myapp.war/WEB-INF/lib; still nothing.

I don't get it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-12-03 18:49:22

创建一个新项目,其中包含您的自定义编码器,然后将其制作为 jar。在 meta -inf 下的 jar 下创建 services 文件夹,并在其下创建文件 org.apache.axis.components.encoding.XMLEncoder 并在此文件中提及您的类名编码器(完全合格)。将此 jar 放在您的类路径中。

make new project say encoder which contain your custom encoder then make it jar.Under jar under meta -inf create services folder and under it create file org.apache.axis.components.encoding.XMLEncoder and in this file mention your class name of your encoder (fully qualified) .place this jar in your class path .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文