Ignore:
Timestamp:
2024-04-06T22:41:44+13:00 (2 months ago)
Author:
anupama
Message:

Tried to replace calls to deprecated and dangerous applet stop(). But one problem remains constant between using stop() and the new, custom stopRunning() when GsdlCollageApplet is run in webswing: Unless the application is manually exited (when run as an application in webswing, the exit button is available), I don't see stop() or stopRunning() called in the log at packages/tomcat/bin/logs/webswing.log. Yet is webswing always relaunching the app and calling its init() method, which would start creating new threads and downloading and other things? What happens to the objects never terminated and garbage collected unless explicitly exited? How do I in Java detect that the webswing browser window has been redirected to another page? TODO: Maybe this can be resolved with a Java-JavaScript communication between webswing and the app. TODO2: MyAffineTransform line 39, why is there a division by zero error at times when the app window dimensions ought to always be positive now? TODO3: I want the status bar gone. It is not being used anyway and at present, sometimes the outlines of its dimensions appear, even though they shouldn't. This occasional behaviour (whether the MyAffineTransform error or status bar) is another bother for debugging.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DownloadUrls.java

    r38885 r38908  
    88import javax.swing.ImageIcon; //****
    99
     10//import org.apache.log4j.*;
    1011
    1112/**
     
    2122
    2223public class DownloadUrls extends Thread {
     24   
    2325    // for GS3   
    2426    String baseURL = null;
     
    3537    String document_root_ = null;
    3638
    37 
     39    /** When this thread is asked to stop running, this variable will be set to true */
     40    private boolean stop_running = false;   
     41
     42    /** When this thread is asked to stop downloading, this variable will be set to true.
     43     * For now the behaviour is the same as stop_running=true on this thread,
     44     * but in case it changes in the future, we have a separate variable.
     45     * Also, calling stopRunning() is not the same as setting stop_running = true, so
     46     * to be careful, a separate variable for stop_downloading could be safer when coding.
     47     */
     48    private boolean stop_downloading = false;
     49   
    3850    /** CHRIS - Holds the contents of the collection's assoc directory */
    3951    //    File[] assocDir_ = null;
     
    181193    }
    182194
    183    
    184 
     195    // some other thread can call this method to tell this thread to stop running
     196    public void stopRunning() {
     197    if (verbosity_ >= 3) {
     198        System.err.println("**** DownloadUrls.stopRunning() called");
     199    }
     200   
     201    stop_running = true;
     202    // Interrupt this thread, even if it's not the one running
     203    // Just want to make sure the DownloadURls' thread the CURL object runs in
     204    // gets interrupted if it's what's currently running
     205    if(!this.isInterrupted()) {
     206        this.interrupt();
     207    }
     208    if(!Thread.currentThread().isInterrupted()) {
     209        Thread.currentThread().interrupt();
     210    }
     211    }
     212
     213    public boolean isStopping() {
     214    return stop_running;
     215    }
    185216
    186217
     
    390421       try{
    391422           // if have completed the maximum number of downloads for the
    392            // application then stop
     423           // application then stop downloading
    393424           if (size == app_.maxDownloads()) {
    394            stop();
     425           // NOTE: the app can continue displaying images forever after download is
     426           // finished, until interrupted/stopped.
     427           // So don't set stop_running=false just because downloads have finished.
     428           //stop_running = true; // Don't do this!
     429           //thread_running = false;
     430           //thread.currentThread().interrupt();
     431
     432           stop_downloading = true;
     433           //stop(); // TODO, remove this, replacing with above
     434           
    395435           }
    396436           
     
    398438       catch (Exception e) {
    399439           thread_running_ = false;
    400            stop();
     440           //stop(); // TODO
     441           stop_downloading = true;
    401442           e.printStackTrace();
    402443       }
     
    412453    public void rec_add_images(String new_url, int depth)
    413454    {
    414 
    415455    if (verbosity_ >= 2) {
    416456        System.err.println("*** Inspecting url: " + new_url);
     
    424464
    425465    // connect to the url
     466    // stopRunning would have set the interrupted flag, and
     467    // CURL checks for that in its loop, *outside* its potentially-blocking read() call
    426468    CURL curl = (app_.gsdlversion == 3) ? new CURL(new_url, app_.baseURL) : new CURL(new_url);
    427469
     
    445487        }
    446488        // process each of the image links according to the parameters given.
    447         for (int i = 0; i < src_links.size(); i++)
     489        for (int i = 0; i < src_links.size() && !stop_running && !stop_downloading; i++)
    448490        {
    449491        URL url = (URL)src_links.get(i);
     
    491533        }
    492534
     535        if(stop_running && verbosity_ >= 3) {
     536        System.err.println("*** DownloadUrls.rec_add_images() - Asked to stop running");
     537        return;
     538        }
     539       
    493540        // get all the <code><a href=</code> links into a vector
    494541        Vector href_links = curl.getHrefLinks();
     
    500547
    501548        // process each of the href links according to the parameters given.
    502         for (int i = 0; i < href_links.size(); i++)
     549        for (int i = 0; i < href_links.size() && !stop_running && !stop_downloading; i++)
    503550        {
    504551           
     
    546593        System.err.println("Unable able to download "+new_url);
    547594    }
     595
     596    if(stop_running && verbosity_ >= 3) {
     597        System.err.println("*** DownloadUrls.rec_add_images() thread has been told to stop.");
     598    }
    548599    }
    549600
     
    566617        return;
    567618        }
    568        
     619
    569620        // open a url to the file written
    570621        URL u = new URL(starting_url_ + "externallinks");
     
    576627        String l = r.readLine();
    577628        // split the line of the space, first part is the image, second part the link
    578         while (l != null) {
     629        while (l != null && !stop_running && !stop_downloading) {
    579630   
    580631        String tmp1 = new String();
     
    600651        r.close();
    601652
     653        if(stop_running && verbosity_ >= 3) {
     654            System.err.println("*** DownloadUrls.externalLinks(): Asked to stop running");
     655        }
     656       
    602657    } catch (Exception e) {
    603658        e.printStackTrace();
     
    612667    visited_url_ = new Hashtable();
    613668    visited_images_ = new Hashtable();
    614 
    615         rec_add_images(starting_url_,1);
     669   
     670    rec_add_images(starting_url_,1);
    616671    download_images_.stopDownload();
    617     System.err.println("Download thread finished.");
     672    System.err.println("DownloadUrls.run() - download thread finished.");
    618673    }
    619674}
Note: See TracChangeset for help on using the changeset viewer.