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: 935158 $ 038 */ 039 public abstract class ResourceBasedEndpoint extends ProcessorEndpoint { 040 protected final transient Log log = LogFactory.getLog(getClass()); 041 private String resourceUri; 042 private ResourceLoader resourceLoader = new DefaultResourceLoader(); 043 private Resource resource; 044 private boolean contentCache; 045 private byte[] buffer; 046 047 protected ResourceBasedEndpoint() { 048 } 049 050 public ResourceBasedEndpoint(String endpointUri, Component component, String resourceUri, Processor processor) { 051 super(endpointUri, component, processor); 052 this.resourceUri = resourceUri; 053 } 054 055 protected ResourceBasedEndpoint(String endpointUri, Processor processor, String resourceUri) { 056 super(endpointUri, processor); 057 this.resourceUri = resourceUri; 058 } 059 060 public Resource getResource() { 061 if (resource == null) { 062 if (log.isDebugEnabled()) { 063 log.debug("Loading resource: " + resourceUri + " using: " + getResourceLoader()); 064 } 065 066 resource = getResourceLoader().getResource(resourceUri); 067 068 if (resource == null) { 069 throw new IllegalArgumentException("Could not find resource for URI: " + resourceUri + " using: " + getResourceLoader()); 070 } 071 } 072 return resource; 073 } 074 075 /** 076 * Gets the resource as an input stream considering the cache flag as well. 077 * <p/> 078 * If cache is enabled then the resource content is cached in an internal buffer and this content is 079 * returned to avoid loading the resource over and over again. 080 * 081 * @return the input stream 082 * @throws IOException is thrown if error loading the content of the resource to the local cache buffer 083 */ 084 public InputStream getResourceAsInputStream() throws IOException { 085 if (resource == null) { 086 // get the resource if not already done 087 resource = getResource(); 088 } 089 // try to get the resource inputstream 090 InputStream is = null; 091 if (contentCache) { 092 synchronized (resource) { 093 if (buffer == null) { 094 if (log.isDebugEnabled()) { 095 log.debug("Reading resource: " + resourceUri + " into the content cache"); 096 } 097 is = getResourceAsInputStreamWithoutCache(); 098 099 buffer = IOConverter.toBytes(is); 100 } 101 } 102 if (log.isDebugEnabled()) { 103 log.debug("Using resource: " + resourceUri + " from the content cache"); 104 } 105 return new ByteArrayInputStream(buffer); 106 } 107 return getResourceAsInputStreamWithoutCache(); 108 } 109 110 111 protected InputStream getResourceAsInputStreamWithoutCache() throws IOException { 112 InputStream result = null; 113 try { 114 result = resource.getInputStream(); 115 } catch (IOException exception) { 116 // Using the camelContext classResolver to load the resource as a fall back 117 result = getCamelContext().getClassResolver().loadResourceAsStream(resourceUri); 118 if (result == null) { 119 log.warn("Cannot get the resource: " + resourceUri + "from the camelContext ClassResolver"); 120 throw exception; 121 } 122 } 123 return result; 124 } 125 126 public boolean isContentCache() { 127 return contentCache; 128 } 129 130 /** 131 * Sets whether to use resource content cache or not - default is <tt>false</tt>. 132 * 133 * @see #getResourceAsInputStream() 134 */ 135 public void setContentCache(boolean contentCache) { 136 this.contentCache = contentCache; 137 } 138 139 public ResourceLoader getResourceLoader() { 140 return resourceLoader; 141 } 142 143 public void setResourceLoader(ResourceLoader resourceLoader) { 144 this.resourceLoader = resourceLoader; 145 } 146 147 public String getResourceUri() { 148 return resourceUri; 149 } 150 151 public void setResourceUri(String resourceUri) { 152 this.resourceUri = resourceUri; 153 } 154 155 }