ExportPkg.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 ExportPkg {
00049   final String name;
00050   final BundlePackages bpkgs;
00051   final ArrayList /* String */ uses;
00052   final ArrayList /* String */ mandatory;
00053   final ArrayList /* String */ include;
00054   final ArrayList /* String */ exclude;
00055   final Version version;
00056   final Map attributes;
00057   boolean zombie = false;
00058 
00059   // Link to pkg entry
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         // assert fullClassName.startsWith(name)
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           // assert fullClassName.startsWith(name)
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   // Private
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 }

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