001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.installer;
028:
029: import com.sun.midp.security.SecurityToken;
030: import com.sun.midp.security.Permissions;
031:
032: import com.sun.midp.midlet.MIDletSuite;
033: import com.sun.midp.midlet.MIDletStateHandler;
034:
035: import com.sun.midp.i18n.Resource;
036: import com.sun.midp.i18n.ResourceConstants;
037:
038: import com.sun.midp.util.Properties;
039:
040: /**
041: * Implements a the required MIDletSuite functionality needed by the
042: * system. The class is only needed for internal romized midlets.
043: */
044: public class InternalMIDletSuiteImpl implements MIDletSuite {
045: /** Display name for permission dialogs. */
046: private String displayName = null;
047:
048: /** The ID of this suite. */
049: private int id;
050:
051: /** Permissions for this suite. */
052: private byte[] permissions;
053:
054: /** Flag for trusted suites. If true the system trust icon is displayed. */
055: private boolean trusted;
056:
057: /** Suite properties for this suite. */
058: private Properties properties;
059:
060: /**
061: * number of midlets in this suite. For a rommized suite assume 1.
062: */
063: private int numberOfMidlets = 1;
064:
065: /**
066: * Creates MIDletSuite for rommized MIDlet.
067: *
068: * @param theDisplayName display name to use in permission dialogs,
069: * and in the MIDlet proxy list
070: * @param theId ID to separate this suite's resources from others
071: *
072: * @return new MIDletSuite object
073: */
074: public static MIDletSuite create(String theDisplayName, int theId) {
075: return new InternalMIDletSuiteImpl(theDisplayName, theId);
076: }
077:
078: /**
079: * Creates MIDletSuite for rommized MIDlet.
080: *
081: * @param theDisplayName display name to use in permission dialogs,
082: * and in the MIDlet proxy list
083: * @param theId unique identifier for this suite
084: */
085: private InternalMIDletSuiteImpl(String theDisplayName, int theId) {
086: if (theDisplayName != null) {
087: displayName = theDisplayName;
088: } else {
089: displayName = Resource
090: .getString(ResourceConstants.AMS_SYSTEM_SUITE_NAME);
091: }
092:
093: id = theId;
094:
095: trusted = true;
096:
097: permissions = (Permissions
098: .forDomain(Permissions.MANUFACTURER_DOMAIN_BINDING))[Permissions.CUR_LEVELS];
099:
100: properties = new Properties();
101: }
102:
103: /**
104: * Provides the number of MIDlets in this suite.
105: *
106: * @return number of MIDlet in the suite
107: */
108: public int getNumberOfMIDlets() {
109: return numberOfMidlets;
110: }
111:
112: /**
113: * Get the name of a MIDlet to display to the user.
114: *
115: * @param className class name of the MIDlet to be checked
116: *
117: * @return name to display to the user
118: */
119: public String getMIDletName(String className) {
120: /*
121: * Each internal MIDlet runs in it own suite,
122: * just return the suite name
123: */
124: return displayName;
125: }
126:
127: /**
128: * Gets the unique ID of the suite.
129: *
130: * @return suite ID
131: */
132: public int getID() {
133: return id;
134: }
135:
136: /**
137: * Gets a property of the suite. A property is an attribute from
138: * either the application descriptor or JAR Manifest.
139: *
140: * @param key the name of the property
141: * @return A string with the value of the property.
142: * <code>null</code> is returned if no value is available for
143: * the key.
144: */
145: public String getProperty(String key) {
146: return properties.getProperty(key);
147: }
148:
149: /**
150: * Replace or add a property to the suite for this run only.
151: *
152: * @param token token with the AMS permission set to allowed,
153: * can be null to use the suite's permission
154: * @param key the name of the property
155: * @param value the value of the property
156: *
157: * @exception SecurityException if the caller's token does not have
158: * internal AMS permission
159: */
160: public void setTempProperty(SecurityToken token, String key,
161: String value) {
162: if (token != null) {
163: token.checkIfPermissionAllowed(Permissions.AMS);
164: } else {
165: MIDletSuite current = MIDletStateHandler
166: .getMidletStateHandler().getMIDletSuite();
167:
168: current.checkIfPermissionAllowed(Permissions.AMS);
169: }
170:
171: properties.setProperty(key, value);
172: }
173:
174: /**
175: * Checks to see the suite has the ALLOW level for specific permission.
176: * This is used for by internal APIs that only provide access to
177: * trusted system applications.
178: *
179: * @param permission permission ID from com.sun.midp.security.Permissions
180: *
181: * @exception SecurityException if the suite is not allowed the permission
182: */
183: public void checkIfPermissionAllowed(int permission) {
184: if (checkPermission(permission) != 1) {
185: throw new SecurityException(SecurityToken.STD_EX_MSG);
186: }
187: }
188:
189: /**
190: * Check for permission and throw an exception if not allowed.
191: * May block to ask the user a question.
192: *
193: * @param permission ID of the permission to check for,
194: * the ID must be from
195: * {@link com.sun.midp.security.Permissions}
196: * @param resource string to insert into the question, can be null if
197: * no %2 in the question
198: *
199: * @exception SecurityException if the permission is not
200: * allowed by this token
201: * @exception InterruptedException if another thread interrupts the
202: * calling thread while this method is waiting to preempt the
203: * display.
204: */
205: public void checkForPermission(int permission, String resource)
206: throws InterruptedException {
207: checkForPermission(permission, resource, null);
208: }
209:
210: /**
211: * Checks for permission and throw an exception if not allowed.
212: * May block to ask the user a question.
213: *
214: * @param permission ID of the permission to check for,
215: * the ID must be from
216: * {@link com.sun.midp.security.Permissions}
217: * @param resource string to insert into the question, can be null if
218: * no %2 in the question
219: * @param extraValue string to insert into the question,
220: * can be null if no %3 in the question
221: *
222: * @exception SecurityException if the permission is not
223: * allowed by this token
224: * @exception InterruptedException if another thread interrupts the
225: * calling thread while this method is waiting to preempt the
226: * display.
227: */
228: public void checkForPermission(int permission, String resource,
229: String extraValue) throws InterruptedException {
230: checkIfPermissionAllowed(permission);
231: }
232:
233: /**
234: * Gets the status of the specified permission.
235: * If no API on the device defines the specific permission
236: * requested then it must be reported as denied.
237: * If the status of the permission is not known because it might
238: * require a user interaction then it should be reported as unknown.
239: *
240: * @param permission to check if denied, allowed, or unknown
241: *
242: * @return 0 if the permission is denied; 1 if the permission is allowed;
243: * -1 if the status is unknown
244: */
245: public int checkPermission(String permission) {
246: for (int i = 0; i < Permissions.NUMBER_OF_PERMISSIONS; i++) {
247: if (Permissions.getName(i).equals(permission)) {
248: return checkPermission(i);
249:
250: }
251: }
252:
253: return 0;
254: }
255:
256: /**
257: * Check to see the suite has the ALLOW level for specific permission.
258: * This is used for by internal APIs that only provide access to
259: * trusted system applications.
260: *
261: * @param permission permission ID from com.sun.midp.security.Permissions
262: *
263: * @param permission to check if denied, allowed, or unknown
264: *
265: * @return 0 if the permission is denied; 1 if the permission is allowed;
266: * -1 if the status is unknown
267: */
268: private int checkPermission(int permission) {
269: if (permission < 0 || permission >= permissions.length) {
270: // report denied
271: return 0;
272: }
273:
274: switch (permissions[permission]) {
275: case Permissions.ALLOW:
276: case Permissions.BLANKET_GRANTED:
277: // report allowed
278: return 1;
279:
280: case Permissions.BLANKET:
281: case Permissions.SESSION:
282: case Permissions.ONESHOT:
283: // report unknown
284: return -1;
285:
286: default:
287: break;
288: }
289:
290: // report denied
291: return 0;
292: }
293:
294: /**
295: * Indicates if the named MIDlet is registered in the suite
296: * with MIDlet-<n> record in the manifest or
297: * application descriptor.
298: *
299: * @param midletClassName class name of the MIDlet to be checked
300: *
301: * @return true if the MIDlet is registered
302: */
303: public boolean isRegistered(String midletClassName) {
304: // Rommized MIDlets don't have JAD MIDlet-n entries.
305: return false;
306: }
307:
308: /**
309: * Indicates if this suite is trusted.
310: * (not to be confused with a domain named "trusted",
311: * this is used for extra checks beyond permission checking)
312: *
313: * @return true if the suite is trusted false if not
314: */
315: public boolean isTrusted() {
316: return trusted;
317: }
318:
319: /**
320: * Get state of classes preverification within the suite.
321: *
322: * @return true because internal suite should be always preverified.
323: */
324: public boolean isVerified() {
325: return true;
326: }
327:
328: /**
329: * Determine if the a MIDlet from this suite can be run. Note that
330: * disable suites can still have their settings changed and their
331: * install info displayed.
332: *
333: * @return true if suite is enabled, false otherwise
334: */
335: public boolean isEnabled() {
336: return true;
337: }
338:
339: /**
340: * Asks the user want to interrupt the current MIDlet with
341: * a new MIDlet that has received network data.
342: *
343: * @param connection connection to place in the permission question or
344: * null for alarm
345: *
346: * @return true if the use wants interrupt the current MIDlet, else false
347: */
348: public boolean permissionToInterrupt(String connection) {
349: // Rommized internal MIDlet can interrupt other MIDlets without asking.
350: return true;
351: }
352:
353: /**
354: * Gets push setting for interrupting other MIDlets.
355: * Reuses the Permissions.
356: *
357: * @return push setting for interrupting MIDlets the value
358: * will be permission level from {@link Permissions}
359: */
360: public byte getPushInterruptSetting() {
361: // Rommized internal MIDlet can interrupt other MIDlets without asking.
362: return Permissions.ALLOW;
363: }
364:
365: /**
366: * Gets push options for this suite.
367: *
368: * @return push options are defined in {@link PushRegistryImpl}
369: */
370: public int getPushOptions() {
371: // There are not push options for rommized suites.
372: return 0;
373: }
374:
375: /**
376: * Gets list of permissions for this suite.
377: *
378: * @return array of permissions from {@link Permissions}
379: */
380: public byte[] getPermissions() {
381: return permissions;
382: }
383:
384: /**
385: * Close the opened MIDletSuite
386: */
387: public void close() {
388: }
389: }
|