Merge "Refactoring data-provider:provider generateDTOs"
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / Resources.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.net.URL;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.FrameworkUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class Resources {
35
36     private static final Logger LOG = LoggerFactory.getLogger(Resources.class);
37
38     private static URL getFileURL(Class<?> cls,String resFile) {
39         Bundle b = FrameworkUtil.getBundle(cls);
40         URL u = null;
41         LOG.debug("try to get file {}", resFile);
42         if (b == null) {
43             LOG.info("Load resource as file: {}", resFile);
44             u = getUrlForRessource(cls,resFile);
45         } else {
46             LOG.info("Load resource from bundle: {}", resFile);
47             u = b.getEntry(resFile);
48         }
49         return u;
50     }
51
52     private static String readFile(final URL u) throws IOException {
53         return readFile(u.openStream());
54     }
55
56     private static String readFile(final InputStream s) throws IOException {
57         // read file
58         final String LR = "\n";
59         BufferedReader in = new BufferedReader(new InputStreamReader(s));
60         StringBuilder sb = new StringBuilder();
61         String inputLine;
62         while ((inputLine = in.readLine()) != null) {
63             sb.append(inputLine+LR);
64         }
65         in.close();
66         s.close();
67         return sb.toString();
68     }
69
70     public static String getFileContent( Class<?> cls, String resFile) {
71          LOG.debug("loading file {} from res", resFile);
72          URL u = getFileURL(cls,resFile);
73          String s=null;
74          if (u == null) {
75              LOG.warn("cannot find resfile: {}", resFile);
76              return null;
77          }
78          try {
79              s=readFile(u);
80          } catch (Exception e) {
81              LOG.warn("problem reading file: {}", e.getMessage());
82          }
83          return s;
84
85     }
86
87     public static URL getUrlForRessource(Class<?> cls,String fileOrDirectory) {
88         //ClassLoader loader = Thread.currentThread().getContextClassLoader();
89         ClassLoader loader = cls.getClassLoader();
90         URL url = loader.getResource(fileOrDirectory);
91         if(url==null && fileOrDirectory.startsWith("/")) {
92             url = loader.getResource(fileOrDirectory.substring(1));
93         }
94         return url;
95     }
96 }