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.automation;
028:
029: import java.io.*;
030: import java.util.*;
031: import com.sun.midp.midletsuite.*;
032: import com.sun.midp.installer.*;
033: import com.sun.midp.configurator.Constants;
034:
035: /**
036: * AutoSuiteStorage implementation
037: */
038: final class AutoSuiteStorageImpl extends AutoSuiteStorage {
039: /** Suites storage */
040: private MIDletSuiteStorage storage;
041:
042: /** Suites installer */
043: private Installer installer;
044:
045: /** Vector containing decriptors of installed suites */
046: private Vector installedSuites;
047:
048: /** The one and only class instance */
049: private static AutoSuiteStorageImpl instance = null;
050:
051: /**
052: * Private constructor: this class is singleton
053: */
054: private AutoSuiteStorageImpl() {
055: storage = MIDletSuiteStorage.getMIDletSuiteStorage();
056: installedSuites = new Vector();
057: }
058:
059: /**
060: * Updates vector of installed suites
061: */
062: private void updateInstalledSuites() {
063: int[] suiteIDs;
064:
065: suiteIDs = storage.getListOfSuites();
066: installedSuites = new Vector(suiteIDs.length);
067:
068: for (int i = 0; i < suiteIDs.length; i++) {
069: AutoSuiteDescriptor suite;
070: try {
071: suite = AutoSuiteDescriptorImpl.getInstanceBySuiteID(
072: suiteIDs[i], storage);
073: installedSuites.addElement(suite);
074: } catch (Exception e) {
075: // ignore and move to next suite
076: }
077: }
078: }
079:
080: /**
081: * Gets class instance
082: *
083: * @return single AutoSuiteStorage instance
084: */
085: synchronized static AutoSuiteStorageImpl getInstance() {
086: if (instance == null) {
087: instance = new AutoSuiteStorageImpl();
088: }
089:
090: return instance;
091: }
092:
093: /**
094: * AutoSuiteStorage implementation
095: */
096:
097: /**
098: * Installs suite from specified location.
099: *
100: * @param location URI pointing to suite jad/jar
101: *
102: * @return AutoSuiteDescriptor representing installed suit
103: */
104: public AutoSuiteDescriptor installSuite(String location)
105: throws IOException, MIDletSuiteLockedException,
106: MIDletSuiteCorruptedException, InvalidJadException,
107: SecurityException {
108:
109: AutoSuiteDescriptor suite = null;
110: int suiteID;
111:
112: installer = AutoGetInstallerTunnel.getInstaller(location);
113: suiteID = installer.installJad(location,
114: Constants.INTERNAL_STORAGE_ID, true, true, null);
115: suite = AutoSuiteDescriptorImpl.getInstanceBySuiteID(suiteID,
116: storage);
117:
118: return suite;
119: }
120:
121: /**
122: * Uninstalls specified suite.
123: *
124: * @param suite suite to uninstall
125: */
126: public void uninstallSuite(AutoSuiteDescriptor suite)
127: throws MIDletSuiteLockedException, IllegalArgumentException {
128:
129: AutoSuiteDescriptorImpl suiteImpl = (AutoSuiteDescriptorImpl) suite;
130: suiteImpl.guaranteeSuiteValid("uninstallSuite");
131:
132: if (suiteImpl.isExternalSuite()) {
133: int suiteID = suiteImpl.getSuiteID();
134: storage.remove(suiteID);
135: }
136:
137: /** uninstalling suite makes it invalid */
138: suiteImpl.invalidate();
139: }
140:
141: /**
142: * Gets a vector of installed suites.
143: *
144: * @return vector with suites descriptors
145: */
146: public Vector getInstalledSuites() {
147: updateInstalledSuites();
148:
149: /** copy vector of installed suites into new vector */
150: int totalSuites = installedSuites.size();
151: Vector suites = new Vector(totalSuites);
152:
153: for (int i = 0; i < totalSuites; i++) {
154: Object o = installedSuites.elementAt(i);
155: suites.addElement(o);
156: }
157:
158: return suites;
159: }
160: }
|