Ticket #20416: 20416.patch

File 20416.patch, 7.2 KB (added by GerdP, 5 years ago)
  • src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java

     
    3838import org.openstreetmap.josm.data.osm.RelationMember;
    3939import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    4040import org.openstreetmap.josm.data.osm.Way;
    41 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    4241import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    4342import org.openstreetmap.josm.spi.preferences.Config;
    4443import org.openstreetmap.josm.tools.Logging;
     
    323322                threadsNumber, Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
    324323        CompletionService<FetchResult> ecs = new ExecutorCompletionService<>(exec);
    325324        List<Future<FetchResult>> jobs = new ArrayList<>();
    326         while (!toFetch.isEmpty()) {
     325        while (!toFetch.isEmpty() && !isCanceled()) {
    327326            jobs.add(ecs.submit(new Fetcher(type, extractIdPackage(toFetch), progressMonitor)));
    328327        }
    329328        // Run the fetchers
     
    555554        public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
    556555            // This method is implemented because of the OsmServerReader inheritance, but not used,
    557556            // as the main target of this class is the call() method.
    558             return fetch(progressMonitor).dataSet;
     557            return fetch().dataSet;
    559558        }
    560559
    561560        @Override
    562561        public FetchResult call() throws Exception {
    563             return fetch(progressMonitor);
     562            return fetch();
    564563        }
    565564
    566565        /**
    567566         * fetches the requested primitives and updates the specified progress monitor.
    568          * @param progressMonitor the progress monitor
    569567         * @return the {@link FetchResult} of this operation
    570568         * @throws OsmTransferException if an error occurs while communicating with the API server
    571569         */
    572         protected FetchResult fetch(ProgressMonitor progressMonitor) throws OsmTransferException {
     570        protected FetchResult fetch() throws OsmTransferException {
    573571            try {
    574                 return multiGetIdPackage(type, pkg, progressMonitor);
     572                return multiGetIdPackage(type, pkg);
    575573            } catch (OsmApiException e) {
    576574                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
    577575                    if (pkg.size() > 4) {
     
    585583                        return res;
    586584                    } else {
    587585                        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);
    589587                    }
    590588                } else {
    591589                    throw e;
     
    605603         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
    606604         * {@link OsmPrimitiveType#RELATION RELATION}
    607605         * @param pkg the package of ids
    608          * @param progressMonitor progress monitor
    609606         * @return the {@link FetchResult} of this operation
    610607         * @throws OsmTransferException if an error occurs while communicating with the API server
    611608         */
    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);
    614612            String request = buildRequestString(type, pkg);
    615613            FetchResult result = null;
    616             try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
     614            try (InputStream in = getInputStream(request, progressMonitor.createSubTaskMonitor(1, false))) {
    617615                if (in == null) return null;
    618616                progressMonitor.subTask(tr("Downloading OSM data..."));
    619617                try {
     
    635633         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
    636634         * {@link OsmPrimitiveType#RELATION RELATION}
    637635         * @param id the id
    638          * @param progressMonitor progress monitor
    639636         * @return the {@link DataSet} resulting of this operation
    640637         * @throws OsmTransferException if an error occurs while communicating with the API server
    641638         */
    642         protected DataSet singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
     639        protected DataSet singleGetId(OsmPrimitiveType type, long id) throws OsmTransferException {
    643640            String request = buildRequestString(type, Collections.singleton(id));
    644641            DataSet result = null;
    645             try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
     642            try (InputStream in = getInputStream(request, progressMonitor.createSubTaskMonitor(1, false))) {
    646643                if (in == null) return null;
    647644                progressMonitor.subTask(tr("Downloading OSM data..."));
    648645                try {
     
    667664         * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
    668665         * {@link OsmPrimitiveType#RELATION RELATION}
    669666         * @param pkg the set of ids
    670          * @param progressMonitor progress monitor
    671667         * @return the {@link FetchResult} of this operation
    672668         * @throws OsmTransferException if an error occurs while communicating with the API server
    673669         */
    674         protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor)
     670        protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg)
    675671                throws OsmTransferException {
    676672            FetchResult result = new FetchResult(new DataSet(), new HashSet<PrimitiveId>());
    677673            String baseUrl = OsmApi.getOsmApi().getBaseUrl();
    678674            for (long id : pkg) {
     675                if (isCanceled()) return result;
    679676                try {
    680677                    String msg;
    681678                    switch (type) {
     
    687684                        default: throw new AssertionError();
    688685                    }
    689686                    progressMonitor.setCustomText(msg);
    690                     result.dataSet.mergeFrom(singleGetId(type, id, progressMonitor));
     687                    result.dataSet.mergeFrom(singleGetId(type, id));
    691688                } catch (OsmApiException e) {
    692689                    if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
    693690                        Logging.info(tr("Server replied with response code 404 for id {0}. Skipping.", Long.toString(id)));
     
    699696            }
    700697            return result;
    701698        }
     699
     700        @Override
     701        public boolean isCanceled() {
     702            return super.isCanceled() || progressMonitor.isCanceled();
     703        }
    702704    }
    703705}