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 and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.datarouter.util;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Timer;
38 import java.util.TimerTask;
39 import java.util.concurrent.ConcurrentHashMap;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 import javax.ws.rs.core.Response.Status;
44 import javax.xml.bind.JAXBException;
46 import org.eclipse.persistence.jaxb.JAXBContextProperties;
47 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
48 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
49 import org.openecomp.cl.eelf.LoggerFactory;
50 import org.openecomp.datarouter.logging.DataRouterMsgs;
52 import org.openecomp.datarouter.util.ExternalOxmModelProcessor;
54 public class OxmModelLoader {
56 private static Map<String, DynamicJAXBContext> versionContextMap = new ConcurrentHashMap<String, DynamicJAXBContext>();
57 private static Map<String, Timer> timers = new ConcurrentHashMap<String, Timer>();
58 private static List<ExternalOxmModelProcessor> oxmModelProcessorRegistry = new ArrayList<ExternalOxmModelProcessor>();
59 final static Pattern p = Pattern.compile("aai_oxm_(.*).xml");
63 private static org.openecomp.cl.api.Logger logger = LoggerFactory.getInstance()
64 .getLogger(OxmModelLoader.class.getName());
66 public synchronized static void loadModels() {
68 File[] listOfFiles = new File(DataRouterConstants.DR_HOME_MODEL).listFiles();
70 if (listOfFiles != null) {
71 for (File file : listOfFiles) {
73 Matcher m = p.matcher(file.getName());
76 OxmModelLoader.loadModel(m.group(1), file);
77 } catch (Exception e) {
78 logger.error(DataRouterMsgs.INVALID_OXM_FILE, file.getName(), e.getMessage());
85 logger.error(DataRouterMsgs.INVALID_OXM_DIR, DataRouterConstants.DR_HOME_MODEL);
91 private static void addtimer(String version,File file){
92 TimerTask task = null;
93 task = new FileWatcher(
95 protected void onChange(File file) {
96 // here we implement the onChange
97 logger.info(DataRouterMsgs.FILE_CHANGED, file.getName());
100 OxmModelLoader.loadModel(version,file);
101 } catch (Exception e) {
108 if (!timers.containsKey(version)) {
109 Timer timer = new Timer("oxm-"+version);
110 timer.schedule(task, new Date(), 10000);
111 timers.put(version, timer);
116 private synchronized static void loadModel(String version,File file) throws JAXBException, FileNotFoundException {
119 InputStream iStream = new FileInputStream(file);
120 Map<String, Object> properties = new HashMap<String, Object>();
121 properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream);
122 final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory
123 .createContextFromOXM(Thread.currentThread().getContextClassLoader(), properties);
124 versionContextMap.put(version, jaxbContext);
125 if ( oxmModelProcessorRegistry != null) {
126 for ( ExternalOxmModelProcessor processor : oxmModelProcessorRegistry ) {
127 processor.onOxmVersionChange(Version.valueOf(version), jaxbContext );
130 addtimer(version,file);
134 public static DynamicJAXBContext getContextForVersion(String version) throws Exception {
135 if (versionContextMap == null || versionContextMap.isEmpty()) {
137 } else if (!versionContextMap.containsKey(version)) {
139 loadModel(version,new File (DataRouterConstants.DR_HOME_MODEL + "aai_oxm_" + version + ".xml"));
140 } catch (Exception e) {
141 throw new Exception(Status.NOT_FOUND.toString());
145 return versionContextMap.get(version);
148 public static Map<String, DynamicJAXBContext> getVersionContextMap() {
149 return versionContextMap;
152 public static void setVersionContextMap(Map<String, DynamicJAXBContext> versionContextMap) {
153 OxmModelLoader.versionContextMap = versionContextMap;
156 public synchronized static void registerExternalOxmModelProcessors(Collection<ExternalOxmModelProcessor> processors) {
157 if(processors != null) {
158 for(ExternalOxmModelProcessor processor : processors) {
159 if(!oxmModelProcessorRegistry.contains(processor)) {
160 oxmModelProcessorRegistry.add(processor);