2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.FrameworkUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class Resources {
36 private static final Logger LOG = LoggerFactory.getLogger(Resources.class);
38 private static URL getFileURL(Class<?> cls,String resFile) {
39 Bundle b = FrameworkUtil.getBundle(cls);
41 LOG.debug("try to get file {}", resFile);
43 LOG.info("Load resource as file: {}", resFile);
44 u = getUrlForRessource(cls,resFile);
46 LOG.info("Load resource from bundle: {}", resFile);
47 u = b.getEntry(resFile);
52 private static String readFile(final URL u) throws IOException {
53 return readFile(u.openStream());
56 private static String readFile(final InputStream s) throws IOException {
58 final String LR = "\n";
59 BufferedReader in = new BufferedReader(new InputStreamReader(s));
60 StringBuilder sb = new StringBuilder();
62 while ((inputLine = in.readLine()) != null) {
63 sb.append(inputLine+LR);
70 public static String getFileContent( Class<?> cls, String resFile) {
71 LOG.debug("loading file {} from res", resFile);
72 URL u = getFileURL(cls,resFile);
75 LOG.warn("cannot find resfile: {}", resFile);
80 } catch (Exception e) {
81 LOG.warn("problem reading file: {}", e.getMessage());
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));