001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.base;
019:
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.Set;
023: import java.util.TreeSet;
024:
025: public final class PackageManager {
026:
027: private static PackageManager packageManager;
028:
029: private Set<Package> packages;
030:
031: private PackageManager() {
032: packages = new TreeSet<Package>();
033: }
034:
035: public static PackageManager getInstance() {
036: if (packageManager == null) {
037: packageManager = new PackageManager();
038: }
039: return packageManager;
040: }
041:
042: public void addPackage(Package pkg) {
043: synchronized (packages) {
044: packages.add(pkg);
045: }
046: }
047:
048: public Set getPackages() {
049: return Collections.unmodifiableSet(packages);
050: }
051:
052: public Package getPackage(ContelligentComponent component)
053: throws PackageNotFoundException {
054: synchronized (packages) {
055: for (Iterator i = packages.iterator(); i.hasNext();) {
056: Package p = (Package) i.next();
057: String packagePath = p.getPath();
058: if ((component.getPath() + "/").startsWith(packagePath
059: + "/")) {
060: return p;
061: }
062: }
063: }
064: throw new PackageNotFoundException("Component '"
065: + component.getPath() + "' not part of a valid package");
066: }
067:
068: public Package getPackage(String path)
069: throws PackageNotFoundException {
070: synchronized (packages) {
071: for (Iterator i = packages.iterator(); i.hasNext();) {
072: Package p = (Package) i.next();
073: String packagePath = p.getPath();
074: if ((path + "/").startsWith(packagePath + "/")) {
075: return p;
076: }
077: }
078: }
079: throw new PackageNotFoundException("Component '" + path
080: + "' not part of a valid package");
081: }
082:
083: public Package getPackage(Type type)
084: throws PackageNotFoundException {
085: synchronized (packages) {
086: for (Iterator i = packages.iterator(); i.hasNext();) {
087: Package p = (Package) i.next();
088: if (type.getName().startsWith(p.getIdentifier() + ".")) {
089: return p;
090: }
091: }
092: }
093: throw new PackageNotFoundException("Type '" + type
094: + "' not part of a valid package");
095: }
096:
097: public final static class Package implements Comparable {
098: private String identifier, name, version, provider;
099:
100: private boolean readonly;
101:
102: private String basePath;
103:
104: public Package(String identifier, String name, String version,
105: String provider, boolean readonly) {
106: this .identifier = identifier;
107: this .name = name;
108: this .version = version;
109: this .provider = provider;
110: this .readonly = readonly;
111: this .basePath = "/" + identifier.replace('.', '/');
112: }
113:
114: public String getIdentifier() {
115: return identifier;
116: }
117:
118: public String getVersion() {
119: return version;
120: }
121:
122: public String getName() {
123: return name;
124: }
125:
126: public String getProvider() {
127: return provider;
128: }
129:
130: public boolean isReadOnly() {
131: return readonly;
132: }
133:
134: public String toString() {
135: return identifier + " (V" + version + ")";
136: }
137:
138: public String getPath() {
139: return basePath;
140: }
141:
142: public int compareTo(Object o) {
143: if (o.equals(this )) {
144: return 0;
145: }
146: if (o instanceof Package) {
147: return this .getIdentifier().compareTo(
148: ((Package) o).getIdentifier());
149: } else {
150: if (hashCode() > o.hashCode()) {
151: return -1;
152: } else if (hashCode() < o.hashCode()) {
153: return 1;
154: } else {
155: return 0;
156: }
157: }
158: }
159: }
160: }
|