Ticket #2106: MakeMOTDconcurrent.patch

File MakeMOTDconcurrent.patch, 6.1 KB (added by xeen, 15 years ago)
  • src/org/openstreetmap/josm/gui/GettingStarted.java

     
    66
    77import java.io.IOException;
    88import java.util.ArrayList;
     9import java.util.concurrent.Executors;
     10import java.util.concurrent.ExecutorService;
     11import java.util.concurrent.Callable;
     12import java.util.concurrent.Future;
    913import java.util.regex.Matcher;
    1014import java.util.regex.Pattern;
    1115
     
    4246            }
    4347        }
    4448    }
     49   
     50    public class readMOTD implements Callable<String> {
     51        private boolean isLocalized;
     52        private boolean isHelp;
     53        private String urlLoc;
     54        private String urlIntl;
     55        private String urlBase;
     56       
     57        readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) {
     58          this.isLocalized = isLocalized;
     59          this.urlBase = urlBase;
     60          this.urlLoc = urlLoc;
     61          this.urlIntl = urlIntl;
     62          this.isHelp = isHelp;
     63        }
    4564
     65        public String call() {
     66            WikiReader wr = new WikiReader(urlBase);
     67            String content = "";
     68            try {
     69                // If we hit a non-localized link here, we already know there's no translated version available
     70                String message = isLocalized ? wr.read(urlLoc) : "";
     71                // Look for non-localized version
     72                if (message.equals(""))
     73                    message = wr.read(urlIntl);
     74
     75                if (!message.equals(""))
     76                    if(isHelp)
     77                        content += message;
     78                    else
     79                        content += "<ul><li>"+ message.substring(8)+"</li></ul>";
     80            } catch (IOException ioe) {
     81                try {
     82                    if(isHelp)
     83                        content += wr.read(urlIntl);
     84                    else
     85                        content += "<ul><li>"+wr.read(urlIntl).substring(8)+"</li></ul>";
     86                } catch (IOException ioe2) {
     87                }
     88            }
     89           
     90            return content;
     91        }
     92    }
     93
    4694    private void assignContent() {
    4795        if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return;
    4896
     
    71119        Matcher matcher = versionPattern.matcher(motdcontent);
    72120        matcher.reset();
    73121
    74         ArrayList<Object> links = new ArrayList<Object>();
     122        ArrayList<String[]> links = new ArrayList<String[]>();
    75123        String linksList="";
    76124        while (matcher.find()) {
    77125            // Discards all but the selected locale and non-localized links
     
    81129            links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)});
    82130            linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
    83131        }
    84 
     132       
     133        // We cannot use Main.worker here because it's single-threaded and
     134        // setting it to multi-threading will cause problems elsewhere
     135        ExecutorService slave = Executors.newCachedThreadPool();
     136       
     137        ArrayList<Future<String>> linkContent = new ArrayList<Future<String>>();
    85138        for(int i=0; i < links.size(); i++) {
    86139            String[] obj = (String[])links.get(i);
    87140            int targetVersion = Integer.parseInt(obj[1]);
     
    89142            Boolean isLocalized = !obj[2].equals("");
    90143
    91144            // Prefer localized over non-localized links, if they're otherwise the same
    92             if(!isLocalized && linksList.indexOf(condition+obj[1]+languageCode+" ") >= 0)
     145            if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
    93146                continue;
    94147
    95148            boolean included = false;
     
    106159              included = myVersion <= targetVersion;
    107160
    108161            if(!included) continue;
    109 
    110             Boolean isHelp = targetVersion == 1;
    111 
     162           
     163            boolean isHelp = targetVersion == 1;
    112164            String urlStart = baseurl + "/wiki/";
    113165            String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt");
    114 
     166            String urlLoc = urlStart + languageCode + urlEnd;
     167            String urlIntl = urlStart + urlEnd;
     168           
     169            // This adds all links to the worker which will download them concurrently
     170            linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
     171        }
     172       
     173        for(int i=0; i < linkContent.size(); i++) {
    115174            try {
    116                 // If we hit a non-localized link here, we already know there's no translated version available
    117                 String message = isLocalized ? wr.read(urlStart + languageCode + urlEnd) : "";
    118                 // Look for non-localized version
    119                 if (message.equals(""))
    120                     message = wr.read(urlStart + urlEnd);
    121 
    122                 if (!message.equals(""))
    123                     if(isHelp)
    124                         content += message;
    125                     else
    126                         content += "<ul><li>"+ message.substring(8)+"</li></ul>";
    127             } catch (IOException ioe) {
    128                 try {
    129                     if(isHelp)
    130                         content += wr.read(urlStart + urlEnd);
    131                     else
    132                         content += "<ul><li>"+wr.read(urlStart + urlEnd).substring(8)+"</li></ul>";
    133                 } catch (IOException ioe2) {
    134                 }
    135             }
     175                content += linkContent.get(i).get();
     176            } catch (Exception e) {}
    136177        }
    137 
     178       
    138179        content = "<html>\n"+
    139180            "<style type=\"text/css\">\n"+
    140181            "body { font-family: sans-serif; font-weight: bold; }\n"+
     
    149190        super(new BorderLayout());
    150191        assignContent();
    151192
    152         // panel.add(GBC.glue(0,1), GBC.eol());
    153         //panel.setMinimumSize(new Dimension(400, 600));
    154193        Component linkGeneral = new LinkGeneral(content);
    155194        JScrollPane scroller = new JScrollPane(linkGeneral);
    156195        scroller.setViewportBorder(new EmptyBorder(10,100,10,100));