ae0d0496bd5a178a0dc62a614f3703c087bc4e67
[ccsdk/features.git] /
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
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.odlux;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.net.URL;
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;
34
35 public class IndexOdluxBundle extends OdluxBundle {
36
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);
46
47         public IndexOdluxBundle() {
48                 super(null, BUNDLENAME_APP);
49
50         }
51         @Override
52         protected String loadFileContent(URL url)
53         {
54                 return this.loadFileContent(url, null);
55         }
56         
57         protected String loadFileContent(URL url, List<String> bundles) {
58                 if (url == null)
59                         return null;
60                 LOG.debug("try to load res " + url.toString());
61                 StringBuilder sb = new StringBuilder();
62                 Matcher matcher;
63                 BufferedReader in;
64                 try {
65                         in = new BufferedReader(new InputStreamReader(url.openStream()));
66
67                         String inputLine;
68                         if (bundles == null) {
69                                 bundles = this.getLoadedBundles();
70                         }
71                         while ((inputLine = in.readLine()) != null) {
72                                 if (url.getFile().endsWith("index.html")) {
73                                         matcher = patternRequire.matcher(inputLine);
74                                         if (matcher.find()) {
75                                                 inputLine = inputLine.substring(0, matcher.start(1)) + "\"" + String.join("\",\"", bundles)
76                                                                 + "\"" + inputLine.substring(matcher.end(1));
77                                         }
78                                         matcher = patternFunction.matcher(inputLine);
79                                         if (matcher.find()) {
80                                                 inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundles)
81                                                                 + inputLine.substring(matcher.end(1));
82                                         }
83                                         matcher = patternFunctionBody.matcher(inputLine);
84                                         if (matcher.find()) {
85                                                 String hlp = "";
86                                                 for (String bundle : bundles) {
87                                                         if (!bundle.equals(BUNDLENAME_APP))
88                                                                 hlp += bundle + ".register();" + LR;
89                                                 }
90                                                 inputLine = inputLine.substring(0, matcher.start(1)) + hlp
91                                                                 + inputLine.substring(matcher.start(1));
92                                         }
93                                 }
94                                 sb.append(inputLine + LR);
95                         }
96                         in.close();
97                 } catch (IOException e) {
98                         LOG.warn("could not load resfile " + url.toString() + ": " + e.getMessage());
99                         return null;
100                 }
101
102                 return sb.toString();
103         }
104
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());
111                 }
112                 list.add(this.getBundleName());
113                 return list;
114         }
115
116         private final Comparator<OdluxBundle> sortorderbundlecomparator = new Comparator<OdluxBundle>() {
117
118                 @Override
119                 public int compare(OdluxBundle arg0, OdluxBundle arg1) {
120
121                         int diff = arg0.getIndex() - arg1.getIndex();
122                         return diff > 0 ? 1 : diff < 0 ? -1 : 0;
123                 }
124         };
125
126         public String getResourceFileContent(String fn, List<String> bundleNames) {
127                 return this.loadFileContent(this.getResource(fn),bundleNames);
128         }
129         public Comparator<OdluxBundle> getBundleSorter() {
130                 return this.sortorderbundlecomparator;
131         }
132 }