1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
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
87
88
89
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 }