📄 obrcommandgroup.java
字号:
/* * Copyright (c) 2004, Richard S. Hall * 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 ungoverned.org 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. * * Original source: Richard S. Hall (heavy@ungoverned.org) * KF Console API adaption: Erik Wistrand (wistrand@knopflerfish.org) * */package org.knopflerfish.osgi.bundle.bundlerepository;import java.io.*;import java.util.Dictionary;import java.util.Hashtable;import org.osgi.framework.*;import org.ungoverned.osgi.service.bundlerepository.BundleRecord;import org.ungoverned.osgi.service.bundlerepository.BundleRepositoryService;import org.ungoverned.osgi.bundle.bundlerepository.Util;import org.ungoverned.osgi.bundle.bundlerepository.FileUtil;import org.knopflerfish.service.console.*;/** * Export the following OBR commands as a KF console command group "obr" * * <pre> * urls [<repository-file-url> ...] * list [<string> ...] * info <bundle-name>[;<version>] ... * deploy [-nodeps] <bundle-name>[;<version>] ... | <bundle-id> ... * install [-nodeps] <bundle-name>[;<version>] ... * start [-nodeps] <bundle-name>[;<version>] ... * update -check * update [-nodeps] <bundle-name>[;<version>] ... | <bundle-id> ... * source [-x] <local-dir> <bundle-name>[;<version>] ... * </pre> */public class ObrCommandGroup extends CommandGroupAdapter{ private BundleContext bc = null; private BundleRepositoryService brs = null; protected ServiceRegistration reg = null; public ObrCommandGroup(BundleContext bc) { super("obr", "OBR commands"); this.bc = bc; ServiceReference sr = bc.getServiceReference(BundleRepositoryService.class.getName()); this.brs = (BundleRepositoryService)bc.getService(sr); if(this.brs == null) { throw new RuntimeException("BundleRepositoryService must be available"); } } public void register() { if(reg == null) { Hashtable props = new Hashtable(); props.put("groupName", getGroupName()); reg = bc.registerService(CommandGroup.class.getName(), this, props); } } void unregister() { if(reg != null) { reg.unregister(); reg = null; } } public final static String USAGE_URLS = "[<url>] ..."; public final static String [] HELP_URLS = new String [] { "List or set repository URLs", "<url> repository URL" }; public int cmdUrls(Dictionary opts, Reader in, PrintWriter out, Session session) { String[] urls = (String[])opts.get("url"); if(urls != null && urls.length > 0) { brs.setRepositoryURLs(urls); } else { urls = brs.getRepositoryURLs(); if (urls != null) { for (int i = 0; i < urls.length; i++) { out.println(urls[i]); } } else { out.println("No repository URLs are set."); } } return 0; } public final static String USAGE_LIST = "[-l] [<name>]"; public final static String [] HELP_LIST = new String [] { "List contents of repository", "-l - long format", "name - name (or substring) for bundles to list", " If no name is given, list all bundles.", }; public int cmdList(Dictionary opts, Reader in, PrintWriter out, Session session) { String substr = (String)opts.get("name"); boolean bLong = null != opts.get("-l"); int nCount = 0; int count = brs.getBundleRecordCount(); if(count == 0) { out.println("No bundles in repositories"); } else { if(bLong) { out.println("No Name Update-location"); } else { out.println("No Name"); } for (int i = 0; i < brs.getBundleRecordCount(); i++) { BundleRecord record = brs.getBundleRecord(i); String name = (String) record.getAttribute(BundleRecord.BUNDLE_NAME); if (name != null) { if ((substr == null) || (name.toLowerCase().indexOf(substr) >= 0)) { nCount++; String version = (String) record.getAttribute(BundleRecord.BUNDLE_VERSION); boolean bCit = true; // name.indexOf(" ") != -1; StringBuffer sb = new StringBuffer(); sb.append(" "); sb.append(Integer.toString(i + 1)); pad(sb, 5); if(bCit) { sb.append("\""); } if (version != null) { sb.append(name + ";" + version); } else { sb.append(name); } if(bCit) { sb.append("\""); } if(bLong) { sb.append(" "); pad(sb, 25); sb.append(record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION)); } out.println(sb.toString()); } } } } if (nCount == 0) { out.println("No matching bundles."); } return 0; } static StringBuffer pad(StringBuffer sb, int len) { while(sb.length() < len) { sb.append(" "); } return sb; } public final static String USAGE_INFO = "<name;version> ..."; public final static String [] HELP_INFO = new String [] { "Show bundle info", "name - name (or substring) for bundles to list", " if name is an integer, use bundle with", " this index (from obr list)"}; public int cmdInfo(Dictionary opts, Reader in, PrintWriter outPW, Session session) { String[] infos = (String[])opts.get("name;version"); ParsedCommand pc = parseInfo(infos); for (int i = 0; (pc != null) && (i < pc.getTargetCount()); i++) { BundleRecord record = null; // If there is no version, then try to retrieve by // name, but error if there are multiple versions. if (pc.getTargetVersion(i) == null) { BundleRecord[] records = brs.getBundleRecords(pc.getTargetName(i)); if (records.length == 1) { record = records[0]; } else { } } else { record = brs.getBundleRecord(pc.getTargetName(i), Util.parseVersionString( pc.getTargetVersion(i))); } if (record != null) { PrintStream outStream = new PrintWriterStream(outPW); record.printAttributes(outStream); } else { outPW.println("Unknown bundle or ambiguous version: " + pc.getTargetName(i)); for (int j = 0; j < brs.getBundleRecordCount(); j++) { BundleRecord r2 = brs.getBundleRecord(j); String name = (String) r2.getAttribute(BundleRecord.BUNDLE_NAME); String version = (String) r2.getAttribute(BundleRecord.BUNDLE_VERSION); if(name.equals(pc.getTargetName(i))) { outPW.println(" \"" + name + ";" + version + "\""); } } } outPW.println(""); } return 0; } public final static String USAGE_DEPLOY = "[-nodeps] <name;version> ..."; public final static String [] HELP_DEPLOY = new String [] { "Deploy bundle(s)", "name;version - name (and optional version)" }; public int cmdDeploy(Dictionary opts, Reader in, PrintWriter outPW, Session session) { String[] infos = (String[])opts.get("name;version"); ParsedCommand pc = parseInfo(infos); boolean bResolve = (null == opts.get("-nodeps")); for (int i = 0; (pc != null) && (i < pc.getTargetCount()); i++) { // Find either the local bundle or the bundle // record so we can get the update location attribute. String updateLocation = null; // First look for update location locally. Bundle bundle = findLocalBundle(pc.getTargetName(i), pc.getTargetVersion(i)); if (bundle != null) { updateLocation = (String) bundle.getHeaders().get(Constants.BUNDLE_UPDATELOCATION); } // If update location wasn't found locally, look in repository. if (updateLocation == null) { BundleRecord record = findBundleRecord(pc.getTargetName(i), pc.getTargetVersion(i)); if (record != null) { updateLocation = (String) record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION); } } if (updateLocation != null) { PrintStream outStream = new PrintWriterStream(outPW); brs.deployBundle( outStream, // Output stream. outStream, // Error stream. updateLocation, // Update location. bResolve, // Resolve dependencies. false); // Start. } else { outPW.println("Unknown bundle or ambiguous version: " + pc.getTargetName(i)); return 1; } } return 0; } public final static String USAGE_INSTALL = "[-nodeps] <name;version> ..."; public final static String [] HELP_INSTALL = new String [] { "Install bundle", "name;version - name and optional version.", " If name starts with '=', use number from obr list",}; public int cmdInstall(Dictionary opts, Reader in, PrintWriter out, Session session) { return doInstallOrStart(opts, in, out, session, false); } public final static String USAGE_START = "[-nodeps] <name;version> ..."; public final static String [] HELP_START = new String [] { "Install and start bundle", "name;version - name and optional version.", " If name starts with =, use number from obr list",}; public int cmdStart(Dictionary opts, Reader in, PrintWriter out, Session session) { return doInstallOrStart(opts, in, out, session, true); } public int doInstallOrStart(Dictionary opts, Reader in, PrintWriter outPW, Session session, boolean bStart) { String[] infos = (String[])opts.get("name;version"); ParsedCommand pc = parseInfo(infos); boolean bResolve = (null == opts.get("-nodeps")); // 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) { PrintStream outStream = new PrintWriterStream(outPW); brs.deployBundle( outStream, // Output stream. outStream, // Error stream. (String) record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION), // Update location. bResolve, // Resolve dependencies. bStart); } else { outPW.println("Not in repository: " + targetName); return 1; } } else { outPW.println("Already installed: " + targetName); } } return 0; } public final static String USAGE_UPDATE = "[-nodeps] [-check] <name;version> ..."; public final static String [] HELP_UPDATE = new String [] { "Update bundle", "name;version - name and optional version" }; public int cmdUpdate(Dictionary opts, Reader in, PrintWriter outPW, Session session) throws IOException { String[] infos = (String[])opts.get("name;version"); boolean bCheck = (null == opts.get("-check")); boolean bResolve = (null == opts.get("-nodeps")); ParsedCommand pc = parseInfo(infos); PrintStream out = new PrintWriterStream(outPW); if (bCheck) { updateCheck(out, out); } 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) { brs.deployBundle( out, // Output stream. out, // Error stream. (String) bundle.getHeaders().get(Constants.BUNDLE_UPDATELOCATION), // Local bundle to update. bResolve, // Resolve dependencies. false); // Start. } else { outPW.println("Not installed: " + targetName); } } } return 0; } private void updateCheck(PrintStream out, PrintStream err) throws IOException { Bundle[] bundles = bc.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++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -