Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

This page contains examples of the multiple xml configuration files (deployment descriptors) that can be used when creating JavaEE Applications.

Through the use of deployment descriptors (XML files that specify component and container behavior), components can be configured to a specific container's environment when deployed, rather than in component code. Features that can be configured at deployment time include security checks, transaction control, and other management responsibilities. [3]

Many of this XML configurations can also be made using annotations in  code.
But if there's duplicate configuration (in code using annotations and in xml configuration files)  the XML configuration always overrides the annotations.
NOTE: check the Annotations section of this site - many of the annotations also have xml alternative examples.

sun-resources.xml

NOTE:  The sun-resources.xml file is a deployment descriptor specific to the GlassFish server only. Therefore, if the customer isn't using GlassFish as the production server, the file should be deleted before the application is deployed. If the sun-resources.xml file isn't removed from the WAR distribution however, it would simply be ignored by the server it is deployed to [2]. This file contains entries that instruct the GlassFish server to create the JDBC resource and connection pool when the application is deployed.

Using NetBeans IDE: The sun-resources.xml file is a deployment descriptor specific to the GlassFish application server. When the project gets deployed, the server will read in any configuration data contained in sun-resources.xml, and set up the connection pool and data source accordingly. Note that once the connection pool and data source exist on the server, your project no longer requires the sun-resources.xml file.
This file is created by NetBeans when using the wizard: File -> New -> Glassfish -> JDBC Resource

The wizard generates a sun-resources.xml file for the project that contains all information required to set up the connection pool and data source on GlassFish.
In the Projects window, expand the Server Resources node and double-click the sun-resources.xml file to open it in the editor. Here you see the XML configuration required to set up the connection pool and data source. (Code below is formatted for readability.)
<resources>
  <jdbc-resource enabled="true"
                 jndi-name="jdbc/affablebean"
                 object-type="user"
                 pool-name="AffableBeanPool">
  </jdbc-resource>

  <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="AffableBeanPool"
                        non-transactional-connections="false"
                        pool-resize-quantity="2"
                        res-type="javax.sql.ConnectionPoolDataSource"
                        statement-timeout-in-seconds="-1"
                        steady-pool-size="8"
                        validate-atmost-once-period-in-seconds="0"
                        wrap-jdbc-objects="false">

    <description>Connects to the affablebean database</description>
    <property name="URL" value="jdbc:mysql://localhost:3306/affablebean"/>
    <property name="User" value="root"/>
    <property name="Password" value="nbuser"/>
  </jdbc-connection-pool>
</resources>
On deployment GlassFish server reads configuration data from the sun-resources.xml file and, in this case, creates the "AffableBeanPool" connection pool, and "jdbc/affablebean" data source.

NOTE: By convention, the JNDI name for a JDBC resource begins with the 'jdbc/' string (in this case the name "jdbc/affablebean" was specified for the data source).

You can see the DataSources and Connection pools from the server node inside NetBeans:

You can associate a data source with any connection pool registered on the server. You can edit property values for connection pools, and unregister both data sources and connection pools from the server.

persistence.xml

defines the persistence unit file that provide the container with information about the data source and how the entities are managed;
A persistence unit is a collection of entity classes that exist in an application and is defined by a persistence.xml configuration file, which is read by your persistence provider (to define for example the data source). In NetBeans the persistence unit (persistence.xml) exists under the Configuration Files node. To create the persistence unit in NetBeans go to New -> Persistence -> Entity Classes from DataBase (or other), in the wizard check the option to "create Persistence Unit"

Set the logging level property to FINEST so that you can view all possible output produced by the persistence provider when the application runs. This enables you to see the SQL that the persistence provider is using on the database, and can facilitate in any required debugging:
<persistence-unit name="AffableBeanPU" transaction-type="JTA">
  <jta-data-source>jdbc/affablebean</jta-data-source>
  <properties>
    <property name="eclipselink.logging.level" value="FINEST"/>
  </properties>
</persistence-unit>
info taken from: The NetBeans E-commerce Tutorial - Adding Entity Classes and Session Beans


web.xml

the web deployment descriptor.
Example:
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <context-param>
        <description>The relative path to product images</description>
        <param-name>productImagePath</param-name>
        <param-value>img/products/</param-value>
    </context-param>

    <context-param>
        <description>The relative path to category images</description>
        <param-name>categoryImagePath</param-name>
        <param-value>img/categories/</param-value>
    </context-param>

    <context-param>
        <description>The delivery surcharge applied to all orders</description>
        <param-name>deliverySurcharge</param-name>
        <param-value>3.00</param-value>
    </context-param>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <jsp-config>
        <jsp-property-group>
            <description>header and footer settings</description>
            <url-pattern>/index.jsp</url-pattern>
            <url-pattern>/WEB-INF/view/*</url-pattern>
            <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
            <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
        </jsp-property-group>
    </jsp-config>
</web-app>

Explanation:
<context-param>
Setting Context Parameters [1]: The owner of an application may want to be able to change certain settings without the need to make intrusive changes to source code. Context parameters enable you application-wide access to parameter values, and provide a convenient way to change parameter values from a single location, should the need arise.

To use context parameters:
  1. List parameter names and values in the web deployment descriptor (web.xml)
  2. <context-param>
        <description>The relative path to product images</description>
        <param-name>productImagePath</param-name>
        <param-value>img/products/</param-value>
    </context-param>
    <context-param>
        <description>The relative path to category images</description>
        <param-name>categoryImagePath</param-name>
        <param-value>img/categories/</param-value>
    </context-param>
  3. Call the parameters in JSP pages using the initParam object (the initParam is a EL "implicit object")
    for example, if you have an initialization parameter named myParam, you can access it from a JSP page with the expression
  4. <div id="indexLeftColumn">
        <div id="welcomeText">
            <p>[ welcome text ]</p>
    
            <!-- test to access context parameters -->
            categoryImagePath: ${initParam.categoryImagePath}
            productImagePath: ${initParam.productImagePath}
        </div>
    </div>
    Note that you can also concatenate values with EL:
    ${initParam.productImagePath}broccoli.png
    ${initParam.productImagePath}${product.name}.png
    For more information on the JSP Expression Language and implicit objects, see the Java EE 5 Tutorial: JavaServer Pages Technology > Unified Expression Language.

<session-config>
set the client sessions timeout for the application.

Resources

[1] - The NetBeans E-commerce Tutorial - Connecting the Application to the Database
[2] - The NetBeans E-commerce Tutorial - Conclusion
[3] - Designing Enterprise Applications with the J2EETM Platform, Second Edition

Sem comentários:

Enviar um comentário