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.model.bundles;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.List;
31 * At startup of each karaf bundle, each UI module creates an instance of this class via blueprint.
32 * Initialize method gets called at loading of bundle.
35 public class OdluxBundle {
37 final static Logger LOG = LoggerFactory.getLogger(OdluxBundle.class);
38 private static final String LR = "\n";
40 private String bundleName;
41 private OdluxBundleLoader loader;
47 public int getIndex() {
52 * @param index the index to set
54 public void setIndex(int index) {
58 public OdluxBundleLoader getLoader() {
62 public void setLoader(OdluxBundleLoader loader) {
66 public void setBundleName(String bundleName) {
67 this.bundleName = bundleName;
70 public String getBundleName() {
71 return this.bundleName;
74 public OdluxBundle() {}
76 protected OdluxBundle(final OdluxBundleLoader loader, final String bundleName) {
78 this.bundleName = bundleName;
81 public void initialize() {
83 LOG.info("Registering resources");
85 List<String> resources = this.getResourceFiles("/odlux");
86 if (resources.size() > 0) {
87 for (String res : resources) {
88 LOG.debug("found res " + res);
91 String res = "/odlux/" + this.getBundleName() + ".js";
92 if (this.hasResource(res)) {
93 LOG.debug("found res " + res);
95 LOG.warn("no resources found in bundle");
97 } catch (IOException e) {
98 LOG.debug("problem searching for resources: " + e.getMessage());
100 if (this.loader != null) {
101 if (this.bundleName == null)
102 LOG.error("bundle name is missing. Bundle can not be registered with odlux");
104 LOG.info("Registering bunlde {}", this.bundleName);
105 this.loader.addBundle(this);
110 public void clean() {
111 LOG.info("Unregistering resources");
113 if (this.loader != null) {
114 this.loader.removeBundle(this);
118 public boolean hasResource(String filename) {
119 return this.getResource(filename) != null;
122 public String getResourceFileContent(String filename) {
123 return this.loadFileContent(this.getResource(filename));
126 protected URL getResource(String filename) {
127 return ClassLoaderUtilExt.getResource(filename, this.getClass());
130 protected String loadFileContent(final URL url ) {
133 LOG.debug("try to load res " + url.toString());
134 StringBuilder sb = new StringBuilder();
137 in = new BufferedReader(new InputStreamReader(url.openStream()));
140 while ((inputLine = in.readLine()) != null) {
141 sb.append(inputLine + LR);
144 } catch (IOException e) {
145 LOG.warn("could not load resfile " + url.toString() + ": " + e.getMessage());
149 return sb.toString();
152 private List<String> getResourceFiles(String path) throws IOException {
153 List<String> filenames = new ArrayList<>();
157 InputStream in = getResourceAsStream(path);
159 BufferedReader br = new BufferedReader(new InputStreamReader(in));
163 while ((resource = br.readLine()) != null) {
164 filenames.add(resource);
168 } catch (Exception e) {
169 LOG.warn("problem loading res " + path);
175 private InputStream getResourceAsStream(String resource) {
176 final InputStream in = getContextClassLoader().getResourceAsStream(resource);
178 return in == null ? getClass().getResourceAsStream(resource) : in;
181 private ClassLoader getContextClassLoader() {
182 return Thread.currentThread().getContextClassLoader();