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.list;
018
019 import java.util.Map;
020
021 import com.hazelcast.core.Hazelcast;
022 import com.hazelcast.core.IList;
023
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Exchange;
026 import org.apache.camel.Producer;
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 /**
032 * Implementation of Hazelcast List {@link Producer}.
033 */
034 public class HazelcastListProducer extends DefaultProducer {
035
036 private final IList<Object> list;
037 private final HazelcastComponentHelper helper = new HazelcastComponentHelper();
038
039 public HazelcastListProducer(Endpoint endpoint, String listName) {
040 super(endpoint);
041 this.list = Hazelcast.getList(listName);
042 }
043
044 public void process(Exchange exchange) throws Exception {
045
046 Map<String, Object> headers = exchange.getIn().getHeaders();
047
048 // get header parameters
049 int operation = -1;
050 Integer pos = null;
051
052 if (headers.containsKey(HazelcastConstants.OBJECT_POS)) {
053 if (!(headers.get(HazelcastConstants.OBJECT_POS) instanceof Integer)) {
054 throw new IllegalArgumentException("OBJECT_POS Should be of type Integer");
055 }
056 pos = (Integer) headers.get(HazelcastConstants.OBJECT_POS);
057 }
058
059 if (headers.containsKey(HazelcastConstants.OPERATION)) {
060 if (headers.get(HazelcastConstants.OPERATION) instanceof String) {
061 operation = helper.lookupOperationNumber((String) headers.get(HazelcastConstants.OPERATION));
062 } else {
063 operation = (Integer) headers.get(HazelcastConstants.OPERATION);
064 }
065 }
066
067 switch (operation) {
068
069 case HazelcastConstants.ADD_OPERATION:
070 this.add(pos, exchange);
071 break;
072
073 case HazelcastConstants.GET_OPERATION:
074 this.get(pos, exchange);
075 break;
076
077 case HazelcastConstants.SETVALUE_OPERATION:
078 this.set(pos, exchange);
079 break;
080
081 case HazelcastConstants.REMOVEVALUE_OPERATION:
082 this.remove(pos, exchange);
083 break;
084
085 default:
086 throw new IllegalArgumentException(String.format("The value '%s' is not allowed for parameter '%s' on the LIST cache.", operation, HazelcastConstants.OPERATION));
087 }
088
089 // finally copy headers
090 HazelcastComponentHelper.copyHeaders(exchange);
091 }
092
093 private void add(Integer pos, Exchange exchange) {
094 final Object body = exchange.getIn().getBody();
095 if (null == pos) {
096 // add the specified element to the end of the list
097 list.add(body);
098 } else {
099 // add the specified element at the specified position
100 list.add(pos, body);
101 }
102 }
103
104 private void get(Integer pos, Exchange exchange) {
105 // TODO: this operation is currently not supported by hazelcast
106 exchange.getOut().setBody(this.list.get(pos));
107 }
108
109 private void set(Integer pos, Exchange exchange) {
110 // TODO: this operation is currently not supported by hazelcast
111 if (null == pos) {
112 throw new IllegalArgumentException("Empty position for set operation.");
113 } else {
114 final Object body = exchange.getIn().getBody();
115 list.set(pos, body);
116 }
117 }
118
119 private void remove(Integer pos, Exchange exchange) {
120 if (null == pos) {
121 // removes the first occurrence in the list
122 final Object body = exchange.getIn().getBody();
123 list.remove(body);
124 } else {
125 // TODO: this operation is currently not supported by hazelcast
126 // removes the element at the specified position
127 int position = pos;
128 list.remove(position);
129 }
130 }
131 }