a4a0e76c3d9fb5f8c41e07df2792aaf3d6d6b412
[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 java.io.IOException;
21 import java.net.URL;
22 import java.util.Iterator;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.FrameworkUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import com.opensymphony.xwork2.util.ClassLoaderUtil;
28
29 public class ClassLoaderUtilExt {
30
31     final static Logger LOG = LoggerFactory.getLogger(ClassLoaderUtilExt.class);
32
33     public static URL getResource(String resourceName, Class<?> callingClass) {
34
35         Iterator<URL> urls = null;
36         URL url = null;
37         try {
38             urls = getResources(resourceName, callingClass, true);
39         } catch (IOException e) {
40             LOG.debug("No resource {}", resourceName);
41         }
42         if (urls != null) {
43             while (urls.hasNext()) {
44                 URL u = urls.next();
45                 LOG.debug("found " + u.toString());
46                 url = u;
47                 break;
48             }
49         }
50         if (url == null) {
51             LOG.debug("res currently not found. try to find with bundle");
52             Bundle b = FrameworkUtil.getBundle(callingClass);
53             if (b != null) {
54                 url = b.getEntry(resourceName);
55             } else {
56                 ClassLoader loader = Thread.currentThread().getContextClassLoader();
57                 url = loader.getResource(resourceName);
58             }
59
60         }
61         return url;
62     }
63
64     public static Iterator<URL> getResources(String resourceName, Class<?> callingClass, boolean aggregate)
65             throws IOException {
66         return ClassLoaderUtil.getResources(resourceName, callingClass, aggregate);
67     }
68
69 }