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.api.management.ManagedResource;
025    import org.apache.camel.api.management.mbean.ManagedResourceEndpointMBean;
026    import org.apache.camel.converter.IOConverter;
027    import org.apache.camel.impl.ProcessorEndpoint;
028    import org.apache.camel.util.IOHelper;
029    import org.apache.camel.util.ResourceHelper;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
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    @ManagedResource(description = "Managed ResourceEndpoint")
038    public abstract class ResourceEndpoint extends ProcessorEndpoint implements ManagedResourceEndpointMBean {
039        protected final transient Logger log = LoggerFactory.getLogger(getClass());
040        private String resourceUri;
041        private boolean contentCache;
042        private volatile byte[] buffer;
043    
044        public ResourceEndpoint() {
045        }
046    
047        public ResourceEndpoint(String endpointUri, Component component, String resourceUri) {
048            super(endpointUri, component);
049            this.resourceUri = resourceUri;
050        }
051    
052        /**
053         * Gets the resource as an input stream considering the cache flag as well.
054         * <p/>
055         * If cache is enabled then the resource content is cached in an internal buffer and this content is
056         * returned to avoid loading the resource over and over again.
057         *
058         * @return the input stream
059         * @throws IOException is thrown if error loading the content of the resource to the local cache buffer
060         */
061        public InputStream getResourceAsInputStream() throws IOException {
062            // try to get the resource input stream
063            InputStream is;
064            if (isContentCache()) {
065                synchronized (this) {
066                    if (buffer == null) {
067                        log.debug("Reading resource: {} into the content cache", resourceUri);
068                        is = getResourceAsInputStreamWithoutCache();
069                        buffer = IOConverter.toBytes(is);
070                        IOHelper.close(is, resourceUri, log);
071                    }
072                }
073                log.debug("Using resource: {} from the content cache", resourceUri);
074                return new ByteArrayInputStream(buffer);
075            }
076    
077            return getResourceAsInputStreamWithoutCache();
078        }
079    
080        protected InputStream getResourceAsInputStreamWithoutCache() throws IOException {
081            return loadResource(resourceUri);
082        }
083    
084        /**
085         * Loads the given resource.
086         *
087         * @param uri uri of the resource.
088         * @return the loaded resource
089         * @throws IOException is thrown if resource is not found or cannot be loaded
090         */
091        protected InputStream loadResource(String uri) throws IOException {
092            return ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), uri);
093        }
094    
095        public boolean isContentCache() {
096            return contentCache;
097        }
098        
099        public void clearContentCache() {
100            log.debug("Clearing resource: {} from the content cache", resourceUri);
101            buffer = null;
102        }
103    
104        /**
105         * Sets whether to use resource content cache or not - default is <tt>false</tt>.
106         *
107         * @see #getResourceAsInputStream()
108         */
109        public void setContentCache(boolean contentCache) {
110            this.contentCache = contentCache;
111        }
112    
113        public String getResourceUri() {
114            return resourceUri;
115        }
116    
117        public void setResourceUri(String resourceUri) {
118            this.resourceUri = resourceUri;
119        }
120    }