有个项目生产环境是Webshpere,WAS提供了基于URL的JNDI Resource, 而我们的自动化集成测试阶段使用的tomcat容器不支持这种类型的Resource,所以需要自己定制。定制很简单,先按tomcat定制resource的方式建一个jar可以读取url,加入到tomcat的classpath,然后就可以通过配置来使用基于URL的JNDI资源了。这里给出一个通过jndi访问文件夹路径的例子。
public class URLFactory implements ObjectFactory
{
@Override
public Object getObjectInstance(Object obj, Name name, Context context, Hashtable<?, ?> environment) throws Exception
{
Reference ref = (Reference) obj;
String url = (String) ref.get("url").getContent();
return new URL(new URL("file:"),url);
}
}
<Resource name="url/demo"
auth="Container"
type="java.net.URL"
factory="com.demo.URLFactory" url="./demo/"/>
<resource-ref>
<description>Object factory for url.</description>
<res-ref-name>url/demo</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<jee:jndi-lookup id="demo" jndi-name=" java:comp/env/url/demo"/>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/cloud_ll/article/details/47159125