ab69d63c8090d51f7898b0f2f52db452b777f71c
[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.InputStreamReader;
25 import java.net.URL;
26
27 /**
28  * At startup of each karaf bundle, each UI module creates an instance of this class via blueprint. Initialize method
29  * gets called at loading of bundle.
30  */
31
32 public class OdluxBundle {
33
34     final static Logger LOG = LoggerFactory.getLogger(OdluxBundle.class);
35     private static final String LR = "\n";
36
37     private String bundleName;
38     private OdluxBundleLoader loader;
39     private int index;
40
41     /**
42      * @return the index
43      */
44     public int getIndex() {
45         return index;
46     }
47
48     /**
49      * @param index the index to set
50      */
51     public void setIndex(int index) {
52         this.index = index;
53     }
54
55     public OdluxBundleLoader getLoader() {
56         return loader;
57     }
58
59     public void setLoader(OdluxBundleLoader loader) {
60         this.loader = loader;
61     }
62
63     public void setBundleName(String bundleName) {
64         this.bundleName = bundleName;
65     }
66
67     public String getBundleName() {
68         return this.bundleName;
69     }
70
71     public OdluxBundle() {}
72
73     protected OdluxBundle(final OdluxBundleLoader loader, final String bundleName) {
74         this.loader = loader;
75         this.bundleName = bundleName;
76     }
77
78     public void initialize() {
79
80         LOG.info("Registering resources");
81         if (this.loader != null) {
82             if (this.bundleName == null)
83                 LOG.error("bundle name is missing. Bundle can not be registered with odlux");
84             else {
85                 LOG.info("Registering bunlde {}", this.bundleName);
86                 this.loader.addBundle(this);
87             }
88         }
89     }
90
91     public void clean() {
92         LOG.info("Unregistering resources");
93
94         if (this.loader != null) {
95             this.loader.removeBundle(this);
96         }
97     }
98
99     public boolean hasResource(String filename) {
100         return this.getResource(filename) != null;
101     }
102
103     public String getResourceFileContent(String filename) {
104         return this.loadFileContent(this.getResource(filename));
105     }
106
107     protected URL getResource(String filename) {
108         return ClassLoaderUtilExt.getResource(filename, this.getClass());
109     }
110
111     protected String loadFileContent(final URL url) {
112         if (url == null)
113             return null;
114         LOG.debug("try to load res " + url.toString());
115         StringBuilder sb = new StringBuilder();
116         BufferedReader in = null;
117         try {
118             in = new BufferedReader(new InputStreamReader(url.openStream()));
119
120             String inputLine;
121             while ((inputLine = in.readLine()) != null) {
122                 sb.append(inputLine + LR);
123             }
124         } catch (IOException e) {
125             LOG.warn("could not load resfile " + url.toString() + ": " + e.getMessage());
126             return null;
127         } finally {
128             if (in != null) {
129                 try {
130                     in.close();
131                 } catch (IOException e) {
132
133                 }
134             }
135         }
136
137         return sb.toString();
138     }
139
140 }