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  package org.openehealth.ipf.commons.ihe.hl7v2.storage;
17  
18  import ca.uhn.hl7v2.model.Message;
19  import net.sf.ehcache.Ehcache;
20  import net.sf.ehcache.Element;
21  import org.apache.commons.lang3.Validate;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.io.Serializable;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import static java.util.Objects.requireNonNull;
31  
32  /**
33   * An Ehcache-based implementation of {@link InteractiveContinuationStorage}.
34   *
35   * @author Dmytro Rud
36   */
37  public class EhcacheInteractiveContinuationStorage implements InteractiveContinuationStorage {
38  
39      private static final transient Logger LOG = LoggerFactory.getLogger(EhcacheInteractiveContinuationStorage.class);
40      private final Ehcache ehcache;
41  
42  
43      public EhcacheInteractiveContinuationStorage(Ehcache ehcache) {
44          requireNonNull(ehcache);
45          this.ehcache = ehcache;
46      }
47  
48  
49      @Override
50      public void put(String continuationPointer, String chainId, Message fragment) {
51          InteractiveContinuationChain chain;
52          Element element = ehcache.get(chainId);
53          if (element != null) {
54              chain = (InteractiveContinuationChain) element.getObjectValue();
55          } else {
56              LOG.debug("Create chain for storage key {}", chainId);
57              chain = new InteractiveContinuationChain();
58              ehcache.put(new Element(chainId, chain));
59          }
60          chain.put(continuationPointer, fragment);
61      }
62  
63  
64      @Override
65      public Message get(
66              String continuationPointer,
67              String chainId)
68      {
69          Element element = ehcache.get(chainId);
70          if (element != null) {
71              InteractiveContinuationChain chain = (InteractiveContinuationChain) element.getObjectValue();
72              return chain.get(continuationPointer);
73          }
74          return null;
75      }
76  
77  
78      @Override
79      public boolean delete(String chainId) {
80          return ehcache.remove(chainId);
81      }
82  
83  
84      
85      /**
86       * Chain of interactive continuation fragments of a query's response.
87       * <p>
88       * Keys correspond to continuation pointers of the fragments;
89       * the key of the first fragment is <code>null</code>.
90       */
91      private static class InteractiveContinuationChain implements Serializable {
92          private final Map<String, Message> responseMessages =
93              Collections.synchronizedMap(new HashMap<String, Message>());
94  
95          public void put(String continuationPointer, Message message) {
96              responseMessages.put(continuationPointer, message);
97          }
98  
99          public Message get(String continuationPointer) {
100             return responseMessages.get(continuationPointer);
101         }
102     }
103 
104 }