2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property.
6 * Copyright © 2017-2018 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aaiutils.oxm;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Collections;
30 import java.util.HashMap;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35 import javax.xml.bind.JAXBException;
36 import org.eclipse.persistence.jaxb.JAXBContextProperties;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
38 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
39 import org.onap.aai.cl.api.Logger;
40 import org.onap.aai.cl.eelf.LoggerFactory;
41 import org.springframework.core.io.Resource;
42 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
43 import org.springframework.core.io.support.ResourcePatternResolver;
46 public class OxmModelLoader {
48 private static final Pattern AAI_OXM_FILE_PATTERN = Pattern.compile("aai_oxm_(.*).xml");
49 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
50 private static final Logger LOGGER = LoggerFactory.getInstance()
51 .getLogger(OxmModelLoader.class.getName());
53 public synchronized static void loadModels() throws Exception {
54 OxmModelLoader.loadModels("classpath*:/oxm/aai_oxm*.xml", AAI_OXM_FILE_PATTERN);
57 synchronized static void loadModels(String oxmResourcesPattern, Pattern aai_oxm_file_pattern) throws Exception {
58 Resource[] resources = getResources(oxmResourcesPattern);
60 for (Resource resource : resources) {
61 Matcher matcher = aai_oxm_file_pattern.matcher(resource.getFilename());
63 if (matcher.matches()) {
65 OxmModelLoader.loadModel(matcher.group(1), resource);
66 } catch (Exception e) {
67 LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
68 + ": " + e.getMessage());
69 throw new Exception("Failed to load schema");
75 private static Resource[] getResources(String oxmResourcesPattern) throws Exception {
76 ClassLoader cl = OxmModelLoader.class.getClassLoader();
77 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
79 Resource[] resources = resolver.getResources(oxmResourcesPattern);
80 if (resources.length == 0) {
81 LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
82 throw new Exception("Failed to load schema");
89 public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
90 if (versionContextMap == null || versionContextMap.isEmpty()) {
92 } else if (!versionContextMap.containsKey(version)) {
93 String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
95 loadModel(version, new File(filename));
96 } catch (Exception e) {
97 throw new FileNotFoundException(filename);
101 return versionContextMap.get(version);
104 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
105 return Collections.unmodifiableMap(versionContextMap);
108 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
109 OxmModelLoader.versionContextMap = versionContextMap;
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);
117 private synchronized static void loadModel(String version, Resource resource)
118 throws JAXBException, IOException {
119 InputStream inputStream = resource.getInputStream();
120 loadModel(version, resource.getFilename(), inputStream);
123 private synchronized static void loadModel(String version, String resourceName, InputStream inputStream)
124 throws JAXBException, IOException {
126 Map<String, Object> properties = new HashMap<>();
127 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
129 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
130 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
132 versionContextMap.put(version, jaxbContext);
134 LOGGER.info(OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName);