448169fad6519376405e8f8a8f1d4ab6d66daf9e
[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.model.bundles;
19
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;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
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.
33  */
34
35 public class OdluxBundle {
36
37     final static Logger LOG = LoggerFactory.getLogger(OdluxBundle.class);
38     private static final String LR = "\n";
39
40     private String bundleName;
41     private OdluxBundleLoader loader;
42     private int index;
43
44     /**
45      * @return the index
46      */
47     public int getIndex() {
48         return index;
49     }
50
51     /**
52      * @param index the index to set
53      */
54     public void setIndex(int index) {
55         this.index = index;
56     }
57
58     public OdluxBundleLoader getLoader() {
59         return loader;
60     }
61
62     public void setLoader(OdluxBundleLoader loader) {
63         this.loader = loader;
64     }
65
66     public void setBundleName(String bundleName) {
67         this.bundleName = bundleName;
68     }
69
70     public String getBundleName() {
71         return this.bundleName;
72     }
73
74     public OdluxBundle() {}
75
76     protected OdluxBundle(final OdluxBundleLoader loader, final String bundleName) {
77         this.loader = loader;
78         this.bundleName = bundleName;
79     }
80
81     public void initialize() {
82
83         LOG.info("Registering resources");
84         try {
85             List<String> resources = this.getResourceFiles("/odlux");
86             if (resources.size() > 0) {
87                 for (String res : resources) {
88                     LOG.debug("found res " + res);
89                 }
90             } else {
91                 String res = "/odlux/" + this.getBundleName() + ".js";
92                 if (this.hasResource(res)) {
93                     LOG.debug("found res " + res);
94                 } else
95                     LOG.warn("no resources found in bundle");
96             }
97         } catch (IOException e) {
98             LOG.debug("problem searching for resources: " + e.getMessage());
99         }
100         if (this.loader != null) {
101             if (this.bundleName == null)
102                 LOG.error("bundle name is missing. Bundle can not be registered with odlux");
103             else {
104                 LOG.info("Registering bunlde {}", this.bundleName);
105                 this.loader.addBundle(this);
106             }
107         }
108     }
109
110     public void clean() {
111         LOG.info("Unregistering resources");
112
113         if (this.loader != null) {
114             this.loader.removeBundle(this);
115         }
116     }
117
118     public boolean hasResource(String filename) {
119         return this.getResource(filename) != null;
120     }
121
122     public String getResourceFileContent(String filename) {
123         return this.loadFileContent(this.getResource(filename));
124     }
125   
126     protected URL getResource(String filename) {
127         return ClassLoaderUtilExt.getResource(filename, this.getClass());
128     }
129
130     protected String loadFileContent(final URL url ) {
131         if (url == null)
132             return null;
133         LOG.debug("try to load res " + url.toString());
134         StringBuilder sb = new StringBuilder();
135         BufferedReader in;
136         try {
137             in = new BufferedReader(new InputStreamReader(url.openStream()));
138
139             String inputLine;
140             while ((inputLine = in.readLine()) != null) {
141                 sb.append(inputLine + LR);
142             }
143             in.close();
144         } catch (IOException e) {
145             LOG.warn("could not load resfile " + url.toString() + ": " + e.getMessage());
146             return null;
147         }
148
149         return sb.toString();
150     }
151
152     private List<String> getResourceFiles(String path) throws IOException {
153         List<String> filenames = new ArrayList<>();
154
155         try {
156
157             InputStream in = getResourceAsStream(path);
158             if (in != null) {
159                 BufferedReader br = new BufferedReader(new InputStreamReader(in));
160                 if (br != null) {
161                     String resource;
162
163                     while ((resource = br.readLine()) != null) {
164                         filenames.add(resource);
165                     }
166                 }
167             }
168         } catch (Exception e) {
169             LOG.warn("problem loading res " + path);
170         }
171
172         return filenames;
173     }
174
175     private InputStream getResourceAsStream(String resource) {
176         final InputStream in = getContextClassLoader().getResourceAsStream(resource);
177
178         return in == null ? getClass().getResourceAsStream(resource) : in;
179     }
180
181     private ClassLoader getContextClassLoader() {
182         return Thread.currentThread().getContextClassLoader();
183     }
184 }