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 GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package javax
.microedition
.swm
;
12 import cc
.squirreljme
.jvm
.suite
.DependencyInfo
;
13 import cc
.squirreljme
.jvm
.suite
.InvalidSuiteException
;
14 import cc
.squirreljme
.jvm
.suite
.MatchResult
;
15 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
16 import cc
.squirreljme
.runtime
.swm
.ByteArrayJarStreamSupplier
;
17 import java
.util
.ArrayList
;
18 import java
.util
.HashMap
;
19 import java
.util
.LinkedHashSet
;
20 import java
.util
.List
;
25 * This class manages the bridge for the suite manager to the native program
31 final class __SystemSuiteManager__
32 implements SuiteManager
34 /** Cache of suites which are available. */
35 private static final Map
<String
, Suite
> _SUITES
=
38 /** Internal lock for suite management. */
39 protected final Object lock
=
47 public void addSuiteListener(SuiteListener __sl
)
49 throw Debugging
.todo();
57 public Suite
getSuite(String __vendor
, String __name
)
59 throw Debugging
.todo();
67 public SuiteInstaller
getSuiteInstaller(byte[] __b
, int __o
,
68 int __l
, boolean __ignuplock
)
69 throws IllegalArgumentException
, SecurityException
71 return new SuiteInstaller(
72 new ByteArrayJarStreamSupplier(__b
, __o
, __l
));
80 public SuiteInstaller
getSuiteInstaller(String __url
,
82 throws IllegalArgumentException
, SecurityException
84 throw Debugging
.todo();
92 public List
<Suite
> getSuites(SuiteType __t
)
93 throws IllegalArgumentException
, NullPointerException
96 throw new NullPointerException("NARG");
98 // Go through suites and find suites to return
99 List
<Suite
> rv
= new ArrayList
<>();
100 for (Suite s
: __SystemSuiteManager__
.__allSuites())
101 if (s
.getSuiteType() == __t
)
112 public void removeSuite(Suite __s
, boolean __ignuplock
)
113 throws IllegalArgumentException
, SuiteLockedException
115 throw Debugging
.todo();
123 public void removeSuiteListener(SuiteListener __sl
)
125 throw Debugging
.todo();
129 * Returns all the suites which are available.
131 * @return All the available suites.
134 static List
<Suite
> __allSuites()
136 throw Debugging
.todo();
138 // Just get every suite
139 List<Suite> rv = new ArrayList<>();
140 for (String as : SuiteAccess.availableSuites())
142 // It is possible they might not load
143 Suite s = __SystemSuiteManager__.__getSuite(as);
152 * Gets the specified suite.
154 * @param __s The suite to get.
155 * @return The suite for the given name.
156 * @throws NullPointerException On null arguments.
159 static Suite
__getSuite(String __s
)
160 throws NullPointerException
163 throw new NullPointerException("NARG");
165 Map
<String
, Suite
> suites
= __SystemSuiteManager__
._SUITES
;
166 synchronized (suites
)
169 Suite rv
= suites
.get(__s
);
173 // Previously did not exist or failed to load
174 if (suites
.containsKey(__s
))
180 suites
.put(__s
, (rv
= new Suite(__s
)));
183 catch (InvalidSuiteException e
)
188 // Just cache it as invalid
189 suites
.put(__s
, null);
196 * Returns the suites which match the given dependency set.
198 * @param __set The set of dependencies to get.
199 * @param __opt If {@code true} include optional dependencies.
200 * @return Suites which satisfy the given dependencies.
201 * @throws NullPointerException On null arguments.
205 static Suite
[] __matchDependencies(DependencyInfo __set
, boolean __opt
)
206 throws NullPointerException
209 throw new NullPointerException("NARG");
211 // Clear all optionals if they are not included
213 __set
= __set
.noOptionals();
215 // No dependencies to search for
219 // Remember the original set for recursive dependency checks
220 DependencyInfo original
= __set
;
223 Set
<Suite
> rv
= new LinkedHashSet
<>();
225 // Go through all binaries and attempt to match
226 for (Suite bin
: __SystemSuiteManager__
.__allSuites())
228 // Only consider matches
229 MatchResult result
= bin
.__matchedDependencies(__set
);
230 if (!result
.hasMatches())
233 // Use this as a dependency
236 // Recursively go down
237 for (Suite sub
: __SystemSuiteManager__
.__matchDependencies(
238 bin
.__suiteInfo().dependencies(), false))
241 // Use remaining unmatched set
242 __set
= result
.unmatched();
244 // If the set was emptied then it will never have any more matches
249 // {@squirreljme.error DG0u Could not locate the suite which
250 // statifies the given dependency. (The dependency to look for)}
252 throw new RuntimeException(
253 String
.format("DG0u %s", __set
));
255 return rv
.<Suite
>toArray(new Suite
[rv
.size()]);