aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/common/src/main/java/org/onap/ccsdk/features/sdnr/wt/common/Resources.java
blob: 1ed74a1eb12c566d7ea0f02ce54df706725201b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Resources {

    private static final Logger LOG = LoggerFactory.getLogger(Resources.class);

    private static URL getFileURL(Class<?> cls,String resFile) {
        Bundle b = FrameworkUtil.getBundle(cls);
        URL u = null;
        LOG.debug("try to get file {}", resFile);
        if (b == null) {
            LOG.info("Load resource as file: {}", resFile);
            u = getUrlForRessource(cls,resFile);
        } else {
            LOG.info("Load resource from bundle: {}", resFile);
            u = b.getEntry(resFile);
        }
        return u;
    }

    private static String readFile(final URL u) throws IOException {
        return readFile(u.openStream());
    }

    private static String readFile(final InputStream s) throws IOException {
        // read file
        final String LR = "\n";
        BufferedReader in = new BufferedReader(new InputStreamReader(s));
        StringBuilder sb = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine+LR);
        }
        in.close();
        s.close();
        return sb.toString();
    }

    public static String getFileContent( Class<?> cls, String resFile) {
         LOG.debug("loading file {} from res", resFile);
         URL u = getFileURL(cls,resFile);
         String s=null;
         if (u == null) {
             LOG.warn("cannot find resfile: {}", resFile);
             return null;
         }
         try {
             s=readFile(u);
         } catch (Exception e) {
             LOG.warn("problem reading file: {}", e.getMessage());
         }
         return s;

    }

    public static URL getUrlForRessource(Class<?> cls,String fileOrDirectory) {
        //ClassLoader loader = Thread.currentThread().getContextClassLoader();
        ClassLoader loader = cls.getClassLoader();
        URL url = loader.getResource(fileOrDirectory);
        if(url==null && fileOrDirectory.startsWith("/")) {
            url = loader.getResource(fileOrDirectory.substring(1));
        }
        return url;
    }
}