View Javadoc
1   /*
2    * Copyright 2018 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.platform.camel.ihe.atna.util;
18  
19  import org.apache.camel.impl.DefaultComponent;
20  import org.openehealth.ipf.commons.audit.AuditContext;
21  import org.openehealth.ipf.commons.audit.AuditException;
22  import org.openehealth.ipf.commons.core.config.ContextFacade;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  
27  /**
28   * @author Christian Ohr
29   */
30  public abstract class AuditConfiguration {
31  
32      public static AuditContext obtainAuditContext(DefaultComponent component, Map<String, Object> parameters) {
33          Boolean audit = component.getAndRemoveParameter(parameters, "audit", Boolean.class, true);
34          AuditContext auditContext = component.resolveAndRemoveReferenceParameter(parameters, "auditContext", AuditContext.class);
35          if (auditContext == null) {
36              if (audit != null && !audit) {
37                  auditContext = AuditContext.noAudit();
38              } else {
39                  Collection<AuditContext> beans = ContextFacade.getBeans(AuditContext.class);
40                  if (beans.size() == 1) {
41                      auditContext = beans.iterator().next();
42                  } else {
43                      throw new AuditException("Must have exactly one bean of type " + AuditContext.class.getName() + ", but was : " + beans.size());
44                  }
45              }
46          }
47          return auditContext;
48      }
49  
50  }