summaryrefslogtreecommitdiffstats
path: root/aai-utils/src/main/java
diff options
context:
space:
mode:
authorBogumil Zebek <bogumil.zebek@nokia.com>2018-06-22 12:28:07 +0200
committerBogumil Zebek <bogumil.zebek@nokia.com>2018-06-22 12:28:07 +0200
commitcdfc83e750b72b6ab79a50a5bbbc31456c103673 (patch)
tree6701e5105ce5290b1213e490a35399d91dd5a724 /aai-utils/src/main/java
parent75370173d19e97967d34b3c117f1e3c0820c2553 (diff)
Add missing tests
Change-Id: I27b9210430c877d7d2bd0d3440ed12f702282fe1 Issue-ID: AAI-1235 Signed-off-by: Bogumil Zebek <bogumil.zebek@nokia.com>
Diffstat (limited to 'aai-utils/src/main/java')
-rw-r--r--aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java168
1 files changed, 89 insertions, 79 deletions
diff --git a/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java b/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java
index 4ced1b53..f535509a 100644
--- a/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java
+++ b/aai-utils/src/main/java/org/onap/aaiutils/oxm/OxmModelLoader.java
@@ -21,107 +21,117 @@
*/
package org.onap.aaiutils.oxm;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.xml.bind.JAXBException;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
+import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
-import java.io.*;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import javax.xml.bind.JAXBException;
-
public class OxmModelLoader {
- final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
+ private static final Pattern AAI_OXM_FILE_PATTERN = Pattern.compile("aai_oxm_(.*).xml");
+ private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<>();
+ private static final Logger LOGGER = LoggerFactory.getInstance()
+ .getLogger(OxmModelLoader.class.getName());
- private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap();
+ public synchronized static void loadModels() throws Exception {
+ OxmModelLoader.loadModels("classpath*:/oxm/aai_oxm*.xml", AAI_OXM_FILE_PATTERN);
+ }
+
+ synchronized static void loadModels(String oxmResourcesPattern, Pattern aai_oxm_file_pattern) throws Exception {
+ Resource[] resources = getResources(oxmResourcesPattern);
+
+ for (Resource resource : resources) {
+ Matcher matcher = aai_oxm_file_pattern.matcher(resource.getFilename());
+
+ if (matcher.matches()) {
+ try {
+ OxmModelLoader.loadModel(matcher.group(1), resource);
+ } catch (Exception e) {
+ LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
+ + ": " + e.getMessage());
+ throw new Exception("Failed to load schema");
+ }
+ }
+ }
+ }
+
+ private static Resource[] getResources(String oxmResourcesPattern) throws Exception {
+ ClassLoader cl = OxmModelLoader.class.getClassLoader();
+ ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
+
+ Resource[] resources = resolver.getResources(oxmResourcesPattern);
+ if (resources.length == 0) {
+ LOGGER.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
+ throw new Exception("Failed to load schema");
+ }
+ return resources;
- private static org.onap.aai.cl.api.Logger logger = LoggerFactory.getInstance()
- .getLogger(OxmModelLoader.class.getName());
- public synchronized static void loadModels() throws Exception {
- ClassLoader cl = OxmModelLoader.class.getClassLoader();
- ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
- Resource[] resources;
- try {
- resources = resolver.getResources("classpath*:/oxm/aai_oxm*.xml");
- } catch (IOException ex) {
- logger.error( OxmModelLoaderMsgs.OXM_LOAD_ERROR, ex.getMessage());
- throw new FileNotFoundException("OXM files not found on classpath.");
}
- if (resources.length == 0) {
- logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "No OXM schema files found on classpath");
- throw new Exception("Failed to load schema");
+ public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
+ if (versionContextMap == null || versionContextMap.isEmpty()) {
+ loadModels();
+ } else if (!versionContextMap.containsKey(version)) {
+ String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
+ try {
+ loadModel(version, new File(filename));
+ } catch (Exception e) {
+ throw new FileNotFoundException(filename);
+ }
+ }
+
+ return versionContextMap.get(version);
}
- for (Resource resource : resources) {
- Matcher matcher = p.matcher(resource.getFilename());
+ public static Map<String, DynamicJAXBContext> getVersionContextMap() {
+ return Collections.unmodifiableMap(versionContextMap);
+ }
- if (matcher.matches()) {
- try {
- OxmModelLoader.loadModel(matcher.group(1), resource);
- } catch (Exception e) {
- logger.error(OxmModelLoaderMsgs.OXM_LOAD_ERROR, "Failed to load " + resource.getFilename()
- + ": " + e.getMessage());
- throw new Exception("Failed to load schema");
- }
- }
+ public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
+ OxmModelLoader.versionContextMap = versionContextMap;
}
- }
-
- public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
- if (versionContextMap == null || versionContextMap.isEmpty()) {
- loadModels();
- } else if (!versionContextMap.containsKey(version)) {
- String filename = OxmModelLoaderConstants.AaiUtils_HOME_MODEL + "aai_oxm_" + version + ".xml";
- try {
- loadModel(version, new File(filename));
- } catch (Exception e) {
- throw new FileNotFoundException(filename);
- }
+
+ private synchronized static void loadModel(String version, File file) throws JAXBException, IOException {
+ InputStream inputStream = new FileInputStream(file);
+ loadModel(version, file.getName(), inputStream);
+ }
+
+ private synchronized static void loadModel(String version, Resource resource)
+ throws JAXBException, IOException {
+ InputStream inputStream = resource.getInputStream();
+ loadModel(version, resource.getFilename(), inputStream);
}
- return versionContextMap.get(version);
- }
-
- public static Map<String, DynamicJAXBContext> getVersionContextMap() {
- return versionContextMap;
- }
-
- public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
- OxmModelLoader.versionContextMap = versionContextMap;
- }
-
- private synchronized static void loadModel ( String version, File file )
- throws JAXBException, IOException {
- InputStream inputStream = new FileInputStream ( file );
- loadModel ( version, file.getName (), inputStream );
- }
-
- private synchronized static void loadModel ( String version, Resource resource )
- throws JAXBException, IOException {
- InputStream inputStream = resource.getInputStream ();
- loadModel ( version, resource.getFilename (), inputStream );
- }
-
- private synchronized static void loadModel ( String version, String resourceName,
- InputStream inputStream )
- throws JAXBException, IOException {
- Map<String, Object> properties = new HashMap<String, Object> ();
- properties.put ( JAXBContextProperties.OXM_METADATA_SOURCE, inputStream );
- final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
- .createContextFromOXM ( Thread.currentThread ().getContextClassLoader (), properties );
- versionContextMap.put ( version, jaxbContext );
- logger.info ( OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName );
+ private synchronized static void loadModel(String version, String resourceName, InputStream inputStream)
+ throws JAXBException, IOException {
+
+ Map<String, Object> properties = new HashMap<>();
+ properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, inputStream);
+
+ final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
+ .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
+
+ versionContextMap.put(version, jaxbContext);
+
+ LOGGER.info(OxmModelLoaderMsgs.LOADED_OXM_FILE, resourceName);
}
}