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 org.apache.camel.Component;
020 import org.apache.camel.Processor;
021 import org.apache.camel.impl.ProcessorEndpoint;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.springframework.core.io.DefaultResourceLoader;
025 import org.springframework.core.io.Resource;
026 import org.springframework.core.io.ResourceLoader;
027
028 /**
029 * A useful base class for endpoints which depend on a resource
030 * such as things like Velocity or XQuery based components
031 *
032 * @version $Revision: 41334 $
033 */
034 public abstract class ResourceBasedEndpoint extends ProcessorEndpoint {
035 protected final transient Log log = LogFactory.getLog(getClass());
036 private final String resourceUri;
037 private ResourceLoader resourceLoader = new DefaultResourceLoader();
038 private Resource resource;
039
040 public ResourceBasedEndpoint(String endpointUri, Component component, String resourceUri, Processor processor) {
041 super(endpointUri, component, processor);
042 this.resourceUri = resourceUri;
043 }
044
045 protected ResourceBasedEndpoint(String endpointUri, Processor processor, String resourceUri) {
046 super(endpointUri, processor);
047 this.resourceUri = resourceUri;
048 }
049
050 public Resource getResource() {
051 if (resource == null) {
052 resource = getResourceLoader().getResource(resourceUri);
053 if (resource == null) {
054 throw new IllegalArgumentException("Could not find resource for URI: " + resourceUri + " using: " + getResourceLoader());
055 }
056 }
057 return resource;
058 }
059
060 public ResourceLoader getResourceLoader() {
061 return resourceLoader;
062 }
063
064 public void setResourceLoader(ResourceLoader resourceLoader) {
065 this.resourceLoader = resourceLoader;
066 }
067 }