title: how to deploy a webapp and create its resources
body: <p>before deploy "myapp.war" on glassfish 4 i have to</p>
  
  <ol>
  <li>create jdbc connection pool (/WEB-INF/glassfish-resources.xml -> <code>jdbc-connection-pool</code> does not work by itself... fine from asadmin)</li>
  <li>create jdbc resource for pool (/WEB-INF/glassfish-resources.xml -> <code>jdbc-resource</code> same as above)</li>
  <li>create auth-realm (nothing auto, using asadmin for now)</li>
  <li>create schema (peristence.xml -> <code>property javax.persistence.schema-generation.create-database-schemas</code>, but is bogus)</li>
  <li>create tables (persistence.xml -> `create-tables', not perfect but at least it works)</li>
  </ol>
  
  <p>now i'm doing:</p>
  
  <ol>
  <li>upload "myapp.war", "glassfish-resources.xml" on /tmp/install</li>
  <li><code>asadmin add-resources ...</code></li>
  <li><code>asadmin create-auth-realm ...</code></li>
  <li><code>asadmin deploy ...</code></li>
  <li><code>asadmin disable myapp ...</code></li>
  <li><code>nano /.../glassfish/applications/myapp/WEB-INF/classes/META-INF/persistence.xml</code></li>
  <li>comment a few lines, <code>ctrl+o</code>, <code>enter</code>, <code>ctrl+x</code>, <code>enter</code></li>
  <li><code>asadmin enable myapp ...</code></li>
  <li><code>rm -Rf /tmp/install</code></li>
  </ol>
  
  <p>and without other suggestion i'm planning to:</p>
  
  <ol>
  <li>upload "myapp.war", "deploy.sh" on /tmp/install</li>
  <li><code>chmod +x deploy.sh</code></li>
  <li><code>./deploy.sh</code></li>
  </ol>
  
  <p>and the script will take care of everything.
  but i'd like to upload <strong>only</strong> a war file via glassfish http console and obtain the same result.</p>
  
  <p>is there a way to have a class or script called <strong>before</strong> <code>contextInitialized</code>?</p>
  
  <p>how would you deploy this thing?</p>
  
  <hr>
  
  <p>for completeness here are some additional info:</p>
  
  <p><strong>/myapp/WEB-INF/classes/META-INF/persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="myapp" transaction-type="JTA"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;jta-data-source&gt;jdbc/myapp&lt;/jta-data-source&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;shared-cache-mode&gt;NONE&lt;/shared-cache-mode&gt;
          &lt;properties&gt;
              &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp"/&gt;
              &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
              &lt;property name="javax.persistence.jdbc.password" value="password"/&gt;
              &lt;property name="javax.persistence.schema-generation.database.action" value="create"/&gt;
              &lt;property name="javax.persistence.schema-generation.create-database-schemas" value="false"/&gt;
              &lt;property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/&gt;
              &lt;property name="javax.persistence.schema-generation.scripts.create-target" value="C:/tmp/myapp_create.ddl"/&gt;
              &lt;property name="javax.persistence.schema-generation.scripts.drop-target" value="C:/tmp/myapp_drop.ddl"/&gt;
              &lt;property name="eclipselink.deploy-on-startup" value="true"/&gt;
              &lt;property name="eclipselink.target-database" value="MySQL"/&gt;
  &lt;!--            &lt;property name="eclipselink.ddl-generation" value="create-tables"/&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.ddl-generation.output-mode" value="database"/&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.create-ddl-jdbc-file-name" value="myapp.ddl"/&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.logging.level" value="FINE" /&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.logging.level.sql" value="FINE"/&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.logging.parameters" value="true"/&gt; --&gt;
  &lt;!--            &lt;property name="eclipselink.logging.logger" value="org.eclipse.persistence.logging.DefaultSessionLog"/&gt; --&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>/myapp/WEB-INF/glassfish-resources.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"&gt;
  &lt;resources&gt;
      &lt;jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="jdbc/myapp_pool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"&gt;
          &lt;property name="serverName" value="localhost"/&gt;
          &lt;property name="portNumber" value="3306"/&gt;
          &lt;property name="databaseName" value="myapp"/&gt;
          &lt;property name="User" value="root"/&gt;
          &lt;property name="Password" value="password"/&gt;
          &lt;property name="URL" value="jdbc:mysql://localhost:3306/myapp"/&gt;
          &lt;property name="driverClass" value="com.mysql.jdbc.Driver"/&gt;
      &lt;/jdbc-connection-pool&gt;
      &lt;jdbc-resource enabled="true" jndi-name="jdbc/myapp" object-type="user" pool-name="jdbc/myapp_pool"/&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>while glassfish understands /myapp/.../persistence.xml (sometimes executing also <code>CREATE SCHEMA myapp</code>, sometimes not, apparently random - but it's fine), </p>
  
  <p>i definitely can't make glassfish read /myapp/WEB-INF/glassfish-resources.xml. it ignores this file.</p>
  
  <p><strong>UPDATE</strong>
  glassfish reads the file but prefixes jndi names with <code>java:app/</code> breaking other references. being aware of this thing i rewrite references with prefix and now it is working fine.
  lastly, i noted that if <code>glassfish-resources.xml</code> is inside <code>META-INF</code> (instead of <code>WEB-INF</code>) glassfish reads the file and it is also present in http ui under "applications > myapp > descriptors"</p>