package kernel; import java.io.*; import java.util.*; import commands.shell; /** * Kernel implementation * * @author Jan Horký * @since 5.11.2008 * @version 4.1 */ public class Kernel { private int actualID; private ClassLoader loader; protected List processes; /** * Create an instance */ protected Kernel() { System.out.println("Loading..."); actualID = 0; loader = this.getClass().getClassLoader(); processes = new ArrayList(); } /** * Initialize Kernel */ protected void init() { System.out.println("Initializing ..."); try { AbstractProcess process = (AbstractProcess) loader.loadClass( "commands.login").newInstance(); processes.add(process); process.init(new Command("login"), null, null, null, null, this); process.exec(); } catch (Exception e) { e.printStackTrace(); } } public void executeMainShell(int idParent) { try { AbstractProcess parent = this.getProcess(idParent); AbstractProcess process = (AbstractProcess) loader.loadClass( "commands.shell").newInstance(); processes.add(process); process.init(new Command("shell"), parent, new PipedInputStream(), new PipedOutputStream(), new PipedOutputStream(), this); parent.setFirstChild(process); process.exec(); /* * waiting for shell exit */ process.join(); System.out.println("Running..."); } catch (Exception e) { e.printStackTrace(); } } public synchronized void initProcesses(int idParent, CommandList list) { AbstractProcess process = null, parent = null; List tmp = new ArrayList(); parent = this.getProcess(idParent); InputStream in = null; OutputStream out = null; /* * creating of all streams */ try { if (list.getInput()!=null) in = new FileInputStream(list.getInput()); } catch (Exception e) { parent.printError(e.getMessage()+"\n"); ((shell) parent).procesEnd(); return; //Thread.sleep(200); //e.printStackTrace(); } try { if (!list.getOutput().equals("")) out = new FileOutputStream(list.getOutput()); } catch (Exception e) { parent.printError(e.getMessage()+"\n"); ((shell) parent).procesEnd(); return; //Thread.sleep(200); } for (Command command : list.getCommands()) { try { tmp.add((AbstractProcess) this.loader.loadClass( "commands." + command.getCommandName()).newInstance()); } catch (Exception e) { for (AbstractProcess ap : tmp) { try { ap.interrupt(); } catch (Exception ex) { } } // e.printStackTrace(); try { parent.printError("Příkaz " + command.getCommandName() + " nenalezen.\n"); ((shell) parent).procesEnd(); Thread.sleep(200); } catch (Exception ex) { } System.out .println("Příkaz " + command.getCommandName() + " nenalezen."); return; } } ArrayList commands = list.getCommands(); try { if (commands.size() == 1) { tmp.get(0) .init( commands.get(0), parent, in, (out == null) ? ((shell) parent).getStdOutPipe().getPipeOut() : out, this.getErrOut(parent), this); } else { tmp.get(0).init(commands.get(0), parent, in, new PipedOutputStream(), this.getErrOut(parent), this); } for (int i = 1; i < commands.size() - 1; i++) { tmp.get(i).init(commands.get(i), parent, new PipedInputStream((PipedOutputStream) tmp.get(i - 1).getOut()), new PipedOutputStream(), this.getErrOut(parent), this); if (i + 1 < commands.size()) { tmp.get(i).setNext(tmp.get(i + 1)); } if (i - 1 >= 0) { tmp.get(i).setPrevious(tmp.get(i - 1)); } } if (commands.size() > 1) { tmp.get(commands.size() - 1) .init( commands.get(commands.size() - 1), parent, new PipedInputStream((PipedOutputStream) tmp.get( commands.size() - 2).getOut()), (out == null) ? ((shell) parent).getStdOutPipe().getPipeOut() : out, this.getErrOut(parent), this); tmp.get(commands.size() - 1).setPrevious(tmp.get(commands.size() - 2)); } for (int i = 0; i < commands.size(); i++) { tmp.get(i).exec(); processes.add(tmp.get(i)); } } catch (IOException e) { e.printStackTrace(); } } public int newID() { return actualID++; } public AbstractProcess getProcess(int id) { AbstractProcess process = null; for (AbstractProcess proc : processes) if (proc.getID() == id) { process = proc; break; } return process; } public void deleteProcess(int id) { for (AbstractProcess proc : processes) if (proc.getID() == id) { this.processes.remove(proc); return; } } private OutputStream getErrOut(AbstractProcess parent) throws IOException { if (parent instanceof shell) return ((shell) parent).initErrDeamon(); else return null; } public List getProcesses() { return processes; } }