providing runtime defaults in Glassfish
A
Java
EE
application is a logical collection of one or more
Java EE modules tied
together by application deployment descriptors. In addition to the
Java EE standard
deployment descriptors, application server vendors use runtime
deployment
descriptors for configuring features specific to their server.
The use of runtime deployment descriptors
adds
additional complexity in deploying an application to a Java
EE application server, as well
as making it harder for a user to port
applications from one such a server to another.
To alleviate the users from
having to always provide runtime deployment descriptors, we have
implemented many runtime defaults in the upcoming Sun Java System
Application
Server 9.0 Beta release (will be referred as
appserver9 below
), also available in Glassfish.
This will
reduce and in some cases eliminate the need for including runtime
deployment descriptors in an application. For example,
we are now providing defaults
for the jndi names of various JNDI environment references (ejb-ref,
resource-ref, resource-env-ref, message-destination-ref). In previous
versions of Sun Java System Application Server, for example,
Sun Java
System Application Server 8.x (will be referred as appserver 8
below),
after the JNDI environment references are defined in the standard
deployment descriptor, mapping of these environment references to their
physical
jndi names would need to be defined in the runtime deployment
descriptors. This is no longer
needed in appserver9 if the physical jndi name is the same as the
environment reference name.
Following is a simple example to illustrate the
simplification. It uses
the resource reference
scenario
described in the
Tomcat
tutorial. It inserts two employees into the Employee
table and displays back the second employee.
The
AccessDBServlet
use a data resource
jdbc/EmployeeDB
to talk to the underlying database:
InitialContext
initCtx = new InitialContext();
Context
envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds
= (DataSource) envCtx.lookup("jdbc/EmployeeDB");
In the standard deployment descriptor (web.xml), it
defines this
resource-ref entry for the data source:
<resource-ref>
<description>Employees Database for HR Applications</description>
<res-ref-name>jdbc/EmployeeDB</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>
To have this application work on appserver8 or port
this application
from Tomcat to appserver8, you would need to map the resource reference
to the
physical jndi name of the jdbc resource created on the server in the
runtime deployment descriptor
(sun-web.xml).
<resource-ref>
<res-ref-name>jdbc/EmployeeDB</res-ref-name>
<jndi-name>jdbc/EmployeeDB</jndi-name>
</resource-ref>
But in appserver9 and
Glassfish,
you don't need to do any mapping. Especially
if this is the only information you needed in the sun-web.xml, now you
are runtime deployment descriptor free.
You can download the complete source of this example from
here
(thanks
Lance for
his help
with database setup). There
is a build.xml provided in the zip file, you can use that to
create the
datasource, create the table, package and deploy the application
(the build.xml is only tested on unix platforms, you will need to
modify it to make it work
for windows). Of course you could also choose to do the tasks manually.
The main ant targets of the
build.xml
are as follows:
deploy-jdbc:
create the jdbc resource jdbc/EmployeeDB on the server
create-sql:
create the Employee table
build:
build and
package the web application
deploy:
deploy the
web application to the server
undeploy:
undeploy the web application from the server
undeploy-jdbc:
delete the jdbc resources jdbc/EmployeeDB from the server
Before you use the build.xml, you need to edit
build.properties
with appropriate values. For example, you need to set appserver.home to
where you installed the appserver/glassfish, and java.home to where you
installed the jdk.
#set this to where
you installed the appserver/glassfish
appserver.home=${env.S1AS_HOME}
#set this to
where you installed the JDK
java.home=${env.JAVA_HOME}
To run the sample, assume you have installed the
appserver/glassfish at
$AS_INSTALL
and unzip the example at
$EXAMPLE_DIR
1. Start the appserver9/Glassfish
cd $AS_INTALL/bin
./asadmin
start-domain
2. Start the derby database in network mode (Derby
database is
integrated with
the appserver9/glassfish)
cd
$AS_INTALL/bin
./asadmin
start-database
3. Creates the datasource, create the table,
package and deploy
the application
cd
$EXAMPLE_DIR
ant
deploy-jdbc create-sql build deploy
4. Point your browser at
http://localhost:8080/dbAccess/dbServlet
5. After you are done, you could undeploy the
application, delete the
table, delete the resource
cd $EXAMPLE_DIR
ant undeploy
delete-sql undeploy-jdbc
6. Stop the appserver/glassfish
cd $AS_INTALL/bin
./asadmin
stop-domain
7. Stop the Derby database
cd
$AS_INTALL/bin
./asadmin
stop-database