home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / applet / downloadthread.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.1 KB  |  149 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)DownloadThread.java   0.00 09-Jan-95
  15.  
  16.         A thread which connects to a URL (or uses Stdin for debugging)
  17.         and runs an instance of pj.io.StoryParser.
  18.  
  19.     Authors:
  20.  
  21.         rphall      Rick Hall
  22.         jlee        james lee
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/applet/DownloadThread.java 2     1/21/96 6:26p Rphall $
  27.  
  28.     History:
  29.  
  30.         09-Jan-95    rphall      Initial Creation
  31.         20-Mar-96    jlee        Added thread's priority change scheme
  32.         26-Mar-96    jlee        DownloadThread's prioriy changes upon PjFrame's visibleness.
  33.  
  34. ---------------------------------------------------------------------------*/
  35.  
  36. package pj.applet;
  37.  
  38. import pj.applet.PjJava;
  39. import pj.io.Paper;
  40. import pj.io.PaperSection;
  41. import pj.io.StoryContainerNotification;
  42. import pj.io.StoryParser;
  43.  
  44. import java.awt.Frame;
  45. import java.io.InputStream;
  46. import java.io.IOException;
  47. import java.lang.Thread;
  48. import java.lang.ThreadGroup;
  49. import java.net.URL;
  50. import java.net.URLConnection;
  51. import java.util.Observable;
  52. import java.util.Observer;
  53.  
  54.  
  55. /**
  56. *
  57. *  Connects to a URL and runs an instance of pj.io.StoryParser.
  58. *
  59. *  @see        pj.io.StoryParser
  60. *  @version    0.00     09-Jan-95
  61. *  @author     rphall
  62. */
  63. public class DownloadThread extends Thread implements Observer
  64.     {
  65.  
  66.     // --- Instance variables
  67.     private boolean bStayAlive;
  68.     private URL url;
  69.     private Paper paper;
  70.  
  71.     // --- Public Constructor
  72.  
  73.     public DownloadThread(URL u, Paper p)
  74.         {
  75.         bStayAlive = true;
  76.         url = u;
  77.         paper = p;
  78.  
  79.         System.out.println("Debug-DownloadThread:constructed");
  80.         }
  81.  
  82.     // --- Public Operations
  83.  
  84.     public void run()
  85.         {
  86.         // Observe paper
  87.         paper.addObserver(this);
  88.  
  89.         InputStream is = null;
  90.         URLConnection urlcConnection;
  91.         if (url != null)
  92.             {
  93.             try {
  94.                 urlcConnection = url.openConnection();
  95.                 is = url.openStream();
  96.                 }
  97.             catch(IOException e)
  98.                 {
  99.                 System.out.println("Debug DownloadThread run, exception " + e.toString() );
  100.                 return;
  101.                 }
  102.             }
  103.         else
  104.             is = System.in;
  105.  
  106.         // Create parser and start parsing
  107.         StoryParser parse_obj = new StoryParser(is,paper);
  108.         try {
  109.             parse_obj.parse();
  110.             }
  111.         catch(Exception e)
  112.             {
  113.             System.out.println("Debug DownloadThread run, exception " + e.toString() );
  114.             return;
  115.             }
  116.  
  117.         // Hang out until notified of EOT
  118.         while (bStayAlive);
  119.  
  120.         } // run
  121.  
  122.     public synchronized void update(Observable who, Object arg)
  123.         {
  124.         if ( !(arg instanceof StoryContainerNotification) )
  125.             return;
  126.  
  127.         if ( PjJava.getPjFrame().isVisible() )
  128.             this.setPriority( Thread.MAX_PRIORITY );
  129.  
  130.         StoryContainerNotification scn = (StoryContainerNotification) arg;
  131.         if (scn.newState != PaperSection.stateAdded)
  132.             return;
  133.  
  134.         // Kludge: should define PaperNotificationEOT
  135.         if (scn.strSubsect.equals(Paper.idPcolEOT))
  136.             {
  137.             //isContent.flush();
  138.             bStayAlive = false;
  139.             }
  140.         } // update
  141.  
  142.     public void finalize()
  143.         {
  144.         if ( isAlive() )
  145.             stop();
  146.         System.out.println("Debug-DownloadThread-finalize:passed");
  147.         }
  148.     } // DownloadThread
  149.