📄 frameworkcommandgroup.java
字号:
// public final static String USAGE_UPDATE = "<bundle> ..."; public final static String[] HELP_UPDATE = new String[] { "Update one or more bundles", "<bundle> Name or id of bundle", "Note: Use refresh command to force the framework to do a package update", "of exported packages used by running bundles." }; public int cmdUpdate(Dictionary opts, Reader in, PrintWriter out, Session session) { Bundle[] b = getBundles((String[]) opts.get("bundle"), true); boolean found = false; for (int i = b.length - 1; i >= 0; i--) { if (b[i] != null) { try { b[i].update(); out.println("Updated: " + showBundle(b[i])); } catch (BundleException e) { Throwable t = e; while (t instanceof BundleException && ((BundleException) t).getNestedException() != null) t = ((BundleException) t).getNestedException(); out.println("Couldn't update: " + showBundle(b[i]) + " (due to: " + t + ")"); } found = true; } } if (!found) { out.println("ERROR! No matching bundle"); return 1; } out .println("Note: Use refresh command to update exported packages in running bundles"); return 0; } public final static String USAGE_FROMUPDATE = "<bundle> <url>"; public final static String[] HELP_FROMUPDATE = new String[] { "Update a bundle from a specific URL", "<bundle> - Name or id of bundle", "<url> - URL to update from", "Note: Use refresh command to force the framework to do a package update", "of exported packages used by running bundles." }; public int cmdFromupdate(Dictionary opts, Reader in, PrintWriter out, Session session) { String bname = (String) opts.get("bundle"); Bundle[] bl = getBundles(new String[] { bname }, true); String fromURL = (String) opts.get("url"); Bundle b = bl[0]; if (b == null) { out.println("ERROR! No matching bundle for '" + bname + "'"); return 1; } try { URL url = new URL(fromURL); URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); b.update(inStream); out.println("Updated: " + showBundle(b)); } catch (BundleException e) { Throwable t = e; while (t instanceof BundleException && ((BundleException) t).getNestedException() != null) t = ((BundleException) t).getNestedException(); out.println("Couldn't update: " + showBundle(b) + " (due to: " + t + ")"); } catch (Exception e) { out.println("Couldn't update: " + showBundle(b) + " (due to: " + e + ")"); } out .println("Note: Use refresh command to update exported packages in running bundles"); return 0; } public final static String USAGE_FROMINSTALL = "<url> [<location>]"; public final static String[] HELP_FROMINSTALL = new String[] { "Install a bundle with a specific location from an URL", "<url> - URL to bundle jar file", "<location> - Optional location string to use for installation", }; public int cmdFrominstall(Dictionary opts, Reader in, PrintWriter out, Session session) { String fromURL = (String) opts.get("url"); String loc = (String) opts.get("location"); if (loc == null) { loc = fromURL; } try { URL url = new URL(fromURL); URLConnection conn = url.openConnection(); InputStream inStream = conn.getInputStream(); Bundle b = bc.installBundle(loc, inStream); out.println("Installed: " + showBundle(b)); } catch (BundleException e) { Throwable t = e; while (t instanceof BundleException && ((BundleException) t).getNestedException() != null) t = ((BundleException) t).getNestedException(); out.println("Couldn't install: url=" + fromURL + ", location=" + loc + " (due to: " + t + ")"); } catch (Exception e) { out.println("Couldn't install: url=" + fromURL + ", location=" + loc + " (due to: " + e + ")"); } return 0; } // // Private methods // private void showstate(PrintWriter out, ServiceReference[] srs) { if (srs != null) { for (int i = 0; i < srs.length; i++) { Object state = srs[i].getProperty("state"); if (state != null) { out.println("State for " + srs[i].getProperty("service.id") + ":"); out.println(state.toString()); } } } } private Bundle[] getBundles(String[] selection, boolean sortNumeric) { return getBundles(selection, sortNumeric, false); } private Bundle[] getBundles(String[] selection, boolean sortNumeric, boolean sortStartLevel) { Bundle[] b = bc.getBundles(); Util.selectBundles(b, selection); if (sortNumeric) { Util.sortBundlesId(b); } else { Util.sortBundles(b, false); } if (sortStartLevel) { sortBundlesStartLevel(b); } return b; } /** * Sort an array of bundle objects based on their start level All entries * with no start level is placed at the end of the array. * * @param b * array of bundles to be sorted, modified with result */ protected void sortBundlesStartLevel(Bundle[] b) { int x = b.length; for (int l = x; x > 0;) { x = 0; int p = Integer.MAX_VALUE; try { p = startLevel.getBundleStartLevel(b[0]); } catch (Exception ignored) { } for (int i = 1; i < l; i++) { int n = Integer.MAX_VALUE; try { n = startLevel.getBundleStartLevel(b[i]); } catch (Exception ignored) { } if (p > n) { x = i - 1; Bundle t = b[x]; b[x] = b[i]; b[i] = t; } else { p = n; } } } } public String showState(Bundle bundle) { StringBuffer sb = new StringBuffer(); try { StringBuffer s = new StringBuffer(Integer.toString(startLevel .getBundleStartLevel(bundle))); while (s.length() < 2) { s.insert(0, " "); } sb.append(s.toString()); } catch (Exception ignored) { sb.append("--"); } sb.append("/"); switch (bundle.getState()) { case Bundle.INSTALLED: sb.append("installed"); break; case Bundle.RESOLVED: sb.append("resolved"); break; case Bundle.STARTING: sb.append("starting"); break; case Bundle.ACTIVE: sb.append("active"); break; case Bundle.STOPPING: sb.append("stopping"); break; case Bundle.UNINSTALLED: sb.append("uninstalled"); break; default: sb.append("ILLEGAL <" + bundle.getState() + "> "); break; } while (sb.length() < 13) { sb.append(" "); } return sb.toString(); } String showBundle(Bundle b) { return Util.shortName(b) + " (#" + b.getBundleId() + ")"; } private void showPerms(PrintWriter out, PermissionInfo[] pi) { final String shift = " "; if (pi == null) { out.println(shift + "DEFAULT"); } else if (pi.length == 0) { out.println(shift + "NONE"); } else { for (int i = 0; i < pi.length; i++) { out.println(shift + pi[i]); } } } // // Set start level command // public final static String USAGE_STARTLEVEL = "[<level>]"; public final static String[] HELP_STARTLEVEL = new String[] { "Shows or sets the global startlevel", "[<level>] new start level", " if no <level> is provided, show current level", }; public int cmdStartlevel(Dictionary opts, Reader in, PrintWriter out, Session session) { String levelStr = (String) opts.get("level"); try { if (levelStr != null) { int level = Integer.parseInt(levelStr); startLevel.setStartLevel(level); } else { out.println("current start level: " + startLevel.getStartLevel()); out.println("initial bundle start level: " + startLevel.getInitialBundleStartLevel()); } return 0; } catch (Exception e) { out.println("Failed to show/set startlevel=" + levelStr); e.printStackTrace(out); return -1; } } // // CD command // public final static String USAGE_CD = "[-reset] [<prefix>] ..."; public final static String[] HELP_CD = new String[] { "Shows or sets URL prefix", "[-reset] reset prefix list to value startup value", "[<prefix>] ... list of URL prefixes for install command", }; public int cmdCd(Dictionary opts, Reader in, PrintWriter out, Session session) { String[] prefixes = (String[]) opts.get("prefix"); try { if (opts.get("-reset") != null) { setupJars(); } if (prefixes == null) { for (int i = 0; i < bundleDirs.length; i++) { out.println(" " + bundleDirs[i]); } } else { bundleDirs = prefixes; } return 0; } catch (Exception e) { out.println("Failed to cd"); e.printStackTrace(out); return -1; } } // // Set bundle start level // public final static String USAGE_BUNDLELEVEL = "<level> [<bundle>] ..."; public final static String[] HELP_BUNDLELEVEL = new String[] { "Set startlevel(s) for bundles", "<level> new start level", "<bundle> Name or id of bundles", " If bundle list is empty, set initial", " start level for new bundles", }; public int cmdBundlelevel(Dictionary opts, Reader in, PrintWriter out, Session session) { int level = -1; try { level = Integer.parseInt((String) opts.get("level")); String[] bls = (String[]) opts.get("bundle"); Bundle[] bl = getBundles(bls, false, false); if (bls == null || bls.length == 0) { startLevel.setInitialBundleStartLevel(level); out.println("initial bundle start level set to " + level); } else { for (int i = 0; i < bl.length; i++) { if (bl[i] != null) { System.out.println("set " + i + " " + bl[i] + " " + level); startLevel.setBundleStartLevel(bl[i], level); } } } return 0; } catch (Exception e) { out.println("Failed to set bundle startlevel=" + level); e.printStackTrace(out); return -1; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -