PackageManager.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.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;
import java.util.*;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.zip.GZIPInputStream;
/**
*
*/
public class PackageManager
{
//--- Constant(s) ---
//--- Data field(s) ---
/**
* A sorted list of PackageInfo objects.
*/
private TreeSet packages = new TreeSet();
private Hashtable packageListByProvides = new Hashtable();
//--- Constructor(s) ---
public PackageManager()
{
}
//--- Method(s) ---
/**
* Adds a package. If info is not valid or a package with the same
* name and version already exists the package is not added.
*/
public void add(PackageInfo info)
{
if (info.isValid()) {
if (packages.contains(info)) {
SortedSet tailSet = packages.tailSet(info);
PackageInfo old = (PackageInfo)tailSet.first();
if (old.isInstalled()) {
old.setAvailable(true);
// do not update already installed package info
return;
}
else {
remove(old);
info.setNew(old.isNew());
}
}
packages.add(info);
info.setAvailable(true);
if (info.getProvides() != null) {
StringTokenizer t
= new StringTokenizer(info.getProvides(), ",");
while (t.hasMoreTokens()) {
String name = t.nextToken().trim();
LinkedList list
= (LinkedList)packageListByProvides.get(name);
if (list == null) {
list = new LinkedList();
packageListByProvides.put(name, list);
}
list.add(info);
}
}
}
}
public PackageInfo[] getConflicts(PackageInfo info)
throws ParseException
{
LinkedList packageTokens = new LinkedList();
DefaultDependencyParser parser = new DefaultDependencyParser();
AbstractToken token = parser.parseConflicts(info);
if (token instanceof CommaToken) {
AbstractToken[] depends = ((CommaToken)token).depends;
for (int i = 0; i < depends.length; i++) {
if (depends[i] instanceof PackageToken) {
packageTokens.add(depends[i]);
}
}
}
else if (token instanceof PackageToken) {
packageTokens.add(token);
}
LinkedList infos = new LinkedList();
for (Iterator it = packageTokens.iterator(); it.hasNext();) {
addConflicts(infos, (PackageToken)it.next());
}
return (PackageInfo[])infos.toArray(new PackageInfo[0]);
}
private void addConflicts(List list, PackageToken token)
{
// packages
for (Iterator it = tailSet(token.name).iterator(); it.hasNext();) {
PackageInfo info = (PackageInfo)it.next();
if (info.getPackage().equals(token.name)) {
if (token.compareMode == null
|| token.equalsVersion(info.compareToVersion
(token.version))) {
// get package node
list.add(info);
}
}
else {
break;
}
}
}
/**
* Returns a list of urls.
*
* @return the empty string
*/
public String getDefaultSources()
{
return "";
}
/**
*
* @exception ParseException thrown, if package file is invalid
* @exception UnsatisfiedDependenciesException thrown, if
* dependencies are not satisfied
*/
public PackageInfo[] getDependencies(PackageInfo info)
throws ParseException, UnsatisfiedDependenciesException
{
DependencyGraph graph = new DependencyGraph(this);
graph.add(info);
graph.buildDependencies(new DefaultDependencyParser());
DefaultResolver r = new DefaultResolver(graph, true);
r.resolve();
return r.getRequired();
}
/**
* Marks all packages that are not installed as unavailable. Marks
* all packages as not new.
*/
public void markAllUnavailable()
{
for (Iterator i = packages(); i.hasNext();) {
PackageInfo info = (PackageInfo)i.next();
info.setAvailable(info.isInstalled());
info.setNew(false);
}
}
/**
* Reads package information from a control file.
*
* @param file the control file
*/
public void read(File file, Properties table) throws IOException
{
FileInputStream in = new FileInputStream(file);
try {
read(in, table);
}
finally {
in.close();
}
}
public void read(File file) throws IOException
{
read(file, null);
}
/**
* Reads package information from a url.
*
* @param location the package file url
* @param downloadUrl the base location of the package files
*/
public void read(String location, Properties table) throws IOException
{
URL url = new URL(location);
InputStream in = url.openStream();
try {
if (location.endsWith(".gz")) {
in = new GZIPInputStream(url.openStream());
}
read(in, table);
}
finally {
in.close();
}
}
/**
* Reads package information from a stream.
*
* @param inStream the stream
* @param flags additional flags that are added to the {@link
* PackageInfo} record; if null, flags are ignored
*/
public void read(InputStream inStream, Properties flags)
throws IOException
{
BufferedReader in
= new BufferedReader(new InputStreamReader(inStream));
Properties p;
while ((p = PackageInfoReader.readNext(in)) != null) {
PackageInfo info = new PackageInfo(p);
if (flags != null) {
info.putAll(flags);
}
add(info);
}
}
public void remove(PackageInfo info)
{
packages.remove(info);
if (info.getProvides() != null) {
StringTokenizer t
= new StringTokenizer(info.getProvides(), ",");
while (t.hasMoreTokens()) {
String name = t.nextToken().trim();
LinkedList list
= (LinkedList)packageListByProvides.get(name);
if (list != null) {
list.remove(info);
}
}
}
}
/**
* Removes all packages that are marked as unavailable.
*/
public void removeUnavailable()
{
LinkedList toRemove = new LinkedList();
for (Iterator i = packages(); i.hasNext();) {
PackageInfo info = (PackageInfo)i.next();
if (!(info.isInstalled() || info.isAvailable())) {
toRemove.add(info);
}
}
for (Iterator i = toRemove.iterator(); i.hasNext();) {
remove((PackageInfo)i.next());
}
}
/**
* Returns an iterator over all {@link PackageInfo} objects.
*/
public Iterator packages()
{
return packages.iterator();
}
/**
* Returns the number of packages.
*/
public int getPackageCount()
{
return packages.size();
}
/**
* Returns a package by name.
*/
public PackageInfo getPackage(String packageName)
{
SortedSet set = tailSet(packageName);
if (!set.isEmpty()) {
PackageInfo info = (PackageInfo)set.first();
if (info.getPackage().equals(packageName)) {
return info;
}
}
return null;
}
public SortedSet tailSet(String packageName)
{
return packages.tailSet(new PackageInfo(packageName));
}
/**
* Returns an array of packages that provide packageName.
*/
public PackageInfo[] getProviders(String packageName)
{
LinkedList list = (LinkedList)packageListByProvides.get(packageName);
return (list != null)
? (PackageInfo[])list.toArray(new PackageInfo[0])
: null;
}
public void write(File file, Properties flags) throws IOException
{
BufferedWriter out = new BufferedWriter(new FileWriter(file));
try {
for (Iterator i = packages.iterator(); i.hasNext();) {
PackageInfo info = (PackageInfo)i.next();
if (flags == null || info.containsProperties(flags)) {
PackageInfoWriter.write(out, info.getProperties());
}
}
}
finally {
try {
out.close();
}
catch (IOException e) {
}
}
}
}
The table below shows all metrics for PackageManager.java.




