1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
30 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleList;
31 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleLoaderImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class IndexOdluxBundle extends OdluxBundle {
37 private static final String LR = "\n";
38 private static Logger LOG = LoggerFactory.getLogger(IndexOdluxBundle.class);
39 private static final String BUNDLENAME_APP = "run";
40 private static final String regexRequire = "require\\(\\[(\"" + BUNDLENAME_APP + "\")\\]";
41 private static final String regexFunction = "function[\\ ]*\\((" + BUNDLENAME_APP + ")\\)[\\ ]*\\{";
42 private static final String regexFunctionBody = "(" + BUNDLENAME_APP + "\\.runApplication\\(\\);)";
43 private static final Pattern patternRequire = Pattern.compile(regexRequire);
44 private static final Pattern patternFunction = Pattern.compile(regexFunction);
45 private static final Pattern patternFunctionBody = Pattern.compile(regexFunctionBody);
47 public IndexOdluxBundle() {
48 super(null, BUNDLENAME_APP);
52 protected String loadFileContent(URL url)
54 return this.loadFileContent(url, null);
57 protected String loadFileContent(URL url, List<String> bundles) {
60 LOG.debug("try to load res " + url.toString());
61 StringBuilder sb = new StringBuilder();
65 in = new BufferedReader(new InputStreamReader(url.openStream()));
68 if (bundles == null) {
69 bundles = this.getLoadedBundles();
71 while ((inputLine = in.readLine()) != null) {
72 if (url.getFile().endsWith("index.html")) {
73 matcher = patternRequire.matcher(inputLine);
75 inputLine = inputLine.substring(0, matcher.start(1)) + "\"" + String.join("\",\"", bundles)
76 + "\"" + inputLine.substring(matcher.end(1));
78 matcher = patternFunction.matcher(inputLine);
80 inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundles)
81 + inputLine.substring(matcher.end(1));
83 matcher = patternFunctionBody.matcher(inputLine);
86 for (String bundle : bundles) {
87 if (!bundle.equals(BUNDLENAME_APP))
88 hlp += bundle + ".register();" + LR;
90 inputLine = inputLine.substring(0, matcher.start(1)) + hlp
91 + inputLine.substring(matcher.start(1));
94 sb.append(inputLine + LR);
97 } catch (IOException e) {
98 LOG.warn("could not load resfile " + url.toString() + ": " + e.getMessage());
102 return sb.toString();
105 private List<String> getLoadedBundles() {
106 List<OdluxBundle> bundles = OdluxBundleLoaderImpl.getInstance().getBundles();
107 List<String> list = new ArrayList<String>();
108 bundles.sort(sortorderbundlecomparator);
109 for (OdluxBundle b : bundles) {
110 list.add(b.getBundleName());
112 list.add(this.getBundleName());
116 private final Comparator<OdluxBundle> sortorderbundlecomparator = new Comparator<OdluxBundle>() {
119 public int compare(OdluxBundle arg0, OdluxBundle arg1) {
121 int diff = arg0.getIndex() - arg1.getIndex();
122 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
126 public String getResourceFileContent(String fn, List<String> bundleNames) {
127 return this.loadFileContent(this.getResource(fn),bundleNames);
129 public Comparator<OdluxBundle> getBundleSorter() {
130 return this.sortorderbundlecomparator;