home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 July / PCO_07_97.ISO / NET_COM / cc32e40.exe / nav40.z / java40.jar / netscape / applet / Task.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-07  |  5.7 KB  |  271 lines

  1. package netscape.applet;
  2.  
  3. import java.net.URL;
  4. import java.util.Vector;
  5. import netscape.application.Application;
  6. import netscape.application.CommandEvent;
  7. import netscape.application.Event;
  8. import netscape.application.EventLoop;
  9. import netscape.util.Enumeration;
  10. import netscape.util.Hashtable;
  11. import netscape.util.InconsistencyException;
  12.  
  13. final class Task implements Runnable {
  14.    static Hashtable taskByName;
  15.    String _taskName;
  16.    TaskThreadGroup group;
  17.    Thread thread;
  18.    URL _codebaseURL;
  19.    URL _archiveURL;
  20.    AppletClassLoader _classLoader;
  21.    String _appClassName;
  22.    Application _application;
  23.    EventLoop eventLoop;
  24.    TaskOwner _taskOwner;
  25.    Vector initialEventQueue = new Vector();
  26.    boolean registered;
  27.    boolean running;
  28.    boolean dead;
  29.    boolean runLocked;
  30.  
  31.    public static void destroyAll() {
  32.       Enumeration var0 = getTasks();
  33.  
  34.       while(var0.hasMoreElements()) {
  35.          Task var1 = (Task)var0.nextElement();
  36.          var1.stop();
  37.       }
  38.  
  39.    }
  40.  
  41.    public static Task currentTask() {
  42.       for(ThreadGroup var0 = Thread.currentThread().getThreadGroup(); var0 != null; var0 = var0.getParent()) {
  43.          if (var0 instanceof TaskThreadGroup) {
  44.             return ((TaskThreadGroup)var0).task();
  45.          }
  46.       }
  47.  
  48.       return null;
  49.    }
  50.  
  51.    public static synchronized Task taskNamed(URL var0, String var1) {
  52.       return taskByName == null ? null : (Task)taskByName.get(new TaskID(var0, var1));
  53.    }
  54.  
  55.    static synchronized void registerTask(Task var0) {
  56.       if (taskByName == null) {
  57.          taskByName = new Hashtable();
  58.       }
  59.  
  60.       if (!var0.registered) {
  61.          taskByName.put(new TaskID(var0.codebaseURL(), var0.taskName()), var0);
  62.          var0.registered = true;
  63.       }
  64.    }
  65.  
  66.    static synchronized void unregisterTask(Task var0) {
  67.       if (taskByName != null && var0.registered) {
  68.          taskByName.remove(new TaskID(var0.codebaseURL(), var0.taskName()));
  69.          var0.registered = false;
  70.       }
  71.    }
  72.  
  73.    public static synchronized Enumeration getTasks() {
  74.       if (taskByName == null) {
  75.          taskByName = new Hashtable();
  76.       }
  77.  
  78.       return ((Hashtable)taskByName.clone()).elements();
  79.    }
  80.  
  81.    public Task(String var1, String var2, URL var3, URL var4, TaskOwner var5) {
  82.       this._taskName = var1;
  83.       this._appClassName = var2;
  84.       this._codebaseURL = var3;
  85.       this._archiveURL = var4;
  86.       this._taskOwner = var5;
  87.       SecurityManager.enablePrivilege("SuperUser");
  88.       this.group = new TaskThreadGroup(var1, this);
  89.       this.thread = new Thread(this.group, this);
  90.       SecurityManager.revertPrivilege();
  91.       registerTask(this);
  92.    }
  93.  
  94.    public String taskName() {
  95.       return this._taskName;
  96.    }
  97.  
  98.    public URL codebaseURL() {
  99.       return this._codebaseURL;
  100.    }
  101.  
  102.    public URL archiveURL() {
  103.       return this._archiveURL;
  104.    }
  105.  
  106.    public TaskOwner taskOwner() {
  107.       return this._taskOwner;
  108.    }
  109.  
  110.    public ThreadGroup threadGroup() {
  111.       return this.group;
  112.    }
  113.  
  114.    public Application application() {
  115.       return this._application;
  116.    }
  117.  
  118.    public ClassLoader classLoader() {
  119.       if (this._classLoader != null) {
  120.          return this._classLoader;
  121.       } else if (this._codebaseURL == null) {
  122.          return null;
  123.       } else {
  124.          this._classLoader = new AppletClassLoader((MozillaAppletContext)null, this._codebaseURL, this._archiveURL);
  125.          return this._classLoader;
  126.       }
  127.    }
  128.  
  129.    public synchronized boolean isRunning() {
  130.       return this.running;
  131.    }
  132.  
  133.    public synchronized boolean isDead() {
  134.       return this.dead;
  135.    }
  136.  
  137.    public void run() {
  138.       synchronized(this){}
  139.  
  140.       try {
  141.          if (this.runLocked) {
  142.             throw new InconsistencyException("Do not run a Task more than once.");
  143.          }
  144.  
  145.          this.runLocked = true;
  146.       } catch (Throwable var30) {
  147.          throw var30;
  148.       }
  149.  
  150.       try {
  151.          this.prepareToRun();
  152.          synchronized(this){}
  153.  
  154.          try {
  155.             this.running = true;
  156.             this.moveEvents();
  157.             this.notifyAll();
  158.          } catch (Throwable var28) {
  159.             throw var28;
  160.          }
  161.  
  162.          this.didStartRunning();
  163.          this._application.run();
  164.       } finally {
  165.          synchronized(this){}
  166.  
  167.          try {
  168.             this.running = false;
  169.             this.dead = true;
  170.             this.notifyAll();
  171.          } catch (Throwable var27) {
  172.             throw var27;
  173.          }
  174.  
  175.          unregisterTask(this);
  176.          this.didStopRunning();
  177.       }
  178.  
  179.       this.stop();
  180.    }
  181.  
  182.    private void prepareToRun() {
  183.       Class var1;
  184.       try {
  185.          var1 = this.loadClass(this._appClassName);
  186.       } catch (ClassNotFoundException var6) {
  187.          ((Throwable)var6).printStackTrace(System.err);
  188.          throw new Error("I'm going under!!!");
  189.       }
  190.  
  191.       Object var2;
  192.       try {
  193.          var2 = var1.newInstance();
  194.       } catch (InstantiationException var4) {
  195.          ((Throwable)var4).printStackTrace(System.err);
  196.          throw new Error("I'm going under!!!");
  197.       } catch (IllegalAccessException var5) {
  198.          ((Throwable)var5).printStackTrace(System.err);
  199.          throw new Error("I'm going under!!!");
  200.       }
  201.  
  202.       this._application = (Application)var2;
  203.       this.eventLoop = this._application.eventLoop();
  204.       if (this._taskOwner != null) {
  205.          this._taskOwner.appDidConstruct(this._application);
  206.       }
  207.  
  208.    }
  209.  
  210.    private void didStartRunning() {
  211.    }
  212.  
  213.    private void didStopRunning() {
  214.    }
  215.  
  216.    public void start() {
  217.       this.thread.start();
  218.    }
  219.  
  220.    public void stop() {
  221.       AppletThreadList var1 = new AppletThreadList(this.group, this);
  222.       MozillaAppletContext.killer.addAppletThread(var1);
  223.    }
  224.  
  225.    public void requestShutdown() {
  226.       if (!this.dead && this._taskOwner != null) {
  227.          this._taskOwner.taskWillShutdown(this);
  228.       }
  229.  
  230.       this._classLoader = null;
  231.    }
  232.  
  233.    private void moveEvents() {
  234.       if (this.initialEventQueue != null) {
  235.          for(int var2 = 0; var2 < this.initialEventQueue.size(); ++var2) {
  236.             CommandEvent var1 = (CommandEvent)this.initialEventQueue.elementAt(var2);
  237.             this.eventLoop.addEvent(var1);
  238.          }
  239.  
  240.          this.initialEventQueue = null;
  241.       }
  242.    }
  243.  
  244.    public synchronized void addEvent(Event var1) {
  245.       if (this.eventLoop != null) {
  246.          if (this.initialEventQueue != null) {
  247.             this.moveEvents();
  248.          }
  249.  
  250.          this.eventLoop.addEvent(var1);
  251.       } else {
  252.          this.initialEventQueue.insertElementAt(var1, this.initialEventQueue.size());
  253.       }
  254.    }
  255.  
  256.    public Class loadClass(String var1) throws ClassNotFoundException {
  257.       AppletClassLoader var2 = (AppletClassLoader)this.classLoader();
  258.       return var2 == null ? Class.forName(var1) : var2.loadClass(var1);
  259.    }
  260.  
  261.    public synchronized void waitUntilRunningOrDead() {
  262.       while(!this.isRunning() && !this.isDead()) {
  263.          try {
  264.             this.wait();
  265.          } catch (InterruptedException var1) {
  266.          }
  267.       }
  268.  
  269.    }
  270. }
  271.