Changeset 4709 in josm for trunk/src/org


Ignore:
Timestamp:
2011-12-25T15:03:43+01:00 (9 years ago)
Author:
simon04
Message:

see #7140 - cache the capabilities call using CacheCustomContent

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/GettingStarted.java

    r4364 r4709  
    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);
  • trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java

    r3947 r4709  
    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
     
    5759     * @return the data to cache
    5860     */
    59     protected abstract byte[] updateData();
     61    protected abstract byte[] updateData() throws T;
    6062
    6163    /**
     
    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())
     
    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())
     
    105107     * @return Returns the data
    106108     */
    107     public byte[] updateForce() {
     109    public byte[] updateForce() throws T {
    108110        this.data = updateData();
    109111        saveToDisk();
     
    116118     * @return Returns the data as String
    117119     */
    118     public String updateForceString() {
     120    public String updateForceString() throws T {
    119121        updateForce();
    120122        try {
     
    130132     * @return the data
    131133     */
    132     public byte[] getData() {
     134    public byte[] getData() throws T {
    133135        if(data == null) {
    134136            loadFromDisk();
     
    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");
     
    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();
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r4690 r4709  
    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);
     
    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());
Note: See TracChangeset for help on using the changeset viewer.