Here you can find the source of loadResource(final Class> bundleClazz, final Class
Parameter | Description |
---|---|
bundleClazz | the class type |
resourceTypeclazz | the resource type |
pathToFile | the path to file |
Parameter | Description |
---|---|
IOException | an exception |
public static <T> T loadResource(final Class<?> bundleClazz, final Class<T> resourceTypeclazz, final String pathToFile) throws IOException
//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; } }