AAI-1523 Batch reformat aai-utils
[aai/aai-common.git] / aai-utils / src / main / java / org / onap / aaiutils / oxm / OxmModelLoader.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property.
6  * Copyright © 2017-2018 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aaiutils.oxm;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36
37 import javax.xml.bind.JAXBException;
38
39 import org.eclipse.persistence.jaxb.JAXBContextProperties;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
42 import org.onap.aai.cl.api.Logger;
43 import org.onap.aai.cl.eelf.LoggerFactory;
44 import org.springframework.core.io.Resource;
45 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
46 import org.springframework.core.io.support.ResourcePatternResolver;
47
48 public class OxmModelLoader {
49
50     private static final Pattern AAI_OXM_FILE_PATTERN = Pattern.compile("aai_oxm_(.*).xml");
51     private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
52     private static final Logger LOGGER = LoggerFactory.getInstance().getLogger(OxmModelLoader.class.getName());
53
54     public synchronized static void loadModels() throws Exception {
55         OxmModelLoader.loadModels("classpath*:/oxm/aai_oxm*.xml", AAI_OXM_FILE_PATTERN);
56     }
57
58     synchronized static void loadModels(String oxmResourcesPattern, Pattern aai_oxm_file_pattern) throws Exception {
59         Resource[] resources = getResources(oxmResourcesPattern);
60
61         for (Resource resource : resources) {
62             Matcher matcher = aai_oxm_file_pattern.matcher(resource.getFilename());
63
64             if (matcher.matches()) {
65                 try {
66                     OxmModelLoader.loadModel(matcher.group(1), resource);
67                 } catch (Exception e) {
68                     LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR,
69                             "Failed to load " + resource.getFilename() + ": " + e.getMessage());
70                     throw new Exception("Failed to load schema");
71                 }
72             }
73         }
74     }
75
76     private static Resource[] getResources(String oxmResourcesPattern) throws Exception {
77         ClassLoader cl = OxmModelLoader.class.getClassLoader();
78         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
79
80         Resource[] resources = resolver.getResources(oxmResourcesPattern);
81         if (resources.length == 0) {
82             LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
83             throw new Exception("Failed to load schema");
84         }
85         return resources;
86
87     }
88
89     public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
90         if (versionContextMap == null || versionContextMap.isEmpty()) {
91             loadModels();
92         } else if (!versionContextMap.containsKey(version)) {
93             String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
94             try {
95                 loadModel(version, new File(filename));
96             } catch (Exception e) {
97                 throw new FileNotFoundException(filename);
98             }
99         }
100
101         return versionContextMap.get(version);
102     }
103
104     public static Map<String, DynamicJAXBContext> getVersionContextMap() {
105         return Collections.unmodifiableMap(versionContextMap);
106     }
107
108     public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
109         OxmModelLoader.versionContextMap = versionContextMap;
110     }
111
112     private synchronized static void loadModel(String version, File file) throws JAXBException, IOException {
113         InputStream inputStream = new FileInputStream(file);
114         loadModel(version, file.getName(), inputStream);
115     }
116
117     private synchronized static void loadModel(String version, Resource resource) throws JAXBException, IOException {
118         InputStream inputStream = resource.getInputStream();
119         loadModel(version, resource.getFilename(), inputStream);
120     }
121
122     private synchronized static void loadModel(String version, String resourceName, InputStream inputStream)
123             throws JAXBException, IOException {
124
125         Map<String, Object> properties = new HashMap<>();
126         properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
127
128         final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
129                 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
130
131         versionContextMap.put(version, jaxbContext);
132
133         LOGGER.info(OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName);
134     }
135
136 }