📄 packages.java
字号:
/* * Copyright (c) 2003-2004, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */package org.knopflerfish.framework;import java.util.*;import org.osgi.framework.*;/** * Here we handle all the java packages that are imported and exported * within framework. * * @author Jan Stein, Erik Wistrand */class Packages { /** * Framework for bundle. */ final Framework framework; /** * All exported and imported packages. */ private HashMap /* String->Pkg */ packages = new HashMap(); /** * List of temporary resolved bundles. */ private ArrayList /* BundleImpl */ tempResolved = null; /** * Map of temporary package providers. */ private HashMap tempProvider = null; /** * Union of flags allowing bundle package access. * <p> * Value is <tt>Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING</tt> * </p> */ public static int RESOLVED_FLAGS = Bundle.RESOLVED | Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING; /** * Construct Packages object. */ Packages(Framework fw) { framework = fw; } /** * Register all packages a bundle needs to export and import. * If it is registered by the system bundle, export it immediately. * * @param exports Exported packages. * @param imports Imported packages. */ synchronized void registerPackages(Iterator exports, Iterator imports) { while (exports.hasNext()) { PkgEntry pe = (PkgEntry)exports.next(); Pkg p = (Pkg)packages.get(pe.name); if (p == null) { p = new Pkg(pe.name); packages.put(pe.name, p); } p.addExporter(pe); if (Debug.packages) { Debug.println("registerPackages: export, " + pe); } } while (imports.hasNext()) { PkgEntry pe = (PkgEntry)imports.next(); Pkg p = (Pkg)packages.get(pe.name); if (p == null) { p = new Pkg(pe.name); packages.put(pe.name, p); } p.addImporter(pe); if (Debug.packages) { Debug.println("registerPackages: import, " + pe); } } } /** * Dynamically check and register a dynamic package import. * * @param pe PkgEntry import to add. * @return PkgEntry for package provider. */ synchronized PkgEntry registerDynamicImport(PkgEntry pe) { if (Debug.packages) { Debug.println("dynamicImportPackage: try " + pe); } Pkg p = (Pkg)packages.get(pe.name); if (p != null) { if(p.provider != null) { if((pe.bundle.getState() & RESOLVED_FLAGS) != 0) { p.addImporter(pe); if (Debug.packages) { Debug.println("dynamicImportPackage: added " + pe); } } else { if (Debug.packages) { Debug.println("dynamicImportPackage: skip add since bundle is not resolved " + pe); } } return p.provider; } else { // If the bundle trying to use dynamic import is resolved but // potential providers are yet not resolved check for providers // and resolve them as necessary // List of bundles we try to resolve but fail List /* BundleImpl */ failedBundles = new ArrayList(); if((pe.bundle.getState() & RESOLVED_FLAGS) != 0) { for(Iterator it = p.exporters.iterator(); it.hasNext();) { PkgEntry pe2 = (PkgEntry)it.next(); int state = pe2.bundle.getUpdatedState(); if((state & RESOLVED_FLAGS) != 0) { p.addImporter(pe); return p.provider; } else { // add to set, to be able to give informative debug info in // the case of all exporters fail to resolve failedBundles.add(pe2.bundle); } } // If we reach this, not potential exporter has been // possible to resolve. framework.listeners.frameworkError(pe.bundle, new Exception("dynamicResolve: failed to resolve " + pe +", unresolved exportes: " + failedBundles)); return null; } else { if (Debug.packages) { Debug.println("dynamicImportPackage: failed because importing bundle is not resolved: " + pe.bundle.toString(2)); } } } } return null; } /** * Unregister bundle packages in framework. If we find exported packages * that has been selected as providers don't unregister them unless the * parameter force is true. If not all exporters were removed, the don't * remove any importers * * @param exports Exported packages. * @param imports Imported packages. * @param force If true force unregistration of package providers. * @return True if all packages were succesfully unregistered, * otherwise false. */ synchronized boolean unregisterPackages(Iterator exports, Iterator imports, boolean force) { boolean allRemoved = true; while (exports.hasNext()) { PkgEntry pe = (PkgEntry)exports.next(); Pkg p = pe.pkg; if (p != null) { if (Debug.packages) { Debug.println("unregisterPackages: unregister export - " + pe); } if (!p.removeExporter(pe)) { if (force) { p.provider = null; p.removeExporter(pe);//XXX - begin L-3 modification p.zombie = false;//XXX - end L-3 modification if (Debug.packages) { Debug.println("unregisterPackages: forced unregister - " + pe); } } else { allRemoved = false; p.zombie = true; if (Debug.packages) { Debug.println("unregisterPackages: failed to unregister - " + pe); } continue; } } if (p.isEmpty()) { packages.remove(pe.name); } } } if (allRemoved) { while (imports.hasNext()) { PkgEntry pe = (PkgEntry)imports.next(); Pkg p = pe.pkg; if (p != null) { if (Debug.packages) { Debug.println("unregisterPackages: unregister import - " + pe.pkgString()); } p.removeImporter(pe); if (p.isEmpty()) { packages.remove(pe.name); } } } } return allRemoved; } /** * Check if a list packages can be resolved. * * @param exports Exported packages. * @param imports Imported packages. * @param bundle Bundle owning packages. * @param pkgs List of packages to be resolved. * @return List of packages not resolvable or null if all were resolved. */ synchronized List checkResolve(Bundle bundle, Iterator pkgs) { if (Debug.packages) { Debug.println("checkResolve: " + bundle); } if (tempResolved != null) { // If we entry with tempResolved set, it means that we already have // resolved bundles. Check that it is true! if (!tempResolved.contains(bundle)) { framework.listeners.frameworkError(bundle, new Exception("checkResolve: InternalError1!")); } return null; } tempProvider = new HashMap(); tempResolved = new ArrayList(); tempResolved.add(bundle); List res = resolvePackages(pkgs); if (res.size() == 0) { for (Iterator i = tempProvider.values().iterator(); i.hasNext();) { PkgEntry pe = (PkgEntry)i.next(); pe.pkg.provider = pe; } tempResolved.remove(0); for (Iterator i = tempResolved.iterator(); i.hasNext();) { BundleImpl bs = (BundleImpl)i.next(); if (bs.getUpdatedState() == Bundle.INSTALLED) { framework.listeners.frameworkError(bs, new Exception("checkResolve: InternalError2!")); } } res = null; } tempProvider = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -