Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

geronimo-web.xml defines the list of dependencies that have to be loaded into the web application class loader. In this case, there are no dependencies. Information about the project (e.g. module's unique identification, any dependencies) is described inside the <environment> tag. It is a good idea to give this module some sort of unique identification, so that it can later be referenced by some other deployable application. This module is in the group org.apache.geronimo.samples.dbtester. The path specified in the <context-root> tag will be the first segment of the URL used to access this web application. So to access this web application the url will be http://<hostname>:<port>/dbtester.

Code Block
xmlxml
borderStylesolid
titlegeronimo-web.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
        
    <environment>
        <moduleId>
            <groupId>org.apache.geronimo.samples</groupId>
            <artifactId>dbtester-war</artifactId>
            <version>2.1</version>
            <type>war</type>
        </moduleId>
    </environment>
                
    <context-root>/dbtester</context-root>

</web-app>

...

web.xml defines two servlets that will act as the control layer between presentation and service layers.

Code Block
xmlxml
borderStylesolid
titleweb.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <description>dbtester Servlet Sample</description>
    <servlet>
        <display-name>ContentTableServlet</display-name>
        <servlet-name>ContentTableServlet</servlet-name>
        <servlet-class>org.apache.geronimo.samples.dbtester.web.ContentTableServlet</servlet-class>
    </servlet>

    <servlet>
        <display-name>ListTablesServlet</display-name>
        <servlet-name>ListTablesServlet</servlet-name>
        <servlet-class>org.apache.geronimo.samples.dbtester.web.ListTablesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ContentTableServlet</servlet-name>
        <url-pattern>/listContent</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>ListTablesServlet</servlet-name>
        <url-pattern>/listTables</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

The most important part of this application is how to access Geronimo kernel and retrieve the list of database pools deployed there. This task is handled by the DBManagerBean class.

Code Block
javajava
borderStylesolid
titleExcerpt from DBManagerBean.java
java
    public DBManagerBean() {
        Kernel kernel = KernelRegistry.getSingleKernel();
        Set<AbstractName> cfList = kernel.listGBeans(new AbstractNameQuery(ResourceSource.class.getName()));

        for (AbstractName name : cfList) {

            try {
                Object rs = kernel.invoke(name, "$getResource", new Object[]{}, new String[]{});

                if (rs instanceof DataSource) {
                    DataSource ds = (DataSource) rs;
                    poolMap.put(name.getArtifact().getArtifactId(), ds);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

To retrieve the list of schemas and their tables, the application uses database metadata provided in a JDBC driver. ResultSet meta data has been used to get record related data and to display database contents. The following code snippet depicts how the application retrieves the schemas and their tables in a DataSource.

java
Code Block
java
borderStylesolid
titleExcerpt from DBManagerBean.java
java
    public Map getTableList(String poolName) throws SQLException {

        Map<String, List<String>> tableMap = new HashMap<String, List<String>>();

        if (poolMap.containsKey(poolName)) {
            DataSource ds = poolMap.get(poolName);
            Connection con = null;
            try {

                con = ds.getConnection();

                DatabaseMetaData metaData = con.getMetaData();
                String[] tableTypes = {"TABLE"};
                ResultSet rs = metaData.getTables(null, null, null, tableTypes);

                while (rs.next()) {
                    String schemaName = rs.getString("TABLE_SCHEM");
                    String tableName = rs.getString("TABLE_NAME");
                    List<String> tableList;

                    if (tableMap.containsKey(schemaName)) {
                        tableList = tableMap.get(schemaName);
                        tableList.add(tableName);
                    } else {
                        tableList = new ArrayList<String>();
                        tableList.add(tableName);
                    }
                    tableMap.put(schemaName, tableList);
                }
            } finally {
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return tableMap;
    }

Usage

The app is available at http://localhost:8080/dbtesterImage Removed. Note that the sample servers you get if you run maven with -Pit will not show any data since they dont include any database pools. A more realistic view comes from installing the plugin or app on a full geronimo server.

...