1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.console.command.store.amq; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.InputStream; 21 import java.util.HashMap; 22 23 import org.apache.commons.collections.ExtendedProperties; 24 import org.apache.velocity.exception.ResourceNotFoundException; 25 import org.apache.velocity.runtime.RuntimeServices; 26 import org.apache.velocity.runtime.resource.Resource; 27 import org.apache.velocity.runtime.resource.loader.FileResourceLoader; 28 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 29 30 public class CustomResourceLoader extends ResourceLoader { 31 32 private final static ThreadLocal<HashMap<String, String>> resourcesTL = new ThreadLocal<HashMap<String, String>>(); 33 private final FileResourceLoader fileResourceLoader = new FileResourceLoader(); 34 35 @Override 36 public void commonInit(RuntimeServices rs, ExtendedProperties configuration) { 37 super.commonInit(rs, configuration); 38 fileResourceLoader.commonInit(rs, configuration); 39 } 40 41 public void init( ExtendedProperties configuration) 42 { 43 fileResourceLoader.init(configuration); 44 } 45 46 /** 47 */ 48 public synchronized InputStream getResourceStream( String name ) 49 throws ResourceNotFoundException 50 { 51 InputStream result = null; 52 53 if (name == null || name.length() == 0) 54 { 55 throw new ResourceNotFoundException ("No template name provided"); 56 } 57 58 String value = null; 59 HashMap<String, String> resources = resourcesTL.get(); 60 if( resources!=null ) { 61 value = resources.get(name); 62 } 63 64 if( value == null ) { 65 result = this.fileResourceLoader.getResourceStream(name); 66 } else { 67 try 68 { 69 result = new ByteArrayInputStream(value.getBytes()); 70 } 71 catch( Exception e ) 72 { 73 throw new ResourceNotFoundException( e.getMessage() ); 74 } 75 } 76 return result; 77 } 78 79 public boolean isSourceModified(Resource resource) 80 { 81 return false; 82 } 83 84 public long getLastModified(Resource resource) 85 { 86 return 0; 87 } 88 89 static public HashMap<String, String> getResources() { 90 return resourcesTL.get(); 91 } 92 93 static public void setResources(HashMap<String, String> arg0) { 94 resourcesTL.set(arg0); 95 } 96 97 }