View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.boot.hl7v2;
18  
19  import org.openehealth.ipf.commons.ihe.hl7v2.storage.UnsolicitedFragmentationStorage;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  import org.springframework.cache.Cache;
23  import org.springframework.cache.CacheManager;
24  
25  /**
26   * CachingUnsolicitedFragmentionStorage that uses a Spring cache abstraction
27   *
28   * @author Christian Ohr
29   * @since 3.2
30   */
31  public class CachingUnsolicitedFragmentionStorage implements UnsolicitedFragmentationStorage {
32  
33      private static final transient Logger LOG = LoggerFactory.getLogger(CachingUnsolicitedFragmentionStorage.class);
34      private static final String UNSOLICITED_FRAGMENTATION_STORAGE = "unsolicitedFragmentionStorage";
35      private final Cache cache;
36  
37      public CachingUnsolicitedFragmentionStorage(CacheManager cacheManager) {
38          this.cache = cacheManager.getCache(UNSOLICITED_FRAGMENTATION_STORAGE);
39      }
40  
41      @Override
42      public void put(String key, StringBuilder accumulator) {
43          cache.put(key, accumulator.toString());
44      }
45  
46      @Override
47      public StringBuilder getAndRemove(String key) {
48          String element = cache.get(key, String.class);
49          if (element != null) {
50              cache.evict(key);
51              return new StringBuilder(element);
52          }
53          return null;
54      }
55  
56  
57  }