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: Injected.java 1605 2008-02-25 21:07:14Z barteo $
26 */
27 package org.microemu;
28
29 import java.io.InputStream;
30 import java.io.PrintStream;
31 import java.io.Serializable;
32
33 import org.microemu.app.util.MIDletOutputStreamRedirector;
34 import org.microemu.app.util.MIDletResourceLoader;
35 import org.microemu.app.util.MIDletSystemProperties;
36 import org.microemu.log.Logger;
37
38 /**
39 * @author vlads
40 *
41 * This code is added to MIDlet application to solve problems with security policy while running in Applet and Webstart.
42 * Also solves resource resource loading paterns commonly used in MIDlet and not aceptable in Java SE application
43 * The calls to this code is injected by ClassLoader or "Save for Web...".
44 *
45 * This class is used instead injected one when application is running in Applet with MicroEmulator.
46 *
47 * Serializable is just internal flag to verify tha proper class is loaded by application.
48 */
49 public final class Injected implements Serializable {
50
51 private static final long serialVersionUID = -1L;
52
53 /**
54 * This allow redirection of stdout to MicroEmulator console
55 */
56 public final static PrintStream out = outPrintStream();
57
58 public final static PrintStream err = errPrintStream();
59
60 static {
61 Logger.addLogOrigin(Injected.class);
62 }
63
64 /**
65 * We don't need to instantiate the class, all access is static
66 */
67 private Injected() {
68
69 }
70
71 private static PrintStream outPrintStream() {
72 //return System.out;
73 return MIDletOutputStreamRedirector.out;
74 }
75
76 private static PrintStream errPrintStream() {
77 //return System.err;
78 return MIDletOutputStreamRedirector.err;
79 }
80
81 /**
82 * Redirect throwable.printStackTrace() to MicroEmulator console
83 */
84 public static void printStackTrace(Throwable t) {
85 Logger.error("MIDlet caught", t);
86 }
87
88 /**
89 * This code Ingected By MicroEmulator to enable access to System properties while running in Applet
90 *
91 * @param key the name of the system property.
92 * @return the string value of the system property,
93 * or <code>null</code> if there is no property with that key.
94 */
95 public static String getProperty(String key) {
96 return MIDletSystemProperties.getProperty(key);
97 }
98
99 /**
100 *
101 * Returns an input stream for reading the specified resource.
102 *
103 * <p> The search order is described in the documentation for {@link
104 * #getResource(String)}. </p>
105 *
106 * @param origClass
107 * @param name The resource name
108 *
109 * @return An input stream for reading the resource, or <tt>null</tt>
110 * if the resource could not be found
111 */
112 public static InputStream getResourceAsStream(Class origClass, String name) {
113 return MIDletResourceLoader.getResourceAsStream(origClass, name);
114 }
115
116 /**
117 * TODO fix ChangeCallsMethodVisitor
118 */
119 public static Throwable handleCatchThrowable(Throwable t) {
120 Logger.error("MIDlet caught", t);
121 return t;
122 }
123 }