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: 743658 $
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 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 if (contentCache) {
088 synchronized (resource) {
089 if (buffer == null) {
090 if (log.isDebugEnabled()) {
091 log.debug("Reading resource: " + resourceUri + " into the content cache");
092 }
093 buffer = IOConverter.toBytes(resource.getInputStream());
094 }
095 }
096 if (log.isDebugEnabled()) {
097 log.debug("Using resource: " + resourceUri + " from the content cache");
098 }
099 return new ByteArrayInputStream(buffer);
100 }
101 return resource.getInputStream();
102 }
103
104 public boolean isContentCache() {
105 return contentCache;
106 }
107
108 /**
109 * Sets wether to use resource content cache or not - default is <tt>false</tt>.
110 *
111 * @see #getResourceAsInputStream()
112 */
113 public void setContentCache(boolean contentCache) {
114 this.contentCache = contentCache;
115 }
116
117 public ResourceLoader getResourceLoader() {
118 return resourceLoader;
119 }
120
121 public void setResourceLoader(ResourceLoader resourceLoader) {
122 this.resourceLoader = resourceLoader;
123 }
124
125 public String getResourceUri() {
126 return resourceUri;
127 }
128
129 public void setResourceUri(String resourceUri) {
130 this.resourceUri = resourceUri;
131 }
132
133 }