📄 obrcommandimpl.java
字号:
{ BundleRecord record = findBundleRecord(pc.getTargetName(i), pc.getTargetVersion(i)); if (record != null) { updateLocation = (String) record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); } } if (updateLocation != null) { m_brs.deployBundle( out, // Output stream. err, // Error stream. updateLocation, // Update location. pc.isResolve(), // Resolve dependencies. false); // Start. } else { err.println("Unknown bundle or amiguous version: " + pc.getTargetName(i)); } } } private void install( String commandLine, String command, PrintStream out, PrintStream err) throws IOException, InvalidSyntaxException { // Parse the command line to get all local targets to install. ParsedCommand pc = parseInstallStart(commandLine); // Loop through each local target and try to find // the corresponding bundle record from the repository. for (int targetIdx = 0; (pc != null) && (targetIdx < pc.getTargetCount()); targetIdx++) { // Get the current target's name and version. String targetName = pc.getTargetName(targetIdx); String targetVersionString = pc.getTargetVersion(targetIdx); // Make sure the bundle is not already installed. Bundle bundle = findLocalBundle(targetName, targetVersionString); if (bundle == null) { // Find the targets bundle record. BundleRecord record = findBundleRecord(targetName, targetVersionString); // If we found a record, try to install it. if (record != null) { m_brs.deployBundle( out, // Output stream. err, // Error stream. (String) record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION), // Update location. pc.isResolve(), // Resolve dependencies. command.equals(START_CMD)); // Start. } else { err.println("Not in repository: " + targetName); } } else { err.println("Already installed: " + targetName); } } } private void update( String commandLine, String command, PrintStream out, PrintStream err) throws IOException, InvalidSyntaxException { // Parse the command line to get all local targets to update. ParsedCommand pc = parseUpdate(commandLine); if (pc.isCheck()) { updateCheck(out, err); } else { // Loop through each local target and try to find // the corresponding locally installed bundle. for (int targetIdx = 0; (pc != null) && (targetIdx < pc.getTargetCount()); targetIdx++) { // Get the current target's name and version. String targetName = pc.getTargetName(targetIdx); String targetVersionString = pc.getTargetVersion(targetIdx); // Find corresponding locally installed bundle. Bundle bundle = findLocalBundle(targetName, targetVersionString); // If we found a locally installed bundle, then // try to update it. if (bundle != null) { m_brs.deployBundle( out, // Output stream. err, // Error stream. (String) bundle.getHeaders().get(Constants.BUNDLE_UPDATELOCATION), // Local bundle to update. pc.isResolve(), // Resolve dependencies. false); // Start. } else { err.println("Not installed: " + targetName); } } } } private void updateCheck(PrintStream out, PrintStream err) throws IOException { Bundle[] bundles = m_context.getBundles(); // Loop through each local target and try to find // the corresponding locally installed bundle. for (int bundleIdx = 0; (bundles != null) && (bundleIdx < bundles.length); bundleIdx++) { // Ignore the system bundle. if (bundles[bundleIdx].getBundleId() == 0) { continue; } // Get the local bundle's update location. String localLoc = (String) bundles[bundleIdx].getHeaders().get(Constants.BUNDLE_UPDATELOCATION); if (localLoc == null) { // Without an update location, there is no way to // check for an update, so ignore the bundle. continue; } // Get the local bundle's version. String localVersion = (String) bundles[bundleIdx].getHeaders().get(Constants.BUNDLE_VERSION); localVersion = (localVersion == null) ? "0.0.0" : localVersion; // Get the matching repository bundle records. BundleRecord[] records = m_brs.getBundleRecords( (String) bundles[bundleIdx].getHeaders().get(Constants.BUNDLE_NAME)); // Loop through all records to see if there is an update. for (int recordIdx = 0; (records != null) && (recordIdx < records.length); recordIdx++) { String remoteLoc = (String) records[recordIdx].getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); if (remoteLoc == null) { continue; } // If the update locations are equal, then compare versions. if (remoteLoc.equals(localLoc)) { String remoteVersion = (String) records[recordIdx].getAttribute(BundleRecord.BUNDLE_VERSION); if (remoteVersion != null) { int result = Util.compareVersion( Util.parseVersionString(remoteVersion), Util.parseVersionString(localVersion)); if (result > 0) { out.println( records[recordIdx].getAttribute(BundleRecord.BUNDLE_NAME) + " update available."); break; } } } } } } private void source( String commandLine, String command, PrintStream out, PrintStream err) throws IOException, InvalidSyntaxException { // Parse the command line to get all local targets to update. ParsedCommand pc = parseSource(commandLine); for (int i = 0; i < pc.getTargetCount(); i++) { BundleRecord record = findBundleRecord( pc.getTargetName(i), pc.getTargetVersion(i)); if (record != null) { String srcURL = (String) record.getAttribute(BundleRecord.BUNDLE_SOURCEURL); if (srcURL != null) { FileUtil.downloadSource( out, err, srcURL, pc.getDirectory(), pc.isExtract()); } else { err.println("Missing source URL: " + pc.getTargetName(i)); } } else { err.println("Not in repository: " + pc.getTargetName(i)); } } } private BundleRecord findBundleRecord(String name, String versionString) { BundleRecord record = null; // If there is no version, then try to retrieve by // name, but error if there are multiple versions. if (versionString == null) { BundleRecord[] records = m_brs.getBundleRecords(name); if (records.length == 1) { record = records[0]; } } else { record = m_brs.getBundleRecord( name, Util.parseVersionString(versionString)); } return record; } private Bundle findLocalBundle(String name, String versionString) { Bundle bundle = null; // Get the name only if there is no version, but error // if there are multiple matches for the same name. if (versionString == null) { // Perhaps the target name is a bundle ID and // not a name, so try to interpret as a long. try { bundle = m_context.getBundle(Long.parseLong(name)); } catch (NumberFormatException ex) { // The bundle is not a number, so look for a local // bundle with the same name. Bundle[] matchingBundles = findLocalBundlesByName(name); // If only one matches, then select is. if (matchingBundles.length == 1) { bundle = matchingBundles[0]; } } } else { // Find the local bundle by name and version. bundle = findLocalBundleByVersion( name, Util.parseVersionString(versionString)); } return bundle; } private Bundle findLocalBundleByVersion(String name, int[] version) { // Get bundles with matching name. Bundle[] targets = findLocalBundlesByName(name); // Find bundle with matching version. if (targets.length > 0) { for (int i = 0; i < targets.length; i++) { String targetName = (String) targets[i].getHeaders().get(BundleRecord.BUNDLE_NAME); int[] targetVersion = Util.parseVersionString((String) targets[i].getHeaders().get(BundleRecord.BUNDLE_VERSION)); if ((targetName != null) && targetName.equalsIgnoreCase(name) && (Util.compareVersion(targetVersion, version) == 0)) { return targets[i]; } } } return null; } private Bundle[] findLocalBundlesByName(String name) { // Get local bundles. Bundle[] bundles = m_context.getBundles(); // Find bundles with matching name. Bundle[] targets = new Bundle[0]; for (int i = 0; i < bundles.length; i++) { String targetName = (String) bundles[i].getHeaders().get(BundleRecord.BUNDLE_NAME); if (targetName == null) { targetName = bundles[i].getLocation(); } if ((targetName != null) && targetName.equalsIgnoreCase(name)) { Bundle[] newTargets = new Bundle[targets.length + 1]; System.arraycopy(targets, 0, newTargets, 0, targets.length); newTargets[targets.length] = bundles[i]; targets = newTargets; } } return targets; } private ParsedCommand parseInfo(String commandLine) throws IOException, InvalidSyntaxException { // Create a stream tokenizer for the command line string, // since the syntax for install/start is more sophisticated. StringReader sr = new StringReader(commandLine); StreamTokenizer tokenizer = new StreamTokenizer(sr); tokenizer.resetSyntax(); tokenizer.quoteChar('\''); tokenizer.quoteChar('\"'); tokenizer.whitespaceChars('\u0000', '\u0020'); tokenizer.wordChars('A', 'Z'); tokenizer.wordChars('a', 'z'); tokenizer.wordChars('0', '9');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -