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: package com.sun.midp.midletsuite;
027:
028: import com.sun.midp.util.Properties;
029: import java.io.IOException;
030:
031: /**
032: * The properties for the suite.
033: */
034: public class SuiteProperties {
035:
036: /** Suite properties from the application descriptor and manifest. */
037: private Properties properties;
038:
039: /** The ID of this suite. */
040: private int suiteId;
041:
042: /**
043: * Package private constructor for SuiteProperties.
044: *
045: * @param id of the suite for these settings
046: */
047: SuiteProperties(int id) {
048: suiteId = id;
049: }
050:
051: /**
052: * Gets a property of the suite. A property is an attribute from
053: * either the application descriptor or JAR Manifest.
054: *
055: * @param key the name of the property
056: * @return A string with the value of the property.
057: * <code>null</code> is returned if no value is available for
058: * the key.
059: */
060: public String getProperty(String key) {
061: if (properties == null) {
062: loadProperties();
063: }
064:
065: return properties.getProperty(key);
066: }
067:
068: /**
069: * Replace or add a property to the suite for this run only.
070: *
071: * @param key the name of the property
072: * @param value the value of the property
073: *
074: * @exception SecurityException if the calling suite does not have
075: * internal API permission
076: */
077: public void setTempProperty(String key, String value) {
078: if (properties == null) {
079: loadProperties();
080: }
081:
082: properties.setProperty(key, value);
083: }
084:
085: /**
086: * Gets the unique ID of the suite.
087: *
088: * @return suite ID
089: */
090: public int getSuiteId() {
091: return suiteId;
092: }
093:
094: /**
095: * Loads suite properties from persistent storage into a properties
096: * object. If an IOException occurs, simply leaves the properties object
097: * empty.
098: */
099: void loadProperties() {
100: String[] propertyList;
101:
102: properties = new Properties();
103:
104: try {
105: propertyList = load();
106: } catch (IOException ioe) {
107: return;
108: }
109:
110: /*
111: * Convert the string pairs into properties.
112: * JAD properties are stored before Manifest properties, but according
113: * to the MIDP spec, for untrusted applications, if an attribute in
114: * the descriptor has the same name as an attribute in the manifest
115: * the value from the descriptor must be used and the value from the
116: * manifest must be ignored. So bellow we loop through the properties
117: * backward to override the properties existing in the manifest.
118: */
119: for (int i = propertyList.length - 2; i >= 0; i -= 2) {
120: properties
121: .setProperty(propertyList[i], propertyList[i + 1]);
122: }
123: }
124:
125: /**
126: * Gets the suite properties from persistent store. Returns the
127: * properties as an array of strings: key0, value0, key1, value1, etc.
128: *
129: * @return an array of property key-value pairs
130: *
131: * @throws IOException if an IO error occurs
132: */
133: native String[] load() throws IOException;
134:
135: /**
136: * Saves the Suite Properties to persistent store
137: */
138: // native void save(String[] propertyList);
139: }
|