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.queue;
018    
019    import java.util.Map;
020    
021    import com.hazelcast.core.Hazelcast;
022    import com.hazelcast.core.IQueue;
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    /**
031     *
032     */
033    public class HazelcastQueueProducer extends DefaultProducer {
034    
035        private IQueue<Object> queue;
036        private HazelcastComponentHelper helper = new HazelcastComponentHelper();
037    
038        public HazelcastQueueProducer(Endpoint endpoint, String queueName) {
039            super(endpoint);
040            this.queue = Hazelcast.getQueue(queueName);
041        }
042    
043        public void process(Exchange exchange) throws Exception {
044    
045            Map<String, Object> headers = exchange.getIn().getHeaders();
046    
047            // get header parameters
048            int operation = -1;
049    
050            if (headers.containsKey(HazelcastConstants.OPERATION)) {
051                if (headers.get(HazelcastConstants.OPERATION) instanceof String) {
052                    operation = helper.lookupOperationNumber((String) headers.get(HazelcastConstants.OPERATION));
053                } else {
054                    operation = (Integer) headers.get(HazelcastConstants.OPERATION);
055                }
056            }
057    
058            switch (operation) {
059    
060            case HazelcastConstants.ADD_OPERATION:
061                this.add(exchange);
062                break;
063    
064            case HazelcastConstants.PUT_OPERATION:
065                this.put(exchange);
066                break;
067    
068            case HazelcastConstants.POLL_OPERATION:
069                this.poll(exchange);
070                break;
071    
072            case HazelcastConstants.PEEK_OPERATION:
073                this.peek(exchange);
074                break;
075    
076            case HazelcastConstants.OFFER_OPERATION:
077                this.offer(exchange);
078                break;
079    
080            case HazelcastConstants.REMOVEVALUE_OPERATION:
081                this.remove(exchange);
082                break;
083    
084            default:
085                throw new IllegalArgumentException(String.format("The value '%s' is not allowed for parameter '%s' on the QUEUE cache.", operation, HazelcastConstants.OPERATION));
086            }
087    
088            // finally copy headers
089            HazelcastComponentHelper.copyHeaders(exchange);
090    
091        }
092    
093        private void add(Exchange exchange) {
094            Object body = exchange.getIn().getBody();
095            this.queue.add(body);
096        }
097    
098        private void put(Exchange exchange) throws InterruptedException {
099            Object body = exchange.getIn().getBody();
100            this.queue.put(body);
101        }
102    
103        private void poll(Exchange exchange) {
104            exchange.getOut().setBody(this.queue.poll());
105        }
106    
107        private void peek(Exchange exchange) {
108            exchange.getOut().setBody(this.queue.peek());
109        }
110    
111        private void offer(Exchange exchange) {
112            Object body = exchange.getIn().getBody();
113            this.queue.offer(body);
114        }
115    
116        private void remove(Exchange exchange) {
117            Object body = exchange.getIn().getBody();
118            if (body != null) {
119                this.queue.remove(body);
120            } else {
121                this.queue.remove();
122            }
123        }
124    }