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
.ExtendedTaskManager
;
14 import java
.io
.OutputStream
;
15 import java
.util
.Arrays
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
21 * This is the task manager which interfaces with the CLDC system support
22 * methods to provide access to tasks and such.
26 final class __SystemTaskManager__
27 implements ExtendedTaskManager
, TaskManager
29 /** Mapping of task IDs to tasks. */
30 static final Map
<Integer
, Task
> _TASKS
=
33 /** Internal lock for chore management. */
34 protected final Object lock
=
42 public void addTaskListener(TaskListener __tl
)
44 throw Debugging
.todo();
52 public Task
getCurrentTask()
54 throw Debugging
.todo();
56 return this.__byId(APIAccessor.chores().currentId());
65 public List
<Task
> getTaskList(boolean __incsys
)
67 throw Debugging
.todo();
69 // Lock so that the task list is always up to date
71 synchronized (this.lock)
73 IntegerArray tids = SystemCall.EASY.taskList(__incsys);
74 int n = tids.length();
78 for (int i = 0; i < n; i++)
79 rv[i] = this.__ofTask(new WrappedTask(tids.get(i)));
82 // Wrap array instead of creating a new list for speed
83 return Arrays.<Task>asList(rv);
92 public void removeTaskListener(TaskListener __tl
)
94 throw Debugging
.todo();
102 public boolean setForeground(Task __t
)
103 throws IllegalArgumentException
105 throw Debugging
.todo();
113 public boolean setPriority(Task __t
, TaskPriority __p
)
114 throws IllegalArgumentException
116 throw Debugging
.todo();
124 public Task
startTask(Suite __s
, String __cn
)
125 throws IllegalArgumentException
, IllegalStateException
,
128 // Forward to the extended task start
129 return this.startTask(__s
, __cn
, null, null, null, null);
137 public final Task
startTask(Suite __s
, String __cn
,
138 Map
<String
, String
> __sprops
, String
[] __args
, OutputStream __stdout
,
139 OutputStream __stderr
)
140 throws IllegalArgumentException
, IllegalStateException
,
143 if (__s
== null || __cn
== null)
144 throw new NullPointerException("NARG");
147 __args
= (__args
== null ?
new String
[0] : __args
.clone());
148 __sprops
= (__sprops
== null ?
new HashMap
<String
, String
>() :
149 new HashMap
<String
, String
>(__sprops
));
151 // Make sure values are actually valid
152 for (int i
= 0, n
= __args
.length
; i
< n
; i
++)
153 if (__args
[i
] == null)
154 throw new NullPointerException("NARG");
155 for (Map
.Entry
<String
, String
> e
: __sprops
.entrySet())
157 String k
= e
.getKey(),
159 if (k
== null || v
== null)
160 throw new NullPointerException("NARG");
163 /* {@squirreljme.error DG0v Cannot start a non-application suite.} */
164 if (__s
.getSuiteType() != SuiteType
.APPLICATION
)
165 throw new IllegalArgumentException("DG0v");
167 // Get all the suites that are available, since we need to determine
168 // dependencies and such
169 List
<Suite
> all
= __SystemSuiteManager__
.__allSuites();
171 // Determine the suites which need to be loaded into the classpath
172 // in order to run the given suite
173 Suite
[] depends
= __SystemSuiteManager__
.__matchDependencies(
174 __s
.__suiteInfo().dependencies(), false);
176 // Load suite names since we need to build the class path
177 int n
= depends
.length
;
178 String
[] names
= new String
[n
+ 1];
179 for (int i
= 0; i
< n
; i
++)
180 names
[i
] = depends
[i
]._name
;
182 // Add our boot suite to the last entry
183 names
[n
] = __s
._name
;
186 Debugging
.debugNote("Suites: %s", Arrays
.<String
>asList(names
));
188 throw Debugging
.todo();
190 // SquirrelJME 0.3.0 allows system properties and alternative output
191 // streams to be specified as well
195 // Copy system properties to key/value pair array
196 int spn = __sprops.size();
197 String[] xprops = new String[spn * 2];
198 Iterator<Map.Entry<String, String>> eit = __sprops.entrySet().
200 for (int i = 0, o = 0; i < spn; i++, o += 2)
202 Map.Entry<String, String> e = eit.next();
204 xprops[o] = e.getKey();
205 xprops[o + 1] = e.getValue();
209 tid = TaskAccess.startTask(names, __cn, __args, xprops,
210 (__stdout == null ? null : new __CCWrapper__(__stdout)),
211 (__stderr == null ? null : new __CCWrapper__(__stderr)));
214 tid = TaskAccess.startTask(names, __cn, __args);
219 /* {@squirreljme.error DG0w Invalid entry point was specified
220 when starting task. (The entry point)} * /
221 if (tid == TaskAccess.ERROR_INVALID_ENTRY)
222 throw new IllegalArgumentException("DG0w " + __cn);
224 /* {@squirreljme.error DG0x Could not launch the task because of
225 an unspecified error. (The error)} * /
226 throw new IllegalArgumentException("DG0x " + tid);
230 todo.DEBUG.note("Created task with TID %d", tid);
232 // Otherwise use cached form
233 return this.__getTask(tid, __s, __cn);
242 public boolean stopTask(Task __t
)
243 throws IllegalArgumentException
, IllegalStateException
245 throw Debugging
.todo();
249 * Returns a task mapped to the given ID.
251 * @param __tid The task ID.
252 * @param __s The suite used.
253 * @param __n The starting class of the task, gives its name.
254 * @return The task for the task.
255 * @throws NullPointerException On null arguments.
258 static final Task
__getTask(int __tid
, Suite __s
, String __n
)
259 throws NullPointerException
261 if (__s
== null || __n
== null)
262 throw new NullPointerException("NARG");
265 Map
<Integer
, Task
> tasks
= __SystemTaskManager__
._TASKS
;
268 // There should not be duplicates
270 Task rv
= tasks
.get(k
);
272 throw Debugging
.oops();
274 tasks
.put(k
, (rv
= new Task(__tid
, __s
, __n
)));