You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2014/02/14 17:26:19 UTC
[1/2] git commit: [KARAF-2761] Java DSL for creating commands
Updated Branches:
refs/heads/master 22bae9241 -> 40140e4f0
[KARAF-2761] Java DSL for creating commands
Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/c159f2e0
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/c159f2e0
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/c159f2e0
Branch: refs/heads/master
Commit: c159f2e0fce101f6955de0d15f731eeb34ebf6e9
Parents: 22bae92
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Fri Feb 14 17:25:16 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Fri Feb 14 17:25:16 2014 +0100
----------------------------------------------------------------------
.../apache/karaf/shell/commands/Commands.java | 345 +++++++++++++++++++
1 file changed, 345 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf/blob/c159f2e0/shell/console/src/main/java/org/apache/karaf/shell/commands/Commands.java
----------------------------------------------------------------------
diff --git a/shell/console/src/main/java/org/apache/karaf/shell/commands/Commands.java b/shell/console/src/main/java/org/apache/karaf/shell/commands/Commands.java
new file mode 100644
index 0000000..b9e782f
--- /dev/null
+++ b/shell/console/src/main/java/org/apache/karaf/shell/commands/Commands.java
@@ -0,0 +1,345 @@
+/*
+ * 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.karaf.shell.commands;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.service.command.CommandProcessor;
+import org.apache.felix.service.command.Function;
+import org.apache.karaf.shell.commands.basic.AbstractCommand;
+import org.apache.karaf.shell.commands.converter.DefaultConverter;
+import org.apache.karaf.shell.commands.converter.GenericType;
+import org.apache.karaf.shell.commands.converter.ReifiedType;
+import org.apache.karaf.shell.console.CompletableFunction;
+import org.apache.karaf.shell.console.Completer;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ *
+ */
+public class Commands {
+
+ protected final List<CommandBuilder> commandBuilders = new ArrayList<CommandBuilder>();
+ protected final List<CompleterBuilder> completerBuilders = new ArrayList<CompleterBuilder>();
+
+ protected DefaultConverter converter;
+
+ protected BundleContext context;
+
+ public BundleContext getContext() {
+ if (context == null) {
+ context = FrameworkUtil.getBundle(getClass()).getBundleContext();
+ }
+ return context;
+ }
+
+ public void setContext(BundleContext context) {
+ this.context = context;
+ }
+
+ public DefaultConverter getConverter() {
+ if (converter == null) {
+ converter = new DefaultConverter(getContext().getBundle());
+ }
+ return converter;
+ }
+
+ public void setConverter(DefaultConverter converter) {
+ this.converter = converter;
+ }
+
+ public void register() {
+ for (CompleterBuilder builder : completerBuilders) {
+ builder.register();
+ }
+ for (CommandBuilder builder : commandBuilders) {
+ builder.register();
+ }
+ }
+
+ public void unregister() {
+ for (CommandBuilder commandBuilder : commandBuilders) {
+ commandBuilder.unregister();
+ }
+ for (CompleterBuilder builder : completerBuilders) {
+ builder.unregister();
+ }
+ }
+
+ public CommandBuilder command() {
+ CommandBuilder commandBuilder = new CommandBuilder();
+ commandBuilders.add(commandBuilder);
+ return commandBuilder;
+ }
+
+ public CommandBuilder command(Class<? extends Action> actionClass) {
+ return command().action(actionClass);
+ }
+
+ public CompleterBuilder completer(Completer completer) {
+ CompleterBuilder completerBuilder = new CompleterBuilder(completer);
+ completerBuilders.add(completerBuilder);
+ return completerBuilder;
+ }
+
+ public class CompleterBuilder {
+ protected Completer completer;
+ protected ServiceRegistration registration;
+
+ public CompleterBuilder(Completer completer) {
+ this.completer = completer;
+ }
+
+ public void register() {
+ if (registration == null) {
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ String[] classes = {
+ Completer.class.getName(),
+ completer.getClass().getName()
+ };
+ registration = getContext().registerService(classes, completer, props);
+ }
+ }
+
+ protected void unregister() {
+ if (registration != null) {
+ registration.unregister();
+ }
+ }
+
+ }
+
+ public class CommandBuilder {
+ protected Class<? extends Action> clazz;
+ protected final List<Object> arguments = new ArrayList<Object>();
+ protected final List<Object> properties = new ArrayList<Object>();
+ protected ServiceRegistration registration;
+ protected final List<Completer> argCompleters = new ArrayList<Completer>();
+ protected final Map<String, Completer> optionCompleters = new HashMap<String, Completer>();
+ protected final Map<String, String> serviceProperties = new HashMap<String, String>();
+
+ public CommandBuilder action(Class<? extends Action> actionClass) {
+ this.clazz = actionClass;
+ return this;
+ }
+
+ public CommandBuilder arguments(Object... arguments) {
+ this.arguments.addAll(Arrays.asList(arguments));
+ return this;
+ }
+
+ public CommandBuilder properties(Object... properties) {
+ this.properties.addAll(Arrays.asList(properties));
+ return this;
+ }
+
+ public CommandBuilder argCompleter(Completer completer) {
+ this.argCompleters.add(completer);
+ return this;
+ }
+
+ public CommandBuilder optionCompleter(String option, Completer completer) {
+ this.optionCompleters.put(option, completer);
+ return this;
+ }
+
+ public CommandBuilder serviceProp(String key, String val) {
+ this.serviceProperties.put(key, val);
+ return this;
+ }
+
+ public void register() {
+ if (registration == null) {
+ Command cmd = getCommand();
+ Hashtable<String, String> props = new Hashtable<String, String>();
+ props.put(CommandProcessor.COMMAND_SCOPE, cmd.scope());
+ props.put(CommandProcessor.COMMAND_FUNCTION, cmd.name());
+ props.putAll(serviceProperties);
+ String[] classes = {
+ Function.class.getName(),
+ CompletableFunction.class.getName(),
+ CommandWithAction.class.getName(),
+ AbstractCommand.class.getName()
+ };
+ registration = getContext().registerService(classes, new CommandWrapper(this), props);
+ }
+ }
+
+ protected void unregister() {
+ if (registration != null) {
+ registration.unregister();
+ }
+ }
+
+ private Action createNewAction() {
+ try {
+ Action action;
+ // Instantiate action
+ Map<Constructor, List<Object>> matches = findMatchingConstructors(clazz, arguments);
+ if (matches.size() == 1) {
+ Map.Entry<Constructor, List<Object>> match = matches.entrySet().iterator().next();
+ action = (Action) match.getKey().newInstance(match.getValue().toArray());
+ } else if (matches.size() == 0) {
+ throw new IllegalArgumentException("Unable to find a matching constructor on class " + clazz.getName() + " for arguments " + arguments + " when instanciating command " + getName());
+ } else {
+ throw new IllegalArgumentException("Multiple matching constructors found on class " + clazz + " for arguments " + arguments + " when instanciating bean " + getName() + ": " + matches.keySet());
+ }
+ // Inject action
+ for (Object prop : properties) {
+ Method setter = null;
+ for (Method method : clazz.getMethods()) {
+ if (method.getName().startsWith("set") && method.getReturnType() == void.class
+ && method.getParameterTypes().length == 1
+ && method.getParameterTypes()[0].isInstance(prop)) {
+ if (setter == null) {
+ setter = method;
+ } else {
+ throw new IllegalArgumentException("Property " + prop + " matches multiple setters on class " + clazz.getName());
+ }
+ }
+ }
+ if (setter == null) {
+ throw new IllegalArgumentException("Property " + prop + " has no matching setter on class " + clazz.getName());
+ }
+ setter.invoke(action, prop);
+ }
+ return action;
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to create action", e);
+ }
+ }
+
+ private String getName() {
+ Command cmd = getCommand();
+ return cmd.scope() + ":" + cmd.name();
+ }
+
+ private Command getCommand() {
+ Command cmd = clazz.getAnnotation(Command.class);
+ if (cmd == null)
+ {
+ throw new IllegalArgumentException("Action class is not annotated with @Command");
+ }
+ return cmd;
+ }
+
+ }
+
+ public static class CommandWrapper extends AbstractCommand implements CompletableFunction {
+ private final CommandBuilder builder;
+
+ public CommandWrapper(CommandBuilder builder) {
+ this.builder = builder;
+ }
+
+ @Override
+ public org.apache.felix.gogo.commands.Action createNewAction() {
+ return builder.createNewAction();
+ }
+
+ @Override
+ public List<Completer> getCompleters() {
+ return builder.argCompleters;
+ }
+
+ @Override
+ public Map<String, Completer> getOptionalCompleters() {
+ return builder.optionCompleters;
+ }
+ }
+
+ private Map<Constructor, List<Object>> findMatchingConstructors(Class type, List<Object> args) {
+ Map<Constructor, List<Object>> matches = new HashMap<Constructor, List<Object>>();
+ // Get constructors
+ List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(type.getConstructors()));
+ // Discard any signature with wrong cardinality
+ for (Iterator<Constructor> it = constructors.iterator(); it.hasNext();) {
+ if (it.next().getParameterTypes().length != args.size()) {
+ it.remove();
+ }
+ }
+ // Find a direct match with assignment
+ if (matches.size() != 1) {
+ Map<Constructor, List<Object>> nmatches = new HashMap<Constructor, List<Object>>();
+ for (Constructor cns : constructors) {
+ boolean found = true;
+ List<Object> match = new ArrayList<Object>();
+ for (int i = 0; i < args.size(); i++) {
+ ReifiedType argType = new GenericType(cns.getGenericParameterTypes()[i]);
+ //If the arg is an Unwrappered bean then we need to do the assignment check against the
+ //unwrappered bean itself.
+ Object arg = args.get(i);
+ Object argToTest = arg;
+ if (!DefaultConverter.isAssignable(argToTest, argType)) {
+ found = false;
+ break;
+ }
+ try {
+ match.add(getConverter().convert(arg, cns.getGenericParameterTypes()[i]));
+ } catch (Throwable t) {
+ found = false;
+ break;
+ }
+ }
+ if (found) {
+ nmatches.put(cns, match);
+ }
+ }
+ if (nmatches.size() > 0) {
+ matches = nmatches;
+ }
+ }
+ // Find a direct match with conversion
+ if (matches.size() != 1) {
+ Map<Constructor, List<Object>> nmatches = new HashMap<Constructor, List<Object>>();
+ for (Constructor cns : constructors) {
+ boolean found = true;
+ List<Object> match = new ArrayList<Object>();
+ for (int i = 0; i < args.size(); i++) {
+ ReifiedType argType = new GenericType(cns.getGenericParameterTypes()[i]);
+ try {
+ Object val = getConverter().convert(args.get(i), argType);
+ match.add(val);
+ } catch (Throwable t) {
+ found = false;
+ break;
+ }
+ }
+ if (found) {
+ nmatches.put(cns, match);
+ }
+ }
+ if (nmatches.size() > 0) {
+ matches = nmatches;
+ }
+ }
+ return matches;
+ }
+
+}
[2/2] git commit: [KARAF-2761] Use the Java DSL for scr:* commands
Posted by gn...@apache.org.
[KARAF-2761] Use the Java DSL for scr:* commands
Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/40140e4f
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/40140e4f
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/40140e4f
Branch: refs/heads/master
Commit: 40140e4f0c0745a41c1a4579f529f07e5819d785
Parents: c159f2e
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Fri Feb 14 17:26:03 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Fri Feb 14 17:26:03 2014 +0100
----------------------------------------------------------------------
.../scr/command/ActivateCommandComponent.java | 81 -------------
.../org/apache/karaf/scr/command/Commands.java | 84 +++++++++++++
.../scr/command/DeactivateCommandComponent.java | 81 -------------
.../scr/command/DetailsCommandComponent.java | 82 -------------
.../karaf/scr/command/ListCommandComponent.java | 76 ------------
.../karaf/scr/command/ScrCommandConstants.java | 4 -
.../karaf/scr/command/ScrCommandSupport.java | 118 -------------------
.../scr/command/action/ActivateAction.java | 3 +
.../scr/command/action/DeactivateAction.java | 3 +
.../karaf/scr/command/action/DetailsAction.java | 5 +-
.../karaf/scr/command/action/ListAction.java | 2 +-
.../scr/command/action/ScrActionSupport.java | 3 +-
12 files changed, 96 insertions(+), 446 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/ActivateCommandComponent.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/ActivateCommandComponent.java b/scr/command/src/main/java/org/apache/karaf/scr/command/ActivateCommandComponent.java
deleted file mode 100644
index d3b2607..0000000
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/ActivateCommandComponent.java
+++ /dev/null
@@ -1,81 +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.karaf.scr.command;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Action;
-import org.apache.felix.scr.ScrService;
-import org.apache.karaf.scr.command.action.ActivateAction;
-import org.apache.karaf.scr.command.completer.ActivateCompleter;
-import org.apache.karaf.shell.console.CompletableFunction;
-import org.apache.karaf.shell.console.Completer;
-
-import aQute.bnd.annotation.component.Activate;
-import aQute.bnd.annotation.component.Component;
-import aQute.bnd.annotation.component.Deactivate;
-import aQute.bnd.annotation.component.Reference;
-
-/**
- * Shell Command used to activate a Declarative Service Component.
- */
-@Component(
- provide=CompletableFunction.class,
- name = ActivateCommandComponent.COMPONENT_NAME,
- enabled = true,
- immediate = true,
- properties={
- ScrCommandConstants.OSGI_COMMAND_SCOPE_KEY + "=" + ScrCommandConstants.SCR_COMMAND,
- ScrCommandConstants.OSGI_COMMAND_FUNCTION_KEY + "=" + ScrCommandConstants.ACTIVATE_FUNCTION,
- ScrCommandConstants.HIDDEN_COMPONENT_KEY + "=true"
- })
-public class ActivateCommandComponent extends ScrCommandSupport {
-
- public static final String COMPONENT_NAME = "ActivateCommand";
-
- public static final String COMPONENT_LABEL = "Apache Karaf SCR Activate Command";
-
- @Override
- public Class<? extends Action> getActionClass() {
- return ActivateAction.class;
- }
-
- @Override
- public List<Class<? extends Completer>> getCompleterClasses() {
- List<Class<? extends Completer>> completers = new ArrayList<Class<? extends Completer>>();
- completers.add(ActivateCompleter.class);
- return completers;
- }
-
- @Activate
- public void activate(){
- logger.info("Activating the " + COMPONENT_LABEL);
- }
-
- @Deactivate
- public void deactivate(){
- logger.info("Deactivating the " + COMPONENT_LABEL);
- }
-
- @Reference
- @Override
- public void setScrService(ScrService scrService) {
- super.setScrService(scrService);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/Commands.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/Commands.java b/scr/command/src/main/java/org/apache/karaf/scr/command/Commands.java
new file mode 100644
index 0000000..70533e5
--- /dev/null
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/Commands.java
@@ -0,0 +1,84 @@
+/*
+ * 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.karaf.scr.command;
+
+import aQute.bnd.annotation.component.Activate;
+import aQute.bnd.annotation.component.Component;
+import aQute.bnd.annotation.component.Deactivate;
+import aQute.bnd.annotation.component.Reference;
+import org.apache.felix.scr.ScrService;
+import org.apache.karaf.scr.command.action.ActivateAction;
+import org.apache.karaf.scr.command.action.DeactivateAction;
+import org.apache.karaf.scr.command.action.DetailsAction;
+import org.apache.karaf.scr.command.action.ListAction;
+import org.apache.karaf.scr.command.completer.ActivateCompleter;
+import org.apache.karaf.scr.command.completer.DeactivateCompleter;
+import org.apache.karaf.scr.command.completer.DetailsCompleter;
+import org.apache.karaf.scr.command.completer.ScrCompleterSupport;
+import org.apache.karaf.shell.commands.Action;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ */
+@Component
+public class Commands extends org.apache.karaf.shell.commands.Commands {
+
+ protected final Logger logger = LoggerFactory.getLogger(getClass().getName());
+ protected ScrService scrService;
+
+ @Activate
+ public void activate() {
+ logger.info("Activating SCR commands");
+ completer(ActivateCompleter.class);
+ completer(DeactivateCompleter.class);
+ completer(DetailsCompleter.class);
+ command(ActivateAction.class);
+ command(DeactivateAction.class);
+ command(DetailsAction.class);
+ command(ListAction.class);
+ register();
+ }
+
+ @Deactivate
+ public void deactivate() {
+ logger.info("Deactivating SCR commands");
+ unregister();
+ }
+
+ @Reference
+ public void setScrService(ScrService scrService) {
+ this.scrService = scrService;
+ }
+
+ public CompleterBuilder completer(Class<? extends ScrCompleterSupport> completerClass) {
+ try {
+ ScrCompleterSupport completer = completerClass.newInstance();
+ completer.setScrService(scrService);
+ return completer(completer);
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to create completer", e);
+ }
+ }
+
+ public CommandBuilder command(Class<? extends Action> actionClass) {
+ return super.command(actionClass)
+ .properties(scrService)
+ .serviceProp(ScrCommandConstants.HIDDEN_COMPONENT_KEY, "true");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/DeactivateCommandComponent.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/DeactivateCommandComponent.java b/scr/command/src/main/java/org/apache/karaf/scr/command/DeactivateCommandComponent.java
deleted file mode 100644
index ae2bf9f..0000000
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/DeactivateCommandComponent.java
+++ /dev/null
@@ -1,81 +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.karaf.scr.command;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Action;
-import org.apache.felix.scr.ScrService;
-import org.apache.karaf.scr.command.action.DeactivateAction;
-import org.apache.karaf.scr.command.completer.DeactivateCompleter;
-import org.apache.karaf.shell.console.CompletableFunction;
-import org.apache.karaf.shell.console.Completer;
-
-import aQute.bnd.annotation.component.Activate;
-import aQute.bnd.annotation.component.Component;
-import aQute.bnd.annotation.component.Deactivate;
-import aQute.bnd.annotation.component.Reference;
-
-/**
- * Shell Command used to deactivate a Declarative Service Component.
- */
-@Component(
- provide=CompletableFunction.class,
- name = DeactivateCommandComponent.COMPONENT_NAME,
- enabled = true,
- immediate = true,
- properties={
- ScrCommandConstants.OSGI_COMMAND_SCOPE_KEY + "=" + ScrCommandConstants.SCR_COMMAND,
- ScrCommandConstants.OSGI_COMMAND_FUNCTION_KEY + "=" + ScrCommandConstants.DEACTIVATE_FUNCTION,
- ScrCommandConstants.HIDDEN_COMPONENT_KEY + "=true"
- })
-public class DeactivateCommandComponent extends ScrCommandSupport {
-
- public static final String COMPONENT_NAME = "DeactivateCommand";
-
- public static final String COMPONENT_LABEL = "Apache Karaf SCR Deactivate Command";
-
- @Override
- public Class<? extends Action> getActionClass() {
- return DeactivateAction.class;
- }
-
- @Override
- public List<Class<? extends Completer>> getCompleterClasses() {
- List<Class<? extends Completer>> completers = new ArrayList<Class<? extends Completer>>();
- completers.add(DeactivateCompleter.class);
- return completers;
- }
-
- @Activate
- public void activate(){
- logger.info("Activating the " + COMPONENT_LABEL);
- }
-
- @Deactivate
- public void deactivate(){
- logger.info("Deactivating the " + COMPONENT_LABEL);
- }
-
- @Reference
- @Override
- public void setScrService(ScrService scrService) {
- super.setScrService(scrService);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/DetailsCommandComponent.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/DetailsCommandComponent.java b/scr/command/src/main/java/org/apache/karaf/scr/command/DetailsCommandComponent.java
deleted file mode 100644
index 51407b0..0000000
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/DetailsCommandComponent.java
+++ /dev/null
@@ -1,82 +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.karaf.scr.command;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Action;
-import org.apache.felix.scr.ScrService;
-import org.apache.karaf.scr.command.action.DetailsAction;
-import org.apache.karaf.scr.command.completer.DetailsCompleter;
-import org.apache.karaf.shell.console.CompletableFunction;
-import org.apache.karaf.shell.console.Completer;
-
-import aQute.bnd.annotation.component.Activate;
-import aQute.bnd.annotation.component.Component;
-import aQute.bnd.annotation.component.Deactivate;
-import aQute.bnd.annotation.component.Reference;
-
-/**
- * Shell Command that prints the current state details of a given
- * Declarative Service Component.
- */
-@Component(
- provide=CompletableFunction.class,
- name = DetailsCommandComponent.COMPONENT_NAME,
- enabled = true,
- immediate = true,
- properties={
- ScrCommandConstants.OSGI_COMMAND_SCOPE_KEY + "=" + ScrCommandConstants.SCR_COMMAND,
- ScrCommandConstants.OSGI_COMMAND_FUNCTION_KEY + "=" + ScrCommandConstants.DETAILS_FUNCTION,
- ScrCommandConstants.HIDDEN_COMPONENT_KEY + "=true"
- })
-public class DetailsCommandComponent extends ScrCommandSupport {
-
- public static final String COMPONENT_NAME = "DetailsCommand";
-
- public static final String COMPONENT_LABEL = "Apache Karaf SCR Details Command";
-
- @Override
- public Class<? extends Action> getActionClass() {
- return DetailsAction.class;
- }
-
- @Override
- public List<Class<? extends Completer>> getCompleterClasses() {
- List<Class<? extends Completer>> completers = new ArrayList<Class<? extends Completer>>();
- completers.add(DetailsCompleter.class);
- return completers;
- }
-
- @Activate
- public void activate(){
- logger.info("Activating the " + COMPONENT_LABEL);
- }
-
- @Deactivate
- public void deactivate(){
- logger.info("Deactivating the " + COMPONENT_LABEL);
- }
-
- @Reference
- @Override
- public void setScrService(ScrService scrService) {
- super.setScrService(scrService);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/ListCommandComponent.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/ListCommandComponent.java b/scr/command/src/main/java/org/apache/karaf/scr/command/ListCommandComponent.java
deleted file mode 100644
index 23d82f3..0000000
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/ListCommandComponent.java
+++ /dev/null
@@ -1,76 +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.karaf.scr.command;
-
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Action;
-import org.apache.felix.scr.ScrService;
-import org.apache.karaf.scr.command.action.ListAction;
-import org.apache.karaf.shell.console.CompletableFunction;
-import org.apache.karaf.shell.console.Completer;
-
-import aQute.bnd.annotation.component.Activate;
-import aQute.bnd.annotation.component.Component;
-import aQute.bnd.annotation.component.Deactivate;
-import aQute.bnd.annotation.component.Reference;
-
-/**
- * Shell Command that lists the available Declarative Service Components.
- */
-@Component(
- provide=CompletableFunction.class,
- name = ListCommandComponent.COMPONENT_NAME,
- enabled = true,
- immediate = true,
- properties={
- ScrCommandConstants.OSGI_COMMAND_SCOPE_KEY + "=" + ScrCommandConstants.SCR_COMMAND,
- ScrCommandConstants.OSGI_COMMAND_FUNCTION_KEY + "=" + ScrCommandConstants.LIST_FUNCTION,
- ScrCommandConstants.HIDDEN_COMPONENT_KEY + "=true"
- })
-public class ListCommandComponent extends ScrCommandSupport {
-
- public static final String COMPONENT_NAME = "ListCommand";
- public static final String COMPONENT_LABEL = "Apache Karaf SCR List Command";
-
- @Override
- public Class<? extends Action> getActionClass() {
- return ListAction.class;
- }
-
- @Override
- public List<Class<? extends Completer>> getCompleterClasses() {
- return null;
- }
-
- @Activate
- public void activate(){
- logger.info("Activating the " + COMPONENT_LABEL);
- }
-
- @Deactivate
- public void deactivate(){
- logger.info("Deactivating the " + COMPONENT_LABEL);
- }
-
- @Reference
- @Override
- public void setScrService(ScrService scrService) {
- super.setScrService(scrService);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandConstants.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandConstants.java b/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandConstants.java
index d7f0d10..9faf449 100644
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandConstants.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandConstants.java
@@ -18,10 +18,6 @@ package org.apache.karaf.scr.command;
public class ScrCommandConstants {
- public static final String OSGI_COMMAND_SCOPE_KEY = "osgi.command.scope";
-
- public static final String OSGI_COMMAND_FUNCTION_KEY = "osgi.command.function";
-
public static final String HIDDEN_COMPONENT_KEY = "hidden.component";
public static final String SCR_COMMAND = "scr";
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandSupport.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandSupport.java b/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandSupport.java
deleted file mode 100644
index 34c4c7e..0000000
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrCommandSupport.java
+++ /dev/null
@@ -1,118 +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.karaf.scr.command;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.apache.karaf.shell.commands.Action;
-import org.apache.karaf.shell.commands.basic.AbstractCommand;
-import org.apache.felix.scr.ScrService;
-import org.apache.karaf.scr.command.action.ScrActionSupport;
-import org.apache.karaf.scr.command.completer.ScrCompleterSupport;
-import org.apache.karaf.shell.console.CompletableFunction;
-import org.apache.karaf.shell.console.Completer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public abstract class ScrCommandSupport extends AbstractCommand implements CompletableFunction {
-
- protected final Logger logger = LoggerFactory.getLogger(getClass().getName());
-
- private ScrService scrService;
- private ReadWriteLock lock = new ReentrantReadWriteLock();
-
- @Override
- public abstract Class<? extends Action> getActionClass();
-
- public abstract List<Class<? extends Completer>> getCompleterClasses();
-
- @Override
- public Action createNewAction() {
- try {
- lock.readLock().lock();
- ScrActionSupport action = (ScrActionSupport) getActionClass().newInstance();
- action.setScrService(getScrService());
- return action;
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } finally {
- lock.readLock().unlock();
- }
- }
-
- public List<Completer> getCompleters() {
- List<Completer> completers = null;
-
- if (getCompleterClasses() != null) {
- try {
- lock.readLock().lock();
- completers = new ArrayList<Completer>();
- for (Class<? extends Completer> completerClass : getCompleterClasses()) {
- ScrCompleterSupport ccs = (ScrCompleterSupport) completerClass.newInstance();
- ccs.setScrService(scrService);
- completers.add(ccs);
- }
-
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } finally {
- lock.readLock().unlock();
- }
- }
- return completers;
- }
-
- public Map<String, Completer> getOptionalCompleters() {
- return null;
- }
-
- /**
- * Returns the instance of ScrService for this instance of ScrCommandSupport.
- *
- * @return the ScrCommandSupport or null
- */
- public ScrService getScrService() {
- return scrService;
- }
-
- public void setScrService(ScrService scrService) {
- try {
- lock.writeLock().lock();
- this.scrService = scrService;
- } finally {
- lock.writeLock().unlock();
- }
- }
-
- public void unsetScrService(ScrService scrService) {
- try {
- lock.writeLock().lock();
- this.scrService = null;
- } finally {
- lock.writeLock().unlock();
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/action/ActivateAction.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ActivateAction.java b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ActivateAction.java
index eac8175..5e435d4 100755
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ActivateAction.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ActivateAction.java
@@ -16,12 +16,14 @@
*/
package org.apache.karaf.scr.command.action;
+import org.apache.karaf.scr.command.completer.ActivateCompleter;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.felix.scr.Component;
import org.apache.felix.scr.ScrService;
import org.apache.karaf.scr.command.ScrCommandConstants;
import org.apache.karaf.scr.command.ScrUtils;
+import org.apache.karaf.shell.commands.Completer;
/**
* Activates the given component by supplying its component name.
@@ -30,6 +32,7 @@ import org.apache.karaf.scr.command.ScrUtils;
public class ActivateAction extends ScrActionSupport {
@Argument(index = 0, name = "name", description = "The name of the Component to activate ", required = true, multiValued = false)
+ @Completer(ActivateCompleter.class)
String name;
@Override
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/action/DeactivateAction.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/action/DeactivateAction.java b/scr/command/src/main/java/org/apache/karaf/scr/command/action/DeactivateAction.java
index e6eba5f..e417459 100755
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/action/DeactivateAction.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/action/DeactivateAction.java
@@ -16,11 +16,13 @@
*/
package org.apache.karaf.scr.command.action;
+import org.apache.karaf.scr.command.completer.DeactivateCompleter;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.felix.scr.Component;
import org.apache.felix.scr.ScrService;
import org.apache.karaf.scr.command.ScrCommandConstants;
+import org.apache.karaf.shell.commands.Completer;
/**
* Deactivates the given component by supplying its component name.
@@ -29,6 +31,7 @@ import org.apache.karaf.scr.command.ScrCommandConstants;
public class DeactivateAction extends ScrActionSupport {
@Argument(index = 0, name = "name", description = "The name of the Component to deactivate ", required = true, multiValued = false)
+ @Completer(DeactivateCompleter.class)
String name;
@Override
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java b/scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java
index ccd92d5..39b462c 100644
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java
@@ -21,8 +21,10 @@ import org.apache.felix.scr.Reference;
import org.apache.felix.scr.ScrService;
import org.apache.karaf.scr.command.ScrCommandConstants;
import org.apache.karaf.scr.command.ScrUtils;
+import org.apache.karaf.scr.command.completer.DetailsCompleter;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Completer;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentConstants;
@@ -35,7 +37,8 @@ import java.util.Hashtable;
@Command(scope = ScrCommandConstants.SCR_COMMAND, name = ScrCommandConstants.DETAILS_FUNCTION, description = "Displays a list of available components")
public class DetailsAction extends ScrActionSupport {
- @Argument(index = 0, name = "name", description = "The name of the Component to display the detials of", required = true, multiValued = false)
+ @Argument(index = 0, name = "name", description = "The name of the Component to display the details of", required = true, multiValued = false)
+ @Completer(DetailsCompleter.class)
String name;
@SuppressWarnings("rawtypes")
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java
index 25686a1..da6b16e 100755
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java
@@ -31,8 +31,8 @@ import java.util.Arrays;
@Command(scope = ScrCommandConstants.SCR_COMMAND, name = ScrCommandConstants.LIST_FUNCTION, description = "Displays a list of available components")
public class ListAction extends ScrActionSupport {
-
private final IdComparator idComparator = new IdComparator();
+
@Override
protected Object doScrAction(ScrService scrService) throws Exception {
if (logger.isDebugEnabled()) {
http://git-wip-us.apache.org/repos/asf/karaf/blob/40140e4f/scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java
----------------------------------------------------------------------
diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java
index 0cf5e5a..0580dae 100755
--- a/scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java
+++ b/scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java
@@ -35,7 +35,7 @@ import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
-public abstract class ScrActionSupport extends SubShellAction {
+public abstract class ScrActionSupport extends AbstractAction {
@Option(name = ScrActionSupport.SHOW_ALL_OPTION, aliases = {ScrActionSupport.SHOW_ALL_ALIAS}, description = "Show all Components including the System Components (hidden by default)", required = false, multiValued = false)
boolean showHidden = false;
@@ -48,7 +48,6 @@ public abstract class ScrActionSupport extends SubShellAction {
private ScrService scrService;
public ScrActionSupport() {
- setSubShell("scr");
}
@Override