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