2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.datarouter.util;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Timer;
36 import java.util.TimerTask;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 import javax.ws.rs.core.Response.Status;
42 import javax.xml.bind.JAXBException;
44 import org.eclipse.persistence.jaxb.JAXBContextProperties;
45 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
46 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
47 import org.openecomp.cl.eelf.LoggerFactory;
48 import org.openecomp.datarouter.logging.DataRouterMsgs;
50 import org.openecomp.datarouter.util.ExternalOxmModelProcessor;
52 public class OxmModelLoader {
54 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>();
55 private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
56 private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<ExternalOxmModelProcessor>();
57 final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
61 private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
62 .getLogger(OxmModelLoader.class.getName());
64 public synchronized static void loadModels() {
66 File[] listOfFiles = new File(DataRouterConstants.DR_HOME_MODEL).listFiles();
68 if (listOfFiles != null) {
69 for (File file : listOfFiles) {
71 Matcher m = p.matcher(file.getName());
74 OxmModelLoader.loadModel(m.group(1), file);
75 } catch (Exception e) {
76 logger.error(DataRouterMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
83 logger.error(DataRouterMsgs.INVALID_OXM_DIR, DataRouterConstants.DR_HOME_MODEL);
89 private static void addtimer(String version,File file){
90 TimerTask task = null;
91 task = new FileWatcher(
93 protected void onChange(File file) {
94 // here we implement the onChange
95 logger.info(DataRouterMsgs.FILE_CHANGED, file.getName());
98 OxmModelLoader.loadModel(version,file);
99 } catch (Exception e) {
106 if (!timers.containsKey(version)) {
107 Timer timer = new Timer("oxm-"+version);
108 timer.schedule(task, new Date(), 10000);
109 timers.put(version, timer);
114 private synchronized static void loadModel(String version,File file) throws JAXBException, FileNotFoundException {
117 InputStream iStream = new FileInputStream(file);
118 Map<String, Object> properties = new HashMap<String, Object>();
119 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
120 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
121 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
122 versionContextMap.put(version, jaxbContext);
123 if ( oxmModelProcessorRegistry != null) {
124 for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
125 processor.onOxmVersionChange(Version.valueOf(version), jaxbContext );
128 addtimer(version,file);
132 public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
133 if (versionContextMap == null || versionContextMap.isEmpty()) {
135 } else if (!versionContextMap.containsKey(version)) {
137 loadModel(version,new File (DataRouterConstants.DR_HOME_MODEL + "aai_oxm_" + version + ".xml"));
138 } catch (Exception e) {
139 throw new Exception(Status.NOT_FOUND.toString());
143 return versionContextMap.get(version);
146 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
147 return versionContextMap;
150 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
151 OxmModelLoader.versionContextMap = versionContextMap;
154 public synchronized static void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
155 if(processors != null) {
156 for(ExternalOxmModelProcessor processor : processors) {
157 if(!oxmModelProcessorRegistry.contains(processor)) {
158 oxmModelProcessorRegistry.add(processor);