Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

geronimo-web.xml defines the list of dependencies that have to be loaded into the web application class loader.

Code Block
xml
xml
borderStylesolid
titlegeronimo-web.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.2">
        
    <environment>
        <moduleId>
            <groupId>org.apache.geronimo.samples.dbtester</groupId>                 
            <artifactId>dbtester</artifactId>
            <version>1.0</version>
        </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
xml
xml
borderStylesolid
titleweb.xmlxml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
	 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	 version="2.4">	
	 
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
  	</welcome-file-list>
    	
    <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>
        
</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
java
java
borderStylesolid
titleDBManagerBean.javajava
	private void init(){
		Kernel kernel = KernelRegistry.getSingleKernel();
		Set cfList = kernel.listGBeans(new AbstractNameQuery(ConnectionFactorySource.class.getName()));
		
		for(Iterator iterator = cfList.iterator();iterator.hasNext();){

			AbstractName name = (AbstractName)iterator.next();
			try {
				Object rs = kernel.invoke(name, "$getResource", new Object[]{}, new String[]{});
				
				if(rs instanceof javax.sql.DataSource){
					DataSource ds = (DataSource)rs;	
					poolMap.put(name.getArtifact().getArtifactId(), ds);
				}
			} catch (GBeanNotFoundException e) {
				e.printStackTrace();
			} catch (NoSuchOperationException e) {
				e.printStackTrace();
			} catch (InternalKernelException e) {
				e.printStackTrace();
			} 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.

Code Block
java
java
borderStylesolid
titleDBManagerBean.javajava
	public Map getTableList(String poolName) throws SQLException {
		
		tableMap = new HashMap();
		
		if(poolMap.containsKey(poolName)){
			DataSource ds = (DataSource)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"); 
					ArrayList tableList = null;
					
					if(tableMap.containsKey(schemaName)){
						tableList = (ArrayList)tableMap.get(schemaName);
						tableList.add(tableName);
					}else {
						tableList = new ArrayList();
						tableList.add(tableName);
					}
					tableMap.put(schemaName, tableList);
				}
			} catch (SQLException e) {
				throw e;
			}finally {
				if(con != null){
					try {
						con.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
		return tableMap;
	}

...