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.atomicnumber;
018
019 import java.util.Map;
020
021 import com.hazelcast.core.AtomicNumber;
022 import com.hazelcast.core.Hazelcast;
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 HazelcastAtomicnumberProducer extends DefaultProducer {
031
032 private final AtomicNumber atomicnumber;
033 private final HazelcastComponentHelper helper = new HazelcastComponentHelper();
034
035 public HazelcastAtomicnumberProducer(Endpoint endpoint, String cacheName) {
036 super(endpoint);
037 this.atomicnumber = Hazelcast.getAtomicNumber(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 int operation = -1;
046
047 if (headers.containsKey(HazelcastConstants.OPERATION)) {
048 if (headers.get(HazelcastConstants.OPERATION) instanceof String) {
049 operation = this.helper.lookupOperationNumber((String) headers.get(HazelcastConstants.OPERATION));
050 } else {
051 operation = (Integer) headers.get(HazelcastConstants.OPERATION);
052 }
053 }
054
055 switch (operation) {
056
057 case HazelcastConstants.INCREMENT_OPERATION:
058 this.increment(exchange);
059 break;
060
061 case HazelcastConstants.DECREMENT_OPERATION:
062 this.decrement(exchange);
063 break;
064
065 case HazelcastConstants.SETVALUE_OPERATION:
066 this.set(exchange);
067 break;
068
069 case HazelcastConstants.GET_OPERATION:
070 this.get(exchange);
071 break;
072
073 case HazelcastConstants.DESTROY_OPERATION:
074 this.destroy();
075 break;
076
077 default:
078 throw new IllegalArgumentException(String.format("The value '%s' is not allowed for parameter '%s' on the ATOMICNUMBER.", operation, HazelcastConstants.OPERATION));
079 }
080
081 // finally copy headers
082 HazelcastComponentHelper.copyHeaders(exchange);
083 }
084
085 private void get(Exchange exchange) {
086 exchange.getOut().setBody(this.atomicnumber.get());
087 }
088
089 private void increment(Exchange exchange) {
090 exchange.getOut().setBody(this.atomicnumber.incrementAndGet());
091 }
092
093 private void decrement(Exchange exchange) {
094 exchange.getOut().setBody(this.atomicnumber.decrementAndGet());
095 }
096
097 private void set(Exchange exchange) {
098 this.atomicnumber.set(exchange.getIn().getBody(Long.class));
099 }
100
101 private void destroy() {
102 this.atomicnumber.destroy();
103 }
104
105 }