1 package org.apache.velocity.runtime.resource; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Hashtable ; 21 import java.util.Vector ; 22 23 import java.io.InputStream ; 24 import java.io.IOException ; 25 26 import org.apache.velocity.runtime.RuntimeServices; 27 import org.apache.velocity.runtime.RuntimeConstants; 28 29 import org.apache.velocity.runtime.resource.ResourceFactory; 30 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 31 import org.apache.velocity.runtime.resource.loader.ResourceLoaderFactory; 32 33 import org.apache.velocity.exception.ResourceNotFoundException; 34 import org.apache.velocity.exception.ParseErrorException; 35 36 import org.apache.commons.collections.ExtendedProperties; 37 38 47 public class ResourceManagerImpl implements ResourceManager 48 { 49 52 public static final int RESOURCE_TEMPLATE = 1; 53 54 57 public static final int RESOURCE_CONTENT = 2; 58 59 62 private static final String RESOURCE_LOADER_IDENTIFIER = "_RESOURCE_LOADER_IDENTIFIER_"; 63 64 68 protected ResourceCache globalCache = null; 69 70 74 protected ArrayList resourceLoaders = new ArrayList (); 75 76 84 private ArrayList sourceInitializerList = new ArrayList (); 85 86 96 private Hashtable sourceInitializerMap = new Hashtable (); 97 98 104 private boolean resourceLoaderInitializersActive = false; 105 106 110 private boolean logWhenFound = true; 111 112 protected RuntimeServices rsvc = null; 113 114 117 public void initialize( RuntimeServices rs ) 118 throws Exception 119 { 120 rsvc = rs; 121 122 rsvc.info("Default ResourceManager initializing. (" + this.getClass() + ")"); 123 124 ResourceLoader resourceLoader; 125 126 assembleResourceLoaderInitializers(); 127 128 for (int i = 0; i < sourceInitializerList.size(); i++) 129 { 130 ExtendedProperties configuration = (ExtendedProperties) sourceInitializerList.get(i); 131 String loaderClass = configuration.getString("class"); 132 133 if ( loaderClass == null) 134 { 135 rsvc.error( "Unable to find '" 136 + configuration.getString(RESOURCE_LOADER_IDENTIFIER) 137 + ".resource.loader.class' specification in configuation." 138 + " This is a critical value. Please adjust configuration."); 139 continue; 140 } 141 142 resourceLoader = ResourceLoaderFactory.getLoader( rsvc, loaderClass); 143 resourceLoader.commonInit( rsvc, configuration); 144 resourceLoader.init(configuration); 145 resourceLoaders.add(resourceLoader); 146 147 } 148 149 152 153 logWhenFound = rsvc.getBoolean( RuntimeConstants.RESOURCE_MANAGER_LOGWHENFOUND, true ); 154 155 158 159 String claz = rsvc.getString( RuntimeConstants.RESOURCE_MANAGER_CACHE_CLASS ); 160 161 Object o = null; 162 163 if ( claz != null && claz.length() > 0 ) 164 { 165 try 166 { 167 o = Class.forName( claz ).newInstance(); 168 } 169 catch (ClassNotFoundException cnfe ) 170 { 171 String err = "The specified class for ResourceCache (" 172 + claz 173 + ") does not exist (or is not accessible to the current classlaoder)."; 174 rsvc.error( err ); 175 176 o = null; 177 } 178 179 if (!(o instanceof ResourceCache) ) 180 { 181 String err = "The specified class for ResourceCache (" 182 + claz 183 + ") does not implement org.apache.runtime.resource.ResourceCache." 184 + " ResourceManager. Using default ResourceCache implementation."; 185 186 rsvc.error( err); 187 188 o = null; 189 } 190 } 191 192 195 196 if ( o == null) 197 o = new ResourceCacheImpl(); 198 199 globalCache = (ResourceCache) o; 200 201 globalCache.initialize( rsvc ); 202 203 rsvc.info("Default ResourceManager initialization complete."); 204 205 } 206 207 214 private void assembleResourceLoaderInitializers() 215 { 216 if (resourceLoaderInitializersActive) 217 { 218 return; 219 } 220 221 Vector resourceLoaderNames = 222 rsvc.getConfiguration().getVector(RuntimeConstants.RESOURCE_LOADER); 223 224 for (int i = 0; i < resourceLoaderNames.size(); i++) 225 { 226 234 String loaderID = 235 resourceLoaderNames.get(i) + "." + RuntimeConstants.RESOURCE_LOADER; 236 237 ExtendedProperties loaderConfiguration = 238 rsvc.getConfiguration().subset(loaderID); 239 240 243 244 if ( loaderConfiguration == null) 245 { 246 rsvc.warn("ResourceManager : No configuration information for resource loader named '" 247 + resourceLoaderNames.get(i) + "'. Skipping."); 248 continue; 249 } 250 251 256 257 loaderConfiguration.setProperty( RESOURCE_LOADER_IDENTIFIER, resourceLoaderNames.get(i)); 258 259 263 sourceInitializerList.add(loaderConfiguration); 264 } 265 266 resourceLoaderInitializersActive = true; 267 } 268 269 284 public Resource getResource(String resourceName, int resourceType, String encoding ) 285 throws ResourceNotFoundException, ParseErrorException, Exception 286 { 287 293 294 Resource resource = globalCache.get(resourceName); 295 296 if( resource != null) 297 { 298 301 302 try 303 { 304 refreshResource( resource, encoding ); 305 } 306 catch( ResourceNotFoundException rnfe ) 307 { 308 313 314 globalCache.remove( resourceName ); 315 316 return getResource( resourceName, resourceType, encoding ); 317 } 318 catch( ParseErrorException pee ) 319 { 320 rsvc.error( 321 "ResourceManager.getResource() exception: " + pee); 322 323 throw pee; 324 } 325 catch( Exception eee ) 326 { 327 rsvc.error( 328 "ResourceManager.getResource() exception: " + eee); 329 330 throw eee; 331 } 332 } 333 else 334 { 335 try 336 { 337 340 341 resource = loadResource( resourceName, resourceType, encoding ); 342 343 if (resource.getResourceLoader().isCachingOn()) 344 { 345 globalCache.put(resourceName, resource); 346 } 347 } 348 catch( ResourceNotFoundException rnfe2 ) 349 { 350 rsvc.error( 351 "ResourceManager : unable to find resource '" + resourceName + 352 "' in any resource loader."); 353 354 throw rnfe2; 355 } 356 catch( ParseErrorException pee ) 357 { 358 rsvc.error( 359 "ResourceManager.getResource() parse exception: " + pee); 360 361 throw pee; 362 } 363 catch( Exception ee ) 364 { 365 rsvc.error( 366 "ResourceManager.getResource() exception new: " + ee); 367 368 throw ee; 369 } 370 } 371 372 return resource; 373 } 374 375 389 protected Resource loadResource(String resourceName, int resourceType, String encoding ) 390 throws ResourceNotFoundException, ParseErrorException, Exception 391 { 392 Resource resource = ResourceFactory.getResource(resourceName, resourceType); 393 394 resource.setRuntimeServices( rsvc ); 395 396 resource.setName( resourceName ); 397 resource.setEncoding( encoding ); 398 399 406 407 long howOldItWas = 0; 409 ResourceLoader resourceLoader = null; 410 411 for (int i = 0; i < resourceLoaders.size(); i++) 412 { 413 resourceLoader = (ResourceLoader) resourceLoaders.get(i); 414 resource.setResourceLoader(resourceLoader); 415 416 420 421 try 422 { 423 if (resource.process()) 424 { 425 433 434 if ( logWhenFound ) 435 { 436 rsvc.info("ResourceManager : found " + resourceName + 437 " with loader " + resourceLoader.getClassName() ); 438 } 439 440 howOldItWas = resourceLoader.getLastModified( resource ); 441 break; 442 } 443 } 444 catch( ResourceNotFoundException rnfe ) 445 { 446 450 } 451 } 452 453 456 if (resource.getData() == null) 457 { 458 throw new ResourceNotFoundException( 459 "Unable to find resource '" + resourceName + "'"); 460 } 461 462 465 466 resource.setLastModified( howOldItWas ); 467 468 resource.setModificationCheckInterval( 469 resourceLoader.getModificationCheckInterval()); 470 471 resource.touch(); 472 473 return resource; 474 } 475 476 491 protected void refreshResource( Resource resource, String encoding ) 492 throws ResourceNotFoundException, ParseErrorException, Exception 493 { 494 502 if ( resource.requiresChecking() ) 503 { 504 507 508 resource.touch(); 509 510 if( resource.isSourceModified() ) 511 { 512 517 518 if (!resource.getEncoding().equals( encoding ) ) 519 { 520 rsvc.error("Declared encoding for template '" + resource.getName() 521 + "' is different on reload. Old = '" + resource.getEncoding() 522 + "' New = '" + encoding ); 523 524 resource.setEncoding( encoding ); 525 } 526 527 531 long howOldItWas = resource.getResourceLoader().getLastModified( resource ); 532 533 536 537 resource.process(); 538 539 543 544 resource.setLastModified( howOldItWas ); 545 } 546 } 547 } 548 549 567 public Resource getResource(String resourceName, int resourceType ) 568 throws ResourceNotFoundException, ParseErrorException, Exception 569 { 570 return getResource( resourceName, resourceType, RuntimeConstants.ENCODING_DEFAULT); 571 } 572 573 582 public String getLoaderNameForResource(String resourceName ) 583 { 584 ResourceLoader resourceLoader = null; 585 586 589 for (int i = 0; i < resourceLoaders.size(); i++) 590 { 591 resourceLoader = (ResourceLoader) resourceLoaders.get(i); 592 593 InputStream is = null; 594 595 599 try 600 { 601 is=resourceLoader.getResourceStream( resourceName ); 602 603 if( is != null) 604 { 605 return resourceLoader.getClass().toString(); 606 } 607 } 608 catch( ResourceNotFoundException e) 609 { 610 613 } 614 finally 615 { 616 620 if (is != null) 621 { 622 try 623 { 624 is.close(); 625 } 626 catch( IOException ioe) 627 { 628 } 629 } 630 } 631 } 632 633 return null; 634 } 635 } 636 637 638 | Popular Tags |