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==========================================================================
 
  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.List;
 
  25 import java.util.regex.Matcher;
 
  26 import java.util.regex.Pattern;
 
  27 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundle;
 
  28 import org.onap.ccsdk.features.sdnr.wt.odlux.model.bundles.OdluxBundleResourceAccess;
 
  29 import org.slf4j.Logger;
 
  30 import org.slf4j.LoggerFactory;
 
  32 public class IndexOdluxBundle extends OdluxBundle implements OdluxBundleResourceAccess {
 
  34     private static Logger LOG = LoggerFactory.getLogger(IndexOdluxBundle.class);
 
  36     private static final String LR = "\n";
 
  37     private static final String BUNDLENAME_APP = "run";
 
  38     private static final String regexRequire = "require\\(\\[(\"" + BUNDLENAME_APP + "\")\\]";
 
  39     private static final String regexFunction = "function[\\ ]*\\((" + BUNDLENAME_APP + ")\\)[\\ ]*\\{";
 
  40     private static final String regexFunctionBody = "(" + BUNDLENAME_APP + "\\.runApplication\\(\\);)";
 
  41     private static final Pattern patternRequire = Pattern.compile(regexRequire);
 
  42     private static final Pattern patternFunction = Pattern.compile(regexFunction);
 
  43     private static final Pattern patternFunctionBody = Pattern.compile(regexFunctionBody);
 
  45     public IndexOdluxBundle() {
 
  46         super(null, BUNDLENAME_APP);
 
  51     protected String loadFileContent(URL url) {
 
  52         return loadFileContent(url, OdluxBundleLoaderImpl.getInstance().getLoadedBundles(this.getBundleName()));
 
  56     public String getResourceFileContent(String fn, List<String> bundleNames) {
 
  57         return loadFileContent(this.getResource(fn), bundleNames);
 
  60     private static String loadFileContent(URL url, List<String> bundlesNamesList) {
 
  64         LOG.debug("try to load res " + url.toString());
 
  65         StringBuilder sb = new StringBuilder();
 
  67         BufferedReader in = null;
 
  69             in = new BufferedReader(new InputStreamReader(url.openStream()));
 
  72             while ((inputLine = in.readLine()) != null) {
 
  73                 if (url.getFile().endsWith("index.html")) {
 
  74                     matcher = patternRequire.matcher(inputLine);
 
  76                         inputLine = inputLine.substring(0, matcher.start(1)) + "\""
 
  77                                 + String.join("\",\"", bundlesNamesList) + "\"" + inputLine.substring(matcher.end(1));
 
  79                     matcher = patternFunction.matcher(inputLine);
 
  81                         inputLine = inputLine.substring(0, matcher.start(1)) + String.join(",", bundlesNamesList)
 
  82                                 + inputLine.substring(matcher.end(1));
 
  84                     matcher = patternFunctionBody.matcher(inputLine);
 
  87                         for (String bundle : bundlesNamesList) {
 
  88                             if (!bundle.equals(BUNDLENAME_APP)) {
 
  89                                 hlp += bundle + ".register();" + LR;
 
  93                                 inputLine.substring(0, matcher.start(1)) + hlp + inputLine.substring(matcher.start(1));
 
  96                 sb.append(inputLine + LR);
 
  99         } catch (IOException e) {
 
 100             LOG.warn("could not load resfile {} : {}", url, e.getMessage());
 
 107             } catch (IOException e) {
 
 112         return sb.toString();