Fix dependency in aai-utils pom
[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 AT&T Intellectual Property.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.aaiutils.oxm;
25
26 import org.eclipse.persistence.jaxb.JAXBContextProperties;
27 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
28 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.springframework.core.io.Resource;
31 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
32 import org.springframework.core.io.support.ResourcePatternResolver;
33
34 import java.io.*;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 import javax.xml.bind.JAXBException;
41
42
43 public class OxmModelLoader {
44
45     final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
46
47   private static Map<String, DynamicJAXBContext> versionContextMap  = new ConcurrentHashMap();
48
49   private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
50       .getLogger(OxmModelLoader.class.getName());
51
52   public synchronized static void loadModels() throws Exception {
53     ClassLoader cl = OxmModelLoader.class.getClassLoader();
54     ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
55     Resource[] resources;
56     try {
57       resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
58     } catch (IOException ex) {
59       logger.error( OxmModelLoaderMsgs.OXM_LOAD_ERROR, ex.getMessage());
60       throw new FileNotFoundException("OXM files not found on classpath.");
61     }
62
63     if (resources.length == 0) {
64       logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
65       throw new Exception("Failed to load schema");
66     }
67
68     for (Resource resource : resources) {
69       Matcher matcher = p.matcher(resource.getFilename());
70
71       if (matcher.matches()) {
72         try {
73           OxmModelLoader.loadModel(matcher.group(1), resource);
74         } catch (Exception e) {
75           logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
76               + ": " + e.getMessage());
77           throw new Exception("Failed to load schema");
78         }
79       }
80     }
81   }
82
83   public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
84     if (versionContextMap == null || versionContextMap.isEmpty()) {
85       loadModels();
86     } else if (!versionContextMap.containsKey(version)) {
87       String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
88       try {
89         loadModel(version, new File(filename));
90       } catch (Exception e) {
91         throw new FileNotFoundException(filename);
92       }
93     }
94
95     return versionContextMap.get(version);
96   }
97
98   public static Map<String, DynamicJAXBContext> getVersionContextMap() {
99     return versionContextMap;
100   }
101
102   public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
103     OxmModelLoader.versionContextMap = versionContextMap;
104   }
105
106   private synchronized static void loadModel ( String version, File file )
107           throws JAXBException, IOException {
108       InputStream inputStream = new FileInputStream ( file );
109       loadModel ( version, file.getName (), inputStream );
110   }
111
112   private synchronized static void loadModel ( String version, Resource resource )
113           throws JAXBException, IOException {
114       InputStream inputStream = resource.getInputStream ();
115       loadModel ( version, resource.getFilename (), inputStream );
116   }
117
118   private synchronized static void loadModel ( String version, String resourceName,
119                                                InputStream inputStream )
120           throws JAXBException, IOException {
121       Map<String, Object> properties = new HashMap<String, Object> ();
122       properties.put ( JAXBContextProperties.OXM_METADATA_SOURCE, inputStream );
123       final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
124               .createContextFromOXM ( Thread.currentThread ().getContextClassLoader (), properties );
125       versionContextMap.put ( version, jaxbContext );
126       logger.info ( OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName );
127     }
128
129 }