PageRenderTime 20ms CodeModel.GetById 6ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/edu/uncc/parsets/data/DownloadManager.java

https://code.google.com/p/parsets/
Java | 112 lines | 67 code | 14 blank | 31 comment | 11 complexity | 248f2f22476a0e4759ee1d11470d0dbd MD5 | raw file
  1package edu.uncc.parsets.data;
  2
  3import java.io.File;
  4import java.io.FileInputStream;
  5import java.io.FileOutputStream;
  6import java.io.InputStream;
  7import java.io.InputStreamReader;
  8import java.io.Reader;
  9import java.net.URL;
 10import java.util.concurrent.LinkedBlockingDeque;
 11import java.util.zip.GZIPInputStream;
 12
 13import edu.uncc.parsets.gui.OnlineDataTab;
 14import edu.uncc.parsets.util.PSLogging;
 15
 16/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 17 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 18 *                     and others (see Authors.txt for full list)
 19 * All rights reserved.
 20 * 
 21 * Redistribution and use in source and binary forms, with or without
 22 * modification, are permitted provided that the following conditions are met:
 23 * 
 24 *    * Redistributions of source code must retain the above copyright
 25 *      notice, this list of conditions and the following disclaimer.
 26 *    * Redistributions in binary form must reproduce the above copyright
 27 *      notice, this list of conditions and the following disclaimer in the
 28 *      documentation and/or other materials provided with the distribution.
 29 *    * Neither the name of UNC Charlotte nor the names of its contributors
 30 *      may be used to endorse or promote products derived from this software
 31 *      without specific prior written permission.
 32 *      
 33 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 34 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 35 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 36 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 37 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 43\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 44
 45public class DownloadManager extends Thread {
 46
 47	LinkedBlockingDeque<OnlineDataSet> queue;
 48	private OnlineDataTab tab;
 49	private OnlineDataSet currentDS;
 50	
 51	public DownloadManager(OnlineDataTab onlineDataTab) {
 52		tab = onlineDataTab;
 53		queue = new LinkedBlockingDeque<OnlineDataSet>();
 54		setDaemon(true);
 55	}
 56	
 57	public void queueDownload(OnlineDataSet ds) {
 58		try {
 59			queue.put(ds);
 60		} catch (InterruptedException e) {
 61			// this queue is not limited, so there is no wait;
 62			// and where there is no wait, it can't be interrupted
 63		}
 64	}
 65	
 66	@Override
 67	public void run() {
 68		byte buffer[] = new byte[4096];
 69		while (true) {
 70			try {
 71				currentDS = queue.take();
 72				if (tab != null)
 73					tab.activateProgressBar(true);
 74				PSLogging.logger.info("Downloading dataset from "+currentDS.getURL());
 75				InputStream stream = (new URL(currentDS.getURL())).openStream();
 76				File tmpFile = File.createTempFile(currentDS.getHandle(), ".json.gz");
 77				tmpFile.deleteOnExit();
 78				FileOutputStream out = new FileOutputStream(tmpFile);
 79				boolean keepReading = true;
 80				while (keepReading) {
 81					int bytes = stream.read(buffer);
 82					if (bytes > 0)
 83						out.write(buffer, 0, bytes);
 84					else
 85						keepReading = false;
 86				}
 87				stream.close();
 88				out.close();
 89				
 90				Reader fileReader = new InputStreamReader(new GZIPInputStream(new FileInputStream(tmpFile)));
 91				if (!tmpFile.delete())
 92					PSLogging.logger.warn("Could not delete temp file "+tmpFile.getAbsolutePath());
 93
 94				JSONImport.importDataSet(LocalDB.getDefaultDB(), fileReader);
 95				LocalDB.getDefaultDB().rescanDB();
 96
 97				if (tab != null && queue.isEmpty())
 98					tab.activateProgressBar(false);
 99
100			} catch (InterruptedException e) {
101				// ignore
102			} catch (Exception e) {
103				PSLogging.logger.error("Error downloading dataset", e);
104			}
105		}
106	}
107	
108	public boolean isDSqueued(DataSet ds) {
109		return queue.contains(ds) || (currentDS == ds);
110	}
111
112}