/** * Fetches the last modification time of the resource * * @param resource Resource object we are finding timestamp of * @param operation string for logging, indicating caller's intention * @return timestamp as long */ private long readLastModified(final Resource resource, final String operation) { long timeStamp = 0; /* get the template name from the resource */ String name = resource.getName(); if (name == null || name.length() == 0) { String msg = "DataSourceResourceLoader: Template name was empty or null"; Logger.error(this, msg); throw new NullPointerException(msg); } else { Connection conn = null; ResultSet rs = null; PreparedStatement ps = null; try { conn = openDbConnection(); ps = getStatement(conn, timestampColumn, name); rs = ps.executeQuery(); if (rs.next()) { Timestamp ts = rs.getTimestamp(timestampColumn); timeStamp = ts != null ? ts.getTime() : 0; } else { String msg = "DataSourceResourceLoader: could not find resource " + name + " while " + operation; Logger.error(this, msg); throw new ResourceNotFoundException(msg); } } catch (SQLException sqle) { String msg = "DataSourceResourceLoader: database problem while " + operation + " of '" + name + "': "; Logger.error(this, msg, sqle); throw ExceptionUtils.createRuntimeException(msg, sqle); } catch (NamingException ne) { String msg = "DataSourceResourceLoader: database problem while " + operation + " of '" + name + "': "; Logger.error(this, msg, ne); throw ExceptionUtils.createRuntimeException(msg, ne); } finally { closeResultSet(rs); closeStatement(ps); closeDbConnection(conn); } } return timeStamp; }
/** * Get an InputStream so that the Runtime can build a template with it. * * @param name name of template to get * @return InputStream containing the template * @throws ResourceNotFoundException if template not found in classpath. */ public InputStream getResourceStream(String name) throws ResourceNotFoundException { InputStream result = null; if (StringUtils.isEmpty(name)) { throw new ResourceNotFoundException("No template name provided"); } /** * look for resource in thread classloader first (e.g. WEB-INF\lib in a servlet container) then * fall back to the system classloader. */ try { result = ClassUtils.getResourceAsStream(getClass(), name); } catch (Exception fnfe) { throw (ResourceNotFoundException) ExceptionUtils.createWithCause( ResourceNotFoundException.class, "problem with template: " + name, fnfe); } if (result == null) { String msg = "ClasspathResourceLoader Error: cannot find resource " + name; throw new ResourceNotFoundException(msg); } return result; }