View Javadoc
1   /*
2    * Copyright 2010 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.ws.correlation;
17  
18  import net.sf.ehcache.Ehcache;
19  import net.sf.ehcache.Element;
20  import org.openehealth.ipf.commons.ihe.ws.cxf.audit.WsAuditDataset;
21  
22  import java.io.Serializable;
23  
24  import static java.util.Objects.requireNonNull;
25  
26  /**
27   * Ehcache-based implementation of asynchronous message correlator.
28   * @author Dmytro Rud
29   */
30  public class EhcacheAsynchronyCorrelator<AuditDatasetType extends WsAuditDataset> implements AsynchronyCorrelator<AuditDatasetType> {
31  
32      private static final String SERVICE_ENDPOINT_URI_SUFFIX = ".serviceEndpoint";
33      private static final String CORRELATION_KEY_SUFFIX      = ".correlationKey";
34      private static final String AUDIT_DATASET_SUFFIX        = ".auditDataset";
35      private static final String ALTERNATIVE_KEY_SUFFIX      = ".alternativeKey";
36      private static final String ALTERNATIVE_KEYS_SUFFIX     = ".alternativeKeys";
37  
38      private final Ehcache ehcache;
39  
40      public EhcacheAsynchronyCorrelator(Ehcache ehcache) {
41          this.ehcache = requireNonNull(ehcache, "ehcache instance");
42      }
43  
44      private void put(String key, String suffix, Serializable value) {
45          ehcache.put(new Element(key + suffix, value));
46      }
47  
48      private <T extends Serializable> T get(String key, String suffix) {
49          Element element = ehcache.get(key + suffix);
50          return (element != null) ? (T) element.getObjectValue() : null;
51      }
52  
53      @Override
54      public void storeServiceEndpointUri(String messageId, String serviceEndpointUri) {
55          put(messageId, SERVICE_ENDPOINT_URI_SUFFIX, requireNonNull(serviceEndpointUri, "service endpoint URI"));
56      }
57  
58      @Override
59      public void storeCorrelationKey(String messageId, String correlationKey) {
60          put(messageId, CORRELATION_KEY_SUFFIX, requireNonNull(correlationKey, "correlation key"));
61      }
62  
63      @Override
64      public void storeAuditDataset(String messageId, WsAuditDataset auditDataset) {
65          put(messageId, AUDIT_DATASET_SUFFIX, requireNonNull(auditDataset, "audit dataset"));
66      }
67  
68      @Override
69      public String getServiceEndpointUri(String messageId) {
70          return get(messageId, SERVICE_ENDPOINT_URI_SUFFIX);
71      }
72  
73      @Override
74      public String getCorrelationKey(String messageId) {
75          return get(messageId, CORRELATION_KEY_SUFFIX);
76      }
77  
78      @Override
79      public AuditDatasetType getAuditDataset(String messageId) {
80          return get(messageId, AUDIT_DATASET_SUFFIX);
81      }
82  
83      @Override
84      public void storeAlternativeKeys(String messageId, String... alternativeKeys) {
85          requireNonNull(alternativeKeys, "alternative keys should be not null");
86          for (String key : alternativeKeys) {
87              put(key, ALTERNATIVE_KEY_SUFFIX, messageId);
88          }
89          put(messageId, ALTERNATIVE_KEYS_SUFFIX, alternativeKeys);
90      }
91  
92      @Override
93      public String getMessageId(String alternativeKey) {
94          return get(alternativeKey, ALTERNATIVE_KEY_SUFFIX);
95      }
96  
97      @Override
98      public boolean delete(String messageId) {
99          String[] alternativeKeys = get(messageId, ALTERNATIVE_KEYS_SUFFIX);
100         if (alternativeKeys != null) {
101             for (String key : alternativeKeys) {
102                 ehcache.remove(key + ALTERNATIVE_KEY_SUFFIX);
103             }
104         }
105         ehcache.remove(messageId + ALTERNATIVE_KEYS_SUFFIX);
106         ehcache.remove(messageId + CORRELATION_KEY_SUFFIX);
107         ehcache.remove(messageId + AUDIT_DATASET_SUFFIX);
108         return ehcache.remove(messageId + SERVICE_ENDPOINT_URI_SUFFIX);
109     }
110 }