home *** CD-ROM | disk | FTP | other *** search
/ Australian PC Authority 1999 May / may1999.iso / INTERNET / COMMUNIC / NETCAST.Z / marimb10.jar / netscape / netcast / ApplicationThread.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-25  |  10.9 KB  |  442 lines

  1. package netscape.netcast;
  2.  
  3. import java.awt.Event;
  4. import java.io.File;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.util.StringTokenizer;
  8. import java.util.Vector;
  9. import marimba.castanet.client.CastanetChannel;
  10. import marimba.castanet.client.CastanetConstants;
  11. import marimba.castanet.client.CastanetEnvironment;
  12. import marimba.castanet.client.CastanetException;
  13. import marimba.castanet.client.CastanetFile;
  14. import marimba.castanet.client.CastanetObject;
  15. import marimba.castanet.client.CastanetObserver;
  16. import marimba.castanet.client.CastanetWorkspace;
  17. import marimba.channel.Application;
  18. import netscape.applet.CastanetChannelInfo;
  19. import netscape.security.ForbiddenTargetException;
  20. import netscape.security.PrivilegeManager;
  21. import netscape.security.Target;
  22.  
  23. public final class ApplicationThread implements CastanetObserver, CastanetConstants, Runnable {
  24.    CastanetEnvironment env;
  25.    CastanetChannel channel;
  26.    Application application;
  27.    CastanetChannelInfo loader;
  28.    ApplicationThreadGroup group;
  29.    ApplicationContext context;
  30.    CastanetChannelInfo info;
  31.    Thread thread;
  32.    Event queue;
  33.    // $FF: renamed from: tm long
  34.    long field_0 = System.currentTimeMillis();
  35.  
  36.    public ApplicationThread(CastanetChannel channel, int msg, Object arg) {
  37.       this.env = ((CastanetObject)channel).getEnvironment();
  38.       this.channel = channel;
  39.       this.context = new ApplicationContext(channel);
  40.       switch (msg) {
  41.          case 404:
  42.          case 405:
  43.             this.sendEvent(channel, msg, arg);
  44.             ((CastanetObject)channel).addObserver(this);
  45.             String readPath = channel.getTransmitter().getDirectory().getPath();
  46.             String writePath = channel.getDataDirectory().getPath();
  47.             String workspacePath = ((CastanetObject)channel).getWorkspace().getDirectory().getAbsolutePath();
  48.             URL[] urls = null;
  49.             String[] zips = null;
  50.             this.group = new ApplicationThreadGroup(((CastanetObject)channel).getName() + "-Group");
  51.             this.group.setMaxPriority(8);
  52.             Vector v = new Vector();
  53.             String codebase = channel.getChannelProperty("codebase");
  54.             if (codebase != null && codebase.indexOf(58) < 0) {
  55.                v.addElement(codebase);
  56.             }
  57.  
  58.             String classpath = channel.getChannelProperty("classpath");
  59.             if (classpath != null) {
  60.                StringTokenizer t = new StringTokenizer(classpath, ":");
  61.  
  62.                while(t.hasMoreElements()) {
  63.                   String cp = t.nextToken();
  64.                   if (cp.indexOf(58) < 0) {
  65.                      v.addElement(cp);
  66.                   }
  67.                }
  68.             }
  69.  
  70.             int pathlen = v.size();
  71.             if (pathlen > 0) {
  72.                urls = new URL[pathlen];
  73.                zips = new String[pathlen];
  74.  
  75.                for(int i = 0; i < pathlen; ++i) {
  76.                   String path = ((String)v.elementAt(i)).trim();
  77.                   if (path.equals(".")) {
  78.                      urls[i] = channel.getBase();
  79.                   } else if (path.endsWith(".zip")) {
  80.                      CastanetFile file = channel.getFile(path);
  81.                      if (file == null) {
  82.                         ((CastanetObject)channel).notifyException(602, 860, file);
  83.                      } else if (file instanceof File) {
  84.                         zips[i] = ((File)file).getAbsolutePath();
  85.                      } else {
  86.                         ((CastanetObject)channel).notifyException(607, 861, file);
  87.                      }
  88.                   } else {
  89.                      try {
  90.                         if (!path.endsWith("/")) {
  91.                            path = path + "/";
  92.                         }
  93.  
  94.                         urls[i] = new URL(channel.getBase(), path);
  95.                      } catch (MalformedURLException var16) {
  96.                         ((CastanetObject)channel).notifyException(602, 860, path);
  97.                      }
  98.                   }
  99.                }
  100.             }
  101.  
  102.             PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  103.             if (privMgr != null) {
  104.                Target target = Target.findTarget("MarimbaInternalTarget");
  105.                if (target != null) {
  106.                   privMgr.enablePrivilege(target);
  107.                }
  108.             }
  109.  
  110.             this.info = new CastanetChannelInfo(pathlen, readPath, writePath, workspacePath, urls, zips, this.group, channel.getBase());
  111.             this.group.loader = this.info;
  112.             this.loader = this.info;
  113.             this.thread = new Thread(this.group, this, ((CastanetObject)channel).getName() + "-Thread");
  114.             this.thread.start();
  115.             return;
  116.          default:
  117.             ((CastanetObject)channel).notify(106, new CastanetException(605, 846, new Integer(msg)));
  118.       }
  119.    }
  120.  
  121.    synchronized void sendEvent(Object target, int msg, Object arg) {
  122.       Event evt = new Event(target, msg, arg);
  123.       if (this.queue == null) {
  124.          this.queue = evt;
  125.          this.notify();
  126.       } else {
  127.          Event q;
  128.          for(q = this.queue; q.evt != null; q = q.evt) {
  129.          }
  130.  
  131.          q.target = evt;
  132.       }
  133.    }
  134.  
  135.    synchronized Event getNextEvent() throws InterruptedException {
  136.       while(this.queue == null) {
  137.          this.wait();
  138.       }
  139.  
  140.       Event evt = this.queue;
  141.       this.queue = evt.evt;
  142.       evt.evt = null;
  143.       return evt;
  144.    }
  145.  
  146.    void kill() {
  147.       try {
  148.          PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  149.          Target target = null;
  150.          if (privMgr != null) {
  151.             target = Target.findTarget("MarimbaInternalTarget");
  152.             if (target != null) {
  153.                privMgr.enablePrivilege(target);
  154.             }
  155.          }
  156.  
  157.          Thread thread = this.thread;
  158.          if (thread != null) {
  159.             try {
  160.                thread.stop();
  161.             } catch (IllegalThreadStateException var6) {
  162.             }
  163.          }
  164.  
  165.          CastanetChannelInfo loader = this.loader;
  166.          if (loader != null) {
  167.             loader.close();
  168.          }
  169.  
  170.          CastanetChannel channel = this.channel;
  171.          if (channel != null) {
  172.             channel.stopped();
  173.          }
  174.       } catch (Throwable var7) {
  175.       }
  176.  
  177.       this.application = null;
  178.       this.loader = null;
  179.       this.thread = null;
  180.       this.queue = null;
  181.       this.context = null;
  182.       CastanetChannel ch = this.channel;
  183.       ApplicationThreadGroup grp = this.group;
  184.       if (ch != null && grp != null) {
  185.          new ThreadGroupStopper(((CastanetObject)ch).getWorkspace(), grp);
  186.          this.channel = null;
  187.          this.group = null;
  188.       }
  189.  
  190.    }
  191.  
  192.    public void notify(CastanetObject target, int msg, Object arg) {
  193.       switch (msg) {
  194.          case 407:
  195.          case 3285287:
  196.          case 3285288:
  197.          case 3285289:
  198.          case 3285291:
  199.          case 3285292:
  200.             this.sendEvent(target, msg, arg);
  201.             return;
  202.          case 408:
  203.             this.kill();
  204.             return;
  205.          default:
  206.       }
  207.    }
  208.  
  209.    public void callChannelStarted(CastanetChannel channel) {
  210.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  211.       Target target = null;
  212.       if (privMgr != null) {
  213.          target = Target.findTarget("MarimbaAppContextTarget");
  214.          if (target != null) {
  215.             privMgr.enablePrivilege(target);
  216.          }
  217.       }
  218.  
  219.       channel.started();
  220.    }
  221.  
  222.    private void callChannelStopped(CastanetChannel ch) {
  223.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  224.       Target target = null;
  225.       if (privMgr != null) {
  226.          target = Target.findTarget("MarimbaAppContextTarget");
  227.          if (target != null) {
  228.             privMgr.enablePrivilege(target);
  229.  
  230.             try {
  231.                privMgr.checkPrivilegeEnabled(target);
  232.             } catch (ForbiddenTargetException var4) {
  233.             }
  234.          }
  235.       }
  236.  
  237.       ch.stopped();
  238.    }
  239.  
  240.    private void callNotifiedUpdate(CastanetChannel channel) {
  241.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  242.       Target target = null;
  243.       if (privMgr != null) {
  244.          target = Target.findTarget("MarimbaInternalTarget");
  245.          if (target != null) {
  246.             privMgr.enablePrivilege(target);
  247.          }
  248.       }
  249.  
  250.       channel.notifiedUpdate();
  251.    }
  252.  
  253.    Class loadChannelMainClass(CastanetChannel channel) throws ClassNotFoundException {
  254.       PrivilegeManager privMgr = PrivilegeManager.getPrivilegeManager();
  255.       Target target = null;
  256.       if (privMgr != null) {
  257.          target = Target.findTarget("MarimbaAppContextTarget");
  258.          if (target != null) {
  259.             privMgr.enablePrivilege(target);
  260.          }
  261.       }
  262.  
  263.       String main = channel.getChannelProperty("main");
  264.       if (main == null) {
  265.          ((CastanetObject)channel).notifyException(602, 862, (Object)null);
  266.          return null;
  267.       } else {
  268.          main = main.trim();
  269.          Class c = this.loader.loadClass(main);
  270.          return c;
  271.       }
  272.    }
  273.  
  274.    public void run() {
  275.       try {
  276.          try {
  277.             for(Event evt = this.getNextEvent(); evt != null; evt = this.getNextEvent()) {
  278.                switch (evt.id) {
  279.                   case 404:
  280.                   case 405:
  281.                      try {
  282.                         Class c = this.loadChannelMainClass(this.channel);
  283.                         if (c == null) {
  284.                            return;
  285.                         }
  286.  
  287.                         this.application = (Application)c.newInstance();
  288.                      } catch (NoSuchMethodError e) {
  289.                         ((Throwable)e).printStackTrace();
  290.                         this.channel.notifyException(602, 863, e);
  291.                         return;
  292.                      } catch (NoClassDefFoundError e) {
  293.                         ((Throwable)e).printStackTrace();
  294.                         this.channel.notifyException(602, 864, e);
  295.                         return;
  296.                      } catch (ClassNotFoundException e) {
  297.                         ((Throwable)e).printStackTrace();
  298.                         this.channel.notifyException(602, 864, e);
  299.                         return;
  300.                      } catch (InstantiationException e) {
  301.                         ((Throwable)e).printStackTrace();
  302.                         this.channel.notifyException(602, 865, e);
  303.                         return;
  304.                      } catch (IllegalAccessException e) {
  305.                         ((Throwable)e).printStackTrace();
  306.                         this.channel.notifyException(602, 866, e);
  307.                         return;
  308.                      } catch (ClassCastException e) {
  309.                         ((Throwable)e).printStackTrace();
  310.                         this.channel.notifyException(602, 867, e);
  311.                         return;
  312.                      } catch (ThreadDeath e) {
  313.                         throw e;
  314.                      } catch (Throwable e) {
  315.                         e.printStackTrace();
  316.                         this.channel.notifyException(602, 845, e);
  317.                         return;
  318.                      }
  319.  
  320.                      try {
  321.                         this.application.setContext(this.context);
  322.                      } catch (ThreadDeath e) {
  323.                         throw e;
  324.                      } catch (Throwable e) {
  325.                         e.printStackTrace();
  326.                         this.channel.notifyException(602, 868, e);
  327.                         return;
  328.                      }
  329.  
  330.                      switch (evt.id) {
  331.                         case 404:
  332.                            try {
  333.                               evt.target = this.application;
  334.                               evt.id = 3285290;
  335.                               if (!this.application.handleEvent(evt)) {
  336.                                  return;
  337.                               }
  338.                            } catch (ThreadDeath e) {
  339.                               throw e;
  340.                            } catch (Throwable e) {
  341.                               e.printStackTrace();
  342.                               this.channel.notifyException(602, 869, e);
  343.                               return;
  344.                            }
  345.                         case 405:
  346.                            try {
  347.                               this.application.start();
  348.                            } catch (ThreadDeath e) {
  349.                               throw e;
  350.                            } catch (Throwable e) {
  351.                               e.printStackTrace();
  352.                               this.channel.notifyException(602, 870, e);
  353.                               return;
  354.                            }
  355.  
  356.                            this.callChannelStarted(this.channel);
  357.                         default:
  358.                            continue;
  359.                      }
  360.                   case 407:
  361.                      if (this.application != null) {
  362.                         try {
  363.                            this.application.stop();
  364.                         } catch (ThreadDeath e) {
  365.                            throw e;
  366.                         } catch (Throwable e) {
  367.                            e.printStackTrace();
  368.                            this.channel.notifyException(602, 872, e);
  369.                         }
  370.                      }
  371.  
  372.                      return;
  373.                   case 3285287:
  374.                      if (this.application != null) {
  375.                         try {
  376.                            evt.target = this.application;
  377.                            this.application.handleEvent(evt);
  378.                            this.callNotifiedUpdate(this.channel);
  379.                         } catch (ThreadDeath e) {
  380.                            throw e;
  381.                         } catch (Throwable e) {
  382.                            e.printStackTrace();
  383.                            this.channel.notifyException(602, 871, e);
  384.                         }
  385.                      }
  386.                      break;
  387.                   case 3285289:
  388.                      this.context.param = null;
  389.                   case 3285288:
  390.                   case 3285291:
  391.                   case 3285292:
  392.                      if (this.application != null) {
  393.                         try {
  394.                            evt.target = this.application;
  395.                            this.application.handleEvent(evt);
  396.                         } catch (ThreadDeath e) {
  397.                            throw e;
  398.                         } catch (Throwable e) {
  399.                            e.printStackTrace();
  400.                            this.channel.notifyException(602, 871, e);
  401.                         }
  402.                      }
  403.                }
  404.             }
  405.  
  406.             return;
  407.          } catch (InterruptedException e) {
  408.             ((Throwable)e).printStackTrace();
  409.          } catch (Throwable e) {
  410.             e.printStackTrace();
  411.          }
  412.  
  413.       } finally {
  414.          CastanetChannel e = this.channel;
  415.          if (e != null) {
  416.             this.env.flush(e.getBase());
  417.             this.callChannelStopped(e);
  418.             ((CastanetObject)e).removeObserver(this);
  419.          }
  420.  
  421.          CastanetChannelInfo ldr = this.loader;
  422.          if (ldr != null) {
  423.             ldr.close();
  424.          }
  425.  
  426.          ApplicationThreadGroup grp = this.group;
  427.          CastanetWorkspace ws = this.channel.getWorkspace();
  428.          this.application = null;
  429.          this.loader = null;
  430.          this.channel = null;
  431.          this.thread = null;
  432.          this.group = null;
  433.          this.queue = null;
  434.          this.context = null;
  435.          if (grp != null) {
  436.             new ThreadGroupStopper(ws, grp);
  437.          }
  438.  
  439.       }
  440.    }
  441. }
  442.