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.camel.component; 018 019 import java.io.ByteArrayInputStream; 020 import java.io.IOException; 021 import java.io.InputStream; 022 023 import org.apache.camel.Component; 024 import org.apache.camel.Processor; 025 import org.apache.camel.converter.IOConverter; 026 import org.apache.camel.impl.ProcessorEndpoint; 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 import org.springframework.core.io.DefaultResourceLoader; 030 import org.springframework.core.io.Resource; 031 import org.springframework.core.io.ResourceLoader; 032 033 /** 034 * A useful base class for endpoints which depend on a resource 035 * such as things like Velocity or XQuery based components. 036 * 037 * @version $Revision: 736637 $ 038 */ 039 public abstract class ResourceBasedEndpoint extends ProcessorEndpoint { 040 protected final transient Log log = LogFactory.getLog(getClass()); 041 private final String resourceUri; 042 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 043 private Resource resource; 044 private boolean contentCache; 045 private byte[] buffer; 046 047 public ResourceBasedEndpoint(String endpointUri, Component component, String resourceUri, Processor processor) { 048 super(endpointUri, component, processor); 049 this.resourceUri = resourceUri; 050 } 051 052 protected ResourceBasedEndpoint(String endpointUri, Processor processor, String resourceUri) { 053 super(endpointUri, processor); 054 this.resourceUri = resourceUri; 055 } 056 057 public Resource getResource() { 058 if (resource == null) { 059 if (log.isDebugEnabled()) { 060 log.debug("Loading resource: " + resourceUri + " using: " + getResourceLoader()); 061 } 062 resource = getResourceLoader().getResource(resourceUri); 063 if (resource == null) { 064 throw new IllegalArgumentException("Could not find resource for URI: " + resourceUri + " using: " + getResourceLoader()); 065 } 066 } 067 return resource; 068 } 069 070 public boolean isContentCache() { 071 return contentCache; 072 } 073 074 /** 075 * Sets wether to use resource content cache or not - default is <tt>false</tt>. 076 * 077 * @see #getResourceAsInputStream() 078 */ 079 public void setContentCache(boolean contentCache) { 080 this.contentCache = contentCache; 081 } 082 083 /** 084 * Gets the resource as an input stream considering the cache flag as well. 085 * <p/> 086 * If cache is enabled then the resource content is cached in an internal buffer and this content is 087 * returned to avoid loading the resource over and over again. 088 * 089 * @return the input stream 090 * @throws IOException is thrown if error loading the content of the resource to the local cache buffer 091 */ 092 public InputStream getResourceAsInputStream() throws IOException { 093 if (resource == null) { 094 // get the resource if not already done 095 resource = getResource(); 096 } 097 if (contentCache) { 098 synchronized (resource) { 099 if (buffer == null) { 100 if (log.isDebugEnabled()) { 101 log.debug("Reading resource: " + resourceUri + " into the content cache"); 102 } 103 buffer = IOConverter.toBytes(resource.getInputStream()); 104 } 105 } 106 if (log.isDebugEnabled()) { 107 log.debug("Using resource: " + resourceUri + " from the content cache"); 108 } 109 return new ByteArrayInputStream(buffer); 110 } 111 return resource.getInputStream(); 112 } 113 114 public ResourceLoader getResourceLoader() { 115 return resourceLoader; 116 } 117 118 public void setResourceLoader(ResourceLoader resourceLoader) { 119 this.resourceLoader = resourceLoader; 120 } 121 122 public String getResourceUri() { 123 return resourceUri; 124 } 125 }