michael@18: /** michael@18: * Copyright (c) 2012, Ben Fortuna michael@18: * All rights reserved. michael@18: * michael@18: * Redistribution and use in source and binary forms, with or without michael@18: * modification, are permitted provided that the following conditions michael@18: * are met: michael@18: * michael@18: * o Redistributions of source code must retain the above copyright michael@18: * notice, this list of conditions and the following disclaimer. michael@18: * michael@18: * o Redistributions in binary form must reproduce the above copyright michael@18: * notice, this list of conditions and the following disclaimer in the michael@18: * documentation and/or other materials provided with the distribution. michael@18: * michael@18: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@18: * may be used to endorse or promote products derived from this software michael@18: * without specific prior written permission. michael@18: * michael@18: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@18: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@18: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@18: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@18: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@18: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@18: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@18: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@18: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@18: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@18: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@18: */ michael@18: package net.fortuna.ical4j.util; michael@18: michael@18: import java.io.InputStream; michael@18: import java.net.URL; michael@18: michael@18: import android.util.Log; michael@18: michael@18: /** michael@18: * @author fortuna michael@18: * michael@18: */ michael@18: public class ResourceLoader { michael@18: michael@18: /** michael@18: * Load a resource via the thread context classloader. If security permissions don't allow michael@18: * this fallback to loading via current classloader. michael@18: * @param name a resource name michael@18: * @return a {@link URL} or null if resource is not found michael@18: */ michael@18: public static URL getResource(String name) { michael@18: URL resource = null; michael@18: try { michael@18: // Hack to bootstrap a multithreaded class loader context michael@18: if (Thread.currentThread().getContextClassLoader() == null) michael@18: Thread.currentThread().setContextClassLoader(ResourceLoader.class.getClassLoader()); michael@18: resource = Thread.currentThread().getContextClassLoader().getResource(name); michael@18: michael@18: if (resource == null) // Flawed build path for assets, try again michael@18: resource = Thread.currentThread().getContextClassLoader().getResource("/" + name); michael@18: } michael@18: catch (SecurityException e) { michael@18: Log.i("MSvB-Hack", "Unable to access context classloader, using default. " + e.getMessage()); michael@18: } michael@18: catch (Exception e) { michael@18: Log.i("MSvB-Hack", "General context classloader error, using default. " + e.getMessage()); michael@18: } michael@18: if (resource == null) { michael@18: resource = ResourceLoader.class.getResource("/" + name); michael@18: } michael@18: return resource; michael@18: } michael@18: michael@18: /** michael@18: * Load a resource via the thread context classloader. If security permissions don't allow michael@18: * this fallback to loading via current classloader. michael@18: * @param name a resource name michael@18: * @return an {@link InputStream} or null if resource is not found michael@18: */ michael@18: public static InputStream getResourceAsStream(String name) { michael@18: InputStream stream = null; michael@18: try { michael@18: // Hack to bootstrap a multithreaded class loader context michael@18: if (Thread.currentThread().getContextClassLoader() == null) michael@18: Thread.currentThread().setContextClassLoader(ResourceLoader.class.getClassLoader()); michael@18: stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); michael@18: if (stream == null) michael@18: stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/" + name); michael@18: } michael@18: catch (SecurityException e) { michael@18: Log.i("MSvB-Hack", "Unable to access context classloader, using default. " + e.getMessage()); michael@18: } michael@18: catch (Exception e) { michael@18: Log.i("MSvB-Hack", "General context classloader error, using default. " + e.getMessage()); michael@18: } michael@18: if (stream == null) { michael@18: stream = ResourceLoader.class.getResourceAsStream("/" + name); michael@18: } michael@18: return stream; michael@18: } michael@18: }