001: /*
002: * Copyright 1990-2007 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.midp.jump.midletsuite;
026:
027: import java.io.IOException;
028: import java.util.ArrayList;
029:
030: import com.sun.midp.midletsuite.MIDletInfo;
031: import com.sun.midp.midletsuite.MIDletSuiteCorruptedException;
032: import com.sun.midp.midletsuite.MIDletSuiteImpl;
033: import com.sun.midp.midletsuite.MIDletSuiteInfo;
034: import com.sun.midp.midletsuite.MIDletSuiteStorage;
035: import com.sun.midp.midletsuite.MIDletSuiteLockedException;
036:
037: import com.sun.midp.jump.MIDletApplication;
038: import com.sun.midp.jump.installer.StorageAccessInterface;
039: import com.sun.jump.common.JUMPContent;
040:
041: public class MIDletSuiteStorageAccessor implements
042: StorageAccessInterface {
043:
044: private MIDletSuiteStorage storage;
045:
046: public MIDletSuiteStorageAccessor() {
047: this .storage = MIDletSuiteStorage.getMIDletSuiteStorage();
048: }
049:
050: public int[] getInstalledMIDletSuiteIds() {
051: return storage.getListOfSuites();
052: }
053:
054: public JUMPContent[] convertToMIDletApplications(int suiteId) {
055: try {
056: MIDletSuiteInfo suiteInfo = getMIDletSuiteInfo(suiteId);
057: MIDletInfo[] midletInfos = getMIDletInfos(suiteInfo);
058:
059: JUMPMIDletSuiteInfo currentMIDletSuiteInfo = new JUMPMIDletSuiteInfo(
060: suiteInfo, midletInfos);
061:
062: return currentMIDletSuiteInfo.getMIDletApplications();
063:
064: } catch (IOException e) {
065: System.err.println(e
066: + " thrown while accessing the midlet suite "
067: + suiteId);
068: return new JUMPContent[0];
069: } catch (MIDletSuiteLockedException e) {
070: System.err.println(e
071: + " thrown while accessing the midlet suite "
072: + suiteId);
073: return new JUMPContent[0];
074: } catch (MIDletSuiteCorruptedException e) {
075: System.err.println(e
076: + " thrown while accessing the midlet suite "
077: + suiteId);
078: return new JUMPContent[0];
079: }
080: }
081:
082: private MIDletSuiteInfo getMIDletSuiteInfo(int id)
083: throws IOException {
084: return storage.getMIDletSuiteInfo(id);
085: }
086:
087: private MIDletInfo[] getMIDletInfos(MIDletSuiteInfo suiteInfo)
088: throws MIDletSuiteLockedException,
089: MIDletSuiteCorruptedException {
090: MIDletSuiteImpl midletSuite = storage.getMIDletSuite(
091: suiteInfo.suiteId, false);
092:
093: MIDletInfo[] midletInfos = new MIDletInfo[midletSuite
094: .getNumberOfMIDlets()];
095: for (int i = 0; i < midletInfos.length; i++) {
096: midletInfos[i] = new MIDletInfo(midletSuite
097: .getProperty("MIDlet-" + (i + 1)));
098: }
099:
100: // Need to unlock midletsuite.
101: midletSuite.close();
102:
103: return midletInfos;
104: }
105:
106: public void remove(int id) {
107: try {
108: storage.remove(id);
109: } catch (MIDletSuiteLockedException e) {
110: new RuntimeException(e);
111: }
112: }
113:
114: class JUMPMIDletSuiteInfo {
115:
116: MIDletSuiteInfo suiteInfo;
117: ArrayList midletApplications;
118:
119: public JUMPMIDletSuiteInfo(MIDletSuiteInfo suiteInfo,
120: MIDletInfo[] midletInfos) {
121: this .suiteInfo = suiteInfo;
122:
123: midletApplications = new ArrayList(midletInfos.length);
124: for (int i = 0; i < midletInfos.length; i++) {
125: MIDletApplication app = new MIDletApplication(
126: midletInfos[i].name, null, suiteInfo.suiteId,
127: midletInfos[i].classname, (i + 1));
128: midletApplications.add(i, app);
129: }
130:
131: }
132:
133: public MIDletSuiteInfo getMIDletSuiteInfo() {
134: return suiteInfo;
135: }
136:
137: public MIDletApplication[] getMIDletApplications() {
138: return (MIDletApplication[]) midletApplications
139: .toArray(new MIDletApplication[] {});
140: }
141:
142: public boolean contains(MIDletApplication midletApp) {
143: return midletApplications.contains(midletApp);
144: }
145: }
146: }
|