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.processor.idempotent.hazelcast;
018    
019    import com.hazelcast.core.Hazelcast;
020    import com.hazelcast.core.IMap;
021    import org.apache.camel.impl.ServiceSupport;
022    import org.apache.camel.spi.IdempotentRepository;
023    
024    public class HazelcastIdempotentRepository extends ServiceSupport implements IdempotentRepository<String> {
025    
026        private String repositoryName;
027        private IMap<String, Object> repo;
028    
029        public HazelcastIdempotentRepository() {
030            this(HazelcastIdempotentRepository.class.getSimpleName());
031        }
032    
033        public HazelcastIdempotentRepository(String repositoryName) {
034            this.repositoryName = repositoryName;
035        }
036    
037        @Override
038        protected void doStart() throws Exception {
039            repo = Hazelcast.getMap(repositoryName);
040        }
041    
042        @Override
043        protected void doStop() throws Exception {
044            // noop
045        }
046    
047        @Override
048        public boolean add(String key) {
049            if (this.contains(key)) {
050                return false;
051            } else {
052                this.repo.put(key, false);
053                return true;
054            }
055        }
056    
057        @Override
058        public boolean confirm(String key) {
059            if (this.contains(key)) {
060                this.repo.put(key, true);
061                return true;
062            } else {
063                return false;
064            }
065        }
066    
067        @Override
068        public boolean contains(String key) {
069            return this.repo.containsKey(key);
070        }
071    
072        @Override
073        public boolean remove(String key) {
074            if (this.contains(key)) {
075                this.repo.remove(key);
076                return true;
077            } else {
078                return false;
079            }
080        }
081    
082        public String getRepositoryName() {
083            return repositoryName;
084        }
085    
086    }