ImportPkg.java

00001 /*
00002  * Copyright (c) 2005-2006, KNOPFLERFISH project
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following
00007  * conditions are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  * - Redistributions in binary form must reproduce the above
00013  *   copyright notice, this list of conditions and the following
00014  *   disclaimer in the documentation and/or other materials
00015  *   provided with the distribution.
00016  *
00017  * - Neither the name of the KNOPFLERFISH project nor the names of its
00018  *   contributors may be used to endorse or promote products derived
00019  *   from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00027  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00030  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00032  * OF THE POSSIBILITY OF SUCH DAMAGE.
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 ImportPkg {
00049   final String name;
00050   final BundlePackages bpkgs;
00051   final String resolution;
00052   final String bundleSymbolicName;
00053   final VersionRange packageRange;
00054   final VersionRange bundleRange;
00055   final Map attributes;
00056 
00057   // Link to pkg entry
00058   Pkg pkg = null;
00059 
00060   // Link to exporter
00061   ExportPkg provider = null;
00062 
00066   ImportPkg(String name, Map tokens, BundlePackages b) {
00067     this.bpkgs = b;
00068     this.name = name;
00069     if (name.startsWith("java.")) {
00070       throw new IllegalArgumentException("You can not import a java.* package");
00071     }
00072     String res = (String)tokens.remove(Constants.RESOLUTION_DIRECTIVE);
00073     if (res != null) {
00074       if (Constants.RESOLUTION_OPTIONAL.equals(res)) {
00075         this.resolution = Constants.RESOLUTION_OPTIONAL;
00076       }  else if (Constants.RESOLUTION_MANDATORY.equals(res)) {
00077         this.resolution = Constants.RESOLUTION_MANDATORY;
00078       } else {
00079         throw new IllegalArgumentException("Directive " + Constants.RESOLUTION_DIRECTIVE +
00080                                            ", unexpected value: " + res);
00081       }
00082     } else {
00083       this.resolution = Constants.RESOLUTION_MANDATORY;
00084     }
00085     this.bundleSymbolicName = (String)tokens.remove(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE);
00086     String versionStr = (String)tokens.remove(Constants.VERSION_ATTRIBUTE);
00087     String specVersionStr = (String)tokens.remove(Constants.PACKAGE_SPECIFICATION_VERSION);
00088     if (specVersionStr != null) {
00089       this.packageRange = new VersionRange(specVersionStr);
00090       if (versionStr != null && !this.packageRange.equals(new VersionRange(versionStr))) {
00091         throw new IllegalArgumentException("Both " + Constants.VERSION_ATTRIBUTE + 
00092                                            " and " + Constants.PACKAGE_SPECIFICATION_VERSION +
00093                                            "are specified, but differs");
00094       }
00095     } else if (versionStr != null) {
00096       this.packageRange = new VersionRange(versionStr);
00097     } else {
00098       this.packageRange = VersionRange.defaultVersionRange;
00099     }
00100     String rangeStr = (String)tokens.remove(Constants.BUNDLE_VERSION_ATTRIBUTE);
00101     if (rangeStr != null) {
00102       this.bundleRange = new VersionRange(rangeStr);
00103     } else {
00104       this.bundleRange = VersionRange.defaultVersionRange;
00105     }
00106     this.attributes = tokens;
00107   }
00108 
00109 
00113   ImportPkg(ImportPkg ip, String name) {
00114     this.name = name;
00115     this.bpkgs = ip.bpkgs;
00116     this.resolution = ip.resolution;
00117     this.bundleSymbolicName = ip.bundleSymbolicName;
00118     this.packageRange = ip.packageRange;
00119     this.bundleRange = ip.bundleRange;
00120     this.attributes = ip.attributes;
00121   }
00122 
00123 
00127   ImportPkg(ImportPkg ip, BundlePackages bpkgs) {
00128     this.name = ip.name;
00129     this.bpkgs = bpkgs;
00130     this.resolution = ip.resolution;
00131     this.bundleSymbolicName = ip.bundleSymbolicName;
00132     this.packageRange = ip.packageRange;
00133     this.bundleRange = ip.bundleRange;
00134     this.attributes = ip.attributes;
00135   }
00136 
00140   ImportPkg(ExportPkg p) {
00141     this.name = p.name;
00142     this.bpkgs = p.bpkgs;
00143     this.resolution = Constants.RESOLUTION_MANDATORY;;
00144     this.bundleSymbolicName = null;
00145     if (p.version == Version.emptyVersion) {
00146       this.packageRange = VersionRange.defaultVersionRange;
00147     } else {
00148       this.packageRange = new VersionRange(p.version.toString());
00149     }
00150     this.bundleRange = VersionRange.defaultVersionRange;
00151     this.attributes = p.attributes;
00152   }
00153 
00154 
00158   synchronized void attachPkg(Pkg p) {
00159     pkg = p;
00160   }
00161 
00162 
00166   synchronized void detachPkg() {
00167     pkg = null;
00168     provider = null;
00169   }
00170 
00171 
00179   public boolean okPackageVersion(Version ver) {
00180     return packageRange.withinRange(ver);
00181   }
00182 
00183 
00190   boolean checkMandatory(List mandatory) {
00191     if (mandatory != null) {
00192       for (Iterator i = mandatory.iterator(); i.hasNext(); ) {
00193         String a = (String)i.next();
00194         if (Constants.VERSION_ATTRIBUTE.equals(a)) {
00195           if (!packageRange.isSpecified()) {
00196             return false;
00197           }
00198         } else if (Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE.equals(a)) {
00199           if (bundleSymbolicName == null) {
00200             return false;
00201           }
00202         } else if (Constants.BUNDLE_VERSION_ATTRIBUTE.equals(a)) {
00203           if (!bundleRange.isSpecified()) {
00204             return false;
00205           }
00206         } else if (!attributes.containsKey(a)) {
00207           return false;
00208         }
00209       }
00210     }
00211     return true;
00212   }
00213 
00214 
00221   boolean overlap(ImportPkg ip) {
00222     if (ip.bundleSymbolicName == null ? bundleSymbolicName != null :
00223         !ip.bundleSymbolicName.equals(bundleSymbolicName)) {
00224       return false;
00225     }
00226 
00227     // Check that all other attributes match
00228     for (Iterator i = attributes.entrySet().iterator(); i.hasNext(); ) {
00229       Map.Entry e = (Map.Entry)i.next();
00230       String a = (String)ip.attributes.get(e.getKey());
00231       if (a == null || !a.equals(e.getValue())) {
00232         return false;
00233       }
00234     }
00235 
00236     if (resolution.equals(Constants.RESOLUTION_MANDATORY) &&
00237         !ip.resolution.equals(Constants.RESOLUTION_MANDATORY)) {
00238       return false;
00239     }
00240     if (!packageRange.withinRange(ip.packageRange)) {
00241       return false;
00242     }
00243     return bundleRange.withinRange(ip.bundleRange);
00244   }
00245 
00246 
00252   public String pkgString() {
00253     // NYI! More info?
00254     if (packageRange.isSpecified()) {
00255       return name + ";" + Constants.VERSION_ATTRIBUTE + "=" + packageRange;
00256     } else {
00257       return name;
00258     }
00259   }
00260 
00261 
00267   public String toString() {
00268     return pkgString() + "(" + bpkgs.bundle + ")";
00269   }
00270 
00271 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4