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