使用 Axis 1 和 Tomcat 的自定义 XML 编码器
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个新项目,其中包含您的自定义编码器,然后将其制作为 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 .