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.ManagedAttribute;
025    import org.apache.camel.api.management.ManagedOperation;
026    import org.apache.camel.api.management.ManagedResource;
027    import org.apache.camel.api.management.mbean.ManagedResourceEndpointMBean;
028    import org.apache.camel.converter.IOConverter;
029    import org.apache.camel.impl.ProcessorEndpoint;
030    import org.apache.camel.spi.UriParam;
031    import org.apache.camel.util.IOHelper;
032    import org.apache.camel.util.ResourceHelper;
033    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    /**
037     * A useful base class for endpoints which depend on a resource
038     * such as things like Velocity or XQuery based components.
039     */
040    @ManagedResource(description = "Managed ResourceEndpoint")
041    public abstract class ResourceEndpoint extends ProcessorEndpoint implements ManagedResourceEndpointMBean {
042        protected final Logger log = LoggerFactory.getLogger(getClass());
043        private volatile byte[] buffer;
044        @UriParam
045        private String resourceUri;
046        @UriParam
047        private boolean contentCache;
048    
049        public ResourceEndpoint() {
050        }
051    
052        public ResourceEndpoint(String endpointUri, Component component, String resourceUri) {
053            super(endpointUri, component);
054            this.resourceUri = resourceUri;
055        }
056    
057        /**
058         * Gets the resource as an input stream considering the cache flag as well.
059         * <p/>
060         * If cache is enabled then the resource content is cached in an internal buffer and this content is
061         * returned to avoid loading the resource over and over again.
062         *
063         * @return the input stream
064         * @throws IOException is thrown if error loading the content of the resource to the local cache buffer
065         */
066        public InputStream getResourceAsInputStream() throws IOException {
067            // try to get the resource input stream
068            InputStream is;
069            if (isContentCache()) {
070                synchronized (this) {
071                    if (buffer == null) {
072                        log.debug("Reading resource: {} into the content cache", resourceUri);
073                        is = getResourceAsInputStreamWithoutCache();
074                        buffer = IOConverter.toBytes(is);
075                        IOHelper.close(is, resourceUri, log);
076                    }
077                }
078                log.debug("Using resource: {} from the content cache", resourceUri);
079                return new ByteArrayInputStream(buffer);
080            }
081    
082            return getResourceAsInputStreamWithoutCache();
083        }
084    
085        protected InputStream getResourceAsInputStreamWithoutCache() throws IOException {
086            return loadResource(resourceUri);
087        }
088    
089        /**
090         * Loads the given resource.
091         *
092         * @param uri uri of the resource.
093         * @return the loaded resource
094         * @throws IOException is thrown if resource is not found or cannot be loaded
095         */
096        protected InputStream loadResource(String uri) throws IOException {
097            return ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), uri);
098        }
099    
100        @ManagedAttribute(description = "Whether the resource is cached")
101        public boolean isContentCache() {
102            return contentCache;
103        }
104    
105        @ManagedOperation(description = "Clears the cached resource, forcing to re-load the resource on next request")
106        public void clearContentCache() {
107            log.debug("Clearing resource: {} from the content cache", resourceUri);
108            buffer = null;
109        }
110    
111        public boolean isContentCacheCleared() {
112            return buffer == null;
113        }
114    
115        @ManagedAttribute(description = "Camel context ID")
116        public String getCamelId() {
117            return getCamelContext().getName();
118        }
119    
120        @ManagedAttribute(description = "Camel ManagementName")
121        public String getCamelManagementName() {
122            return getCamelContext().getManagementName();
123        }
124    
125        @ManagedAttribute(description = "Endpoint service state")
126        public String getState() {
127            return getStatus().name();
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 String getResourceUri() {
140            return resourceUri;
141        }
142    
143        public void setResourceUri(String resourceUri) {
144            this.resourceUri = resourceUri;
145        }
146    }