1 package org.apache.velocity.runtime.resource.loader; 2 3 18 19 import java.io.InputStream ; 20 import java.io.BufferedInputStream ; 21 import java.util.Hashtable ; 22 23 import javax.sql.DataSource ; 24 import javax.naming.InitialContext ; 25 26 import org.apache.velocity.runtime.Runtime; 27 import org.apache.velocity.runtime.resource.Resource; 28 29 import org.apache.velocity.exception.ResourceNotFoundException; 30 31 import org.apache.commons.collections.ExtendedProperties; 32 import java.sql.Connection ; 33 import java.sql.ResultSet ; 34 import java.sql.Statement ; 35 import java.sql.SQLException ; 36 37 110 public class DataSourceResourceLoader extends ResourceLoader 111 { 112 private String dataSourceName; 113 private String tableName; 114 private String keyColumn; 115 private String templateColumn; 116 private String timestampColumn; 117 private InitialContext ctx; 118 private DataSource dataSource; 119 120 public void init( ExtendedProperties configuration) 121 { 122 dataSourceName = configuration.getString("resource.datasource"); 123 tableName = configuration.getString("resource.table"); 124 keyColumn = configuration.getString("resource.keycolumn"); 125 templateColumn = configuration.getString("resource.templatecolumn"); 126 timestampColumn = configuration.getString("resource.timestampcolumn"); 127 128 Runtime.info("Resources Loaded From: " + dataSourceName + "/" + tableName); 129 Runtime.info( "Resource Loader using columns: " + keyColumn + ", " 130 + templateColumn + " and " + timestampColumn); 131 Runtime.info("Resource Loader Initalized."); 132 } 133 134 public boolean isSourceModified(Resource resource) 135 { 136 return (resource.getLastModified() != 137 readLastModified(resource, "checking timestamp")); 138 } 139 140 public long getLastModified(Resource resource) 141 { 142 return readLastModified(resource, "getting timestamp"); 143 } 144 145 152 public synchronized InputStream getResourceStream( String name ) 153 throws ResourceNotFoundException 154 { 155 if (name == null || name.length() == 0) 156 { 157 throw new ResourceNotFoundException ("Need to specify a template name!"); 158 } 159 160 try 161 { 162 Connection conn = openDbConnection(); 163 164 try 165 { 166 ResultSet rs = readData(conn, templateColumn, name); 167 168 try 169 { 170 if (rs.next()) 171 { 172 return new 173 BufferedInputStream (rs.getAsciiStream(templateColumn)); 174 } 175 else 176 { 177 String msg = "DataSourceResourceLoader Error: cannot find resource " 178 + name; 179 Runtime.error(msg ); 180 181 throw new ResourceNotFoundException (msg); 182 } 183 } 184 finally 185 { 186 rs.close(); 187 } 188 } 189 finally 190 { 191 closeDbConnection(conn); 192 } 193 } 194 catch(Exception e) 195 { 196 String msg = "DataSourceResourceLoader Error: database problem trying to load resource " 197 + name + ": " + e.toString(); 198 199 Runtime.error( msg ); 200 201 throw new ResourceNotFoundException (msg); 202 203 } 204 205 } 206 207 215 private long readLastModified(Resource resource, String i_operation) 216 { 217 220 221 String name = resource.getName(); 222 try 223 { 224 Connection conn = openDbConnection(); 225 226 try 227 { 228 ResultSet rs = readData(conn, timestampColumn, name); 229 try 230 { 231 if (rs.next()) 232 { 233 return rs.getTimestamp(timestampColumn).getTime(); 234 } 235 else 236 { 237 Runtime.error("DataSourceResourceLoader Error: while " 238 + i_operation 239 + " could not find resource " + name); 240 } 241 } 242 finally 243 { 244 rs.close(); 245 } 246 } 247 finally 248 { 249 closeDbConnection(conn); 250 } 251 } 252 catch(Exception e) 253 { 254 Runtime.error( "DataSourceResourceLoader Error: error while " 255 + i_operation + " when trying to load resource " 256 + name + ": " + e.toString() ); 257 } 258 return 0; 259 } 260 261 267 private Connection openDbConnection() 268 throws Exception 269 { 270 if(ctx == null) 271 { 272 ctx = new InitialContext (); 273 } 274 275 if(dataSource == null) 276 { 277 dataSource = (DataSource )ctx.lookup(dataSourceName); 278 } 279 280 return dataSource.getConnection(); 281 } 282 283 286 private void closeDbConnection(Connection conn) 287 { 288 try 289 { 290 conn.close(); 291 } 292 catch (Exception e) 293 { 294 Runtime.info( 295 "DataSourceResourceLoader Quirk: problem when closing connection: " 296 + e.toString()); 297 } 298 } 299 300 313 private ResultSet readData(Connection conn, String columnNames, String templateName) 314 throws SQLException 315 { 316 Statement stmt = conn.createStatement(); 317 318 String sql = "SELECT " + columnNames 319 + " FROM " + tableName 320 + " WHERE " + keyColumn + " = '" + templateName + "'"; 321 322 return stmt.executeQuery(sql); 323 } 324 } 325 | Popular Tags |