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.map; 018 019 import java.util.Collection; 020 import java.util.Map; 021 022 import com.hazelcast.core.Hazelcast; 023 import com.hazelcast.core.IMap; 024 import com.hazelcast.query.SqlPredicate; 025 026 import org.apache.camel.Exchange; 027 import org.apache.camel.component.hazelcast.HazelcastComponentHelper; 028 import org.apache.camel.component.hazelcast.HazelcastConstants; 029 import org.apache.camel.impl.DefaultProducer; 030 031 public class HazelcastMapProducer extends DefaultProducer { 032 033 private final IMap<String, Object> cache; 034 private final HazelcastComponentHelper helper = new HazelcastComponentHelper(); 035 036 public HazelcastMapProducer(HazelcastMapEndpoint endpoint, String cacheName) { 037 super(endpoint); 038 this.cache = Hazelcast.getMap(cacheName); 039 } 040 041 public void process(Exchange exchange) throws Exception { 042 043 Map<String, Object> headers = exchange.getIn().getHeaders(); 044 045 // get header parameters 046 String oid = null; 047 int operation = -1; 048 String query = null; 049 050 if (headers.containsKey(HazelcastConstants.OBJECT_ID)) { 051 oid = (String) headers.get(HazelcastConstants.OBJECT_ID); 052 } 053 054 if (headers.containsKey(HazelcastConstants.OPERATION)) { 055 056 // producer allows int (HazelcastConstants) and string values 057 if (headers.get(HazelcastConstants.OPERATION) instanceof String) { 058 operation = helper.lookupOperationNumber((String) headers.get(HazelcastConstants.OPERATION)); 059 } else { 060 operation = (Integer) headers.get(HazelcastConstants.OPERATION); 061 } 062 } 063 064 if (headers.containsKey(HazelcastConstants.QUERY)) { 065 query = (String) headers.get(HazelcastConstants.QUERY); 066 } 067 068 switch (operation) { 069 070 case HazelcastConstants.PUT_OPERATION: 071 this.put(oid, exchange); 072 break; 073 074 case HazelcastConstants.GET_OPERATION: 075 this.get(oid, exchange); 076 break; 077 078 case HazelcastConstants.DELETE_OPERATION: 079 this.delete(oid); 080 break; 081 082 case HazelcastConstants.UPDATE_OPERATION: 083 this.update(oid, exchange); 084 break; 085 086 case HazelcastConstants.QUERY_OPERATION: 087 this.query(query, exchange); 088 break; 089 090 default: 091 throw new IllegalArgumentException(String.format("The value '%s' is not allowed for parameter '%s' on the MAP cache.", operation, HazelcastConstants.OPERATION)); 092 } 093 094 // finally copy headers 095 HazelcastComponentHelper.copyHeaders(exchange); 096 097 } 098 099 /** 100 * query map with a sql like syntax (see http://www.hazelcast.com/) 101 */ 102 private void query(String query, Exchange exchange) { 103 Collection<Object> result = this.cache.values(new SqlPredicate(query)); 104 exchange.getOut().setBody(result); 105 } 106 107 /** 108 * update an object in your cache (the whole object will be replaced) 109 */ 110 private void update(String oid, Exchange exchange) { 111 Object body = exchange.getIn().getBody(); 112 this.cache.lock(oid); 113 this.cache.replace(oid, body); 114 this.cache.unlock(oid); 115 } 116 117 /** 118 * remove an object from the cache 119 */ 120 private void delete(String oid) { 121 this.cache.remove(oid); 122 } 123 124 /** 125 * find an object by the given id and give it back 126 */ 127 private void get(String oid, Exchange exchange) { 128 exchange.getOut().setBody(this.cache.get(oid)); 129 } 130 131 /** 132 * put a new object into the cache 133 */ 134 private void put(String oid, Exchange exchange) { 135 Object body = exchange.getIn().getBody(); 136 this.cache.put(oid, body); 137 } 138 }