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: MIDletSystemProperties.java 1858 2008-12-12 09:54:52Z barteo $
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   * @author vlads
45   * 
46   * This class is called by MIDlet to access System Property. Call injection is
47   * made by MIDlet ClassLoaded
48   * 
49   */
50  public class MIDletSystemProperties {
51  
52  	/**
53  	 * This may be a configuration option. But not for applet and Web Start.
54  	 */
55  	public static boolean applyToJavaSystemProperties = true;
56  
57  	/**
58  	 * Permits null values.
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  	/* The context to be used when starting MicroEmulator */
73  	private static AccessControlContext acc;
74  
75  	private static void initOnce() {
76  		// Can't use static initializer because of applyToJavaSystemProperties
77  		// in applet
78  		if (initialized) {
79  			return;
80  		}
81  		initialized = true;
82  		// This are set in Config
83  		// setProperty("microedition.configuration", "CLDC-1.1");
84  		// setProperty("microedition.profiles", "MIDP-2.0");
85  		setProperty("microedition.platform", "MicroEmulator");
86  		setProperty("microedition.encoding", getSystemProperty("file.encoding"));
87  	}
88  
89  	/**
90  	 * Allow Access to system properties from MIDlet
91  	 */
92  	public static void initContext() {
93  		acc = AccessController.getContext();
94  	}
95  
96  	/**
97  	 * Gets the system property indicated by the specified key. The only
98  	 * function called by MIDlet
99  	 * 
100 	 * @param key
101 	 *            the name of the system property
102 	 * @return
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 		// Restore System Properties from previous device activation.
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 }