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.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        
046        /**
047         */
048        public synchronized InputStream getResourceStream( String name )
049            throws ResourceNotFoundException
050        {
051            InputStream result = null;
052            
053            if (name == null || name.length() == 0)
054            {
055                throw new ResourceNotFoundException ("No template name provided");
056            }
057            
058            String value = null;
059            HashMap<String, String> resources = resourcesTL.get();
060            if( resources!=null ) {
061                    value = resources.get(name);
062            }
063            
064            if( value == null ) {
065                    result = this.fileResourceLoader.getResourceStream(name);
066            } else {
067                try 
068                {
069                    result = new ByteArrayInputStream(value.getBytes());
070                }
071                catch( Exception e )
072                {
073                    throw new ResourceNotFoundException( e.getMessage() );
074                }
075            }
076            return result;
077        }
078        
079        public boolean isSourceModified(Resource resource)
080        {
081            return false;
082        }
083    
084        public long getLastModified(Resource resource)
085        {
086            return 0;
087        }
088    
089            static public HashMap<String, String> getResources() {
090                    return resourcesTL.get();
091            }
092    
093            static public void setResources(HashMap<String, String> arg0) {
094                    resourcesTL.set(arg0);
095            }
096    
097    }