/hg/icedtea-web: Resource refactor for setters and getters

aazores at icedtea.classpath.org aazores at icedtea.classpath.org
Tue May 20 21:09:27 UTC 2014


changeset 8151448ecea2 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=8151448ecea2
author: Andrew Azores <aazores at redhat.com>
date: Tue May 20 16:52:21 2014 -0400

	Resource refactor for setters and getters

	2014-05-20  Lukasz Dracz  <ldracz at redhat.com>
	            Jie Kang      <jkang at redhat.com>

	    * netx/net/sourceforge/jnlp/cache/Resource.java:
	    (location, localFile, requestVersion, downloadVersion,
	    transferred, size, status) made fields private and added
	    setters and getters, and all calling sites refactored
	    * netx/net/sourceforge/jnlp/cache/ResourceTracker.java:
	    Calling sites refactored
	    * netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java:
	    Calling sites refactored
	    * tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java:
	    (testGetLocation, testGetRequestVersion, testGetDownloadVersion,
	    testTransferredIsZero, testIncrementTransferred, testSizeIsNegativeOne,
	    testSetSize, testStatusIsCopied) added tests


diffstat:

 ChangeLog                                                    |   16 +
 netx/net/sourceforge/jnlp/cache/Resource.java                |  107 +++++++++-
 netx/net/sourceforge/jnlp/cache/ResourceTracker.java         |   73 +++---
 netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java      |    8 +-
 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java |   85 ++++++++-
 5 files changed, 234 insertions(+), 55 deletions(-)

diffs (truncated from 575 to 500 lines):

diff -r 22761e42d5b3 -r 8151448ecea2 ChangeLog
--- a/ChangeLog	Wed May 14 15:44:19 2014 -0400
+++ b/ChangeLog	Tue May 20 16:52:21 2014 -0400
@@ -1,3 +1,19 @@
+2014-05-20  Lukasz Dracz  <ldracz at redhat.com>
+            Jie Kang      <jkang at redhat.com>
+
+	* netx/net/sourceforge/jnlp/cache/Resource.java:
+	(location, localFile, requestVersion, downloadVersion,
+	transferred, size, status) made fields private and added
+	setters and getters, and all calling sites refactored
+	* netx/net/sourceforge/jnlp/cache/ResourceTracker.java:
+	Calling sites refactored
+	* netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java:
+	Calling sites refactored
+	* tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java:
+	(testGetLocation, testGetRequestVersion, testGetDownloadVersion,
+	testTransferredIsZero, testIncrementTransferred, testSizeIsNegativeOne,
+	testSetSize, testStatusIsCopied) added tests
+
 2014-05-14  Andrew Azores  <aazores at redhat.com>
 
 	* tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java:
diff -r 22761e42d5b3 -r 8151448ecea2 netx/net/sourceforge/jnlp/cache/Resource.java
--- a/netx/net/sourceforge/jnlp/cache/Resource.java	Wed May 14 15:44:19 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/Resource.java	Tue May 20 16:52:21 2014 -0400
@@ -21,6 +21,7 @@
 import java.util.Collection;
 import java.util.EnumSet;
 import java.util.List;
+import java.util.Set;
 
 import net.sourceforge.jnlp.Version;
 import net.sourceforge.jnlp.util.WeakList;
@@ -62,7 +63,6 @@
         STARTED // enqueued or being worked on
     }
 
-
     /** list of weak references of resources currently in use */
     private static final WeakList<Resource> resources = new WeakList<>();
 
@@ -70,31 +70,31 @@
     private final WeakList<ResourceTracker> trackers = new WeakList<>();
 
     /** the remote location of the resource */
-    final URL location;
+    private final URL location;
 
     /** the location to use when downloading */
     private URL downloadLocation;
 
     /** the local file downloaded to */
-    File localFile;
+    private File localFile;
 
     /** the requested version */
-    final Version requestVersion;
+    private final Version requestVersion;
 
     /** the version downloaded from server */
-    Version downloadVersion;
+    private Version downloadVersion;
 
     /** amount in bytes transferred */
-    volatile long transferred = 0;
+    private volatile long transferred = 0;
 
     /** total size of the resource, or -1 if unknown */
-    volatile long size = -1;
+    private volatile long size = -1;
 
     /** the status of the resource */
-    final EnumSet<Status> status = EnumSet.noneOf(Status.class);
-
+    private final EnumSet<Status> status = EnumSet.noneOf(Status.class);
+    
     /** Update policy for this resource */
-    final UpdatePolicy updatePolicy;
+    private final UpdatePolicy updatePolicy;
 
     /**
      * Create a resource.
@@ -152,8 +152,8 @@
      * Set the url to use for downloading the resource
      * @param location
      */
-    public void setDownloadLocation(URL location) {
-        downloadLocation = location;
+    public void setDownloadLocation(URL downloadLocation) {
+        this.downloadLocation = downloadLocation;
     }
 
     /**
@@ -170,6 +170,85 @@
             return null;
         }
     }
+    
+    /**
+     * Returns the local file currently being downloaded
+     */
+    public File getLocalFile() {
+    	return localFile;
+    }
+    
+    /**
+     * Sets the local file to be downloaded
+     */
+    public void setLocalFile(File localFile) {
+    	this.localFile = localFile;
+    }
+    
+    /**
+     * Returns the requested version
+     */
+    public Version getRequestVersion() {
+    	return requestVersion;
+    }
+    
+    /**
+     * Returns the version downloaded from server
+     */
+    public Version getDownloadVersion() {
+    	return downloadVersion;
+    }
+    
+    /**
+     * Sets the version downloaded from server
+     */
+    public void setDownloadVersion(Version downloadVersion) {
+    	this.downloadVersion = downloadVersion;
+    }
+    
+    /**
+     * Returns the amount in bytes transferred
+     */
+    public long getTransferred() {
+    	return transferred;
+    }
+    
+    /**
+     * Sets the amount transferred
+     */
+    public void setTransferred(long transferred) {
+    	this.transferred = transferred;
+    }
+    
+    /**
+     * Increments the amount transferred (in bytes)
+     */
+    public void incrementTransferred(long incTrans) {
+    	transferred += incTrans;
+    }
+
+    /**
+     * Returns the size of the resource
+     * @return size of resource (-1 if unknown)
+     */
+    public long getSize() {
+    	return size;
+    }
+
+    /**
+     * Sets the size of the resource
+     */
+    public void setSize(long size) {
+        this.size = size;
+    }
+
+    /**
+     * Returns the status of the resource
+     */
+    public Set<Status> getCopyOfStatus() {
+        return EnumSet.copyOf(status);
+
+    }
 
     /**
      * Check if the specified flag is set.
@@ -195,8 +274,6 @@
 
     /**
      * Returns the update policy for this resource
-     *
-     * @return The update policy
      */
     public UpdatePolicy getUpdatePolicy() {
         return this.updatePolicy;
@@ -355,5 +432,7 @@
     public String toString() {
         return "location=" + location.toString() + " state=" + getStatusString();
     }
+    
+    
 
 }
diff -r 22761e42d5b3 -r 8151448ecea2 netx/net/sourceforge/jnlp/cache/ResourceTracker.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Wed May 14 15:44:19 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java	Tue May 20 16:52:21 2014 -0400
@@ -239,7 +239,7 @@
      * @return whether the resource are already downloaded
      */
     private boolean checkCache(Resource resource, UpdatePolicy updatePolicy) {
-        if (!CacheUtil.isCacheable(resource.location, resource.downloadVersion)) {
+        if (!CacheUtil.isCacheable(resource.getLocation(), resource.getDownloadVersion())) {
             // pretend that they are already downloaded; essentially
             // they will just 'pass through' the tracker as if they were
             // never added (for example, not affecting the total download size).
@@ -251,15 +251,15 @@
         }
 
         if (updatePolicy != UpdatePolicy.ALWAYS && updatePolicy != UpdatePolicy.FORCE) { // save loading entry props file
-            CacheEntry entry = new CacheEntry(resource.location, resource.downloadVersion);
+            CacheEntry entry = new CacheEntry(resource.getLocation(), resource.getDownloadVersion());
 
             if (entry.isCached() && !updatePolicy.shouldUpdate(entry)) {
-                OutputController.getLogger().log("not updating: " + resource.location);
+                OutputController.getLogger().log("not updating: " + resource.getLocation());
 
                 synchronized (resource) {
-                    resource.localFile = CacheUtil.getCacheFile(resource.location, resource.downloadVersion);
-                    resource.size = resource.localFile.length();
-                    resource.transferred = resource.localFile.length();
+                    resource.setLocalFile(CacheUtil.getCacheFile(resource.getLocation(), resource.getDownloadVersion()));
+                    resource.setSize(resource.getLocalFile().length());
+                    resource.setTransferred(resource.getLocalFile().length());
                     resource.changeStatus(EnumSet.noneOf(Resource.Status.class), EnumSet.of(DOWNLOADED, CONNECTED, STARTED));
                 }
                 fireDownloadEvent(resource);
@@ -317,7 +317,7 @@
 
         Collection<Resource.Status> status;
         synchronized (resource) {
-            status = resource.status;
+            status = resource.getCopyOfStatus();
         }
 
         DownloadEvent event = new DownloadEvent(this, resource);
@@ -381,8 +381,8 @@
             if (resource.isSet(ERROR))
                 return null;
 
-            if (resource.localFile != null)
-                return resource.localFile;
+            if (resource.getLocalFile() != null)
+                return resource.getLocalFile();
 
             if (location.getProtocol().equalsIgnoreCase("file")) {
                 File file = UrlUtils.decodeUrlAsFile(location);
@@ -416,10 +416,10 @@
             if (!(resource.isSet(DOWNLOADED) || resource.isSet(ERROR)))
                 waitForResource(location, 0);
 
-            if (resource.localFile != null)
-                return new FileInputStream(resource.localFile);
+            if (resource.getLocalFile() != null)
+                return new FileInputStream(resource.getLocalFile());
 
-            return resource.location.openStream();
+            return resource.getLocation().openStream();
         } catch (InterruptedException ex) {
             throw new IOException("wait was interrupted");
         }
@@ -474,7 +474,7 @@
     public long getAmountRead(URL location) {
         // not atomic b/c transferred is a long, but so what (each
         // byte atomic? so probably won't affect anything...)
-        return getResource(location).transferred;
+        return getResource(location).getTransferred();
     }
 
     /**
@@ -544,7 +544,7 @@
      * @throws IllegalResourceDescriptorException if the resource is not being tracked
      */
     public long getTotalSize(URL location) {
-        return getResource(location).size; // atomic
+        return getResource(location).getSize(); // atomic
     }
 
     /**
@@ -642,7 +642,7 @@
     private void downloadResource(Resource resource) {
         resource.fireDownloadEvent(); // fire DOWNLOADING
         URLConnection con = null;
-        CacheEntry origEntry = new CacheEntry(resource.location, resource.downloadVersion); // This is where the jar file will be.
+        CacheEntry origEntry = new CacheEntry(resource.getLocation(), resource.getDownloadVersion()); // This is where the jar file will be.
         origEntry.lock();
 
         try {
@@ -658,11 +658,11 @@
              * foo.jar, the server might send us foo.jar.pack.gz or foo.jar.gz
              * instead. So we save the file with the appropriate extension
              */
-            URL downloadLocation = resource.location;
+            URL downloadLocation = resource.getLocation();
 
             String contentEncoding = con.getContentEncoding();
 
-            OutputController.getLogger().log(OutputController.Level.ERROR_DEBUG, "Downloading" + resource.location + " using " +
+            OutputController.getLogger().log(OutputController.Level.ERROR_DEBUG, "Downloading" + resource.getLocation() + " using " +
                         realLocation + " (encoding : " + contentEncoding + ")");
 
             boolean packgz = "pack200-gzip".equals(contentEncoding) ||
@@ -679,9 +679,10 @@
                 downloadLocation = new URL(downloadLocation.toString() + ".gz");
             }
 
-            File downloadLocationFile = CacheUtil.getCacheFile(downloadLocation, resource.downloadVersion);
-            CacheEntry downloadEntry = new CacheEntry(downloadLocation, resource.downloadVersion);
-            File finalFile = CacheUtil.getCacheFile(resource.location, resource.downloadVersion); // This is where extracted version will be, or downloaded file if not compressed.
+            File downloadLocationFile = CacheUtil.getCacheFile(downloadLocation, resource.getDownloadVersion());
+            CacheEntry downloadEntry = new CacheEntry(downloadLocation, resource.getDownloadVersion());
+            // This is where extracted version will be, or downloaded file if not compressed.
+            File finalFile = CacheUtil.getCacheFile(resource.getLocation(), resource.getDownloadVersion());
 
             if (!downloadEntry.isCurrent(con.getLastModified())) {
                 // Make sure we don't re-download the file. however it will wait as if it was downloading.
@@ -693,10 +694,10 @@
                 long lastModified = con.getLastModified();
 
                 InputStream in = new BufferedInputStream(con.getInputStream());
-                OutputStream out = CacheUtil.getOutputStream(downloadLocation, resource.downloadVersion);
+                OutputStream out = CacheUtil.getOutputStream(downloadLocation, resource.getDownloadVersion());
 
                 while (-1 != (rlen = in.read(buf))) {
-                    resource.transferred += rlen;
+                    resource.incrementTransferred(rlen);
                     out.write(buf, 0, rlen);
                 }
 
@@ -718,11 +719,11 @@
                  */
                 if (packgz) {
                     GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil
-                            .getCacheFile(downloadLocation, resource.downloadVersion)));
+                            .getCacheFile(downloadLocation, resource.getDownloadVersion())));
                     InputStream inputStream = new BufferedInputStream(gzInputStream);
 
                     JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(CacheUtil
-                            .getCacheFile(resource.location, resource.downloadVersion)));
+                            .getCacheFile(resource.getLocation(), resource.getDownloadVersion())));
 
                     Unpacker unpacker = Pack200.newUnpacker();
                     unpacker.unpack(inputStream, outputStream);
@@ -732,11 +733,11 @@
                     gzInputStream.close();
                 } else if (gzip) {
                     GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil
-                            .getCacheFile(downloadLocation, resource.downloadVersion)));
+                            .getCacheFile(downloadLocation, resource.getDownloadVersion())));
                     InputStream inputStream = new BufferedInputStream(gzInputStream);
 
                     BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(CacheUtil
-                            .getCacheFile(resource.location, resource.downloadVersion)));
+                            .getCacheFile(resource.getLocation(), resource.getDownloadVersion())));
 
                     while (-1 != (rlen = inputStream.read(buf))) {
                         outputStream.write(buf, 0, rlen);
@@ -747,7 +748,7 @@
                     gzInputStream.close();
                 }
             } else {
-                resource.transferred = downloadLocationFile.length();
+                resource.setTransferred(downloadLocationFile.length());
             }
 
             if (!downloadLocationFile.getPath().equals(finalFile.getPath())) {
@@ -787,17 +788,17 @@
     private void initializeResource(Resource resource) {
         resource.fireDownloadEvent(); // fire CONNECTING
 
-        CacheEntry entry = new CacheEntry(resource.location, resource.requestVersion);
+        CacheEntry entry = new CacheEntry(resource.getLocation(), resource.getRequestVersion());
         entry.lock();
 
         try {
-            File localFile = CacheUtil.getCacheFile(resource.location, resource.downloadVersion);
+            File localFile = CacheUtil.getCacheFile(resource.getLocation(), resource.getDownloadVersion());
 
             // connect
             URL finalLocation = findBestUrl(resource);
 
             if (finalLocation == null) {
-                OutputController.getLogger().log(OutputController.Level.ERROR_ALL, "Attempted to download " + resource.location + ", but failed to connect!");
+                OutputController.getLogger().log(OutputController.Level.ERROR_ALL, "Attempted to download " + resource.getLocation() + ", but failed to connect!");
                 throw new NullPointerException("finalLocation == null"); // Caught below
             }
 
@@ -806,14 +807,14 @@
             connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip");
 
             int size = connection.getContentLength();
-            boolean current = CacheUtil.isCurrent(resource.location, resource.requestVersion, connection.getLastModified()) && resource.getUpdatePolicy() != UpdatePolicy.FORCE;
+            boolean current = CacheUtil.isCurrent(resource.getLocation(), resource.getRequestVersion(), connection.getLastModified()) && resource.getUpdatePolicy() != UpdatePolicy.FORCE;
             if (!current) {
                 if (entry.isCached()) {
                     entry.markForDelete();
                     entry.store();
                     // Old entry will still exist. (but removed at cleanup)
-                    localFile = CacheUtil.makeNewCacheFile(resource.location, resource.downloadVersion);
-                    CacheEntry newEntry = new CacheEntry(resource.location, resource.requestVersion);
+                    localFile = CacheUtil.makeNewCacheFile(resource.getLocation(), resource.getDownloadVersion());
+                    CacheEntry newEntry = new CacheEntry(resource.getLocation(), resource.getRequestVersion());
                     newEntry.lock();
                     entry.unlock();
                     entry = newEntry;
@@ -821,9 +822,9 @@
             }
 
             synchronized (resource) {
-                resource.localFile = localFile;
+                resource.setLocalFile(localFile);
                 // resource.connection = connection;
-                resource.size = size;
+                resource.setSize(size);
                 resource.changeStatus(EnumSet.of(CONNECT, CONNECTING), EnumSet.of(CONNECTED));
 
                 // check if up-to-date; if so set as downloaded
@@ -1170,7 +1171,7 @@
     private Resource getResource(URL location) {
         synchronized (resources) {
             for (Resource resource : resources) {
-                if (CacheUtil.urlEquals(resource.location, location))
+                if (CacheUtil.urlEquals(resource.getLocation(), location))
                     return resource;
             }
         }
diff -r 22761e42d5b3 -r 8151448ecea2 netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java	Wed May 14 15:44:19 2014 -0400
+++ b/netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java	Tue May 20 16:52:21 2014 -0400
@@ -120,7 +120,7 @@
             return resource.getLocation();
         }
         String filename = location.substring(lastSlash + 1);
-        if (useVersion && resource.requestVersion != null) {
+        if (useVersion && resource.getRequestVersion() != null) {
             // With 'useVersion', j2-commons-cli.jar becomes, for example, j2-commons-cli__V1.0.jar
             String parts[] = filename.split("\\.", -1 /* Keep blank strings*/);
 
@@ -129,7 +129,7 @@
                 sb.append(parts[i]);
                 // Append __V<number> before last '.'
                 if (i == parts.length -2) {
-                    sb.append("__V" + resource.requestVersion);
+                    sb.append("__V" + resource.getRequestVersion());
                 }
                 sb.append('.');
             }
@@ -172,13 +172,13 @@
         if (!query.isEmpty()) {
             query = "?" + query;
         }
-        if (resource.requestVersion != null && resource.requestVersion.isVersionId()) {
+        if (resource.getRequestVersion() != null && resource.getRequestVersion().isVersionId()) {
             if (!query.isEmpty()) {
                 query += "&";
             } else {
                 query = "?" + query;
             }
-            query += "version-id=" + resource.requestVersion;
+            query += "version-id=" + resource.getRequestVersion();
         }
         try {
             URL url = new URL(protocol + userInfo + host + port + path + query);
diff -r 22761e42d5b3 -r 8151448ecea2 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java	Wed May 14 15:44:19 2014 -0400
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTest.java	Tue May 20 16:52:21 2014 -0400
@@ -37,14 +37,23 @@
 
 package net.sourceforge.jnlp.cache;
 
-import static net.sourceforge.jnlp.cache.Resource.Status.*;
+import static net.sourceforge.jnlp.cache.Resource.Status.CONNECT;
+import static net.sourceforge.jnlp.cache.Resource.Status.CONNECTED;
+import static net.sourceforge.jnlp.cache.Resource.Status.CONNECTING;
+import static net.sourceforge.jnlp.cache.Resource.Status.DOWNLOAD;
+import static net.sourceforge.jnlp.cache.Resource.Status.DOWNLOADED;
+import static net.sourceforge.jnlp.cache.Resource.Status.DOWNLOADING;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
+
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Collection;
 import java.util.EnumSet;
+import java.util.Set;
 
 import net.sourceforge.jnlp.Version;
 
@@ -52,6 +61,80 @@
 
 public class ResourceTest {
 
+    public static final long INCREMENT_TRANSFERRED_CONSTANT = 10;
+


More information about the distro-pkg-dev mailing list