title: How do you use global JNDI URL resources in Tomcat 8?
body: <p>I am trying to set up a server running Tomcat 8 to have JNDI URL resources available to a large number of apps running on that server. The apps are plain jsp/servlet with Maven (No Spring).  </p>
  
  <p>I have 2 examples of global resources I am trying to use, one is a DataSource which is working correctly and one is the URL that does not.</p>
  
  <p><strong>DataSource</strong></p>
  
  <p>In Tomcats context.xml inside the GlobalNamingResources element</p>
  
  <pre><code>&lt;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" /&gt;
  </code></pre>
  
  <p>In Tomcats web.xml, top level element</p>
  
  <pre><code>&lt;resource-ref&gt;
     &lt;res-ref-name&gt;APPNAME&lt;/res-ref-name&gt;
     &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
     &lt;res-auth&gt;Container&lt;/res-auth&gt;
     &lt;res-sharing-scope&gt;Unshareable&lt;/res-sharing-scope&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>That's it for the DataSource, and it does work</p>
  
  <p><strong>For the URL resource</strong>, I have this in the server.xml</p>
  
  <pre><code>&lt;Resource name="url/LOGIN_MODULE_CONFIGURATION_SERVICE" auth="Container"
     type="java.net.URL" factory="com.mycompany.objectfactory.JndiUrlObjectFactory"
     url="AValidUrl" /&gt;
  </code></pre>
  
  <p>My object factory was created the same way as in this answer: <a href="https://stackoverflow.com/questions/18351869/tomcat-use-ftp-connection-via-jndi">Tomcat: use FTP connection via JNDI</a> Except I added a few println statements to prove that it was being used and had the correct url which it does.</p>
  
  <p>I have the following Tomcats web.xml</p>
  
  <pre><code>&lt;resource-ref&gt;
     &lt;res-ref-name&gt;url/LOGIN_MODULE_CONFIGURATION_SERVICE&lt;/res-ref-name&gt;
     &lt;res-type&gt;java.net.URL&lt;/res-type&gt;
     &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>Finally the code that accesses this.  This code was the result of decompiling a jar,  it is not a third party jar but I do not have the source code.  The exception occurs in the <code>jndiReference = ic.lookup(serviceName);</code> line.  <code>serviceName</code> is the name of the resource and <code>jndiRoot</code> is always null (The code that calls this method passes in null).  The <code>getInitialContext()</code> method just creates a new instance of <code>javax.naming.InitialContext</code> if one was not already created.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>The line from the properties file that maps to the resource <code>propertyserviceurl=url/LOGIN_MODULE_CONFIGURATION_SERVICE</code></p>
  
  <p>I have also tried adding a ResourceLink to Tomcats context.xml.  I have tried making the name of the ResourceLink unique and the value of global the same as the actual Resource.  Also tried modifying the property file to map to the ResourceLink name instead of the Resource but I always get the same results.</p>
  
  <pre><code>&lt;ResourceLink name="url/LOGIN_MODULE_CONFIGURATION_SERVICE" 
      global="url/LOGIN_MODULE_CONFIGURATION_SERVICE" type="java.net.URL" /&gt;
  </code></pre>
  
  <p>The Exception </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I have spent a few days on this one and am having trouble figuring out what configuration I need to get this to work.  I think the biggest hurdle is that I can't change the java code that tries to access the resource.</p>