Update the license for 2017-2018 license
[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 package org.onap.aaiutils.oxm;
23
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;
31
32 import java.io.*;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 import javax.xml.bind.JAXBException;
39
40
41 public class OxmModelLoader {
42
43     final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
44
45   private static Map<String, DynamicJAXBContext> versionContextMap  = new ConcurrentHashMap();
46
47   private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
48       .getLogger(OxmModelLoader.class.getName());
49
50   public synchronized static void loadModels() throws Exception {
51     ClassLoader cl = OxmModelLoader.class.getClassLoader();
52     ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
53     Resource[] resources;
54     try {
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.");
59     }
60
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");
64     }
65
66     for (Resource resource : resources) {
67       Matcher matcher = p.matcher(resource.getFilename());
68
69       if (matcher.matches()) {
70         try {
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");
76         }
77       }
78     }
79   }
80
81   public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
82     if (versionContextMap == null || versionContextMap.isEmpty()) {
83       loadModels();
84     } else if (!versionContextMap.containsKey(version)) {
85       String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
86       try {
87         loadModel(version, new File(filename));
88       } catch (Exception e) {
89         throw new FileNotFoundException(filename);
90       }
91     }
92
93     return versionContextMap.get(version);
94   }
95
96   public static Map<String, DynamicJAXBContext> getVersionContextMap() {
97     return versionContextMap;
98   }
99
100   public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
101     OxmModelLoader.versionContextMap = versionContextMap;
102   }
103
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 );
108   }
109
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 );
114   }
115
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 );
125     }
126
127 }