📄 cmcommands.java
字号:
/* * Copyright (c) 2003, KNOPFLERFISH project * 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 KNOPFLERFISH project 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. */package org.knopflerfish.bundle.cm.commands.impl;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.io.PushbackReader;import java.io.Reader;import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.math.BigInteger;import java.net.MalformedURLException;import java.net.URL;import java.security.AccessController;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import java.util.Dictionary;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.knopflerfish.service.console.CommandGroupAdapter;import org.knopflerfish.service.console.Session;import org.knopflerfish.shared.cm.CMDataReader;import org.knopflerfish.shared.cm.DictionaryUtils;import org.knopflerfish.util.sort.Sort;import org.osgi.framework.BundleContext;import org.osgi.framework.Constants;import org.osgi.framework.Filter;import org.osgi.framework.InvalidSyntaxException;import org.osgi.framework.ServiceEvent;import org.osgi.framework.ServiceListener;import org.osgi.framework.ServiceReference;import org.osgi.service.cm.Configuration;import org.osgi.service.cm.ConfigurationAdmin;// ******************** CMCommands ********************/** * * Console interface to the CM. * * * * @author Per Gustafson * * @version $Id: CMCommands.java,v 1.1.1.1 2004/03/05 20:34:54 wistrand Exp $ */public class CMCommands extends CommandGroupAdapter implements ServiceListener { /*************************************************************************** * Key in the session properties dictionary used to store the current (open) * configuration.* **************************************************************************/ private static final String CURRENT = "org.knopflerfish.bundle.cm.commands.impl.current"; /*************************************************************************** * Key in the session properties dictionary used to store the dictionary * that is edited for the current configuration.* **************************************************************************/ private static final String EDITED = "org.knopflerfish.bundle.cm.commands.impl.edited"; /*************************************************************************** * Key in the session properties dictionary used to store the * result of * the latest list command for later reference using -i options to several * commands. * **************************************************************************/ private static final String LISTED_CONFIGS = "org.knopflerfish.bundle.cm.commands.impl.listed.configs"; BundleContext bc; ServiceReference refCA = null; private static Class classBigDecimal; private static Constructor consBigDecimal; static { try { classBigDecimal = Class.forName("java.math.BigDecimal"); consBigDecimal = classBigDecimal .getConstructor(new Class[] { String.class }); } catch (Exception ignore) { classBigDecimal = null; consBigDecimal = null; } } public CMCommands(BundleContext bc) { super("configuration", "Configuration commands"); this.bc = bc; refCA = bc.getServiceReference(ConfigurationAdmin.class.getName()); try { bc.addServiceListener(this, "(objectClass=" + ConfigurationAdmin.class.getName() + ")"); } catch (InvalidSyntaxException ignored) { } } public final static String USAGE_LIST = "[<selection>] ..."; public final static String[] HELP_LIST = new String[] { "List the pids of existing configurations.", "<selection> A pid that can contain wildcards '*',", " or an ldap filter, or an index in output", " from the latest use of this command.", " If no selection is given all existing pids", " will be listed." }; public int cmdList(Dictionary opts, Reader in, PrintWriter out, Session session) { int retcode = 1; // 1 initially not set to 0 until end of try block ConfigurationAdmin srvCA = null; try { srvCA = getCA(); String[] selection = (String[]) opts.get("selection"); Configuration[] cs = null; if (selection == null) { cs = srvCA.listConfigurations(null); } else { cs = getConfigurations(session, srvCA, selection); } if (cs == null || cs.length == 0) { out.println("No configurations available"); } else { sortConfigurationArray(cs); setSessionProperty(session, LISTED_CONFIGS, cs); out.println("Available configurations:"); for (int i = 0; i < cs.length; ++i) { out.println("[" + i + "] " + cs[i].getPid()); } } retcode = 0; // Success! } catch (Exception e) { out.println("List failed. Details:"); String reason = e.getMessage(); out.println(reason == null ? "<unknown>" : reason); } finally { if (srvCA != null) { bc.ungetService(refCA); } } return retcode; } public final static String USAGE_SHOW = "[<selection>] ..."; public final static String[] HELP_SHOW = new String[] { "Show the saved versions of configurations.", "<selection> A pid that can contain wildcards '*',", " or an ldap filter, or an index in output", " from the latest use of the 'list' command.", " If no selection is given all configurations", " will be shown.", " Use 'current' command to see the properties", " of the currently edited configuration." }; public int cmdShow(Dictionary opts, Reader in, PrintWriter out, Session session) { ConfigurationAdmin srvCA = null; try { srvCA = getCA(); String[] selection = (String[]) opts.get("selection"); Configuration[] cs = getConfigurations(session, srvCA, selection); if (cs == null || cs.length == 0) { throw new Exception("No matching configurations for selection."); } for (int i = 0; i < cs.length; ++i) { if (i > 0) { out.println(); } Dictionary d = cs[i].getProperties(); if (d == null) { out.println("No properties set in " + cs[i].getPid()); } else { out.println("Properties for " + cs[i].getPid()); printDictionary(out, d); } } } catch (Exception e) { out.println("Show failed. Details:"); String reason = e.getMessage(); out.println(reason == null ? "<unknown>" : reason); } finally { if (srvCA != null) { bc.ungetService(refCA); } } return 0; } public final static String USAGE_CREATE = "[-f] <pid>"; public final static String[] HELP_CREATE = new String[] { "Create a configuration and open it for editing.", "-f If specified the pid argument is a factory pid.", "<pid> Pid or factory pid of configuration to create", " depending on if -f flag is specified." }; public int cmdCreate(Dictionary opts, Reader in, PrintWriter out, Session session) { int retcode = 1; // 1 initially not set to 0 until end of try block setCurrent(session, null); setEditingDict(session, null); ConfigurationAdmin srvCA = null; try { srvCA = getCA(); String pid = (String) opts.get("pid"); boolean createFactoryConfiguration = opts.get("-f") != null; Configuration cfg = null; if (createFactoryConfiguration) { cfg = srvCA.createFactoryConfiguration(pid, null); } else { cfg = srvCA.getConfiguration(pid, null); } if (cfg == null) { throw new Exception("Failed creating configuration for " + pid); } setCurrent(session, cfg); retcode = 0; // Success! } catch (Exception e) { out.println("Create failed. Details:"); String reason = e.getMessage(); out.println(reason == null ? "<unknown>" : reason); } finally { if (srvCA != null) { bc.ungetService(refCA); } } return retcode; } public final static String USAGE_DELETE = "<selection>"; public final static String[] HELP_DELETE = new String[] { "Delete an existing configuration.", "<selection> A pid that can contain wildcards '*',", " or an ldap filter, or an index in output", " from the latest use of the 'list' command.", " If the selection doesn't match exactly one", " configuration it will have to be refined." }; public int cmdDelete(Dictionary opts, Reader in, PrintWriter out, Session session) { int retcode = 1; // 1 initially not set to 0 until end of try block ConfigurationAdmin srvCA = null; try { String selection = (String) opts.get("selection"); srvCA = getCA(); Configuration[] cs = getConfigurations(session, srvCA, selection); if (cs == null || cs.length == 0) { throw new Exception( "Selection didn't match any configurations. " + "Change your selection to match exactly " + "one configuration."); } else if (cs.length == 1) { out.println("Deleting " + cs[0].getPid()); Configuration current = getCurrent(session); if (current != null && current.getPid().equals(cs[0].getPid())) { setCurrent(session, null); setEditingDict(session, null); } cs[0].delete(); } else { throw new Exception( "Selection matched " + cs.length + " configurations. " + "Refine your selection to match exactly one configuration."); } retcode = 0; // Success! } catch (Exception e) { out.println("Delete failed. Details:"); String reason = e.getMessage(); out.println(reason == null ? "<unknown>" : reason); } finally { if (srvCA != null) { bc.ungetService(refCA); } } return retcode; } public final static String USAGE_EDIT = "<selection>"; public final static String[] HELP_EDIT = new String[] { "Edit an existing configuration.", "<selection> A pid that can contain wildcards '*',", " or an ldap filter, or an index in output", " from the latest use of the 'list' command.", " If the selection doesn't match exactly one", " configuration it will have to be refined." }; public int cmdEdit(Dictionary opts, Reader in, PrintWriter out, Session session) { int retcode = 1; // 1 initially not set to 0 until end of try block setEditingDict(session, null); setCurrent(session, null); ConfigurationAdmin srvCA = null; try { String selection = (String) opts.get("selection"); srvCA = getCA(); Configuration[] cs = getConfigurations(session, srvCA, selection); if (cs == null || cs.length == 0) { throw new Exception( "Selection didn't match any configurations. " + "Use 'create' to create the configuration you want to edit " + "if it doesnt exist, or change your selection to match " + "exactly one configuration."); } else if (cs.length == 1) { out.println("Editing " + cs[0].getPid()); setCurrent(session, cs[0]); } else { throw new Exception( "Selection matched "
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -