TAMAYA-297: Added, improved commands, improved formatting of table styled outputs.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/cdd839ce Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/cdd839ce Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/cdd839ce Branch: refs/heads/master Commit: cdd839ceb4ba73cee32f0cecb0175b26c8ca4cfd Parents: 9205386 Author: anatole <[email protected]> Authored: Tue Sep 19 12:57:10 2017 +0200 Committer: anatole <[email protected]> Committed: Tue Sep 19 12:57:10 2017 +0200 ---------------------------------------------------------------------- osgi/karaf-shell/pom.xml | 7 +- .../karaf/shell/DefaultDisableCommand.java | 3 +- .../karaf/shell/EvaluateCMConfigCommand.java | 63 ---------- .../tamaya/karaf/shell/GetPolicyCommand.java | 2 +- .../tamaya/karaf/shell/HistoryClearCommand.java | 69 +++++++++++ .../tamaya/karaf/shell/HistoryGetCommand.java | 116 +++++++++++++++++++ .../apache/tamaya/karaf/shell/InfoCommand.java | 4 +- .../tamaya/karaf/shell/PolicyGetCommand.java | 49 ++++++++ .../tamaya/karaf/shell/PolicySetCommand.java | 70 +++++++++++ .../tamaya/karaf/shell/PropertyCommand.java | 75 ------------ .../tamaya/karaf/shell/PropertyGetCommand.java | 86 ++++++++++++++ .../karaf/shell/PropertySourceCommand.java | 16 ++- .../karaf/shell/PropertySourcesCommand.java | 13 ++- .../tamaya/karaf/shell/SetPolicyCommand.java | 68 ----------- .../apache/tamaya/karaf/shell/StringUtil.java | 46 ++++++++ .../org/apache/tamaya/karaf/shell/commands | 15 ++- 16 files changed, 478 insertions(+), 224 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/pom.xml ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/pom.xml b/osgi/karaf-shell/pom.xml index cb857dc..e7c1558 100644 --- a/osgi/karaf-shell/pom.xml +++ b/osgi/karaf-shell/pom.xml @@ -29,7 +29,7 @@ <artifactId>tamaya-karaf-shell_alpha</artifactId> <packaging>jar</packaging> - <name>Apache Tamaya :: Karaf :: Shell</name> + <name>Apache Tamaya :: OSGI :: Karaf :: Shell</name> <description>Tamaya Karaf Shell Commands</description> <dependencies> @@ -44,6 +44,11 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.apache.karaf.shell</groupId> <artifactId>org.apache.karaf.shell.core</artifactId> <version>${dependency.karaf.version}</version> http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java index 0c14da5..99461dc 100644 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/DefaultDisableCommand.java @@ -28,6 +28,7 @@ import org.apache.karaf.shell.api.console.CommandLine; import org.apache.karaf.shell.api.console.Completer; import org.apache.karaf.shell.api.console.Session; import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.OperationMode; import org.apache.tamaya.osgi.TamayaConfigPlugin; import java.io.IOException; @@ -58,7 +59,7 @@ public class DefaultDisableCommand implements Action{ @Override public int complete(Session session, CommandLine commandLine, List<String> candidates) { StringsCompleter delegate = new StringsCompleter(); - for(TamayaConfigPlugin.OperationMode mode:TamayaConfigPlugin.OperationMode.values()) { + for(OperationMode mode: OperationMode.values()) { delegate.getStrings().add(mode.toString()); } return delegate.complete(session, commandLine, candidates); http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java deleted file mode 100644 index d22022a..0000000 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/EvaluateCMConfigCommand.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.karaf.shell; - -import org.apache.karaf.shell.api.action.Action; -import org.apache.karaf.shell.api.action.Argument; -import org.apache.karaf.shell.api.action.Command; -import org.apache.karaf.shell.api.action.lifecycle.Reference; -import org.apache.karaf.shell.api.action.lifecycle.Service; -import org.osgi.service.cm.Configuration; -import org.osgi.service.cm.ConfigurationAdmin; - -import java.io.IOException; - -@Command(scope = "tamaya", name = "cm-config", description="Show the current Tamaya configuration.") -@Service -public class EvaluateCMConfigCommand implements Action{ - - @Argument(index = 0, name = "pid", description = "The component's PID.", - required = true, multiValued = false) - String pid = null; - - @Argument(index = 1, name = "location", description = "The component's configuration location.", - required = false, multiValued = false) - String location = null; - - @Reference - private ConfigurationAdmin cm; - - public Object execute() throws IOException { - Configuration config = cm.getConfiguration(pid, location); - System.out.println("OSGI Configuration for PID: " + pid); - System.out.println("----------------------------------------------------------"); - if(config!=null){ - System.out.println("PID: " + config.getPid()); - System.out.println("Factory-PID: " + config.getFactoryPid()); - System.out.println("Location: " + config.getBundleLocation()); - System.out.println("Change Count: " + config.getChangeCount()); - System.out.println("Properties:"); - System.out.println(config.getProperties()); - }else{ - System.out.println("No OSGI Config found for PID: " + config.getPid()); - } - return null; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java index 3f1f1f0..42ebbd1 100644 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/GetPolicyCommand.java @@ -35,7 +35,7 @@ public class GetPolicyCommand implements Action{ @Override public Object execute() throws IOException { - System.out.println(this.configPlugin.getOperationMode()); + System.out.println(this.configPlugin.getDefaultOperationMode()); return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryClearCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryClearCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryClearCommand.java new file mode 100644 index 0000000..93f0dc2 --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryClearCommand.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.karaf.shell.api.action.*; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.ConfigHistory; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Command(scope = "tamaya", name = "history-delete", description="Deletes the history of changes Tamaya applied to the OSGI configuration.") +@Service +public class HistoryClearCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Argument(index = 0, name = "pid", description = "Allows to filter on the given PID.", + required = false, multiValued = false) + String pid; + + @Override + public Object execute() throws IOException { + int size = ConfigHistory.history(pid).size(); + ConfigHistory.clearHistory(pid); + System.out.println("Deleted entries: " + size); + return null; + } + + @Service + public static final class FilterCompleter implements Completer { + + @Override + public int complete(Session session, CommandLine commandLine, List<String> candidates) { + StringsCompleter delegate = new StringsCompleter(); + for(ConfigHistory.TaskType taskType:ConfigHistory.TaskType.values()) { + delegate.getStrings().add(taskType.toString()); + } + return delegate.complete(session, commandLine, candidates); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryGetCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryGetCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryGetCommand.java new file mode 100644 index 0000000..fd60c48 --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/HistoryGetCommand.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.karaf.shell.api.action.*; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.ConfigHistory; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Command(scope = "tamaya", name = "history-get", description="Gets the history of changes Tamaya applied to the OSGI configuration.") +@Service +public class HistoryGetCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Argument(index = 0, name = "pid", description = "Allows to filter on the given PID.", + required = false, multiValued = false) + String pid; + + @Option(name = "--type", aliases = "-t", description = "Allows to filter the events types shown.", + required = false, multiValued = true) + @Completion(FilterCompleter.class) + private String[] eventTypes; + + @Override + public Object execute() throws IOException { + List<ConfigHistory> history = ConfigHistory.history(pid); + history = filterTypes(history); + System.out.print(StringUtil.format("Typ", 10)); + System.out.print(StringUtil.format("PID", 30)); + System.out.print(StringUtil.format("Key", 30)); + System.out.print(StringUtil.format("Value", 40)); + System.out.println(StringUtil.format("Previous Value", 40)); + System.out.println(StringUtil.printRepeat("-", 140)); + for(ConfigHistory h:history){ + System.out.print(StringUtil.format(h.getType().toString(), 10)); + System.out.print(StringUtil.format(h.getPid(), 30)); + System.out.print(StringUtil.format(h.getKey(), 30)); + System.out.print(StringUtil.format(String.valueOf(h.getValue()), 40)); + System.out.println(String.valueOf(h.getPreviousValue())); + } + return null; + } + + private List<ConfigHistory> filterPid(List<ConfigHistory> history) { + if(pid==null){ + return history; + } + List<ConfigHistory> result = new ArrayList<>(); + for(ConfigHistory h:history){ + if(h.getPid().equals(pid)){ + result.add(h); + } + } + return result; + } + + private List<ConfigHistory> filterTypes(List<ConfigHistory> history) { + if(eventTypes==null){ + return history; + } + List<ConfigHistory> result = new ArrayList<>(); + Set<ConfigHistory.TaskType> types = new HashSet<>(); + for(String tVal:eventTypes) { + types.add(ConfigHistory.TaskType.valueOf(tVal)); + } + for(ConfigHistory h:history){ + if(types.contains(h.getType())){ + result.add(h); + } + } + return result; + } + + @Service + public static final class FilterCompleter implements Completer { + + @Override + public int complete(Session session, CommandLine commandLine, List<String> candidates) { + StringsCompleter delegate = new StringsCompleter(); + for(ConfigHistory.TaskType taskType:ConfigHistory.TaskType.values()) { + delegate.getStrings().add(taskType.toString()); + } + return delegate.complete(session, commandLine, candidates); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java index f9dafed..d50e97b 100644 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/InfoCommand.java @@ -38,8 +38,8 @@ public class InfoCommand implements Action { public Object execute() throws IOException { Configuration config = ConfigurationProvider.getConfiguration(); System.out.println(config.toString() + "\n\n" - + "OSGI OperationPolicy: " + configPlugin.getOperationMode() + '\n' - + "Default Disabled : " + configPlugin.isDefaultDisabled()); + + StringUtil.format("Default OperationMode:", 30) + configPlugin.getDefaultOperationMode() + '\n' + + StringUtil.format("Default Disabled: ", 30) + configPlugin.isDefaultDisabled()); return null; } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicyGetCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicyGetCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicyGetCommand.java new file mode 100644 index 0000000..350c802 --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicyGetCommand.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Completion; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; +import java.util.List; + +@Command(scope = "tamaya", name = "policy-get", description="Gets the current Tamaya operation policy.") +@Service +public class PolicyGetCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Override + public Object execute() throws IOException { + System.out.println(this.configPlugin.getDefaultOperationMode()); + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicySetCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicySetCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicySetCommand.java new file mode 100644 index 0000000..7e2af6a --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PolicySetCommand.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Completion; +import org.apache.karaf.shell.api.action.lifecycle.Reference; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.shell.api.console.CommandLine; +import org.apache.karaf.shell.api.console.Completer; +import org.apache.karaf.shell.api.console.Session; +import org.apache.karaf.shell.support.completers.StringsCompleter; +import org.apache.tamaya.osgi.OperationMode; +import org.apache.tamaya.osgi.TamayaConfigPlugin; + +import java.io.IOException; +import java.util.List; + +@Command(scope = "tamaya", name = "policy-set", description="Sets the current Tamaya operation policy.") +@Service +public class PolicySetCommand implements Action{ + + @Reference + private TamayaConfigPlugin configPlugin; + + @Argument(index = 0, name = "operationPolicy", description = "The operation policy how Tamaya intercepts OSGI configuration.", + required = true, multiValued = false) + @Completion(OperationModeCompleter.class) + String policy = null; + + @Override + public Object execute() throws IOException { + OperationMode opMode = OperationMode.valueOf(policy); + this.configPlugin.setDefaultOperationMode(opMode); + System.out.println("OperationMode="+opMode.toString()); + return null; + } + + @Service + public static final class OperationModeCompleter implements Completer { + + @Override + public int complete(Session session, CommandLine commandLine, List<String> candidates) { + StringsCompleter delegate = new StringsCompleter(); + for(OperationMode mode:OperationMode.values()) { + delegate.getStrings().add(mode.toString()); + } + return delegate.complete(session, commandLine, candidates); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java deleted file mode 100644 index 04c792e..0000000 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyCommand.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.karaf.shell; - -import org.apache.karaf.shell.api.action.Action; -import org.apache.karaf.shell.api.action.Argument; -import org.apache.karaf.shell.api.action.Command; -import org.apache.karaf.shell.api.action.Option; -import org.apache.karaf.shell.api.action.lifecycle.Service; -import org.apache.tamaya.Configuration; -import org.apache.tamaya.ConfigurationProvider; -import org.apache.tamaya.functions.ConfigurationFunctions; -import org.apache.tamaya.spi.PropertySource; -import org.apache.tamaya.spi.PropertyValue; - -import java.io.IOException; - -@Command(scope = "tamaya", name = "property", description="Show the current Tamaya configuration.") -@Service -public class PropertyCommand implements Action{ - - @Argument(index = 0, name = "key", description = "The target property source id.", - required = false, multiValued = false) - String key = null; - - @Option(name = "propertysource", aliases = "ps", description = "The target property source id.", - required = false, multiValued = false) - String propertysource = null; - - public Object execute() throws IOException { - Configuration config = ConfigurationProvider.getConfiguration(); - if(propertysource!=null){ - PropertySource ps = config.getContext().getPropertySource(propertysource); - if(ps==null){ - System.out.println("No such propertysource: " + propertysource); - }else { - PropertyValue val = ps.get(key); - if(val==null){ - System.out.println("PropertySource: " + propertysource + "\nUndefined key: " + key); - }else { - System.out.println("PropertySource: " + propertysource + "\n" + - ps.get(key)); - } - } - }else{ - for(PropertySource ps:config.getContext().getPropertySources()){ - PropertyValue val = ps.get(key); - if(val==null){ - System.out.println("PropertySource: " + propertysource + "\nUndefined key: " + key); - }else { - System.out.println("PropertySource: " + propertysource + "\n" + - ps.get(key)); - } - } - } - return null; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyGetCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyGetCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyGetCommand.java new file mode 100644 index 0000000..061bcfe --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertyGetCommand.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +import org.apache.commons.codec.binary.StringUtils; +import org.apache.karaf.shell.api.action.Action; +import org.apache.karaf.shell.api.action.Argument; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Service; +import org.apache.karaf.util.StringEscapeUtils; +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.apache.tamaya.functions.ConfigurationFunctions; +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.io.IOException; + +@Command(scope = "tamaya", name = "property-get", description="Get a Tamaya property.") +@Service +public class PropertyGetCommand implements Action{ + + @Argument(index = 0, name = "key", description = "The target property source id.", + required = false, multiValued = false) + String key = null; + + @Option(name="extended", aliases = "e", description = "Also print extended property value attributes.") + boolean extended; + + @Option(name = "propertysource", aliases = "ps", description = "The target property source id.", + required = false, multiValued = false) + String propertysource = null; + + public Object execute() throws IOException { + Configuration config = ConfigurationProvider.getConfiguration(); + if(propertysource!=null){ + PropertySource ps = config.getContext().getPropertySource(propertysource); + if(ps==null){ + System.out.println("ERR: No such propertysource: " + propertysource); + }else { + PropertyValue val = ps.get(key); + if(val==null){ + System.out.println("ERR: PropertySource: " + propertysource + " - undefined key: " + key); + }else { + if(extended) { + System.out.println(StringUtil.format("PropertySource", 25) + StringUtil.format("Value", 25)); + System.out.println(StringUtil.format(propertysource, 25) + StringUtil.format(val.getValue(), 55)); + }else{ + System.out.println(val.getValue()); + } + } + } + }else{ + System.out.println(StringUtil.format("PropertySource", 25) + StringUtil.format("Value", 25)); + for(PropertySource ps:config.getContext().getPropertySources()){ + PropertyValue val = ps.get(key); + if(val!=null){ + if(extended) { + System.out.println(StringUtil.format(propertysource, 25) + StringUtil.format(val.toString(), 55)); + }else{ + System.out.println(StringUtil.format(propertysource, 25) + StringUtil.format(val.getValue(), 55)); + } + } + } + } + return null; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourceCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourceCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourceCommand.java index fc42c54..c3e1c57 100644 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourceCommand.java +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourceCommand.java @@ -44,12 +44,20 @@ public class PropertySourceCommand implements Action{ if(ps==null){ System.out.println("No such propertysource: " + propertysource); }else { - System.out.println("ID: " + ps.getName()); - System.out.println("Ordinal: " + ps.getOrdinal()); - System.out.println("Class: " + ps.getClass().getName()); + System.out.println(StringUtil.format("ID:", 20) + ps.getName()); + System.out.println(StringUtil.format("Ordinal:", 20) + ps.getOrdinal()); + System.out.println(StringUtil.format("Class:", 20) + ps.getClass().getName()); System.out.println("Properties:"); + System.out.print(StringUtil.format(" Key", 20)); + System.out.print(StringUtil.format("Value", 20)); + System.out.print(StringUtil.format("Source", 20)); + System.out.println(StringUtil.format("Meta-Entries", 20)); + System.out.println(StringUtil.printRepeat("-", 80)); for(PropertyValue pv:ps.getProperties().values()) { - System.out.println(pv); + System.out.print(" " + StringUtil.format(pv.getKey(), 20)); + System.out.print(StringUtil.format(pv.getValue(), 20)); + System.out.print(StringUtil.format(pv.getSource(), 20)); + System.out.println(StringUtil.format(pv.getMetaEntries().toString(), 80)); } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourcesCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourcesCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourcesCommand.java index 74c5985..60289b9 100644 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourcesCommand.java +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/PropertySourcesCommand.java @@ -35,11 +35,16 @@ public class PropertySourcesCommand implements Action{ public Object execute() throws IOException { Configuration config = ConfigurationProvider.getConfiguration(); + System.out.print(StringUtil.format("ID", 20)); + System.out.print(StringUtil.format("Ordinal", 20)); + System.out.print(StringUtil.format("Class", 40)); + System.out.println(StringUtil.format("Property Count", 5)); + System.out.println(StringUtil.printRepeat("-", 80)); for(PropertySource ps:config.getContext().getPropertySources()){ - System.out.println("ID: " + ps.getName()); - System.out.println("Ordinal: " + ps.getOrdinal()); - System.out.println("Class: " + ps.getClass().getName()); - System.out.println("Property Count:" + ps.getProperties().size()); + System.out.print(StringUtil.format(ps.getName(), 20)); + System.out.print(StringUtil.format(String.valueOf(ps.getOrdinal()), 20)); + System.out.print(StringUtil.format(ps.getClass().getName(), 40)); + System.out.println(StringUtil.format(String.valueOf(ps.getProperties().size()), 5)); System.out.println("---"); } return null; http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/SetPolicyCommand.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/SetPolicyCommand.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/SetPolicyCommand.java deleted file mode 100644 index 0a22c81..0000000 --- a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/SetPolicyCommand.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.tamaya.karaf.shell; - -import org.apache.karaf.shell.api.action.Action; -import org.apache.karaf.shell.api.action.Argument; -import org.apache.karaf.shell.api.action.Command; -import org.apache.karaf.shell.api.action.Completion; -import org.apache.karaf.shell.api.action.lifecycle.Reference; -import org.apache.karaf.shell.api.action.lifecycle.Service; -import org.apache.karaf.shell.api.console.CommandLine; -import org.apache.karaf.shell.api.console.Completer; -import org.apache.karaf.shell.api.console.Session; -import org.apache.karaf.shell.support.completers.StringsCompleter; -import org.apache.tamaya.osgi.TamayaConfigPlugin; - -import java.io.IOException; -import java.util.List; - -@Command(scope = "tamaya", name = "set-policy", description="Sets the current Tamaya overriding policy.") -@Service -public class SetPolicyCommand implements Action{ - - @Reference - private TamayaConfigPlugin configPlugin; - - @Argument(index = 0, name = "operationPolicy", description = "The operation policy how Tamaya intercepts OSGI configuration.", - required = true, multiValued = false) - @Completion(OperationModeCompleter.class) - String policy = null; - - @Override - public Object execute() throws IOException { - TamayaConfigPlugin.OperationMode opMode = TamayaConfigPlugin.OperationMode.valueOf(policy); - this.configPlugin.setOperationMode(opMode); - return null; - } - - @Service - public static final class OperationModeCompleter implements Completer { - - @Override - public int complete(Session session, CommandLine commandLine, List<String> candidates) { - StringsCompleter delegate = new StringsCompleter(); - for(TamayaConfigPlugin.OperationMode mode:TamayaConfigPlugin.OperationMode.values()) { - delegate.getStrings().add(mode.toString()); - } - return delegate.complete(session, commandLine, candidates); - } - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/StringUtil.java ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/StringUtil.java b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/StringUtil.java new file mode 100644 index 0000000..f32144b --- /dev/null +++ b/osgi/karaf-shell/src/main/java/org/apache/tamaya/karaf/shell/StringUtil.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.karaf.shell; + +/** + * Some String related helper methods. + */ +final class StringUtil { + + private StringUtil(){} + + public static String format(String in, int length){ + if(in==null){ + in = ""; + } + int count = length - in.length(); + if(count<0){ + return in.substring(0,length-3) + "..."; + } + return in + printRepeat(" ", count); + } + + public static String printRepeat(String s, int times) { + StringBuilder b = new StringBuilder(); + for(int i=0;i<times;i++){ + b.append(s); + } + return b.toString(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/cdd839ce/osgi/karaf-shell/src/main/resources/META-INF/services/org/apache/tamaya/karaf/shell/commands ---------------------------------------------------------------------- diff --git a/osgi/karaf-shell/src/main/resources/META-INF/services/org/apache/tamaya/karaf/shell/commands b/osgi/karaf-shell/src/main/resources/META-INF/services/org/apache/tamaya/karaf/shell/commands index 7c9fb12..d8f2000 100644 --- a/osgi/karaf-shell/src/main/resources/META-INF/services/org/apache/tamaya/karaf/shell/commands +++ b/osgi/karaf-shell/src/main/resources/META-INF/services/org/apache/tamaya/karaf/shell/commands @@ -17,11 +17,16 @@ # under the License. # org.apache.tamaya.karaf.shell.ConfigCommand -org.apache.tamaya.karaf.shell.EvaluateCMConfigCommand +org.apache.tamaya.karaf.shell.DefaultDisableCommand +org.apache.tamaya.karaf.shell.GetPolicyCommand +org.apache.tamaya.karaf.shell.HistoryClearCommand +org.apache.tamaya.karaf.shell.HistoryGetCommand org.apache.tamaya.karaf.shell.InfoCommand -org.apache.tamaya.karaf.shell.PropertyCommand +org.apache.tamaya.karaf.shell.PolicyGetCommand +org.apache.tamaya.karaf.shell.PolicySetCommand +org.apache.tamaya.karaf.shell.PropertyGetCommand org.apache.tamaya.karaf.shell.PropertySourceCommand org.apache.tamaya.karaf.shell.PropertySourcesCommand -org.apache.tamaya.karaf.shell.SetPolicyCommand -org.apache.tamaya.karaf.shell.GetPolicyCommand -org.apache.tamaya.karaf.shell.DefaultDisableCommand + + +
