001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024:
025: package com.sun.jump.common;
026:
027: import java.net.MalformedURLException;
028: import java.util.Properties;
029: import java.net.URL;
030: import com.sun.jump.common.JUMPContent;
031: import java.util.Enumeration;
032: import java.io.ByteArrayOutputStream;
033: import java.io.ByteArrayInputStream;
034:
035: /**
036: * A representation of executable application content in the jump environment.
037: * Because we can have different app types (e.g., XLET v. MIDLET),
038: * use this class to hold relevant information needed to start
039: * an application properly.
040: */
041: public class JUMPApplication implements java.io.Serializable,
042: JUMPContent {
043:
044: protected Properties props = null; // additional properties
045:
046: public static final String ICONPATH_KEY = "JUMPApplication_iconPath";
047: public static final String TITLE_KEY = "JUMPApplication_title";
048: public static final String APPMODEL_KEY = "JUMPApplication_appModel";
049: public static final String ID_KEY = "JUMPApplication_id";
050: /**
051: * A hint to the WindowingModule regarding the area of the screen
052: * this JUMPApplication requires.
053: * The value should be in the syntax of "x,y-wxh", for example "0,50-640x430".
054: **/
055: public static final String ID_SCREEN_BOUNDS = "JUMPApplication_screenBounds";
056:
057: /**
058: * Create an instance of an application.
059: * @param title The application's title, can be null
060: * @param iconPath The location of the application's icon in, can be null
061: * @param type The application's type
062: * @param id The installation id of the application
063: */
064: public JUMPApplication(String title, URL iconPath,
065: JUMPAppModel type, int id) {
066:
067: if (title != null) {
068: addProperty(TITLE_KEY, title);
069: }
070:
071: if (iconPath != null) {
072: addProperty(ICONPATH_KEY, iconPath.getFile());
073: }
074:
075: if (type != null) {
076: addProperty(APPMODEL_KEY, type.getName());
077: }
078:
079: addProperty(ID_KEY, Integer.toString(id));
080:
081: }
082:
083: /**
084: * Create an instance of an application from a Properties object
085: * @param props The properties that correspond to this application
086: */
087: private JUMPApplication(Properties props) {
088: this .props = props;
089:
090: if (props.getProperty(ID_KEY) == null
091: || props.getProperty(APPMODEL_KEY) == null)
092: throw new IllegalArgumentException(
093: "Properties do not include " + APPMODEL_KEY
094: + " or " + ID_KEY);
095: }
096:
097: /**
098: * Create a binary representation of this JUMPApplication object
099: */
100: public byte[] toByteArray() {
101: try {
102: ByteArrayOutputStream baos = new ByteArrayOutputStream();
103: props.store(baos, "");
104: return baos.toByteArray();
105: } catch (java.io.IOException e) {
106: System.err
107: .println("Error serializing this JUMPApplication object");
108: e.printStackTrace();
109: return null;
110: }
111: }
112:
113: /**
114: * Create a JUMPApplication from its binary representation
115: */
116: public static JUMPApplication fromByteArray(byte[] propBytes) {
117: try {
118: Properties p = new Properties();
119: ByteArrayInputStream bais = new ByteArrayInputStream(
120: propBytes);
121: p.load(bais);
122: return new JUMPApplication(p);
123: } catch (java.io.IOException e) {
124: System.err
125: .println("Error deserializing an JUMPApplication object");
126: e.printStackTrace();
127: return null;
128: }
129: }
130:
131: public String toString() {
132: StringBuffer sb = new StringBuffer();
133: sb.append("[app type=\"" + getAppType() + "\",");
134: sb.append(" iconPath=\"" + getIconPath() + "\",");
135: sb.append(" title=\"" + getTitle() + "\",");
136: sb.append(" props=[" + getPropsAsString() + "] ]");
137: return sb.toString();
138: }
139:
140: private String getPropsAsString() {
141: if (props == null) {
142: return "null";
143: }
144:
145: StringBuffer sb = new StringBuffer();
146: for (Enumeration e = getPropertyNames(); e.hasMoreElements();) {
147: String name = (String) e.nextElement();
148: String value = getProperty(name);
149: sb.append(name + "=" + value + ", ");
150: }
151: return sb.toString();
152: }
153:
154: /**
155: * Determine the type of this application.
156: *
157: * @return One of JUMPApplication's defined application types,
158: * as defined in JUMPAppModel.
159: */
160: public JUMPAppModel getAppType() {
161: String type = getProperty(APPMODEL_KEY);
162: if (type.equals(JUMPAppModel.XLET.getName())) {
163: return JUMPAppModel.XLET;
164: } else if (type.equals(JUMPAppModel.MAIN.getName())) {
165: return JUMPAppModel.MAIN;
166: } else if (type.equals(JUMPAppModel.MIDLET.getName())) {
167: return JUMPAppModel.MIDLET;
168: }
169: return null;
170: }
171:
172: /**
173: * Get the application's title.
174: * @return The application's title.
175: */
176: public String getTitle() {
177: return getProperty(TITLE_KEY);
178: }
179:
180: /**
181: * Set the application's title.
182: *
183: */
184: public void setTitle(String title) {
185: addProperty(TITLE_KEY, title);
186: return;
187: }
188:
189: /**
190: * Obtain the installed application id for this object.
191: * @return The installed application id.
192: */
193: public int getId() {
194: return Integer.parseInt(getProperty(ID_KEY));
195: }
196:
197: /**
198: * Set the installed application id for this object.
199: * @param id The installed application id.
200: */
201: public void setId(String id) {
202: addProperty(ID_KEY, id);
203: }
204:
205: /**
206: * Get the path to the application's icon.
207: * @return A URL defining the path to the icon in
208: * the downloaded content.
209: */
210: public URL getIconPath() {
211: String file = getProperty(ICONPATH_KEY);
212: URL url = null;
213: if (file == null)
214: return null;
215:
216: try {
217: url = new URL("file", null, file);
218: } catch (MalformedURLException ex) {
219: ex.printStackTrace();
220: }
221: return url;
222: }
223:
224: /**
225: * Set the path to the application's icon.
226: */
227: public void setIconPath(URL path) {
228: addProperty(ICONPATH_KEY, path.getFile());
229: return;
230: }
231:
232: /**
233: * Add a key/value pair to the application's list
234: * of properties.
235: * @param key - A key value.
236: * @param value - An object be associated with the key.
237: * @throws NullPointerException - If either key or value is
238: * <code>null</code>.
239: */
240: public void addProperty(String key, String value) //throws SyntaxException
241: {
242: if (key == null || value == null) {
243: throw new NullPointerException("null key or value");
244: }
245: if (props == null) {
246: props = new Properties();
247: }
248: props.put(key, value);
249:
250: return;
251: }
252:
253: /**
254: * Get a key/value pair to the application's list
255: * of properties.
256: * @param key - A key to search the properties for.
257: * @throws NullPointerException - If key is
258: * <code>null</code>.
259: */
260: public String getProperty(String key) //throws SyntaxException
261: {
262: if (props != null) {
263: return (String) props.get(key);
264: }
265:
266: return null;
267: }
268:
269: /**
270: * Returns the names of this JUMPApplication's property entries as an
271: * Enumeration of String objects, or an empty Enumeration if
272: * the JUMPApplication have no properties associated.
273: */
274: public Enumeration getPropertyNames() {
275: if (props != null) {
276: return props.keys();
277: }
278:
279: return null;
280: }
281:
282: public String getContentType() {
283: return "Application";
284: }
285:
286: /**
287: * Returns true if two JUMPApplications' APPMODEL_KEY and
288: * ID_KEY properties hold the same value.
289: */
290: public boolean equals(Object obj) {
291: if (!(obj instanceof JUMPApplication))
292: return false;
293:
294: JUMPApplication other = (JUMPApplication) obj;
295: return (getProperty(APPMODEL_KEY).equals(
296: other.getProperty(APPMODEL_KEY)) && getProperty(ID_KEY)
297: .equals(other.getProperty(ID_KEY)));
298: }
299:
300: /**
301: * Returns the value of ID_KEY property.
302: */
303: public int hashCode() {
304: return Integer.parseInt(getProperty(ID_KEY));
305: }
306: }
|