home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIPHEFT062001.ISO / browser / nc32lyc / comm.z / java40.jar / netscape / applet / Task.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-08-15  |  6.6 KB  |  273 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("UniversalThreadAccess");
  88.       SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  89.       this.group = new TaskThreadGroup(var1, this);
  90.       this.thread = new Thread(this.group, this);
  91.       SecurityManager.revertPrivilege();
  92.       registerTask(this);
  93.    }
  94.  
  95.    public String taskName() {
  96.       return this._taskName;
  97.    }
  98.  
  99.    public URL codebaseURL() {
  100.       return this._codebaseURL;
  101.    }
  102.  
  103.    public URL archiveURL() {
  104.       return this._archiveURL;
  105.    }
  106.  
  107.    public TaskOwner taskOwner() {
  108.       return this._taskOwner;
  109.    }
  110.  
  111.    public ThreadGroup threadGroup() {
  112.       return this.group;
  113.    }
  114.  
  115.    public Application application() {
  116.       return this._application;
  117.    }
  118.  
  119.    public ClassLoader classLoader() {
  120.       if (this._classLoader != null) {
  121.          return this._classLoader;
  122.       } else if (this._codebaseURL == null) {
  123.          return null;
  124.       } else {
  125.          URL[] var1 = new URL[]{this._archiveURL};
  126.          this._classLoader = new AppletClassLoader((MozillaAppletContext)null, this._codebaseURL, var1);
  127.          return this._classLoader;
  128.       }
  129.    }
  130.  
  131.    public synchronized boolean isRunning() {
  132.       return this.running;
  133.    }
  134.  
  135.    public synchronized boolean isDead() {
  136.       return this.dead;
  137.    }
  138.  
  139.    public void run() {
  140.       synchronized(this){}
  141.  
  142.       try {
  143.          if (this.runLocked) {
  144.             throw new InconsistencyException("Do not run a Task more than once.");
  145.          }
  146.  
  147.          this.runLocked = true;
  148.       } catch (Throwable var30) {
  149.          throw var30;
  150.       }
  151.  
  152.       try {
  153.          this.prepareToRun();
  154.          synchronized(this){}
  155.  
  156.          try {
  157.             this.running = true;
  158.             this.moveEvents();
  159.             this.notifyAll();
  160.          } catch (Throwable var28) {
  161.             throw var28;
  162.          }
  163.  
  164.          this.didStartRunning();
  165.          this._application.run();
  166.       } finally {
  167.          synchronized(this){}
  168.  
  169.          try {
  170.             this.running = false;
  171.             this.dead = true;
  172.             this.notifyAll();
  173.          } catch (Throwable var27) {
  174.             throw var27;
  175.          }
  176.  
  177.          unregisterTask(this);
  178.          this.didStopRunning();
  179.       }
  180.  
  181.       this.stop();
  182.    }
  183.  
  184.    private void prepareToRun() {
  185.       Class var1;
  186.       try {
  187.          var1 = this.loadClass(this._appClassName);
  188.       } catch (ClassNotFoundException var6) {
  189.          ((Throwable)var6).printStackTrace(System.err);
  190.          throw new Error("I'm going under!!!");
  191.       }
  192.  
  193.       Object var2;
  194.       try {
  195.          var2 = var1.newInstance();
  196.       } catch (InstantiationException var4) {
  197.          ((Throwable)var4).printStackTrace(System.err);
  198.          throw new Error("I'm going under!!!");
  199.       } catch (IllegalAccessException var5) {
  200.          ((Throwable)var5).printStackTrace(System.err);
  201.          throw new Error("I'm going under!!!");
  202.       }
  203.  
  204.       this._application = (Application)var2;
  205.       this.eventLoop = this._application.eventLoop();
  206.       if (this._taskOwner != null) {
  207.          this._taskOwner.appDidConstruct(this._application);
  208.       }
  209.  
  210.    }
  211.  
  212.    private void didStartRunning() {
  213.    }
  214.  
  215.    private void didStopRunning() {
  216.    }
  217.  
  218.    public void start() {
  219.       this.thread.start();
  220.    }
  221.  
  222.    public void stop() {
  223.       AppletThreadList var1 = new AppletThreadList(this.group, this);
  224.       MozillaAppletContext.killer.addAppletThread(var1);
  225.    }
  226.  
  227.    public void requestShutdown() {
  228.       if (!this.dead && this._taskOwner != null) {
  229.          this._taskOwner.taskWillShutdown(this);
  230.       }
  231.  
  232.       this._classLoader = null;
  233.    }
  234.  
  235.    private void moveEvents() {
  236.       if (this.initialEventQueue != null) {
  237.          for(int var2 = 0; var2 < this.initialEventQueue.size(); ++var2) {
  238.             CommandEvent var1 = (CommandEvent)this.initialEventQueue.elementAt(var2);
  239.             this.eventLoop.addEvent(var1);
  240.          }
  241.  
  242.          this.initialEventQueue = null;
  243.       }
  244.    }
  245.  
  246.    public synchronized void addEvent(Event var1) {
  247.       if (this.eventLoop != null) {
  248.          if (this.initialEventQueue != null) {
  249.             this.moveEvents();
  250.          }
  251.  
  252.          this.eventLoop.addEvent(var1);
  253.       } else {
  254.          this.initialEventQueue.insertElementAt(var1, this.initialEventQueue.size());
  255.       }
  256.    }
  257.  
  258.    public Class loadClass(String var1) throws ClassNotFoundException {
  259.       AppletClassLoader var2 = (AppletClassLoader)this.classLoader();
  260.       return var2 == null ? Class.forName(var1) : var2.loadClass(var1);
  261.    }
  262.  
  263.    public synchronized void waitUntilRunningOrDead() {
  264.       while(!this.isRunning() && !this.isDead()) {
  265.          try {
  266.             this.wait();
  267.          } catch (InterruptedException var1) {
  268.          }
  269.       }
  270.  
  271.    }
  272. }
  273.