2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.aaiutils.oxm;
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;
35 import java.util.HashMap;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
40 import javax.xml.bind.JAXBException;
43 public class OxmModelLoader {
45 final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
47 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap();
49 private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
50 .getLogger(OxmModelLoader.class.getName());
52 public synchronized static void loadModels() throws Exception {
53 ClassLoader cl = OxmModelLoader.class.getClassLoader();
54 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
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.");
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");
68 for (Resource resource : resources) {
69 Matcher matcher = p.matcher(resource.getFilename());
71 if (matcher.matches()) {
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");
83 public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
84 if (versionContextMap == null || versionContextMap.isEmpty()) {
86 } else if (!versionContextMap.containsKey(version)) {
87 String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
89 loadModel(version, new File(filename));
90 } catch (Exception e) {
91 throw new FileNotFoundException(filename);
95 return versionContextMap.get(version);
98 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
99 return versionContextMap;
102 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
103 OxmModelLoader.versionContextMap = versionContextMap;
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 );
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 );
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 );