PackageInstaller.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.pkg |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.pkg;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.loader.XNapClassLoader;
import org.xnap.net.NetHelper;
import org.xnap.util.FileHelper;
import org.xnap.util.Formatter;
import org.xnap.util.StringHelper;
public class PackageInstaller {
//--- Constant(s) ---
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(PackageInstaller.class);
private PackageInfo[] packages;
private DownloadRunner dlRunner;
private PackageInstallerListener listener;
private boolean successful = false;
//--- Constructor(s) ---
public PackageInstaller(PackageInfo[] packages,
PackageInstallerListener listener)
{
this.packages = packages;
this.listener = listener;
int totalSize = 0;
for (int i = 0; i < packages.length; i++) {
totalSize += packages[i].getSize();
}
listener.setTotalMinimum(0);
listener.setTotalValue(0);
listener.setTotalMaximum(totalSize);
listener.setText(XNap.tr("Need to download {0} in {1} packages.",
Formatter.formatSize(totalSize),
new Integer(packages.length)));
}
//--- Method(s) ---
public PackageInfo[] getPackageInfos()
{
return packages;
}
public boolean isSuccessful()
{
return successful;
}
public void start()
{
dlRunner = new DownloadRunner();
Thread t = new Thread(dlRunner, "PluginDownload");
t.start();
}
// --- Inner Class(es) ---
private class DownloadRunner implements Runnable
{
private long totalTransferred = 0;
public File createTempFile(String filename) throws IOException
{
File pathes[] = XNapClassLoader.getSearchPath();
for (int i = 0; i < pathes.length; i++) {
File f = new File(pathes[i], filename);
logger.debug("trying " + f.getAbsolutePath());
if (f.exists() && f.canWrite()) {
return f;
}
else if ((f.getParentFile().exists()
|| f.getParentFile().mkdirs())
&& f.createNewFile()) {
return f;
}
}
return null;
}
public File download(InputStream in, String filename)
throws IOException
{
File tempFile = createTempFile(filename + ".part");
if (tempFile == null) {
throw new IOException(XNap.tr("Could not create file."));
}
File target = new File(tempFile.getParentFile(), filename);
if (target.exists() && !target.delete()) {
throw new IOException
(XNap.tr("A file with this name already exists."));
}
OutputStream out = null;
try {
out = new FileOutputStream(tempFile.getAbsolutePath(), false);
listener.setText(XNap.tr("Downloading {0}", filename));
long transferred = 0;
int c = 0;
byte[] b = new byte[500];
while (c != -1 && !listener.isCancelled()) {
c = in.read(b);
if (c != -1) {
out.write(b, 0, c);
transferred += c;
totalTransferred += c;
listener.setValue((int)transferred);
listener.setTotalValue((int)totalTransferred);
}
}
out.close();
}
catch (IOException e) {
if (out != null) {
try {
out.close();
tempFile.delete();
}
catch (IOException e2) {
}
}
throw e;
}
if (!tempFile.renameTo(target)) {
throw new IOException(XNap.tr("Could not rename file."));
}
return target;
}
public File downloadPackage(PackageInfo info)
{
String filename = info.getDownloadFilename();
if (filename != null) {
File file = new File(filename);
if (file.exists() && file.length() == info.getSize()) {
return file;
}
}
String[] urls = info.getDownloadURLs();
if (/*(info.isPlugin() || info.isBase())
&& */(urls.length > 0
&& (urls[0].indexOf("sf.net") > 0
|| urls[0].indexOf("sourceforge.net") > 0))) {
// request sf download url to make use of download counters
try {
URL url = new URL("http://prdownloads.sourceforge.net/xnap/"
+ info.getFilename() + "?use_mirror=unc");
InputStream in = url.openStream();
try {
in.read();
}
finally {
in.close();
}
}
catch (IOException e) {
}
}
for (int i = 0; i < urls.length; i++) {
String location = urls[i] + info.getFilename();
listener.setText(XNap.tr("Getting {0}", location));
listener.setMinimum(0);
listener.setValue(0);
listener.setMaximum((int)info.getSize());
long value = totalTransferred;
try {
URL url = new URL(location);
// make sure filename does not contain a '/'
filename = StringHelper.lastToken(info.getFilename(), "/");
if (filename.length() == 0) {
filename = info.getFilename();
}
return download(url.openStream(), filename);
}
catch (IOException e) {
// reset total value
listener.setTotalValue((int)value);
logger.info("Download failed", e);
}
}
// download failed from all locations
return null;
}
public String unzip(File file) throws IOException
{
listener.setText(XNap.tr("Unzipping {0}", file.getName()));
String controlPath = null;
File base = file.getParentFile();
ZipFile zip = new ZipFile(file);
int i = 0;
for (Enumeration it = zip.entries();
it.hasMoreElements(); i++) {
ZipEntry entry = (ZipEntry)it.nextElement();
File target = new File(base.getAbsolutePath()
+ File.separatorChar + entry.getName());
if (entry.isDirectory()) {
logger.debug("creating directory: "
+ target.getAbsolutePath());
target.mkdirs();
}
else {
logger.debug("unzipping file: "
+ target.getAbsolutePath());
InputStream in = zip.getInputStream(entry);
try {
FileHelper.copy(in, new FileOutputStream(target));
}
finally {
try {
in.close();
}
catch (IOException e) {
}
}
if (target.getName().equals("control")) {
controlPath = target.getParentFile().getAbsolutePath();
}
}
}
file.delete();
return controlPath;
}
public void run()
{
try {
for (int i = 0; i < packages.length
&& !listener.isCancelled(); i++) {
File target = downloadPackage(packages[i]);
if (target != null) {
try {
String path = unzip(target);
if (path == null) {
throw new IOException
(XNap.tr("Invalid package, control file is missing."));
}
packages[i].getProperties().setProperty
("Control-Path", path);
}
catch (IOException e) {
logger.debug("Error unziping plugin", e);
listener.setText(NetHelper.getErrorMessage(e));
return;
}
// FIX: check md5 sum
// remember successful download, acctually the
// package is already unzipped
packages[i].setDownloadFilename
(target.getAbsolutePath());
}
else {
listener.setText
(XNap.tr("Unable to download package {0}.",
packages[i].getName()));
return;
}
}
successful = !listener.isCancelled();
if (successful) {
for (int i = 0; i < packages.length; i++) {
packages[i].setInstalled(true);
File file
= new File(packages[i].getDownloadFilename());
file.delete();
}
listener.setText(XNap.tr("Finished successfully."));
}
else {
listener.setText(XNap.tr("Cancelled by user."));
}
listener.setPercent(1.0);
listener.setTotalPercent(1.0);
}
finally {
listener.done();
}
}
}
}
The table below shows all metrics for PackageInstaller.java.




