View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: MIDletResourceLoader.java 1740 2008-06-05 06:33:28Z vlads $
26   */
27  package org.microemu.app.util;
28  
29  import java.io.InputStream;
30  
31  import org.microemu.Injected;
32  import org.microemu.log.Logger;
33  import org.microemu.util.ThreadUtils;
34  
35  /**
36   * @author vlads
37   * 
38   * Use MIDletResourceLoader to load resources. To solve resource resource
39   * loading paterns commonly used in MIDlet and not aceptable in Java SE
40   * application when System class is called to load resource
41   * 
42   * j2me example:
43   * 
44   * String.class.getResourceAsStream(resourceName)
45   * 
46   */
47  public class MIDletResourceLoader {
48  
49  	// TODO make this configurable
50  
51  	public static boolean traceResourceLoading = false;
52  
53  	/**
54  	 * @deprecated find better solution to share variable
55  	 */
56  	public static ClassLoader classLoader;
57  
58  	private static final String FQCN = Injected.class.getName();
59  
60  	public static InputStream getResourceAsStream(Class origClass, String resourceName) {
61  		if (traceResourceLoading) {
62  			Logger.debug("Loading MIDlet resource", resourceName);
63  		}
64  		if (classLoader != origClass.getClassLoader()) {
65  			// showWarning
66  			String callLocation = ThreadUtils.getCallLocation(FQCN);
67  			if (callLocation != null) {
68  				Logger.warn("attempt to load resource [" + resourceName + "] using System ClasslLoader from "
69  						+ callLocation);
70  			}
71  		}
72  		resourceName = resolveName(origClass, resourceName);
73  
74  		InputStream is = classLoader.getResourceAsStream(resourceName);
75  		if (is == null) {
76  			Logger.debug("Resource not found ", resourceName);
77  			return null;
78  		} else {
79  			return new MIDletResourceInputStream(is);
80  		}
81  	}
82  
83  	private static String resolveName(Class origClass, String name) {
84  		if (name == null) {
85  			return name;
86  		}
87  		if (!name.startsWith("/")) {
88  			while (origClass.isArray()) {
89  				origClass = origClass.getComponentType();
90  			}
91  			String baseName = origClass.getName();
92  			int index = baseName.lastIndexOf('.');
93  			if (index != -1) {
94  				name = baseName.substring(0, index).replace('.', '/') + "/" + name;
95  			}
96  		} else {
97  			name = name.substring(1);
98  		}
99  		return name;
100 	}
101 }