obrcommandgroup.java

来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 856 行 · 第 1/2 页

JAVA
856
字号
      {	// 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 = 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;		}	      }	    }	  }      }  }    /*  public final static String USAGE_SOURCE = "[-x] <localDir> <name;version> ...";  public final static String [] HELP_SOURCE = new String [] {    "Get source for a bundle",    "name;version - name and optional version" };    public int cmdSource(Dictionary opts, Reader in, PrintWriter outPW, Session session) {    String[] infos   = (String[])opts.get("name;version");    String localDir  = (String)opts.get("localDir");    boolean bExtract = opts.get("-x") != null;        if(localDir == null) {      localDir = ".";    }        ParsedCommand pc = parseInfo(infos);    PrintStream out = new PrintWriterStream(outPW);        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, 				    out, 				    srcURL, 				    localDir, 				    bExtract);	  } else {	    outPW.println("Missing source URL: " + pc.getTargetName(i));	    return 1;	  }	} else {	  outPW.println("Not in repository: " + pc.getTargetName(i));	  return 1;	}      }    return 0;  }  */    private BundleRecord findBundleRecord(String name, String versionString)  {    BundleRecord record = null;    if(name.startsWith("=")) {      int id = -1;      try {	id = Integer.parseInt(name.substring(1));	record = brs.getBundleRecord(id - 1);	if(record != null) {	  System.out.println(id + " " + record.getAttribute(BundleRecord.BUNDLE_UPDATELOCATION));	  return record;	}      } catch (Exception ignored) {      }    }    // If there is no version, then try to retrieve by    // name, but error if there are multiple versions.    if (versionString == null)      {	BundleRecord[] records =	  brs.getBundleRecords(name);	if (records.length == 1)	  {	    record = records[0];	  }      }    else      {	record = 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 = bc.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 = bc.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;    }  ParsedCommand parseInfo(String[] infos) {    ParsedCommand pc = new ParsedCommand();    for(int i = 0; infos != null && i < infos.length; i++) {      String name    = infos[i];      String version = null;      int ix = infos[i].indexOf(";");      if(ix != -1) {	name    = infos[i].substring(0, ix);	version = infos[i].substring(ix + 1);      }      pc.addTarget(name, version);    }    return pc;  }    private static class ParsedCommand  {    private static final int NAME_IDX = 0;    private static final int VERSION_IDX = 1;        private String[][] m_targets = new String[0][];        public int getTargetCount()    {      return m_targets.length;    }        public String getTargetName(int i)    {      if ((i < 0) || (i >= getTargetCount()))	{	  return null;	}      return m_targets[i][NAME_IDX];    }        public String getTargetVersion(int i)    {      if ((i < 0) || (i >= getTargetCount()))	{	  return null;	}      return m_targets[i][VERSION_IDX];    }        public void addTarget(String name, String version)    {      String[][] newTargets = new String[m_targets.length + 1][];      System.arraycopy(m_targets, 0, newTargets, 0, m_targets.length);      newTargets[m_targets.length] = new String[] { name, version };      m_targets = newTargets;    }  }    /**   * Wrap a PrintWriter into a PrintStream by overriding all methods.   */  public static class PrintWriterStream extends PrintStream {    PrintWriter pw;    boolean     bClose = false;        /**     * @param pw underlying writer to which all data is send to     * @param bClose if <tt>true</tt> close the underlying writer     *               when <tt>PrintWriterStream.close()</tt> is called.     */    public PrintWriterStream(PrintWriter pw, boolean bClose) {      super(new ByteArrayOutputStream()); // This is really a dummy stream      this.pw     = pw;      this.bClose = bClose;    }        /**     * Same as <tt>PrintWriterStream(pw, false)</tt>     */    public PrintWriterStream(PrintWriter pw) {      this(pw, false);    }        /**     * Only closes the underlying stream if     * constructued with the close flag.     */    public void close() {      super.close();      if(bClose) {	pw.close();      }    }        /**     * Write using the trivial, but possibly not always correct translation:     * <pre>     *  write((int) byte)     * </pre>     */    public void write(byte[] buf, int off, int len) {       for(int i = off; i < off + len; i++) {	write((int)buf[i]);      }    }         public void write(int b) {       pw.write(b);    }         public boolean checkError() {      return pw.checkError();    }        public void flush() {      pw.flush();    }        public void print(boolean b) {      pw.print(b);    }        public void print(char c) {      pw.print(c);    }        public void print(char[] s) {      pw.print(s);    }        public void print(double d) {      pw.print(d);    }    public void print(float f) {      pw.print(f);    }        public void print(int i) {      pw.print(i);    }        public void print(long l) {      pw.print(l);    }        public void print(Object obj) {      pw.print(obj);    }        public void print(String s) {      pw.print(s);    }        public void println() {      pw.println();    }        public void println(boolean x) {      pw.println(x);    }        public void println(char x) {      pw.println(x);    }        public void println(char[] x) {       pw.println(x);    }        public void println(double x) {       pw.println(x);    }         public void println(float x) {       pw.println(x);    }        public void println(int x) {       pw.println(x);    }        public void println(long x) {       pw.println(x);    }        public void println(Object x) {       pw.println(x);    }        public void println(String x) {       pw.println(x);    }   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?