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 java.io.InputStream;
020 import java.io.Serializable;
021
022 import net.sf.ehcache.CacheException;
023 import net.sf.ehcache.Ehcache;
024 import net.sf.ehcache.Element;
025
026 import org.apache.camel.Endpoint;
027 import org.apache.camel.Exchange;
028 import org.apache.camel.NoTypeConversionAvailableException;
029 import org.apache.camel.impl.DefaultProducer;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class CacheProducer extends DefaultProducer {
034 private static final transient Logger LOG = LoggerFactory.getLogger(CacheProducer.class);
035 private CacheConfiguration config;
036 private Ehcache cache;
037
038 public CacheProducer(Endpoint endpoint, CacheConfiguration config) throws Exception {
039 super(endpoint);
040 this.config = config;
041 }
042
043 @Override
044 protected void doStart() throws Exception {
045 super.doStart();
046 }
047
048 @Override
049 public CacheEndpoint getEndpoint() {
050 return (CacheEndpoint) super.getEndpoint();
051 }
052
053 public void process(Exchange exchange) throws Exception {
054 LOG.trace("Cache Name: {}", config.getCacheName());
055
056 cache = getEndpoint().initializeCache();
057
058 String key = exchange.getIn().getHeader(CacheConstants.CACHE_KEY, String.class);
059 String operation = exchange.getIn().getHeader(CacheConstants.CACHE_OPERATION, String.class);
060
061 if (operation == null) {
062 throw new CacheException(CacheConstants.CACHE_OPERATION + " header not specified in message");
063 }
064 if ((key == null) && (!operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETEALL))) {
065 throw new CacheException(CacheConstants.CACHE_KEY + " is not specified in message header or endpoint URL.");
066 }
067
068 performCacheOperation(exchange, operation, key);
069
070 //cleanup the cache headers
071 exchange.getIn().removeHeader(CacheConstants.CACHE_KEY);
072 exchange.getIn().removeHeader(CacheConstants.CACHE_OPERATION);
073 }
074
075 private void performCacheOperation(Exchange exchange, String operation, String key) throws Exception {
076 Object element;
077
078 if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_ADD)) {
079 LOG.debug("Adding an element with key {} into the Cache", key);
080 element = createElementFromBody(exchange, CacheConstants.CACHE_OPERATION_ADD);
081 cache.put(new Element(key, element));
082 } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_UPDATE)) {
083 LOG.debug("Updating an element with key {} into the Cache", key);
084 element = createElementFromBody(exchange, CacheConstants.CACHE_OPERATION_UPDATE);
085 cache.put(new Element(key, element));
086 } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETEALL)) {
087 LOG.debug("Deleting All elements from the Cache");
088 cache.removeAll();
089 } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETE)) {
090 LOG.debug("Deleting an element with key {} into the Cache", key);
091 cache.remove(key);
092 } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_GET)) {
093 LOG.debug("Quering an element with key {} from the Cache", key);
094 if (cache.isKeyInCache(key) && cache.get(key) != null) {
095 exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
096 exchange.getIn().setBody(cache.get(key).getValue());
097 } else {
098 exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
099 }
100 } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_CHECK)) {
101 LOG.debug("Querying an element with key {} from the Cache", key);
102 if (cache.isKeyInCache(key)) {
103 exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
104 } else {
105 exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
106 }
107 } else {
108 throw new CacheException(CacheConstants.CACHE_OPERATION + " " + operation + " is not supported.");
109 }
110 }
111
112 private Object createElementFromBody(Exchange exchange, String cacheOperation) throws NoTypeConversionAvailableException {
113 Object element;
114 Object body = exchange.getIn().getBody();
115 if (body == null) {
116 throw new CacheException("Body cannot be null for operation " + cacheOperation);
117 } else if (body instanceof Serializable) {
118 element = body;
119 } else {
120 InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
121 // Read InputStream into a byte[] buffer
122 element = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, is);
123 }
124 return element;
125 }
126
127 }