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 ca.uhn.hl7v2.model.Message;
20  import org.openehealth.ipf.commons.ihe.hl7v2.storage.InteractiveContinuationStorage;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.cache.Cache;
24  import org.springframework.cache.CacheManager;
25  
26  import java.io.Serializable;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * InteractiveContinuationStorage that uses a Spring cache abstraction
33   *
34   * @author Christian Ohr
35   * @since 3.2
36   */
37  public class CachingInteractiveHl7v2ContinuationStorage implements InteractiveContinuationStorage {
38  
39      private static final transient Logger LOG = LoggerFactory.getLogger(CachingInteractiveHl7v2ContinuationStorage.class);
40      private static final String INTERACTIVE_CONTINUATION_CACHE = "interactiveHl7v2ContinuationCache";
41      private final Cache cache;
42  
43      public CachingInteractiveHl7v2ContinuationStorage(CacheManager cacheManager) {
44          this.cache = cacheManager.getCache(INTERACTIVE_CONTINUATION_CACHE);
45      }
46  
47      @Override
48      public void put(String continuationPointer, String chainId, Message fragment) {
49          InteractiveContinuationChain chain = cache.get(chainId, InteractiveContinuationChain.class);
50          if (chain == null) {
51              LOG.debug("Create chain for storage key {}", chainId);
52              chain = new InteractiveContinuationChain();
53              cache.put(chainId, chain);
54          }
55          chain.put(continuationPointer, fragment);
56      }
57  
58      @Override
59      public Message get(String continuationPointer, String chainId) {
60          InteractiveContinuationChain chain = cache.get(chainId, InteractiveContinuationChain.class);
61          if (chain != null) {
62              return chain.get(continuationPointer);
63          }
64          return null;
65      }
66  
67      @Override
68      public boolean delete(String chainId) {
69          if (cache.get(chainId) != null) {
70              cache.evict(chainId);
71              return true;
72          }
73          return false;
74      }
75  
76      /**
77       * Chain of interactive continuation fragments of a query's response.
78       * <p>
79       * Keys correspond to continuation pointers of the fragments;
80       * the key of the first fragment is <code>null</code>.
81       */
82      private static class InteractiveContinuationChain implements Serializable {
83          private final Map<String, Message> responseMessages =
84                  Collections.synchronizedMap(new HashMap<>());
85  
86          public void put(String continuationPointer, Message message) {
87              responseMessages.put(continuationPointer, message);
88          }
89  
90          public Message get(String continuationPointer) {
91              return responseMessages.get(continuationPointer);
92          }
93      }
94  }