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;
24 import org.eclipse.persistence.jaxb.JAXBContextProperties;
25 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
26 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.springframework.core.io.Resource;
29 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
30 import org.springframework.core.io.support.ResourcePatternResolver;
33 import java.util.HashMap;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import javax.xml.bind.JAXBException;
41 public class OxmModelLoader {
43 final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
45 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap();
47 private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
48 .getLogger(OxmModelLoader.class.getName());
50 public synchronized static void loadModels() throws Exception {
51 ClassLoader cl = OxmModelLoader.class.getClassLoader();
52 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
55 resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
56 } catch (IOException ex) {
57 logger.error( OxmModelLoaderMsgs.OXM_LOAD_ERROR, ex.getMessage());
58 throw new FileNotFoundException("OXM files not found on classpath.");
61 if (resources.length == 0) {
62 logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
63 throw new Exception("Failed to load schema");
66 for (Resource resource : resources) {
67 Matcher matcher = p.matcher(resource.getFilename());
69 if (matcher.matches()) {
71 OxmModelLoader.loadModel(matcher.group(1), resource);
72 } catch (Exception e) {
73 logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
74 + ": " + e.getMessage());
75 throw new Exception("Failed to load schema");
81 public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
82 if (versionContextMap == null || versionContextMap.isEmpty()) {
84 } else if (!versionContextMap.containsKey(version)) {
85 String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
87 loadModel(version, new File(filename));
88 } catch (Exception e) {
89 throw new FileNotFoundException(filename);
93 return versionContextMap.get(version);
96 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
97 return versionContextMap;
100 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
101 OxmModelLoader.versionContextMap = versionContextMap;
104 private synchronized static void loadModel ( String version, File file )
105 throws JAXBException, IOException {
106 InputStream inputStream = new FileInputStream ( file );
107 loadModel ( version, file.getName (), inputStream );
110 private synchronized static void loadModel ( String version, Resource resource )
111 throws JAXBException, IOException {
112 InputStream inputStream = resource.getInputStream ();
113 loadModel ( version, resource.getFilename (), inputStream );
116 private synchronized static void loadModel ( String version, String resourceName,
117 InputStream inputStream )
118 throws JAXBException, IOException {
119 Map<String, Object> properties = new HashMap<String, Object> ();
120 properties.put ( JAXBContextProperties.OXM_METADATA_SOURCE, inputStream );
121 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
122 .createContextFromOXM ( Thread.currentThread ().getContextClassLoader (), properties );
123 versionContextMap.put ( version, jaxbContext );
124 logger.info ( OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName );