aboutsummaryrefslogtreecommitdiffstats
path: root/netx/net/sourceforge/jnlp/cache/CacheEntry.java
blob: 9299962e458aca77519021cb0692cc4cc1dc0867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) 2001-2003 Jon A. Maxwell (JAM)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package net.sourceforge.jnlp.cache;

import static net.sourceforge.jnlp.runtime.Translator.R;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;
import java.security.*;
import javax.jnlp.*;

import net.sourceforge.jnlp.*;
import net.sourceforge.jnlp.runtime.*;
import net.sourceforge.jnlp.util.*;

/**
 * Describes an entry in the cache.<p>
 *
 * @author <a href="mailto:jmaxwell@users.sourceforge.net">Jon A. Maxwell (JAM)</a> - initial author
 * @version $Revision: 1.10 $
 */
public class CacheEntry {

    /** the remote resource location */
    private URL location;

    /** the requested version */
    private Version version;

    /** info about the cached file */
    private PropertiesFile properties;

    /**
     * Create a CacheEntry for the resources specified as a remote
     * URL.
     *
     * @param location the remote resource location
     * @param version the version of the resource
     */
    public CacheEntry(URL location, Version version) {
        this.location = location;
        this.version = version;

        File infoFile = CacheUtil.getCacheFile(location, version);
        infoFile = new File(infoFile.getPath() + ".info"); // replace with something that can't be clobbered

        properties = new PropertiesFile(infoFile, R("CAutoGen"));
    }

    /**
     * Initialize the cache entry data from a connection to the
     * remote resource (does not store data).
     */
    void initialize(URLConnection connection) {
        long modified = connection.getLastModified();
        long length = connection.getContentLength(); // an int

        properties.setProperty("content-length", Long.toString(length));
        properties.setProperty("last-modified", Long.toString(modified));
    }

    /**
     * Returns the remote location this entry caches.
     */
    public URL getLocation() {
        return location;
    }

    /**
     * Returns the time in the local system clock that the file was
     * most recently checked for an update.
     */
    public long getLastUpdated() {
        try {
            return Long.parseLong(properties.getProperty("last-updated"));
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * Sets the time in the local system clock that the file was
     * most recently checked for an update.
     */
    public void setLastUpdated(long updatedTime) {
        properties.setProperty("last-updated", Long.toString(updatedTime));
    }

    /**
     * Returns whether there is a version of the URL contents in
     * the cache and it is up to date.  This method may not return
     * immediately.
     *
     * @param connection a connection to the remote URL
     * @return whether the cache contains the version
     */
    public boolean isCurrent(URLConnection connection) {
        boolean cached = isCached();

        if (!cached)
            return false;

        try {
            long remoteModified = connection.getLastModified();
            long cachedModified = Long.parseLong(properties.getProperty("last-modified"));

            if (remoteModified > 0 && remoteModified <= cachedModified)
                return true;
            else
                return false;
        } catch (Exception ex) {
            if (JNLPRuntime.isDebug())
                ex.printStackTrace();

            return cached; // if can't connect return whether already in cache
        }
    }

    /**
     * Returns true if the cache has a local copy of the contents
     * of the URL matching the specified version string.
     *
     * @return true if the resource is in the cache
     */
    public boolean isCached() {
        File localFile = CacheUtil.getCacheFile(location, version);
        if (!localFile.exists())
            return false;

        try {
            long cachedLength = localFile.length();
            long remoteLength = Long.parseLong(properties.getProperty("content-length", "-1"));

            if (remoteLength >= 0 && cachedLength != remoteLength)
                return false;
            else
                return true;
        } catch (Exception ex) {
            if (JNLPRuntime.isDebug())
                ex.printStackTrace();

            return false; // should throw?
        }
    }

    /**
     * Save the current information for the cache entry.
     */
    protected void store() {
        properties.store();
    }

}