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.servicemix.components.cache;
018:
019: import java.util.Map;
020:
021: import javax.jbi.JBIException;
022: import javax.jbi.messaging.Fault;
023: import javax.jbi.messaging.InOut;
024: import javax.jbi.messaging.MessageExchange;
025: import javax.jbi.messaging.MessagingException;
026: import javax.jbi.messaging.NormalizedMessage;
027:
028: import org.apache.servicemix.components.util.TransformComponentSupport;
029: import org.apache.servicemix.expression.Expression;
030: import org.apache.servicemix.expression.PropertyExpression;
031: import org.apache.servicemix.jbi.NoOutMessageAvailableException;
032:
033: /**
034: * Implements a caching layer on top of a service invocation to avoid calling an expensive remote service too often.
035: * The cache can be a simple Map based cache or a full <a href="http://www.jcp.org/en/jsr/detail?id=107">JCache</a> instance.
036: *
037: * @version $Revision: 426415 $
038: */
039: public class CacheComponent extends TransformComponentSupport {
040:
041: public static final PropertyExpression KEY_PROPERTY_EXPRESSION = new PropertyExpression(
042: "org.apache.servicemix.key");
043:
044: private Map cache;
045: private Expression keyExpression = KEY_PROPERTY_EXPRESSION;
046:
047: public Map getCache() {
048: return cache;
049: }
050:
051: public void setCache(Map cache) {
052: this .cache = cache;
053: }
054:
055: public Expression getKeyExpression() {
056: return keyExpression;
057: }
058:
059: public void setKeyExpression(Expression keyExpression) {
060: this .keyExpression = keyExpression;
061: }
062:
063: // Implementation methods
064: //-------------------------------------------------------------------------
065: protected void init() throws JBIException {
066: super .init();
067: if (cache == null) {
068: throw new JBIException("You must specify a cache property");
069: }
070: }
071:
072: protected boolean transform(MessageExchange exchange,
073: NormalizedMessage in, NormalizedMessage out)
074: throws MessagingException {
075: Object key = keyExpression.evaluate(exchange, in);
076: if (key != null) {
077: NormalizedMessage message = (NormalizedMessage) cache
078: .get(key);
079: if (message != null) {
080: getMessageTransformer().transform(exchange, message,
081: out);
082: return true;
083: }
084: }
085:
086: InOut inOut = getExchangeFactory().createInOutExchange();
087: NormalizedMessage request = inOut.createMessage();
088: getMessageTransformer().transform(exchange, in, request);
089: inOut.setInMessage(request);
090: getDeliveryChannel().sendSync(inOut);
091:
092: NormalizedMessage response = inOut.getOutMessage();
093: Fault fault = inOut.getFault();
094: Exception error = inOut.getError();
095: if (fault != null) {
096: fail(exchange, fault);
097: } else if (error != null) {
098: fail(exchange, error);
099: } else if (response != null) {
100: getMessageTransformer().transform(exchange, response, out);
101:
102: if (key != null) {
103: cache.put(key, response);
104: }
105: } else {
106: throw new NoOutMessageAvailableException(exchange);
107: }
108: return true;
109: }
110:
111: }
|