📄 cmcommands.java
字号:
private boolean isEditing(Session session) { return session.getProperties().get(EDITED) != null; } /*************************************************************************** * Helper method that gets the editing dictionary of the current * * configuration from the session. Returns a new empty dictionary if current * is set but have no dictionary set yet.* **************************************************************************/ private Dictionary getEditingDict(Session session) { Dictionary dict = (Dictionary) session.getProperties().get(EDITED); if (dict == null) { Configuration cfg = getCurrent(session); if (cfg != null) { dict = cfg.getProperties(); } if (dict == null) { dict = new Hashtable(); } setEditingDict(session, dict); } return dict; } private boolean configurationHasChanged(ConfigurationAdmin ca, Configuration c1) throws Exception { String pid = c1.getPid(); Configuration c2 = ca.getConfiguration(pid, null); return DictionaryUtils.dictionariesAreNotEqual(c1.getProperties(), c2 .getProperties()); } private Configuration[] getConfigurations(Session session, ConfigurationAdmin cm, String[] selection) throws Exception { Filter[] filters = convertToFilters(session, selection); return getConfigurationsMatchingFilters(cm, filters); } private Configuration[] getConfigurations(Session session, ConfigurationAdmin cm, String selection) throws Exception { return getConfigurations(session, cm, new String[] { selection }); } private Filter[] convertToFilters(Session session, String[] selection) throws Exception { if (selection == null) { return null; } Filter[] filters = new Filter[selection.length]; for (int i = 0; i < selection.length; ++i) { String current = selection[i]; Filter filter = null; if (isInteger(current)) { filter = tryToCreateFilterFromIndex(session, current); } else if (startsWithParenthesis(current)) { filter = tryToCreateFilterFromLdapExpression(current); } else { filter = tryToCreateFilterFromPidContainingWildcards(current); } if (filter == null) { throw new Exception("Unable to handle selection argument " + current); } filters[i] = filter; } return filters; } private boolean isInteger(String possiblyAnInteger) { try { Integer.parseInt(possiblyAnInteger); } catch (NumberFormatException e) { return false; } return true; } private boolean startsWithParenthesis(String selection) { return selection.startsWith("("); } private Filter tryToCreateFilterFromIndex(Session session, String index) throws Exception { String pid = getPidWithIndexInLastList(session, index); return tryToCreateFilterFromPidContainingWildcards(pid); } private Filter tryToCreateFilterFromPidContainingWildcards( String pidContainingWildcards) throws Exception { return tryToCreateFilterFromLdapExpression("(" + Constants.SERVICE_PID + "=" + pidContainingWildcards + ")"); } private Filter tryToCreateFilterFromLdapExpression(String ldapExpression) throws Exception { return bc.createFilter(ldapExpression); } private String getPidWithIndexInLastList(Session session, String index) throws Exception { Configuration[] cs = (Configuration[]) session.getProperties().get( LISTED_CONFIGS); if (cs == null) { throw new Exception( "The 'list' command has not been used yet to create a list."); } if (cs.length == 0) { throw new Exception( "No configurations listed by latest 'list' call."); } int i = Integer.parseInt(index); if (i < 0 || cs.length <= i) { throw new Exception("Invalid index." + ((cs.length == 1) ? "0 is the only valid index." : ("Valid indices are 0 to " + (cs.length - 1)))); } String pid = cs[i].getPid(); if (pid == null) { throw new Exception("Unable to retrieve pid with index " + index + " from last 'list'."); } return pid; } private Configuration[] getConfigurationsMatchingFilters( ConfigurationAdmin cm, Filter[] filters) throws Exception { Configuration[] cs = cm.listConfigurations(null); if (cs == null || cs.length == 0) { return new Configuration[0]; } if (filters == null || filters.length == 0) { return cs; } Vector matching = new Vector(); for (int i = 0; i < cs.length; ++i) { for (int j = 0; j < filters.length; ++j) { if (filters[j].match(cs[i].getProperties())) { matching.addElement(cs[i]); break; } } } Configuration[] result = new Configuration[matching.size()]; matching.copyInto(result); return result; } /*************************************************************************** * Helper method that sets the editing dictionary of the current * configuration in the session.* **************************************************************************/ private void setEditingDict(Session session, Dictionary dict) { if (dict == null) session.getProperties().remove(EDITED); else session.getProperties().put(EDITED, dict); } private void printDictionary(PrintWriter out, Dictionary d) { String[] keyNames = new String[d.size()]; int i = 0; for (Enumeration keys = d.keys(); keys.hasMoreElements();) { keyNames[i++] = (String) keys.nextElement(); } Sort.sortStringArray(keyNames); for (i = 0; i < keyNames.length; i++) { out.print(" "); out.print(keyNames[i]); out.print(": "); printValue(out, d.get(keyNames[i])); out.println(); } } private void printValue(PrintWriter out, Object val) { if (val instanceof Vector) { Vector v = (Vector) val; out.print("{"); for (int i = 0; i < v.size(); i++) { if (i > 0) out.print(", "); printValue(out, v.elementAt(i)); } out.print("}"); } else if (val.getClass().isArray()) { int length = Array.getLength(val); out.print("["); for (int i = 0; i < length; i++) { if (i > 0) out.print(", "); printValue(out, Array.get(val, i)); } out.print("]"); } else { out.print(val.toString()); } } private Object stringToObjectOfClass(String str, Class c) { if (str == null) { return null; } Object o = null; try { if (c == null || c == String.class) { o = str; } else if (str.length() == 0) { // None of the other Classes can handle a zero length String o = null; } else if (c == Integer.class) { return new Integer(str); } else if (c == Long.class) { o = new Long(str); } else if (c == Float.class) { o = new Float(str); } else if (c == Double.class) { o = new Double(str); } else if (c == Byte.class) { o = new Byte(str); } else if (c == Short.class) { o = new Short(str); } else if (c == BigInteger.class) { o = new BigInteger(str); } else if (classBigDecimal != null && c == classBigDecimal) { if (consBigDecimal != null) { o = consBigDecimal.newInstance(new Object[] { str }); } else { o = null; } } else if (c == Character.class) { o = new Character(str.charAt(0)); } else if (c == Boolean.class) { o = new Boolean(str); } else { o = null; } } catch (Exception ignored) { o = null; } return o; } Object createValue(String type, String def) { def = def.equals("") ? null : def; if (type.equals("String")) { return def == null ? new String() : new String(def); } else if (type.equals("Integer")) { return def == null ? new Integer(0) : new Integer(def); } else if (type.equals("Long")) { return def == null ? new Long(0) : new Long(def); } else if (type.equals("Float")) { return def == null ? new Float(0) : new Float(def); } else if (type.equals("Double")) { return def == null ? new Double(0) : new Double(def); } else if (type.equals("Byte")) { return def == null ? new Byte("0") : new Byte(def); } else if (type.equals("Short")) { return def == null ? new Short("0") : new Short(def); } else if (type.equals("BigInteger")) { return def == null ? new BigInteger("0") : new BigInteger(def); } else if (type.equals("BigDecimal")) { Object o = null; if (classBigDecimal != null && consBigDecimal != null) { def = def == null ? "0" : def; try { o = consBigDecimal.newInstance(new Object[] { def }); } catch (Exception ignored) { o = null; } } return o; } else if (type.equals("Character")) { return def == null ? new Character('a') : new Character(def .charAt(0)); } else if (type.equals("Boolean")) { return def == null ? new Boolean(false) : new Boolean(def); } else { // Unsupported type return null; } } // //////////////// private static void sortConfigurationArray(Configuration[] a) { sortConfigurationArray(a, 0, a.length); } private static void sortConfigurationArray(Configuration[] a, int fromIndex, int toIndex) { int middle; if (a == null) return; if (fromIndex + 1 < toIndex) { middle = (fromIndex + toIndex) / 2; sortConfigurationArray(a, fromIndex, middle); sortConfigurationArray(a, middle, toIndex); mergeConfigurationArray(a, fromIndex, toIndex); } } private static void mergeConfigurationArray(Configuration[] a, int fromIndex, int toIndex) { int i, j, k, middle, n; n = toIndex - fromIndex; Configuration[] b = new Configuration[n]; // temporary array k = 0; middle = (fromIndex + toIndex) / 2; // Copy lower half to array b for (i = fromIndex; i < middle; i++) b[k++] = a[i]; // Copy upper half to array b in oppsite order for (j = toIndex - 1; j >= middle; j--) b[k++] = a[j]; i = 0; j = n - 1; k = fromIndex; // Copy back next-greatest element at each time // until i and j cross while (i <= j) { if (b[i].getPid().compareTo(b[j].getPid()) < 0) a[k++] = b[i++]; else a[k++] = b[j--]; } } // ////////////////////////////////// public void serviceChanged(ServiceEvent event) { switch (event.getType()) { case ServiceEvent.REGISTERED: ServiceReference sr = event.getServiceReference(); if (refCA != sr) { refCA = sr; } break; case ServiceEvent.MODIFIED: break; case ServiceEvent.UNREGISTERING: if (refCA != null) { refCA = null; } break; default: break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -