Java Resource Path Get loadResource(final Class bundleClazz, final Class resourceTypeclazz, final String pathToFile)

Here you can find the source of loadResource(final Class bundleClazz, final Class resourceTypeclazz, final String pathToFile)

Description

Loads a resource from any OSGi Bundle

License

Apache License

Parameter

Parameter Description
bundleClazz the class type
resourceTypeclazz the resource type
pathToFile the path to file

Exception

Parameter Description
IOException an exception

Declaration

public static <T> T loadResource(final Class<?> bundleClazz, final Class<T> resourceTypeclazz,
        final String pathToFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 Amit Kumar Mondal <admin@amitinside.com>
 *
 * 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.//  w w w . ja v  a 2  s  .  c  o  m
 *******************************************************************************/

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import org.osgi.framework.Bundle;

import org.osgi.framework.FrameworkUtil;

public class Main {
    /**
     * Loads a resource from any OSGi Bundle
     *
     * @param bundleClazz
     *            the class type
     * @param resourceTypeclazz
     *            the resource type
     * @param pathToFile
     *            the path to file
     * @return
     * @throws IOException
     */
    public static <T> T loadResource(final Class<?> bundleClazz, final Class<T> resourceTypeclazz,
            final String pathToFile) throws IOException {
        final Bundle bundle = FrameworkUtil.getBundle(bundleClazz);
        final URL url = bundle.getEntry(pathToFile);
        final InputStream stream = new FileInputStream(url.getFile());

        try {
            if (resourceTypeclazz.isInstance(InputStream.class)) {
                return resourceTypeclazz.cast(stream);
            }
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            stream.close();
        }

        return null;
    }
}

Related

  1. getResourceStream(final String bundle, final String path)
  2. getResourceStream(String path)
  3. getStringFromResource(Class clazz, String path)
  4. getSystemResource(String path)
  5. getSystemResource(String path)
  6. loadResources(ClassLoader loader, String path)
  7. resourceExists(String path)