Ticket #7140: 7140_cache.patch

File 7140_cache.patch, 6.0 KB (added by simon04, 12 years ago)
  • src/org/openstreetmap/josm/gui/GettingStarted.java

    diff --git a/src/org/openstreetmap/josm/gui/GettingStarted.java b/src/org/openstreetmap/josm/gui/GettingStarted.java
    index 828b1fe..5a72523 100644
    a b public class GettingStarted extends JPanel {  
    5353    /**
    5454     * Grabs current MOTD from cache or webpage and parses it.
    5555     */
    56     private static class MotdContent extends CacheCustomContent {
     56    private static class MotdContent extends CacheCustomContent<RuntimeException> {
    5757        public MotdContent() {
    5858            super("motd.html", CacheCustomContent.INTERVAL_DAILY);
    5959        }
  • src/org/openstreetmap/josm/io/CacheCustomContent.java

    diff --git a/src/org/openstreetmap/josm/io/CacheCustomContent.java b/src/org/openstreetmap/josm/io/CacheCustomContent.java
    index acdcc49..9a46e97 100644
    a b import org.openstreetmap.josm.Main;  
    1616 * Use this class if you want to cache and store a single file that gets updated regularly.
    1717 * Unless you flush() it will be kept in memory. If you want to cache a lot of data and/or files,
    1818 * use CacheFiles
     19 * @param <T> a {@link Throwable} that may be thrown during {@link #updateData()},
     20 * use {@link RuntimeException} if no exception must be handled.
    1921 * @author xeen
    2022 *
    2123 */
    22 public abstract class CacheCustomContent {
     24public abstract class CacheCustomContent<T extends Throwable> {
    2325    /**
    2426     * Common intervals
    2527     */
    public abstract class CacheCustomContent {  
    5658     * executed in the current thread.
    5759     * @return the data to cache
    5860     */
    59     protected abstract byte[] updateData();
     61    protected abstract byte[] updateData() throws T;
    6062
    6163    /**
    6264     * This function serves as a comfort hook to perform additional checks if the cache is valid
    public abstract class CacheCustomContent {  
    8284     * Updates data if required
    8385     * @return Returns the data
    8486     */
    85     public byte[] updateIfRequired() {
     87    public byte[] updateIfRequired() throws T {
    8688        if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
    8789                || !isCacheValid())
    8890            return updateForce();
    public abstract class CacheCustomContent {  
    9395     * Updates data if required
    9496     * @return Returns the data as string
    9597     */
    96     public String updateIfRequiredString() {
     98    public String updateIfRequiredString() throws T {
    9799        if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
    98100                || !isCacheValid())
    99101            return updateForceString();
    public abstract class CacheCustomContent {  
    104106     * Executes an update regardless of updateInterval
    105107     * @return Returns the data
    106108     */
    107     public byte[] updateForce() {
     109    public byte[] updateForce() throws T {
    108110        this.data = updateData();
    109111        saveToDisk();
    110112        Main.pref.putInteger("cache." + ident, (int)(new Date().getTime()/1000));
    public abstract class CacheCustomContent {  
    115117     * Executes an update regardless of updateInterval
    116118     * @return Returns the data as String
    117119     */
    118     public String updateForceString() {
     120    public String updateForceString() throws T {
    119121        updateForce();
    120122        try {
    121123            return new String(data,"utf-8");
    public abstract class CacheCustomContent {  
    129131     * Returns the data without performing any updates
    130132     * @return the data
    131133     */
    132     public byte[] getData() {
     134    public byte[] getData() throws T {
    133135        if(data == null) {
    134136            loadFromDisk();
    135137        }
    public abstract class CacheCustomContent {  
    140142     * Returns the data without performing any updates
    141143     * @return the data as String
    142144     */
    143     public String getDataString() {
     145    public String getDataString() throws T {
    144146        try {
    145147            return new String(getData(), "utf-8");
    146148        } catch(UnsupportedEncodingException e){
    public abstract class CacheCustomContent {  
    152154    /**
    153155     * Tries to load the data using the given ident from disk. If this fails, data will be updated
    154156     */
    155     private void loadFromDisk() {
     157    private void loadFromDisk() throws T {
    156158        if(Main.applet)
    157159            this.data = updateForce();
    158160        else {
  • src/org/openstreetmap/josm/io/OsmApi.java

    diff --git a/src/org/openstreetmap/josm/io/OsmApi.java b/src/org/openstreetmap/josm/io/OsmApi.java
    index 31c1055..e01d3eb 100644
    a b public class OsmApi extends OsmConnection {  
    152152        return host;
    153153    }
    154154
     155    private class CapabilitiesCache extends CacheCustomContent<OsmTransferException> {
     156
     157        ProgressMonitor monitor;
     158        boolean fastFail;
     159
     160        public CapabilitiesCache(ProgressMonitor monitor, boolean fastFail) {
     161            super("capabilities" + getBaseUrl().hashCode(), CacheCustomContent.INTERVAL_WEEKLY);
     162            this.monitor = monitor;
     163            this.fastFail = fastFail;
     164        }
     165        @Override
     166        protected byte[] updateData() throws OsmTransferException {
     167            String s = sendRequest("GET", "capabilities", null, monitor, false, fastFail);
     168            return s.getBytes();
     169        }
     170    }
     171
    155172    public void initialize(ProgressMonitor monitor) throws OsmApiInitializationException, OsmTransferCanceledException {
    156173        initialize(monitor, false);
    157174    }
    public class OsmApi extends OsmConnection {  
    167184            return;
    168185        cancel = false;
    169186        try {
    170             String s = sendRequest("GET", "capabilities", null, monitor, false, fastFail);
     187            String s = new CapabilitiesCache(monitor, fastFail).updateIfRequiredString();
    171188            InputSource inputSource = new InputSource(new StringReader(s));
    172189            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new CapabilitiesParser());
    173190            if (capabilities.supportsVersion("0.6")) {