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