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;
018    
019    import java.util.Date;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import org.apache.camel.Exchange;
024    
025    public class HazelcastComponentHelper {
026    
027        private final HashMap<String, Integer> mapping = new HashMap<String, Integer>();
028    
029        public HazelcastComponentHelper() {
030            this.init();
031        }
032    
033        public static void copyHeaders(Exchange ex) {
034            // get in headers
035            Map<String, Object> headers = ex.getIn().getHeaders();
036    
037            // delete item id
038            if (headers.containsKey(HazelcastConstants.OBJECT_ID)) {
039                headers.remove(HazelcastConstants.OBJECT_ID);
040            }
041    
042            if (headers.containsKey(HazelcastConstants.OPERATION)) {
043                headers.remove(HazelcastConstants.OPERATION);
044            }
045    
046            // set out headers
047            ex.getOut().setHeaders(headers);
048        }
049    
050        public static void setListenerHeaders(Exchange ex, String listenerType, String listenerAction, String cacheName) {
051            ex.getOut().setHeader(HazelcastConstants.CACHE_NAME, cacheName);
052            HazelcastComponentHelper.setListenerHeaders(ex, listenerType, listenerAction);
053        }
054    
055        public static void setListenerHeaders(Exchange ex, String listenerType, String listenerAction) {
056            ex.getOut().setHeader(HazelcastConstants.LISTENER_ACTION, listenerAction);
057            ex.getOut().setHeader(HazelcastConstants.LISTENER_TYPE, listenerType);
058            ex.getOut().setHeader(HazelcastConstants.LISTENER_TIME, new Date().getTime());
059        }
060    
061        /**
062         * Allows the use of speaking operation names (e.g. for usage in Spring DSL)
063         */
064        public int lookupOperationNumber(String operation) {
065            if (this.mapping.containsKey(operation)) {
066                return this.mapping.get(operation);
067            } else {
068                throw new IllegalArgumentException(String.format("Operation '%s' is not supported by this component.", operation));
069            }
070        }
071    
072        private void init() {
073            // fill map with values
074            this.mapping.put("put", HazelcastConstants.PUT_OPERATION);
075            this.mapping.put("delete", HazelcastConstants.DELETE_OPERATION);
076            this.mapping.put("get", HazelcastConstants.GET_OPERATION);
077            this.mapping.put("update", HazelcastConstants.UPDATE_OPERATION);
078            this.mapping.put("query", HazelcastConstants.QUERY_OPERATION);
079    
080            // multimap
081            this.mapping.put("removevalue", HazelcastConstants.REMOVEVALUE_OPERATION);
082    
083            // atomic numbers
084            this.mapping.put("increment", HazelcastConstants.INCREMENT_OPERATION);
085            this.mapping.put("decrement", HazelcastConstants.DECREMENT_OPERATION);
086            this.mapping.put("setvalue", HazelcastConstants.SETVALUE_OPERATION);
087            this.mapping.put("destroy", HazelcastConstants.DESTROY_OPERATION);
088    
089            // queue
090            this.mapping.put("add", HazelcastConstants.ADD_OPERATION);
091            this.mapping.put("offer", HazelcastConstants.OFFER_OPERATION);
092            this.mapping.put("peek", HazelcastConstants.PEEK_OPERATION);
093            this.mapping.put("poll", HazelcastConstants.POLL_OPERATION);
094        }
095    
096    }