001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.activemq.console.command.store.amq;
018
019 import java.io.ByteArrayInputStream;
020 import java.io.InputStream;
021 import java.util.HashMap;
022
023 import org.apache.commons.collections.ExtendedProperties;
024 import org.apache.velocity.exception.ResourceNotFoundException;
025 import org.apache.velocity.runtime.RuntimeServices;
026 import org.apache.velocity.runtime.resource.Resource;
027 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
028 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
029
030 public class CustomResourceLoader extends ResourceLoader {
031
032 private final static ThreadLocal<HashMap<String, String>> resourcesTL = new ThreadLocal<HashMap<String, String>>();
033 private final FileResourceLoader fileResourceLoader = new FileResourceLoader();
034
035 @Override
036 public void commonInit(RuntimeServices rs, ExtendedProperties configuration) {
037 super.commonInit(rs, configuration);
038 fileResourceLoader.commonInit(rs, configuration);
039 }
040
041 public void init( ExtendedProperties configuration)
042 {
043 fileResourceLoader.init(configuration);
044
045 // AMQ-3665: Turn on template caching as otherwise the journal reader
046 // could run out of memory on large journal files
047 this.setCachingOn(true);
048 }
049
050 /**
051 */
052 public synchronized InputStream getResourceStream( String name )
053 throws ResourceNotFoundException
054 {
055 InputStream result = null;
056
057 if (name == null || name.length() == 0)
058 {
059 throw new ResourceNotFoundException ("No template name provided");
060 }
061
062 String value = null;
063 HashMap<String, String> resources = resourcesTL.get();
064 if( resources!=null ) {
065 value = resources.get(name);
066 }
067
068 if( value == null ) {
069 result = this.fileResourceLoader.getResourceStream(name);
070 } else {
071 try
072 {
073 result = new ByteArrayInputStream(value.getBytes());
074 }
075 catch( Exception e )
076 {
077 throw new ResourceNotFoundException( e.getMessage() );
078 }
079 }
080 return result;
081 }
082
083 public boolean isSourceModified(Resource resource)
084 {
085 return false;
086 }
087
088 public long getLastModified(Resource resource)
089 {
090 return 0;
091 }
092
093 static public HashMap<String, String> getResources() {
094 return resourcesTL.get();
095 }
096
097 static public void setResources(HashMap<String, String> arg0) {
098 resourcesTL.set(arg0);
099 }
100
101 }