1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package javax
.microedition
.swm
;
12 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
13 import cc
.squirreljme
.runtime
.swm
.JarStreamSupplier
;
17 * This performs the installation and tracks progress.
21 final class __SuiteTracker__
22 extends SuiteManagementTracker
24 /** Bytes per percent threshold. */
25 private static final int _PERCENT_THRESHOLD
=
28 /** The suite when it is installed. */
29 volatile Suite _suite
;
32 * Initializes the tracker.
34 * @param __i The owning installer.
35 * @throws NullPointerException On null arguments.
38 __SuiteTracker__(SuiteInstaller __i
)
39 throws NullPointerException
42 throw new NullPointerException("NARG");
44 // Setup thread which performs the actual installation
45 Thread thread
= new Thread(new __Runner__(this, __i
),
46 "SquirrelJME-Suite-Installer");
55 public Suite
getSuite()
61 * This thread performs the installation.
65 private static final class __Runner__
68 /** The tracker which is given the suite when finished. */
69 protected final __SuiteTracker__ tracker
;
71 /** The supplier for the JAR data. */
72 protected final JarStreamSupplier supplier
;
74 /** Listeners for suites. */
75 private final Set
<SuiteInstallListener
> _listeners
;
78 * Initializes the runner.
80 * @param __tracker The tracker where the resulting suite is placed.
81 * @param __i The installer used.
82 * @throws NullPointerException On null arguments.
85 private __Runner__(__SuiteTracker__ __tracker
, SuiteInstaller __i
)
86 throws NullPointerException
88 if (__tracker
== null || __i
== null)
89 throw new NullPointerException("NARG");
91 this.tracker
= __tracker
;
92 this.supplier
= __i
._supplier
;
93 this._listeners
= __i
._listeners
;
105 throw Debugging
.todo();
107 // Read the JAR data stream
109 __update(SuiteInstallStage.DOWNLOAD_DATA, 0);
110 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
111 InputStream is = this.supplier.get())
113 // Read in source data
114 byte[] buf = new byte[512];
115 for (int count = 0, last = 0;;)
117 int rc = is.read(buf);
122 baos.write(buf, 0, rc);
124 // Update progress for the first initial set of blocks
128 int now = count / _PERCENT_THRESHOLD;
129 if (last != now && now <= 99)
131 __update(SuiteInstallStage.DOWNLOAD_DATA, now);
137 // Get the entire data buffer
139 data = baos.toByteArray();
142 // Could not read the JAR
143 catch (IOException e)
145 __done(InstallErrorCodes.IO_FILE_ERROR);
148 __update(SuiteInstallStage.DOWNLOAD_DATA, 100);
150 // Need the library manager
151 __SystemSuiteManager__ ssm =
152 (__SystemSuiteManager__)ManagerFactory.getSuiteManager();
153 LibrariesClient manager = ssm._manager;
155 // Send it to the kernel
156 __update(SuiteInstallStage.VERIFYING, 0);
157 LibraryInstallationReport report = manager.install(
158 data, 0, data.length);
161 int error = report.error();
164 // Determine the error code
166 InstallErrorCodes[] codes = InstallErrorCodes.values();
167 InstallErrorCodes code = (error >= 0 &&
168 error < codes.length ? codes[error] :
169 InstallErrorCodes.OTHER_ERROR);
171 // This will be the only chance to print the installation
173 /* {@squirreljme.error DG0t Failed to install the program
174 due to the specified error. (The error code; The more
175 detailed message associated with the error)} * /
176 System.err.printf("DG0t %s %s%n", code, report.message());
183 // Set the suite used
184 this.tracker._suite = new Suite(report.library());
186 // Did not fail, but report it anyway
187 __update(SuiteInstallStage.VERIFYING, 100);
190 __update(SuiteInstallStage.DONE, 100);
191 __done(InstallErrorCodes.NO_ERROR);
198 // Just print the trace
201 // And use some other error code to indicate failure
202 this.__done(InstallErrorCodes
.OTHER_ERROR
);
207 * Called when installation has finished, potentially with an error.
209 * @param __code The error code.
212 private void __done(InstallErrorCodes __code
)
214 __SuiteTracker__ tracker
= this.tracker
;
215 for (SuiteInstallListener l
: this._listeners
)
218 l
.installationDone(__code
, tracker
);
226 * Updates the current install stage.
228 * @param __stage The current installation stage.
229 * @param __pct The percentage complete.
232 private void __update(SuiteInstallStage __stage
, int __pct
)
234 __SuiteTracker__ tracker
= this.tracker
;
235 for (SuiteInstallListener l
: this._listeners
)
238 l
.updateStatus(tracker
, __stage
, __pct
);