Changeset 1664 in josm


Ignore:
Timestamp:
2009-06-11T23:04:51+02:00 (15 years ago)
Author:
Gubaer
Message:

fixed #2656 - JOSM makes excessive capabilities requests to the server

Location:
trunk/src/org/openstreetmap/josm/io
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r1663 r1664  
    2121import java.util.ArrayList;
    2222import java.util.Collection;
     23import java.util.HashMap;
    2324import java.util.Properties;
    2425import java.util.StringTokenizer;
     
    5152    static public final int DEFAULT_MAX_NUM_RETRIES = 5;
    5253
     54    /** the collection of instantiated OSM APIs */
     55    private static HashMap<String, OsmApi> instances = new HashMap<String, OsmApi>();
     56
     57    /**
     58     * replies the {@see OsmApi} for a given server URL
     59     *
     60     * @param serverUrl  the server URL
     61     * @return the OsmApi
     62     * @throws IllegalArgumentException thrown, if serverUrl is null
     63     *
     64     */
     65    static public OsmApi getOsmApi(String serverUrl) {
     66        OsmApi api = instances.get(serverUrl);
     67        if (api == null) {
     68            api = new OsmApi(serverUrl);
     69        }
     70        return api;
     71    }
     72    /**
     73     * replies the {@see OsmApi} for the URL given by the preference <code>osm-server.url</code>
     74     *
     75     * @return the OsmApi
     76     * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set
     77     *
     78     */
     79    static public OsmApi getOsmApi() {
     80        String serverUrl = Main.pref.get("osm-server.url");
     81        if (serverUrl == null)
     82            throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url"));
     83        return getOsmApi(serverUrl);
     84    }
     85
     86    /** the server URL */
     87    private String serverUrl;
     88
    5389    /**
    5490     * Object describing current changeset
     
    64100     * Minimum API version accepted by server, from capabilities response
    65101     */
    66     private String minVersion = null;
     102    //private String minVersion = null;
    67103
    68104    /**
    69105     * Maximum API version accepted by server, from capabilities response
    70106     */
    71     private String maxVersion = null;
     107    //private String maxVersion = null;
    72108
    73109    /**
     
    77113    private String maxArea = null;
    78114
     115    /** the api capabilities */
     116    private Capabilities capabilities = new Capabilities();
     117
    79118    /**
    80119     * true if successfully initialized
     
    89128     */
    90129    private class CapabilitiesParser extends DefaultHandler {
     130        @Override
     131        public void startDocument() throws SAXException {
     132            capabilities.clear();
     133        }
     134
    91135        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    92             if (qName.equals("version")) {
    93                 minVersion = atts.getValue("minimum");
    94                 maxVersion = atts.getValue("maximum");
    95             } else if (qName.equals("area")) {
    96                 maxArea = atts.getValue("maximum");
     136            for (int i=0; i< qName.length(); i++) {
     137                capabilities.put(qName, atts.getQName(i), atts.getValue(i));
    97138            }
    98139        }
     140    }
     141
     142    /**
     143     * creates an OSM api for a specific server URL
     144     *
     145     * @param serverUrl the server URL. Must not be null
     146     * @exception IllegalArgumentException thrown, if serverUrl is null
     147     */
     148    protected OsmApi(String serverUrl)  {
     149        if (serverUrl == null)
     150            throw new IllegalArgumentException(tr("parameter '{0} must not be null", "serverUrl"));
     151        this.serverUrl = serverUrl;
     152    }
     153
     154    /**
     155     * creates an instance of the OSM API. Initializes the server URL with the
     156     * value of the preference <code>osm-server.url</code>
     157     *
     158     * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set
     159     */
     160    protected OsmApi() {
     161        this.serverUrl = Main.pref.get("osm-server.url");
     162        if (serverUrl == null)
     163            throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url"));
    99164    }
    100165
     
    136201     * @exception Exception any other exception
    137202     */
    138     public void initialize() throws UnknownHostException,SocketTimeoutException, ConnectException,Exception {
     203    public void initialize() throws OsmApiInitializationException {
     204        if (initialized)
     205            return;
    139206        initAuthentication();
    140207        try {
    141             initialized = true; // note: has to be before the sendRequest or that will throw!
    142             String capabilities = sendRequest("GET", "capabilities", null);
    143             InputSource inputSource = new InputSource(new StringReader(capabilities));
     208            String s = sendRequest("GET", "capabilities", null);
     209            InputSource inputSource = new InputSource(new StringReader(s));
    144210            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
    145             if (maxVersion.compareTo("0.6") >= 0) {
     211            if (capabilities.supportsVersion("0.6")) {
    146212                version = "0.6";
    147             } else if (minVersion.compareTo("0.5") <= 0) {
     213            } else if (capabilities.supportsVersion("0.5")) {
    148214                version = "0.5";
    149215            } else {
    150216                System.err.println(tr("This version of JOSM is incompatible with the configured server."));
    151217                System.err.println(tr("It supports protocol versions 0.5 and 0.6, while the server says it supports {0} to {1}.",
    152                         minVersion, maxVersion));
     218                        capabilities.get("version", "minimum"), capabilities.get("version", "maximum")));
    153219                initialized = false;
    154220            }
    155221            System.out.println(tr("Communications with {0} established using protocol version {1}",
    156                     Main.pref.get("osm-server.url"),
     222                    serverUrl,
    157223                    version));
    158224            osmWriter.setVersion(version);
     225            initialized = true;
    159226        } catch (Exception ex) {
    160227            initialized = false;
    161             throw ex;
     228            throw new OsmApiInitializationException(ex);
    162229        }
    163230    }
     
    215282     */
    216283    public String getBaseUrl() {
    217         StringBuffer rv = new StringBuffer(Main.pref.get("osm-server.url"));
     284        StringBuffer rv = new StringBuffer(serverUrl);
    218285        if (version != null) {
    219286            rv.append("/");
     
    235302     */
    236303    public void createPrimitive(OsmPrimitive osm) throws OsmTransferException {
     304        initialize();
    237305        osm.id = parseLong(sendRequest("PUT", which(osm)+"/create", toXml(osm, true)));
    238306        osm.version = 1;
     
    248316     */
    249317    public void modifyPrimitive(OsmPrimitive osm) throws OsmTransferException {
     318        initialize();
    250319        if (version.equals("0.5")) {
    251320            // legacy mode does not return the new object version.
     
    263332     */
    264333    public void deletePrimitive(OsmPrimitive osm) throws OsmTransferException {
     334        initialize();
    265335        // legacy mode does not require payload. normal mode (0.6 and up) requires payload for version matching.
    266336        sendRequest("DELETE", which(osm)+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false));
     
    288358     */
    289359    public void stopChangeset() throws OsmTransferException {
     360        initialize();
    290361        Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset..."));
    291362        sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null);
     
    306377            throw new OsmTransferException(tr("No changeset present for diff upload"));
    307378
     379        initialize();
    308380        final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
    309 
    310381
    311382        CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
     
    362433            String requestBody) throws OsmTransferException {
    363434
    364         if (!initialized) throw new OsmTransferException(tr("Not initialized"));
    365 
    366435        StringBuffer responseBody = new StringBuffer();
    367436
    368437        int retries = Main.pref.getInteger("osm-server.max-num-retries", DEFAULT_MAX_NUM_RETRIES);
    369438        retries = Math.max(0,retries);
    370 
    371439
    372440        while(true) { // the retry loop
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r1608 r1664  
    2929 */
    3030public abstract class OsmServerReader extends OsmConnection {
    31    
    32     private OsmApi api = new OsmApi();
    33    
     31
     32    private OsmApi api = OsmApi.getOsmApi();
     33
    3434    /**
    3535     * Open a connection to the given url and return a reader on the input stream
     
    4040     */
    4141    protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
    42        
     42
    4343        // initialize API. Abort download in case of configuration or network
    4444        // errors
     
    4848        } catch(Exception e) {
    4949            JOptionPane.showMessageDialog(
    50                 null,
    51                 tr(   "Failed to initialize communication with the OSM server {0}.\n"
    52                     + "Check the server URL in your preferences and your internet connection.",
    53                     Main.pref.get("osm-server.url")
    54                 ),
    55                 tr("Error"),
    56                 JOptionPane.ERROR_MESSAGE
     50                    null,
     51                    tr(   "Failed to initialize communication with the OSM server {0}.\n"
     52                            + "Check the server URL in your preferences and your internet connection.",
     53                            Main.pref.get("osm-server.url")
     54                    ),
     55                    tr("Error"),
     56                    JOptionPane.ERROR_MESSAGE
    5757            );
    5858            e.printStackTrace();
     
    6666    protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
    6767
    68 //        System.out.println("download: "+urlStr);
     68        //        System.out.println("download: "+urlStr);
    6969        URL url = new URL(urlStr);
    7070        activeConnection = (HttpURLConnection)url.openConnection();
     
    7474        }
    7575
    76         if (Main.pref.getBoolean("osm-server.use-compression", true))
     76        if (Main.pref.getBoolean("osm-server.use-compression", true)) {
    7777            activeConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
     78        }
    7879
    7980        activeConnection.setConnectTimeout(15000);
     
    8990            return null;
    9091        if (activeConnection.getResponseCode() == 500)
    91         {
    9292            throw new IOException(tr("Server returned internal error. Try a reduced area or retry after waiting some time."));
    93         }
    94 //      System.out.println("got return: "+activeConnection.getResponseCode());
    9593
    9694        String encoding = activeConnection.getContentEncoding();
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r1663 r1664  
    3333    public Collection<OsmPrimitive> processed;
    3434
    35     private OsmApi api = new OsmApi();
     35    private OsmApi api = OsmApi.getOsmApi();
    3636
    3737    private static final int MSECS_PER_SECOND = 1000;
Note: See TracChangeset for help on using the changeset viewer.