我正在尝试设置一个运行 Tomcat 8 的服务器,以使 JNDI URL 资源可用于该服务器上运行的大量应用程序。这些应用程序是带有 Maven(无 Spring)的普通 jsp/servlet。
我有 2 个尝试使用的全局资源示例,一个是正常工作的 DataSource,一个是不能正常工作的 URL。
数据源
在 GlobalNamingResources 元素内的 Tomcats context.xml 中
<Resource name="APPNAME" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="appuser"
password="password" driverClassName="oracle.jdbc.driver.OracleDriver"
url="aUrl" />
在 Tomcats web.xml 中,顶级元素
<resource-ref>
<res-ref-name>APPNAME</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
</resource-ref>
数据源就是这样,它确实有效
对于 URL 资源,我在 server.xml 中有这个
<Resource name="url/LOGIN_MODULE_CONFIGURATION_SERVICE" auth="Container"
type="java.net.URL" factory="com.mycompany.objectfactory.JndiUrlObjectFactory"
url="AValidUrl" />
我的对象工厂的创建方式与此答案相同:Tomcat:通过 JNDI 使用 FTP 连接除了我添加了一些 println 语句来证明它正在被使用并且具有正确的 url。
我有以下 Tomcats web.xml
<resource-ref>
<res-ref-name>url/LOGIN_MODULE_CONFIGURATION_SERVICE</res-ref-name>
<res-type>java.net.URL</res-type>
<res-auth>Container</res-auth>
</resource-ref>
最后是访问它的代码。这段代码是反编译jar的结果,它不是第三方jar,但我没有源代码。异常发生jndiReference = ic.lookup(serviceName);
在行中。 serviceName
是资源的名称,jndiRoot
始终为null(调用该方法的代码传入null)。该方法只是创建一个尚未getInitialContext()
创建的新实例。javax.naming.InitialContext
public static URL getUrl(final String serviceName, final String jndiRoot) {
URL result = null;
if (!isEmpty((Object) serviceName)) {
final InitialContext ic = getInitialContext();
Object jndiReference = null;
try {
jndiReference = ic.lookup(serviceName);
} catch (NamingException ex) {
ex.printStackTrace();
if (!isEmpty((Object) jndiRoot)) {
final String name = String.valueOf(jndiRoot) + serviceName;
try {
jndiReference = ic.lookup(name);
} catch (NamingException ex2) {
ex.printStackTrace();
}
}
}
if (jndiReference instanceof URL) {
result = (URL) jndiReference;
} else if (jndiReference instanceof Reference) {
final Reference reference = (Reference) jndiReference;
final RefAddr refAddr = reference.get("spec");
try {
result = new URL(getString(refAddr.getContent()));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
return result;
}
映射到资源的属性文件中的行propertyserviceurl=url/LOGIN_MODULE_CONFIGURATION_SERVICE
我还尝试将 ResourceLink 添加到 Tomcats context.xml。我尝试使 ResourceLink 的名称唯一,并且 global 的值与实际资源相同。还尝试修改属性文件以映射到 ResourceLink 名称而不是 Resource 但我总是得到相同的结果。
<ResourceLink name="url/LOGIN_MODULE_CONFIGURATION_SERVICE"
global="url/LOGIN_MODULE_CONFIGURATION_SERVICE" type="java.net.URL" />
例外
javax.naming.NameNotFoundException: Name [url/LOGIN_MODULE_CONFIGURATION_SERVICE] is not bound in this Context. Unable to find [url].
at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.mycompany.common.supplement.web.WebHelper.getUrl(WebHelper.java:739)
我在这个上花了几天时间,无法弄清楚我需要什么配置才能让它工作。我认为最大的障碍是我无法更改尝试访问资源的 java 代码。