00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package org.knopflerfish.framework;
00036
00037 import java.util.*;
00038
00039 import org.osgi.framework.Constants;
00040 import org.osgi.framework.Version;
00041
00042
00048 class ExportPkg {
00049 final String name;
00050 final BundlePackages bpkgs;
00051 final ArrayList uses;
00052 final ArrayList mandatory;
00053 final ArrayList include;
00054 final ArrayList exclude;
00055 final Version version;
00056 final Map attributes;
00057 boolean zombie = false;
00058
00059
00060 Pkg pkg = null;
00061
00065 ExportPkg(String name, Map tokens, BundlePackages b) {
00066 this.bpkgs = b;
00067 this.name = name;
00068 if (name.startsWith("java.")) {
00069 throw new IllegalArgumentException("You can not export a java.* package");
00070 }
00071 this.uses = Util.parseEnumeration(Constants.USES_DIRECTIVE,
00072 (String)tokens.remove(Constants.USES_DIRECTIVE));
00073 this.mandatory = Util.parseEnumeration(Constants.MANDATORY_DIRECTIVE,
00074 (String)tokens.remove(Constants.MANDATORY_DIRECTIVE));
00075 this.include = Util.parseEnumeration(Constants.INCLUDE_DIRECTIVE,
00076 (String)tokens.remove(Constants.INCLUDE_DIRECTIVE));
00077 this.exclude = Util.parseEnumeration(Constants.EXCLUDE_DIRECTIVE,
00078 (String)tokens.remove(Constants.EXCLUDE_DIRECTIVE));
00079 String versionStr = (String)tokens.remove(Constants.VERSION_ATTRIBUTE);
00080 String specVersionStr = (String)tokens.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
00081 if (specVersionStr != null) {
00082 this.version = new Version(specVersionStr);
00083 if (versionStr != null && !this.version.equals(new Version(versionStr))) {
00084 throw new IllegalArgumentException("Both " + Constants.VERSION_ATTRIBUTE +
00085 "and " + Constants.PACKAGE_SPECIFICATION_VERSION +
00086 "are specified, and differs");
00087 }
00088 } else if (versionStr != null) {
00089 this.version = new Version(versionStr);
00090 } else {
00091 this.version = Version.emptyVersion;
00092 }
00093 if (tokens.containsKey(Constants.BUNDLE_VERSION_ATTRIBUTE)) {
00094 throw new IllegalArgumentException("Export definition illegally contains attribute, " +
00095 Constants.BUNDLE_VERSION_ATTRIBUTE);
00096 }
00097 if (tokens.containsKey(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE)) {
00098 throw new IllegalArgumentException("Export definition illegally contains attribute, " +
00099 Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE);
00100 }
00101 this.attributes = tokens;
00102 }
00103
00104
00108 ExportPkg(ExportPkg ep, String name) {
00109 this.name = name;
00110 this.bpkgs = ep.bpkgs;
00111 this.uses = ep.uses;
00112 this.mandatory = ep.mandatory;
00113 this.include = ep.include;
00114 this.exclude = ep.exclude;
00115 this.version = ep.version;
00116 this.attributes = ep.attributes;
00117 }
00118
00119
00123 ExportPkg(ExportPkg ep, BundlePackages b) {
00124 this.name = ep.name;
00125 this.bpkgs = b;
00126 this.uses = ep.uses;
00127 this.mandatory = ep.mandatory;
00128 this.include = ep.include;
00129 this.exclude = ep.exclude;
00130 this.version = ep.version;
00131 this.attributes = ep.attributes;
00132 }
00133
00134
00138 synchronized void attachPkg(Pkg p) {
00139 pkg = p;
00140 }
00141
00142
00146 synchronized void detachPkg() {
00147 pkg = null;
00148 zombie = false;
00149 }
00150
00151
00156 boolean checkFilter(String fullClassName) {
00157 String clazz = null;
00158 boolean ok = true;
00159 if (fullClassName != null) {
00160 if (include != null) {
00161
00162 clazz = fullClassName.substring(name.length() + 1);
00163 for (Iterator i = include.iterator(); i.hasNext(); ) {
00164 if (Util.filterMatch((String)i.next(), clazz)) {
00165 break;
00166 }
00167 if (!i.hasNext()) {
00168 ok = false;
00169 }
00170 }
00171 }
00172 if (ok && exclude != null) {
00173 if (clazz == null) {
00174
00175 clazz = fullClassName.substring(name.length() + 1);
00176 }
00177 for (Iterator i = exclude.iterator(); i.hasNext(); ) {
00178 if (Util.filterMatch((String)i.next(), clazz)) {
00179 ok = false;
00180 break;
00181 }
00182 }
00183 }
00184 }
00185 return ok;
00186 }
00187
00188
00194 synchronized boolean isProvider() {
00195 if (pkg != null) {
00196 synchronized (pkg) {
00197 return pkg.providers.contains(this);
00198 }
00199 }
00200 return false;
00201 }
00202
00203
00204
00211 synchronized Collection getPackageImporters() {
00212 if (pkg != null) {
00213 Set res = new HashSet();
00214 synchronized (pkg) {
00215 for (Iterator i = pkg.importers.iterator(); i.hasNext(); ) {
00216 ImportPkg ip = (ImportPkg)i.next();
00217 if (ip.provider == this) {
00218 res.add(ip.bpkgs.bundle);
00219 }
00220 }
00221 }
00222 return res;
00223 }
00224 return null;
00225 }
00226
00227
00228
00229
00230
00236 public String pkgString() {
00237 if (version != Version.emptyVersion) {
00238 return name + ";" + Constants.PACKAGE_SPECIFICATION_VERSION + "=" + version;
00239 } else {
00240 return name;
00241 }
00242 }
00243
00244
00250 public String toString() {
00251 StringBuffer sb = new StringBuffer(pkgString());
00252 sb.append('(');
00253 if (zombie) {
00254 sb.append("zombie, ");
00255 }
00256 sb.append(bpkgs.toString());
00257 sb.append(')');
00258 return sb.toString();
00259 }
00260
00261 }