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.hazelcast.multimap;
018
019 import java.util.Map;
020
021 import com.hazelcast.core.Hazelcast;
022 import com.hazelcast.core.MultiMap;
023
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.component.hazelcast.HazelcastComponentHelper;
027 import org.apache.camel.component.hazelcast.HazelcastConstants;
028 import org.apache.camel.impl.DefaultProducer;
029
030 public class HazelcastMultimapProducer extends DefaultProducer {
031
032 private final MultiMap<Object, Object> cache;
033 private final HazelcastComponentHelper helper = new HazelcastComponentHelper();
034
035 public HazelcastMultimapProducer(Endpoint endpoint, String cacheName) {
036 super(endpoint);
037 this.cache = Hazelcast.getMultiMap(cacheName);
038 }
039
040 public void process(Exchange exchange) throws Exception {
041
042 Map<String, Object> headers = exchange.getIn().getHeaders();
043
044 // get header parameters
045 String oid = null;
046 int operation = -1;
047
048 if (headers.containsKey(HazelcastConstants.OBJECT_ID)) {
049 oid = (String) headers.get(HazelcastConstants.OBJECT_ID);
050 }
051
052 if (headers.containsKey(HazelcastConstants.OPERATION)) {
053 if (headers.get(HazelcastConstants.OPERATION) instanceof String) {
054 operation = helper.lookupOperationNumber((String) headers.get(HazelcastConstants.OPERATION));
055 } else {
056 operation = (Integer) headers.get(HazelcastConstants.OPERATION);
057 }
058 }
059
060 switch (operation) {
061 case HazelcastConstants.PUT_OPERATION:
062 this.put(oid, exchange);
063 break;
064
065 case HazelcastConstants.GET_OPERATION:
066 this.get(oid, exchange);
067 break;
068
069 case HazelcastConstants.DELETE_OPERATION:
070 this.delete(oid);
071 break;
072
073 case HazelcastConstants.REMOVEVALUE_OPERATION:
074 this.removevalue(oid, exchange);
075 break;
076
077 default:
078 throw new IllegalArgumentException(String.format("The value '%s' is not allowed for parameter '%s' on the MULTIMAP cache.", operation, HazelcastConstants.OPERATION));
079 }
080
081 // finally copy headers
082 HazelcastComponentHelper.copyHeaders(exchange);
083 }
084
085 private void put(String oid, Exchange exchange) {
086 Object body = exchange.getIn().getBody();
087 this.cache.put(oid, body);
088 }
089
090 private void get(String oid, Exchange exchange) {
091 exchange.getOut().setBody(this.cache.get(oid));
092 }
093
094 private void delete(String oid) {
095 this.cache.remove(oid);
096 }
097
098 private void removevalue(String oid, Exchange exchange) {
099 this.cache.remove(oid, exchange.getIn().getBody());
100 }
101
102 }