title: Correct way to make datasources/resources a deploy-time setting
body: <p>I have a web-app that requires two settings:</p>
  
  <ul>
  <li>A JDBC datasource</li>
  <li>A string token</li>
  </ul>
  
  <p>I desperately want to be able to deploy one .war to various different containers (jetty,tomcat,gf3 minimum) and configure these settings at application level within the container.</p>
  
  <p>My code does this:</p>
  
  <pre><code>InitialContext ctx = new InitialContext();
  Context envCtx = (javax.naming.Context) ctx.lookup("java:comp/env");
  token = (String)envCtx.lookup("token");
  ds = (DataSource)envCtx.lookup("jdbc/datasource")
  </code></pre>
  
  <p>Let's assume I've used the glassfish management interface to create two jdbc resources: jdbc/test-datasource and jdbc/live-datasource which connect to different copies of the same schema, on different servers, different credentials etc. Say I want to deploy this to glassfish with and point it at the test datasource, I might have this in my sun-web.xml:</p>
  
  <pre><code>...
  &lt;resource-ref&gt;
    &lt;res-ref-name&gt;jdbc/datasource&lt;/res-ref-name&gt;
    &lt;jndi-name&gt;jdbc/test-datasource&lt;/jndi-name&gt;
  &lt;/resource-ref&gt;
  ...
  </code></pre>
  
  <p><em>but</em></p>
  
  <ul>
  <li>sun-web.xml goes <em>inside</em> my war, right?</li>
  <li>surely there must be a way to do this through the management interface</li>
  </ul>
  
  <p>Am I even trying to do the right thing? Do other containers make this any easier? I'd be particularly interested in how jetty 7 handles this since I use it for development.</p>
  
  <p><strong>EDIT</strong> Tomcat has a reasonable way to do this:</p>
  
  <p>Create <code>$TOMCAT_HOME/conf/Catalina/localhost/webapp.xml</code> with:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context antiResourceLocking="false" privileged="true"&gt;
    &lt;!-- String resource --&gt;
    &lt;Environment name="token" value="value of token" type="java.lang.String" override="false" /&gt;
  
    &lt;!-- Linking to a global resource --&gt;
    &lt;ResourceLink name="jdbc/datasource1" global="jdbc/test" type="javax.sql.DataSource" /&gt;
  
    &lt;!-- Derby --&gt;
    &lt;Resource name="jdbc/datasource2"
      type="javax.sql.DataSource"
      auth="Container"
      driverClassName="org.apache.derby.jdbc.EmbeddedDataSource"
      url="jdbc:derby:test;create=true"
      /&gt;
  
    &lt;!-- H2 --&gt;
    &lt;Resource name="jdbc/datasource3"
      type="javax.sql.DataSource"
      auth="Container"
      driverClassName="org.h2.jdbcx.JdbcDataSource"
      url="jdbc:h2:~/test"
      username="sa"
      password=""
      /&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>Note that <code>override="false"</code> means the opposite. It means that this setting <em>can't be overriden</em> by <code>web.xml</code>.</p>
  
  <p>I like this approach because the file is part of the <em>container</em> configuration not the war, but it's not part of the global configuration; it's webapp specific.</p>
  
  <p>I guess I expect a bit more from glassfish since it is supposed to have a full web admin interface, but I would be happy enough with something equivalent to the above.</p>