1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.microemu.app.util;
28
29 import java.security.AccessControlContext;
30 import java.security.AccessController;
31 import java.security.PrivilegedExceptionAction;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.Vector;
39
40 import org.microemu.device.Device;
41 import org.microemu.log.Logger;
42
43
44
45
46
47
48
49
50 public class MIDletSystemProperties {
51
52
53
54
55 public static boolean applyToJavaSystemProperties = true;
56
57
58
59
60 private static final Map props = new HashMap();
61
62 private static final Map permissions = new HashMap();
63
64 private static Map systemPropertiesPreserve;
65
66 private static List systemPropertiesDevice;
67
68 private static boolean wanrOnce = true;
69
70 private static boolean initialized = false;
71
72
73 private static AccessControlContext acc;
74
75 private static void initOnce() {
76
77
78 if (initialized) {
79 return;
80 }
81 initialized = true;
82
83
84
85 setProperty("microedition.platform", "MicroEmulator");
86 setProperty("microedition.encoding", getSystemProperty("file.encoding"));
87 }
88
89
90
91
92 public static void initContext() {
93 acc = AccessController.getContext();
94 }
95
96
97
98
99
100
101
102
103
104 public static String getProperty(String key) {
105 initOnce();
106 if (props.containsKey(key)) {
107 return (String) props.get(key);
108 }
109 String v = getDynamicProperty(key);
110 if (v != null) {
111 return v;
112 }
113 try {
114 return getSystemProperty(key);
115 } catch (SecurityException e) {
116 return null;
117 }
118 }
119
120 public static String getSystemProperty(String key) {
121 try {
122 if (acc != null) {
123 return getSystemPropertySecure(key);
124 } else {
125 return System.getProperty(key);
126 }
127 } catch (SecurityException e) {
128 return null;
129 }
130 }
131
132 private static String getSystemPropertySecure(final String key) {
133 try {
134 return (String) AccessController.doPrivileged(new PrivilegedExceptionAction() {
135 public Object run() {
136 return System.getProperty(key);
137 }
138 }, acc);
139 } catch (Throwable e) {
140 return null;
141 }
142 }
143
144 private static String getDynamicProperty(String key) {
145 if (key.equals("microedition.locale")) {
146 return Locale.getDefault().getLanguage();
147 }
148 return null;
149 }
150
151 public static Set getPropertiesSet() {
152 initOnce();
153 return props.entrySet();
154 }
155
156 public static String setProperty(String key, String value) {
157 initOnce();
158 if (applyToJavaSystemProperties) {
159 try {
160 if (value == null) {
161 System.getProperties().remove(key);
162 } else {
163 System.setProperty(key, value);
164 }
165 } catch (SecurityException e) {
166 if (wanrOnce) {
167 wanrOnce = false;
168 Logger.error("Cannot update Java System.Properties", e);
169 Logger.debug("Continue ME2 operations with no updates to system Properties");
170 }
171 }
172 }
173 return (String) props.put(key, value);
174 }
175
176 public static String clearProperty(String key) {
177 if (applyToJavaSystemProperties) {
178 try {
179 System.getProperties().remove(key);
180 } catch (SecurityException e) {
181 if (wanrOnce) {
182 wanrOnce = false;
183 Logger.error("Cannot update Java System.Properties", e);
184 }
185 }
186 }
187 return (String) props.remove(key);
188 }
189
190 public static void setProperties(Map properties) {
191 initOnce();
192 for (Iterator i = properties.entrySet().iterator(); i.hasNext();) {
193 Map.Entry e = (Map.Entry) i.next();
194 setProperty((String) e.getKey(), (String) e.getValue());
195 }
196 }
197
198 public static int getPermission(String permission) {
199 Integer value = (Integer) permissions.get(permission);
200 if (value == null) {
201 return -1;
202 } else {
203 return value.intValue();
204 }
205 }
206
207 public static void setPermission(String permission, int value) {
208 permissions.put(permission, new Integer(value));
209 }
210
211 public static void setDevice(Device newDevice) {
212 initOnce();
213
214 if (systemPropertiesDevice != null) {
215 for (Iterator iter = systemPropertiesDevice.iterator(); iter.hasNext();) {
216 clearProperty((String) iter.next());
217 }
218 }
219 if (systemPropertiesPreserve != null) {
220 for (Iterator i = systemPropertiesPreserve.entrySet().iterator(); i.hasNext();) {
221 Map.Entry e = (Map.Entry) i.next();
222 setProperty((String) e.getKey(), (String) e.getValue());
223 }
224 }
225 systemPropertiesDevice = new Vector();
226 systemPropertiesPreserve = new HashMap();
227 for (Iterator i = newDevice.getSystemProperties().entrySet().iterator(); i.hasNext();) {
228 Map.Entry e = (Map.Entry) i.next();
229 String key = (String) e.getKey();
230 if (props.containsKey(key)) {
231 systemPropertiesPreserve.put(key, props.get(key));
232 } else {
233 systemPropertiesDevice.add(key);
234 }
235 setProperty(key, (String) e.getValue());
236 }
237 }
238 }