
org.microemu.app.util.MIDletResourceLoader Maven / Gradle / Ivy
/**
* MicroEmulator
* Copyright (C) 2006-2007 Bartek Teodorczyk
* Copyright (C) 2006-2007 Vlad Skarzhevskyy
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Id: MIDletResourceLoader.java 1404 2007-10-15 19:40:48Z vlads $
*/
package org.microemu.app.util;
import java.io.InputStream;
import org.microemu.Injected;
import org.microemu.log.Logger;
import org.microemu.util.ThreadUtils;
/**
* @author vlads
*
* Use MIDletResourceLoader to load resources.
* To solve resource resource loading paterns commonly used in MIDlet and not aceptable in Java SE application
* when System class is called to load resource
*
* j2me example:
*
* String.class.getResourceAsStream(resourceName)
*
*/
public class MIDletResourceLoader {
//TODO make this configurable
public static boolean traceResourceLoading = false;
/**
* @deprecated find better solution to share variable
*/
public static ClassLoader classLoader;
private static final String FQCN = Injected.class.getName();
public static InputStream getResourceAsStream(Class origClass, String resourceName) {
if (traceResourceLoading) {
Logger.debug("Loading MIDlet resource", resourceName);
}
if (classLoader != origClass.getClassLoader()) {
// showWarning
String callLocation = ThreadUtils.getCallLocation(FQCN);
if (callLocation != null) {
Logger.warn("attempt to load resource [" + resourceName + "] using System ClasslLoader from " + callLocation);
}
}
resourceName = resolveName(origClass, resourceName);
InputStream is = classLoader.getResourceAsStream(resourceName);
if (is == null) {
Logger.debug("Resource not found ", resourceName);
}
return is;
}
private static String resolveName(Class origClass, String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
while (origClass.isArray()) {
origClass = origClass.getComponentType();
}
String baseName = origClass.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = baseName.substring(0, index).replace('.', '/') + "/" +name;
}
} else {
name = name.substring(1);
}
return name;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy