2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.aai.introspection;
23 import org.eclipse.persistence.dynamic.DynamicType;
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.openecomp.aai.util.AAIConstants;
29 import javax.xml.bind.JAXBException;
31 import java.io.FileInputStream;
32 import java.io.FileNotFoundException;
33 import java.io.InputStream;
34 import java.util.HashMap;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 public class ModelInjestor {
41 private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>();
42 private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\.");
43 private static final Pattern uriPattern = Pattern.compile("(v\\d+)\\/");
47 * Instantiates a new model injestor.
49 private ModelInjestor() {
52 } catch (FileNotFoundException | JAXBException e) {
53 throw new RuntimeException(e);
57 private static class Helper {
58 private static final ModelInjestor INSTANCE = new ModelInjestor();
62 * Gets the single instance of ModelInjestor.
64 * @return single instance of ModelInjestor
66 public synchronized static ModelInjestor getInstance() {
67 return Helper.INSTANCE;
73 * @throws FileNotFoundException the file not found exception
74 * @throws JAXBException the JAXB exception
76 private void injestModels() throws FileNotFoundException, JAXBException {
78 for (Version version : Version.values()) {
79 this.injestModel(version);
86 * @param version the version
87 * @throws JAXBException the JAXB exception
88 * @throws FileNotFoundException the file not found exception
90 private void injestModel (Version version) throws JAXBException, FileNotFoundException {
91 String fileName = this.getOXMFileName(version);
92 InputStream iStream = new FileInputStream(new File(fileName));
93 Map<String, Object> properties = new HashMap<String, Object>();
94 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
95 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties);
96 versionContextMap.put(version, jaxbContext);
100 * Gets the version from class name.
102 * @param classname the classname
103 * @return the version from class name
105 public Version getVersionFromClassName (String classname) {
106 Matcher m = classNamePattern.matcher(classname);
107 String version = "v2"; //for the OXM, only the v2 ones don't include a model name, hence this default
109 version = m.group(1);
112 return Version.valueOf(version);
116 * Gets the context for URI.
119 * @return the context for URI
121 public DynamicJAXBContext getContextForURI(String uri) {
122 DynamicJAXBContext result = null;
123 Matcher m = uriPattern.matcher(uri);
124 Version version = null;
126 version = Version.valueOf(m.group(1));
127 result = versionContextMap.get(version);
134 * Gets the context for version.
136 * @param version the version
137 * @return the context for version
139 public DynamicJAXBContext getContextForVersion(Version version) {
140 DynamicJAXBContext result = null;
142 result = versionContextMap.get(version);
149 * Gets the dynamic type for class name.
151 * @param classname the classname
152 * @return the dynamic type for class name
154 public DynamicType getDynamicTypeForClassName(String classname) {
155 DynamicType result = null;
156 DynamicJAXBContext context = null;
158 Version version = this.getVersionFromClassName(classname);
160 context = versionContextMap.get(version);
162 if (context != null) {
163 result = context.getDynamicType(classname);
169 public String getOXMFileName(Version v) {
170 return AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + v.toString() + ".xml";