Ticket #20416: 20416.patch
| File 20416.patch, 7.2 KB (added by , 5 years ago) |
|---|
-
src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java
38 38 import org.openstreetmap.josm.data.osm.RelationMember; 39 39 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 40 40 import org.openstreetmap.josm.data.osm.Way; 41 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;42 41 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 43 42 import org.openstreetmap.josm.spi.preferences.Config; 44 43 import org.openstreetmap.josm.tools.Logging; … … 323 322 threadsNumber, Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY)); 324 323 CompletionService<FetchResult> ecs = new ExecutorCompletionService<>(exec); 325 324 List<Future<FetchResult>> jobs = new ArrayList<>(); 326 while (!toFetch.isEmpty() ) {325 while (!toFetch.isEmpty() && !isCanceled()) { 327 326 jobs.add(ecs.submit(new Fetcher(type, extractIdPackage(toFetch), progressMonitor))); 328 327 } 329 328 // Run the fetchers … … 555 554 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { 556 555 // This method is implemented because of the OsmServerReader inheritance, but not used, 557 556 // as the main target of this class is the call() method. 558 return fetch( progressMonitor).dataSet;557 return fetch().dataSet; 559 558 } 560 559 561 560 @Override 562 561 public FetchResult call() throws Exception { 563 return fetch( progressMonitor);562 return fetch(); 564 563 } 565 564 566 565 /** 567 566 * fetches the requested primitives and updates the specified progress monitor. 568 * @param progressMonitor the progress monitor569 567 * @return the {@link FetchResult} of this operation 570 568 * @throws OsmTransferException if an error occurs while communicating with the API server 571 569 */ 572 protected FetchResult fetch( ProgressMonitor progressMonitor) throws OsmTransferException {570 protected FetchResult fetch() throws OsmTransferException { 573 571 try { 574 return multiGetIdPackage(type, pkg , progressMonitor);572 return multiGetIdPackage(type, pkg); 575 573 } catch (OsmApiException e) { 576 574 if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { 577 575 if (pkg.size() > 4) { … … 585 583 return res; 586 584 } else { 587 585 Logging.info(tr("Server replied with response code 404, retrying with an individual request for each object.")); 588 return singleGetIdPackage(type, pkg , progressMonitor);586 return singleGetIdPackage(type, pkg); 589 587 } 590 588 } else { 591 589 throw e; … … 605 603 * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY}, 606 604 * {@link OsmPrimitiveType#RELATION RELATION} 607 605 * @param pkg the package of ids 608 * @param progressMonitor progress monitor609 606 * @return the {@link FetchResult} of this operation 610 607 * @throws OsmTransferException if an error occurs while communicating with the API server 611 608 */ 612 protected FetchResult multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) 613 throws OsmTransferException { 609 protected FetchResult multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) throws OsmTransferException { 610 if (isCanceled()) 611 return new FetchResult(null, null); 614 612 String request = buildRequestString(type, pkg); 615 613 FetchResult result = null; 616 try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {614 try (InputStream in = getInputStream(request, progressMonitor.createSubTaskMonitor(1, false))) { 617 615 if (in == null) return null; 618 616 progressMonitor.subTask(tr("Downloading OSM data...")); 619 617 try { … … 635 633 * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY}, 636 634 * {@link OsmPrimitiveType#RELATION RELATION} 637 635 * @param id the id 638 * @param progressMonitor progress monitor639 636 * @return the {@link DataSet} resulting of this operation 640 637 * @throws OsmTransferException if an error occurs while communicating with the API server 641 638 */ 642 protected DataSet singleGetId(OsmPrimitiveType type, long id , ProgressMonitor progressMonitor) throws OsmTransferException {639 protected DataSet singleGetId(OsmPrimitiveType type, long id) throws OsmTransferException { 643 640 String request = buildRequestString(type, Collections.singleton(id)); 644 641 DataSet result = null; 645 try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {642 try (InputStream in = getInputStream(request, progressMonitor.createSubTaskMonitor(1, false))) { 646 643 if (in == null) return null; 647 644 progressMonitor.subTask(tr("Downloading OSM data...")); 648 645 try { … … 667 664 * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY}, 668 665 * {@link OsmPrimitiveType#RELATION RELATION} 669 666 * @param pkg the set of ids 670 * @param progressMonitor progress monitor671 667 * @return the {@link FetchResult} of this operation 672 668 * @throws OsmTransferException if an error occurs while communicating with the API server 673 669 */ 674 protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg , ProgressMonitor progressMonitor)670 protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) 675 671 throws OsmTransferException { 676 672 FetchResult result = new FetchResult(new DataSet(), new HashSet<PrimitiveId>()); 677 673 String baseUrl = OsmApi.getOsmApi().getBaseUrl(); 678 674 for (long id : pkg) { 675 if (isCanceled()) return result; 679 676 try { 680 677 String msg; 681 678 switch (type) { … … 687 684 default: throw new AssertionError(); 688 685 } 689 686 progressMonitor.setCustomText(msg); 690 result.dataSet.mergeFrom(singleGetId(type, id , progressMonitor));687 result.dataSet.mergeFrom(singleGetId(type, id)); 691 688 } catch (OsmApiException e) { 692 689 if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { 693 690 Logging.info(tr("Server replied with response code 404 for id {0}. Skipping.", Long.toString(id))); … … 699 696 } 700 697 return result; 701 698 } 699 700 @Override 701 public boolean isCanceled() { 702 return super.isCanceled() || progressMonitor.isCanceled(); 703 } 702 704 } 703 705 }
