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.cache;
018    
019    import org.apache.camel.Component;
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.Message;
023    import org.apache.camel.Processor;
024    import org.apache.camel.Producer;
025    import org.apache.camel.impl.DefaultEndpoint;
026    import org.apache.camel.impl.DefaultExchange;
027    import org.apache.camel.impl.DefaultMessage;
028    import org.apache.camel.util.ObjectHelper;
029    
030    public class CacheEndpoint extends DefaultEndpoint {
031        private CacheConfiguration config;
032        private CacheManagerFactory cacheManagerFactory;
033    
034        public CacheEndpoint() {
035        }
036    
037        public CacheEndpoint(String endpointUri, Component component, CacheConfiguration config,
038                             CacheManagerFactory cacheManagerFactory) {
039            super(endpointUri, component);
040            this.config = config;
041            this.cacheManagerFactory = cacheManagerFactory;
042        }
043    
044        public Consumer createConsumer(Processor processor) throws Exception {
045            ObjectHelper.notNull(config, "config");
046            ObjectHelper.notNull(cacheManagerFactory, "cacheManagerFactory");
047            return new CacheConsumer(this, processor, config);
048        }
049    
050        public Producer createProducer() throws Exception {
051            ObjectHelper.notNull(config, "config");
052            ObjectHelper.notNull(cacheManagerFactory, "cacheManagerFactory");
053            return new CacheProducer(this, config);
054        }
055    
056        public boolean isSingleton() {
057            return true;
058        }
059    
060        public CacheConfiguration getConfig() {
061            return config;
062        }
063    
064        public void setConfig(CacheConfiguration config) {
065            this.config = config;
066        }
067    
068        public CacheManagerFactory getCacheManagerFactory() {
069            return cacheManagerFactory;
070        }
071    
072        public void setCacheManagerFactory(CacheManagerFactory cacheManagerFactory) {
073            this.cacheManagerFactory = cacheManagerFactory;
074        }
075    
076        public Exchange createCacheExchange(String operation, String key, Object value) {
077            Exchange exchange = new DefaultExchange(this.getCamelContext(), getExchangePattern());
078            Message message = new DefaultMessage();
079            message.setHeader("CACHE_OPERATION", operation);
080            message.setHeader("CACHE_KEY", key);
081            message.setBody(value);
082            exchange.setIn(message);
083            return exchange;
084        }
085        
086    }