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