View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: Injected.java 1252 2007-05-15 17:10:39Z vlads $
21   */
22  package org.microemu;
23  
24  import java.io.InputStream;
25  import java.io.PrintStream;
26  import java.io.Serializable;
27  
28  import org.microemu.app.util.MIDletOutputStreamRedirector;
29  import org.microemu.app.util.MIDletResourceLoader;
30  import org.microemu.app.util.MIDletSystemProperties;
31  import org.microemu.log.Logger;
32  
33  /**
34   * @author vlads
35   *
36   * This code is added to MIDlet application to solve problems with security policy  while running in Applet and Webstart.
37   * Also solves resource resource loading paterns commonly used in MIDlet and not aceptable in Java SE application
38   * The calls to this code is injected by ClassLoader or "Save for Web...".
39   * 
40   * This class is used instead injected one when application is running in Applet with MicroEmulator. 
41   *
42   * Serializable is just internal flag to verify tha proper class is loaded by application.
43   */
44  public final class Injected implements Serializable {
45  
46  	private static final long serialVersionUID = -1L;
47  
48  	/**
49  	 * This allow redirection of stdout to MicroEmulator console
50  	 */
51  	public final static PrintStream out = outPrintStream();
52  
53  	public final static PrintStream err = errPrintStream();
54  
55  	static {
56  		Logger.addLogOrigin(Injected.class);
57  	}
58  	
59  	/**
60  	 * We don't need to instantiate the class, all access is static
61  	 */
62  	private Injected() {
63  		
64  	}
65  	
66  	private static PrintStream outPrintStream() {
67  		//return System.out;
68  		return MIDletOutputStreamRedirector.out;
69  	}
70  
71  	private static PrintStream errPrintStream() {
72  		//return System.err;
73  		return MIDletOutputStreamRedirector.err;
74  	}
75  	
76  	/**
77  	 * Redirect throwable.printStackTrace() to MicroEmulator console
78  	 */
79  	public static void printStackTrace(Throwable t) {
80  		Logger.error("MIDlet caught", t);
81  	}
82  	
83  	/**
84  	 * This code Ingected By MicroEmulator to enable access to System properties while running in Applet
85       *
86       * @param      key   the name of the system property.
87       * @return     the string value of the system property,
88       *             or <code>null</code> if there is no property with that key.
89  	 */
90  	public static String getProperty(String key) {
91  		return MIDletSystemProperties.getProperty(key);
92  	}
93  	
94  	/**
95  	 * 
96  	 * Returns an input stream for reading the specified resource.
97       *
98       * <p> The search order is described in the documentation for {@link
99       * #getResource(String)}.  </p>
100      *
101      * @param  origClass
102      * @param  name  The resource name
103      *
104      * @return  An input stream for reading the resource, or <tt>null</tt>
105      *          if the resource could not be found
106 	 */
107 	public static InputStream getResourceAsStream(Class origClass, String name)  {
108 		return MIDletResourceLoader.getResourceAsStream(origClass, name);
109 	}
110 
111 	/**
112 	 * TODO fix ChangeCallsMethodVisitor
113 	 */
114 	public static Throwable handleCatchThrowable(Throwable t) {
115 		Logger.error("MIDlet caught", t);
116 		return t;
117 	}
118 }