Revert StaticDisplayState; For hosted only allow debug to be used.
[SquirrelJME.git] / modules / meep-swm / src / main / java / javax / microedition / swm / __SystemTaskManager__.java
blobcda8d34db32058279905976bec9469b7d7f2266c
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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.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;
18 import java.util.Map;
20 /**
21 * This is the task manager which interfaces with the CLDC system support
22 * methods to provide access to tasks and such.
24 * @since 2017/12/07
26 final class __SystemTaskManager__
27 implements ExtendedTaskManager, TaskManager
29 /** Mapping of task IDs to tasks. */
30 static final Map<Integer, Task> _TASKS =
31 new HashMap<>();
33 /** Internal lock for chore management. */
34 protected final Object lock =
35 new Object();
37 /**
38 * {@inheritDoc}
39 * @since 2017/12/07
41 @Override
42 public void addTaskListener(TaskListener __tl)
44 throw Debugging.todo();
47 /**
48 * {@inheritDoc}
49 * @since 2017/12/07
51 @Override
52 public Task getCurrentTask()
54 throw Debugging.todo();
56 return this.__byId(APIAccessor.chores().currentId());
60 /**
61 * {@inheritDoc}
62 * @since 2017/12/07
64 @Override
65 public List<Task> getTaskList(boolean __incsys)
67 throw Debugging.todo();
69 // Lock so that the task list is always up to date
70 Task[] rv;
71 synchronized (this.lock)
73 IntegerArray tids = SystemCall.EASY.taskList(__incsys);
74 int n = tids.length();
76 // Wrap all the tasks
77 rv = new Task[n];
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);
87 /**
88 * {@inheritDoc}
89 * @since 2017/12/07
91 @Override
92 public void removeTaskListener(TaskListener __tl)
94 throw Debugging.todo();
97 /**
98 * {@inheritDoc}
99 * @since 2017/12/07
101 @Override
102 public boolean setForeground(Task __t)
103 throws IllegalArgumentException
105 throw Debugging.todo();
109 * {@inheritDoc}
110 * @since 2017/12/07
112 @Override
113 public boolean setPriority(Task __t, TaskPriority __p)
114 throws IllegalArgumentException
116 throw Debugging.todo();
120 * {@inheritDoc}
121 * @since 2018/10/29
123 @Override
124 public Task startTask(Suite __s, String __cn)
125 throws IllegalArgumentException, IllegalStateException,
126 NullPointerException
128 // Forward to the extended task start
129 return this.startTask(__s, __cn, null, null, null, null);
133 * {@inheritDoc}
134 * @since 2019/02/02
136 @Override
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,
141 NullPointerException
143 if (__s == null || __cn == null)
144 throw new NullPointerException("NARG");
146 // Defensive copies
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(),
158 v = e.getValue();
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;
185 // Debug
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
192 int tid;
193 if (true)
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().
199 iterator();
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();
208 // Forward launch
209 tid = TaskAccess.startTask(names, __cn, __args, xprops,
210 (__stdout == null ? null : new __CCWrapper__(__stdout)),
211 (__stderr == null ? null : new __CCWrapper__(__stderr)));
213 else
214 tid = TaskAccess.startTask(names, __cn, __args);
216 // It did not work
217 if (tid < 0)
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);
229 // Debug
230 todo.DEBUG.note("Created task with TID %d", tid);
232 // Otherwise use cached form
233 return this.__getTask(tid, __s, __cn);
238 * {@inheritDoc}
239 * @since 2017/12/07
241 @Override
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.
256 * @since 2018/11/04
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");
264 // Lock on the tasks
265 Map<Integer, Task> tasks = __SystemTaskManager__._TASKS;
266 synchronized (tasks)
268 // There should not be duplicates
269 Integer k = __tid;
270 Task rv = tasks.get(k);
271 if (rv != null)
272 throw Debugging.oops();
274 tasks.put(k, (rv = new Task(__tid, __s, __n)));
275 return rv;