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.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    public class CacheEndpoint extends DefaultEndpoint {
032        private static final transient Log LOG = LogFactory.getLog(CacheEndpoint.class);
033        CacheConfiguration config;
034        
035        public CacheEndpoint(String endpointUri, Component component, CacheConfiguration config) {
036            super(endpointUri, component);
037            this.config = config;
038        }
039    
040        public Consumer createConsumer(Processor processor) throws Exception {
041            return new CacheConsumer(this, processor, config);
042        }
043    
044        public Producer createProducer() throws Exception {
045            return new CacheProducer(this, config);
046        }
047    
048        public boolean isSingleton() {
049            return false;
050        }
051    
052        public CacheConfiguration getConfig() {
053            return config;
054        }
055    
056        public void setConfig(CacheConfiguration config) {
057            this.config = config;
058        }
059    
060        public Exchange createCacheExchange(String operation, String key, Object value) {
061            Exchange exchange = new DefaultExchange(this.getCamelContext(), getExchangePattern());
062            Message message = new DefaultMessage();
063            message.setHeader("CACHE_OPERATION", operation);
064            message.setHeader("CACHE_KEY", key);
065            message.setBody(value);
066            exchange.setIn(message);
067            return exchange;
068        }
069        
070    }