Hi David,


At a first glance:
 - DBResourceLoader.isSourceModified() has too much code in common
   with DBResourceLoader.getLastModified();
 - There is some more code repeated.

Without (almost) changing the functionality, what about this
implementation (just the DBResourceLoader class)?


Paulo

The code follows:




public class DBResourceLoader extends ResourceLoader
{
     private String dataSourceName;
     private String tableName;
     private String keyColumn;
     private String templateColumn;
     private String timestampColumn;
     private InitialContext ctx;
     private DataSource dataSource;

     /*
      * This should probably be moved into the super class,
      * the stand init stuff. For the properties that all
      * loaders will probably share.
      */
     public void init(Map initializer)
     {
         dataSourceName  = (String) initializer.get("resource.datasource");
         tableName       = (String) initializer.get("resource.table");
         keyColumn       = (String) initializer.get("resource.keycolumn");
         templateColumn  = (String)
initializer.get("resource.templatecolumn");
         timestampColumn = (String)
initializer.get("resource.timestampcolumn");
         Runtime.info("Resources Loaded From: " + dataSourceName + "/" +
tableName);
         Runtime.info( "Resource Loader using columns: " + keyColumn + ", "
                       + templateColumn + " and " + timestampColumn
                     );
         Runtime.info("Resource Loader Initalized.");
     }


     public boolean isSourceModified(Resource resource)
     {
         return (resource.getLastModified() != readLastModified(resource,
"checking timestamp"));
     }

     public long getLastModified(Resource resource)
     {
         return readLastModified(resource, "getting timestamp");
     }

     /**
      * Get an InputStream so that the Runtime can build a
      * template with it.
      */
     public synchronized InputStream getResourceStream( String name )
         throws Exception
     {
         if (name == null || name.length() == 0)
             throw new Exception ("Need to specify a template name!");

         try
         {   Connection conn = openDbConnection();
             try
             {   ResultSet rs = readData(conn, templateColumn, name);
                 try
                 {   if (rs.next())
                         return new
BufferedInputStream(rs.getAsciiStream(templateColumn));
                     else
                         Runtime.error("DBResourceLoader Error: cannot find
resource " + name);
                 }
                 finally
                 {   rs.close();
                 }
             }
             finally
             {   closeDbConnection(conn);
             }
         }
         catch(Exception e)
         {   Runtime.error( "DBResourceLoader Error: database problem trying
to load resource "
                            + name + ": " + e.toString()
                          );
         }
         return null;
     }


     private long readLastModified(Resource resource, String i_operation)
     {
         String name = resource.getName();
         try
         {   Connection conn = openDbConnection();
             try
             {   ResultSet rs = readData(conn, timestampColumn, name);
                 try
                 {   if (rs.next())
                         return rs.getLong(timestampColumn);
                     else
                         Runtime.error("DBResourceLoader Error: while " +
i_operation + " could not find resource " + name);
                 }
                 finally
                 {   rs.close();
                 }
             }
             finally
             {   closeDbConnection(conn);
             }
         }
         catch(Exception e)
         {   Runtime.error( "DBResourceLoader Error: error while " +
i_operation + " when trying to load resource "
                            + name + ": " + e.toString()
                          );
         }
         return 0;
     }


     private Connection openDbConnection()
         throws Exception
    {
         if(ctx == null)
             ctx = new InitialContext();
         if(dataSource == null)
             dataSource = (DataSource)ctx.lookup(dataSourceName);

         return dataSource.getConnection();
     }

     private void closeDbConnection(Connection conn)
     {   try {
             conn.close();
         } catch (Exception e) {
             Runtime.info("DBResourceLoader Quirk: problem when closing
connection: " + e.toString());
         }
     }

     private ResultSet readData(Connection conn, String columnNames, String
templateName)
         throws SQLException
     {   Statement stmt = conn.createStatement();
         String sql = "select " + columnNames
                      + " from " + tableName
                      + " where " + keyColumn + " = '" + templateName + "'";

         return stmt.executeQuery(sql);
     }
}

Reply via email to