📄 bundlerepositoryserviceimpl.java
字号:
} else { out.print("Updating: "); } out.println(Util.getBundleName(localBundle)); // Actually perform the update. try { localBundle.update(); } catch (BundleException ex) { err.println("Update error: " + Util.getBundleName(localBundle)); ex.printStackTrace(err); return false; } } else { // Print out an "installing" message. if (!deployLocation.equals(updateLocation)) { out.print("Installing dependency: "); } else { out.print("Installing: "); } record = findBundleRecordByUpdate(deployLocation); out.println(record.getAttribute(BundleRecord.BUNDLE_NAME)); // Actually perform the install. try { Bundle bundle = m_context.installBundle(deployLocation); // If necessary, save the installed bundle to be // started later. if (isStart) { if (startList == null) { startList = new ArrayList(); } startList.add(bundle); } } catch (BundleException ex) { err.println("Install error: " + record.getAttribute(BundleRecord.BUNDLE_NAME)); ex.printStackTrace(err); return false; } } } // If necessary, start bundles after installing them all. if (isStart) { for (int i = 0; (startList != null) && (i < startList.size()); i++) { localBundle = (Bundle) startList.get(i); try { localBundle.start(); } catch (BundleException ex) { err.println("Update error: " + Util.getBundleName(localBundle)); ex.printStackTrace(); } } } return true; } return false; } public BundleRecord[] resolvePackages(PackageDeclaration[] pkgs) throws ResolveException { // Create a list that will contain the transitive closure of // all import dependencies; use a list because this will keep // everything in order. List deployList = new ArrayList(); resolvePackages(pkgs, deployList); // Convert list of update locations to an array of bundle // records and return it. BundleRecord[] records = new BundleRecord[deployList.size()]; for (int i = 0; i < deployList.size(); i++) { String updateLocation = (String) deployList.get(i); records[i] = findBundleRecordByUpdate(updateLocation); } return records; } public void resolvePackages( PackageDeclaration[] pkgs, List deployList) throws ResolveException { for (int pkgIdx = 0; (pkgs != null) && (pkgIdx < pkgs.length); pkgIdx++) { // If the package can be locally resolved, then // it can be completely ignored; otherwise, try // to find a resolving bundle. if (!isLocallyResolvable(pkgs[pkgIdx])) { // Select resolving bundle for current package. BundleRecord source = selectResolvingBundle(pkgs[pkgIdx]); // If there is no resolving bundle, then throw a // resolve exception. if (source == null) { throw new ResolveException(pkgs[pkgIdx]); } // Get the update location of the resolving bundle. String updateLocation = (String) source.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); // If the resolving bundle's update location is already // in the deploy list, then just ignore it; otherwise, // add it to the deploy list and resolve its packages. if (!deployList.contains(updateLocation)) { deployList.add(updateLocation); PackageDeclaration[] imports = (PackageDeclaration[]) source.getAttribute(BundleRecord.IMPORT_PACKAGE); resolvePackages(imports, deployList); } } } } /** * Returns a locally installed bundle that has an update location * manifest attribute that matches the specified update location * value. * @param updateLocation the update location attribute for which to search. * @return a bundle with a matching update location attribute or * <tt>null</tt> if one could not be found. **/ private Bundle findLocalBundleByUpdate(String updateLocation) { Bundle[] locals = m_context.getBundles(); for (int i = 0; i < locals.length; i++) { String localUpdateLocation = (String) locals[i].getHeaders().get(BundleRecord.BUNDLE_UPDATELOCATION); if ((localUpdateLocation != null) && localUpdateLocation.equals(updateLocation)) { return locals[i]; } } return null; } private boolean isUpdateAvailable( PrintStream out, PrintStream err, Bundle bundle) { // Get the bundle's update location. String updateLocation = (String) bundle.getHeaders().get(Constants.BUNDLE_UPDATELOCATION); // Get associated repository bundle recorded for the // local bundle and see if an update is necessary. BundleRecord record = findBundleRecordByUpdate(updateLocation); if (record == null) { err.println(Util.getBundleName(bundle) + " not in repository."); return false; } // Check bundle version againts bundle record version. int[] bundleVersion = Util.parseVersionString( (String) bundle.getHeaders().get(Constants.BUNDLE_VERSION)); int[] recordVersion = Util.parseVersionString( (String) record.getAttribute(BundleRecord.BUNDLE_VERSION)); if (Util.compareVersion(recordVersion, bundleVersion) > 0) { return true; } return false; } /** * Returns the bundle record corresponding to the specified update * location string; update location strings are assumed to be * unique. * @param updateLocation the update location of the bundle record * to retrieve. * @return the corresponding bundle record or <tt>null</tt>. **/ private synchronized BundleRecord findBundleRecordByUpdate(String updateLocation) { if (!m_initialized) { initialize(); } for (int i = 0; i < m_bundleList.size(); i++) { String location = (String) getBundleRecord(i).getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); if ((location != null) && location.equalsIgnoreCase(updateLocation)) { return getBundleRecord(i); } } return null; } /** * Determines whether a package is resolvable at the local framework. * @param target the package declaration to check for availability. * @return <tt>true</tt> if the package is available locally, * <tt>false</tt> otherwise. **/ private synchronized boolean isLocallyResolvable(PackageDeclaration target) { // Get package admin service. ServiceReference ref = m_context.getServiceReference( "org.osgi.service.packageadmin.PackageAdmin"); if (ref == null) { return false; } PackageAdmin pa = (PackageAdmin) m_context.getService(ref); if (pa == null) { return false; } ExportedPackage[] exports = pa.getExportedPackages(null); if (exports != null) { for (int i = 0; (exports != null) && (i < exports.length); i++) { PackageDeclaration source = new PackageDeclaration( exports[i].getName(), exports[i].getSpecificationVersion()); if (source.doesSatisfy(target)) { return true; } } } return false; } /** * Selects a single source bundle record for the target package from * the repository. The algorithm tries to select a source bundle record * if it is already installed locally in the framework; this approach * favors updating already installed bundles rather than installing * new ones. If no matching bundles are installed locally, then the * first bundle record providing the target package is returned. * @param targetPkg the target package for which to select a source * bundle record. * @return the selected bundle record or <tt>null</tt> if no sources * could be found. **/ private BundleRecord selectResolvingBundle(PackageDeclaration targetPkg) { BundleRecord[] sources = findResolvingBundles(targetPkg); if (sources == null) { return null; } // Try to select a source bundle record that is already // installed locally. for (int i = 0; i < sources.length; i++) { String updateLocation = (String) sources[i].getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); if (updateLocation != null) { Bundle bundle = findLocalBundleByUpdate(updateLocation); if (bundle != null) { return sources[i]; } } } // If none of the sources are installed locally, then // just pick the first one.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -