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  package org.openehealth.ipf.commons.spring.map.config;
17  
18  import org.openehealth.ipf.commons.spring.map.MappingResourceHolder;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.core.io.Resource;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  
27  /**
28   * This class should be used to define the custom mappings
29   * in the spring context definition.
30   *
31   * <pre class="code">
32   *    &lt;!-- either as a list of mapping definitions --&gt;
33   *    &lt;bean id="customMapping3"
34   *        class="org.openehealth.ipf.commons.map.config.CustomMappings"&gt;
35   *        &lt;property name="mappingResources"&gt;
36   *            &lt;list&gt;
37   *                &lt;value&gt;classpath:configurer1.map&lt;/value&gt;
38   *                &lt;value&gt;classpath:configurer2.map&lt;/value&gt;
39   *            &lt;/list&gt;
40   *        &lt;/property&gt;
41   *    &lt;/bean&gt;
42   *
43   *    &lt;!-- or as a single mapping definition --&gt;
44   *    &lt;bean id="customMappingSingle"
45   *        class="org.openehealth.ipf.commons.map.config.CustomMappings"&gt;
46   *        &lt;property name="mappingResource" value="classpath:configurer3.map" /&gt;
47   *    &lt;/bean&gt;</pre>
48   *
49   * @see CustomMappingsConfigurer
50   * @author Christian Ohr
51   * @author Boris Stanojevic
52   *
53   */
54  public class CustomMappings implements MappingResourceHolder {
55  
56      private static final Logger LOG = LoggerFactory.getLogger(CustomMappings.class);
57  
58      private Collection<Resource> mappingResources = new ArrayList<>();
59  
60      @Override
61      public Collection<? extends Resource> getMappingResources() {
62          return mappingResources;
63      }
64  
65      @Override
66      public void setMappingResources(Collection<? extends Resource> mappingResources) {
67          this.mappingResources = new ArrayList<>(mappingResources.size());
68          for (Resource mappingResource : mappingResources) {
69              if (mappingResource.exists() && mappingResource.isReadable()) {
70                  this.mappingResources.add(mappingResource);
71              } else {
72                  LOG.warn("Could not read mapping script {}", mappingResource.getFilename());
73              }
74          }
75      }
76  
77      @Override
78      public void setMappingResource(Resource mappingResource) {
79          setMappingResources(Collections.singleton(mappingResource));
80      }
81  
82  }