XNapPackageManager.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.IOException;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.component.ProgressMonitor;
import org.xnap.loader.XNapClassLoader;
import org.xnap.util.FileHelper;
import org.xnap.util.Preferences;
/**
*
*/
public class XNapPackageManager extends PackageManager {
//--- Constant(s) ---
public static final String AVAILABLE_FILENAME
= FileHelper.getHomeDir() + "available";
public static final String SOURCES_FILENAME
= FileHelper.getHomeDir() + "sources.list";
public static final String STATUS_FILENAME
= FileHelper.getHomeDir() + "status";
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(XNapPackageManager.class);
private static XNapPackageManager singleton = new XNapPackageManager();
private boolean initialized;
//--- Constructor(s) ---
private XNapPackageManager()
{
}
//--- Method(s) ---
public static XNapPackageManager getInstance()
{
return singleton;
}
public PackageInfo createFakePackage(File file)
{
if (!file.getName().endsWith(".jar")) {
throw new IllegalArgumentException("Not a jar file.");
}
String filename
= file.getName().substring(0, file.getName().length() - 4);
PackageInfo info = new PackageInfo();
int i = filename.lastIndexOf("-");
if (i < 0 || i == filename.length() - 1) {
info.setPackage(filename);
info.setVersion("1.0");
}
else {
info.setPackage(filename.substring(0, i));
info.setVersion(filename.substring(i + 1, filename.length()));
}
info.setClassPath(new String[] { file.getName() });
info.setControlPath(file.getParentFile().getAbsolutePath());
info.setSection("local");
info.setDescription(XNap.tr("Locally installed jar file."));
info.setStatus(PackageInfo.STATUS_INSTALLED);
return info;
}
/**
* Adds installed packages and saves the list of packages to
* {@link #AVAILABLE_FILENAME}.
*/
public void discover()
{
File pathes[] = XNapClassLoader.getSearchPath();
for (int i = 0; i < pathes.length; i++) {
discover(pathes[i]);
}
String pathnames[] = Preferences.getInstance().getJarIncludePath();
for (int i = 0; i < pathnames.length; i++) {
discover(new File(pathnames[i]));
}
}
/**
* Adds all packages found in path and recursively in all sub directories.
*/
public void discover(File path)
{
File[] files = path.listFiles();
if (files != null) {
boolean isXNapPackage = false;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
discover(files[i]);
}
else if (files[i].getName().equals("control")) {
try {
Properties table = new Properties();
table.put("Control-Path", path.getAbsolutePath());
table.put("Status", PackageInfo.STATUS_INSTALLED);
read(files[i], table);
isXNapPackage = true;
}
catch (IOException e) {
// we should handle this
}
}
}
if (isXNapPackage) {
// do not add fake jar packages to avoid duplication
return;
}
for (int i = 0; i < files.length; i++) {
if (files[i].getName().endsWith(".jar")) {
PackageInfo info = createFakePackage(files[i]);
add(info);
}
}
}
}
public String getDefaultSources(boolean includeUnstable)
{
StringBuffer sb = new StringBuffer();
sb.append("http://xnap.sf.net/stable/packages.gz"
+ " http://xnap.sf.net/stable/mirrors.list\n");
if (!includeUnstable) {
sb.append("#");
}
sb.append("http://xnap.sf.net/unstable/packages.gz"
+ " http://xnap.sf.net/stable/mirrors.list\n");
return sb.toString();
}
public String getDefaultSources()
{
return getDefaultSources(false);
}
public void initialize()
{
if (!initialized) {
initialized = true;
logger.info("Initializing package manager");
if (XNap.isRunFromCvs()) {
try {
Properties table = new Properties();
table.put("Class-Path", "");
table.put("Depends", "");
table.put("Status", PackageInfo.STATUS_INSTALLED);
read(new File("metainfo/control"), table);
}
catch (IOException e) {
logger.debug("error reading control file", e);
}
return;
}
if (XNap.clearPackageInfos) {
// do not read package files, search hard disk instead
discover();
}
else {
File f = new File(STATUS_FILENAME);
if (f.exists()) {
try {
read(new File(STATUS_FILENAME));
}
catch (IOException e) {
logger.debug("error reading status file", e);
}
}
else {
discover();
}
try {
read(new File(AVAILABLE_FILENAME));
}
catch (IOException e) {
logger.debug("error reading available file", e);
}
}
write();
}
}
/**
* Updates the list of packages from the urls specified in {@link
* #SOURCES_FILENAME}.
*
* <p>This method runs asynchronus. Make sure the package manager
* is not modified until monitor.done() is invoked.
*/
public void update(ProgressMonitor monitor)
{
PackageUpdater updater = new PackageUpdater
(this, new File(SOURCES_FILENAME), monitor);
Thread t = new Thread(updater, "PackageUpdater");
if (monitor != null) {
monitor.setThread(t);
}
t.start();
}
/**
* Saves the list of packages.
*/
public void write()
{
try {
write(new File(STATUS_FILENAME), new File(AVAILABLE_FILENAME));
}
catch (IOException e) {
logger.warn("Error writing list of packages", e);
}
}
private void write(File statusFile, File availableFile) throws IOException
{
// save list of installed packages
Properties table = new Properties();
table.put("Status", PackageInfo.STATUS_INSTALLED);
write(statusFile, table);
// save other packages
table = new Properties();
table.put("Available", "True");
write(availableFile, table);
}
}
The table below shows all metrics for XNapPackageManager.java.




