From d91dd9234623850ba252eeba64c2f276506c66b3 Mon Sep 17 00:00:00 2001 From: Scott England-Sullivan Date: Wed, 9 Jan 2013 09:51:10 -0600 Subject: [PATCH 1/4] Merge SCR hidden component code --- .../scr/command/ActivateCommandComponent.java | 11 +++- .../scr/command/DeactivateCommandComponent.java | 11 +++- .../karaf/scr/command/DetailsCommandComponent.java | 13 +++- .../karaf/scr/command/ListCommandComponent.java | 11 +++- .../karaf/scr/command/ScrCommandConstants.java | 2 + .../karaf/scr/command/ScrCommandSupport.java | 31 ++++---- .../org/apache/karaf/scr/command/ScrUtils.java | 4 +- .../karaf/scr/command/action/ActivateAction.java | 3 +- .../karaf/scr/command/action/DetailsAction.java | 62 +++++++++------- .../karaf/scr/command/action/ListAction.java | 29 +++++--- .../karaf/scr/command/action/ScrActionSupport.java | 74 +++++++++++++++++--- .../scr/command/completer/ActivateCompleter.java | 7 +- .../scr/command/completer/DeactivateCompleter.java | 10 +-- .../scr/command/completer/ScrCompleterSupport.java | 31 +++++++-- .../karaf/scr/management/ScrServiceMBean.java | 37 ++++++++++ .../management/internal/ScrServiceMBeanImpl.java | 55 ++++++++++++--- 16 files changed, 289 insertions(+), 102 deletions(-) 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 index 78a89f8..2b41b3a 100644 --- 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 @@ -34,8 +34,15 @@ 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}) +@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"; 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 index 1b6bbeb..88a4114 100644 --- 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 @@ -34,8 +34,15 @@ 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}) +@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"; 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 index 4bf83a0..4a9bab4 100644 --- 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 @@ -1,4 +1,4 @@ -/* + /* * 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. @@ -35,8 +35,15 @@ 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}) +@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"; 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 index a60e73f..8cf0339 100644 --- 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 @@ -32,8 +32,15 @@ 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}) +@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"; 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 882ad7a..d7f0d10 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 @@ -22,6 +22,8 @@ public class ScrCommandConstants { 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"; public static final String LIST_FUNCTION = "list"; 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 index 9dd860a..2ed1de1 100644 --- 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 @@ -33,7 +33,7 @@ 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; @@ -48,8 +48,7 @@ public abstract class ScrCommandSupport extends AbstractCommand implements Compl public Action createNewAction() { try { lock.readLock().lock(); - ScrActionSupport action = (ScrActionSupport) getActionClass() - .newInstance(); + ScrActionSupport action = (ScrActionSupport)getActionClass().newInstance(); action.setScrService(getScrService()); return action; } catch (InstantiationException e) { @@ -69,8 +68,7 @@ public abstract class ScrCommandSupport extends AbstractCommand implements Compl lock.readLock().lock(); completers = new ArrayList(); for (Class completerClass : getCompleterClasses()) { - ScrCompleterSupport ccs = (ScrCompleterSupport) completerClass - .newInstance(); + ScrCompleterSupport ccs = (ScrCompleterSupport)completerClass.newInstance(); ccs.setScrService(scrService); completers.add(ccs); } @@ -85,36 +83,37 @@ public abstract class ScrCommandSupport extends AbstractCommand implements Compl } return completers; } - + public Map getOptionalCompleters() { - return null; + return null; } - + /** - * Returns the instance of ScrService for this instance of ScrCommandSupport. - * + * 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) { + public void setScrService(ScrService scrService) { try { lock.writeLock().lock(); - this.scrService = scrService; + this.scrService = scrService; } finally { lock.writeLock().unlock(); } - } + } - public void unsetScrService(ScrService scrService) { + public void unsetScrService(ScrService scrService) { try { lock.writeLock().lock(); - this.scrService = null; + this.scrService = null; } finally { lock.writeLock().unlock(); } - } + } } diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrUtils.java b/scr/command/src/main/java/org/apache/karaf/scr/command/ScrUtils.java index 2904887..4c3dad6 100644 --- a/scr/command/src/main/java/org/apache/karaf/scr/command/ScrUtils.java +++ b/scr/command/src/main/java/org/apache/karaf/scr/command/ScrUtils.java @@ -21,10 +21,10 @@ import java.lang.reflect.Array; import org.apache.felix.scr.Component; public class ScrUtils { - + @SuppressWarnings("unchecked") public static T[] emptyIfNull(Class clazz, T[] objects) { - return objects == null ? (T[])Array.newInstance(clazz,0): objects; + return objects == null ? (T[])Array.newInstance(clazz, 0) : objects; } public static String getState(int componentState) { 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 b826a91..be6143f 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 @@ -25,7 +25,6 @@ import org.apache.karaf.scr.command.ScrUtils; /** * Activates the given component by supplying its component name. - * */ @Command(scope = ScrCommandConstants.SCR_COMMAND, name = ScrCommandConstants.ACTIVATE_FUNCTION, description = "Activates a Component for the given name") public class ActivateAction extends ScrActionSupport { @@ -35,7 +34,7 @@ public class ActivateAction extends ScrActionSupport { @Override protected Object doScrAction(ScrService scrService) throws Exception { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("Activate Action"); logger.debug(" Activating the Component: " + name); } 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 88148d0..6634a78 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 @@ -16,6 +16,8 @@ */ package org.apache.karaf.scr.command.action; +import java.util.Hashtable; + import org.apache.felix.scr.Component; import org.apache.felix.scr.Reference; import org.apache.felix.scr.ScrService; @@ -28,7 +30,8 @@ import org.osgi.framework.ServiceReference; import org.osgi.service.component.ComponentConstants; /** - * Displays the details associated with a given component by supplying its component name. + * Displays the details associated with a given component by supplying its + * component name. */ @Command(scope = ScrCommandConstants.SCR_COMMAND, name = ScrCommandConstants.DETAILS_FUNCTION, description = "Displays a list of available components") public class DetailsAction extends ScrActionSupport { @@ -37,9 +40,9 @@ public class DetailsAction extends ScrActionSupport { String name; @SuppressWarnings("rawtypes") - @Override + @Override protected Object doScrAction(ScrService scrService) throws Exception { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("Executing the Details Action"); } System.out.println(getBoldString("Component Details")); @@ -47,49 +50,54 @@ public class DetailsAction extends ScrActionSupport { for (Component component : ScrUtils.emptyIfNull(Component.class, components)) { printDetail(" Name : ", component.getName()); printDetail(" State : ", ScrUtils.getState(component.getState())); + + Hashtable props = (Hashtable)component.getProperties(); + if (!props.isEmpty()) { + System.out.println(getBoldString(" Properties : ")); + for (Object key : props.keySet()) { + Object value = props.get(key); + printDetail(" ", key + "=" + value); + } + } Reference[] references = component.getReferences(); System.out.println(getBoldString("References")); - for (Reference reference : ScrUtils.emptyIfNull(Reference.class,references)) { + for (Reference reference : ScrUtils.emptyIfNull(Reference.class, references)) { printDetail(" Reference : ", reference.getName()); - printDetail(" State : ", (reference.isSatisfied())?"satisfied":"unsatisfied"); - printDetail(" Multiple : ", (reference.isMultiple() ? "multiple" : "single" )); - printDetail(" Optional : ", (reference.isOptional() ? "optional" : "mandatory" )); - printDetail(" Policy : ", (reference.isStatic() ? "static" : "dynamic" )); + printDetail(" State : ", (reference.isSatisfied()) ? "satisfied" : "unsatisfied"); + printDetail(" Multiple : ", (reference.isMultiple() ? "multiple" : "single")); + printDetail(" Optional : ", (reference.isOptional() ? "optional" : "mandatory")); + printDetail(" Policy : ", (reference.isStatic() ? "static" : "dynamic")); // list bound services - ServiceReference[] boundRefs = reference.getServiceReferences(); + ServiceReference[] boundRefs = reference.getServiceReferences(); for (ServiceReference serviceReference : ScrUtils.emptyIfNull(ServiceReference.class, boundRefs)) { final StringBuffer b = new StringBuffer(); - b.append( "Bound Service ID " ); - b.append( serviceReference.getProperty( Constants.SERVICE_ID ) ); + b.append("Bound Service ID "); + b.append(serviceReference.getProperty(Constants.SERVICE_ID)); - String componentName = ( String ) serviceReference.getProperty( ComponentConstants.COMPONENT_NAME ); - if ( componentName == null ) - { - componentName = ( String ) serviceReference.getProperty( Constants.SERVICE_PID ); - if ( componentName == null ) - { - componentName = ( String ) serviceReference.getProperty( Constants.SERVICE_DESCRIPTION ); + String componentName = (String)serviceReference.getProperty(ComponentConstants.COMPONENT_NAME); + if (componentName == null) { + componentName = (String)serviceReference.getProperty(Constants.SERVICE_PID); + if (componentName == null) { + componentName = (String)serviceReference.getProperty(Constants.SERVICE_DESCRIPTION); } } - if ( componentName != null ) - { - b.append( " (" ); - b.append( componentName ); - b.append( ")" ); + if (componentName != null) { + b.append(" ("); + b.append(componentName); + b.append(")"); } printDetail(" Service Reference : ", b.toString()); } - - if(ScrUtils.emptyIfNull(ServiceReference.class, boundRefs).length == 0) - { + + if (ScrUtils.emptyIfNull(ServiceReference.class, boundRefs).length == 0) { printDetail(" Service Reference : ", "No Services bound"); } } } - + return null; } 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 169333a..44e6632 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 @@ -30,21 +30,32 @@ public class ListAction extends ScrActionSupport { @Override protected Object doScrAction(ScrService scrService) throws Exception { - if(logger.isDebugEnabled()){ + if (logger.isDebugEnabled()) { logger.debug("Executing the List Action"); } - System.out.println(getBoldString( - " ID State Component Name")); + System.out.println(getBoldString(" ID State Component Name")); Component[] components = scrService.getComponents(); for (Component component : ScrUtils.emptyIfNull(Component.class, components)) { - String name = component.getName(); - String id = buildLeftPadBracketDisplay(component.getId() + "", 4); - String state = buildRightPadBracketDisplay( - ScrUtils.getState(component.getState()), 16); - System.out.println( - "[" + id + "] [" + state + "] " + name); + if (showHidden) { + // We display all because we are overridden + printComponent(component); + } else { + if (ScrActionSupport.isHiddenComponent(component)) { + // do nothing + } else { + // We aren't hidden so print it + printComponent(component); + } + } } return null; } + private void printComponent(Component component) { + String name = component.getName(); + String id = buildLeftPadBracketDisplay(component.getId() + "", 4); + String state = buildRightPadBracketDisplay(ScrUtils.getState(component.getState()), 16); + System.out.println("[" + id + "] [" + state + "] " + name); + } + } 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 d05eaad..9a6071f 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 @@ -16,22 +16,39 @@ */ package org.apache.karaf.scr.command.action; +import java.util.Arrays; +import java.util.Hashtable; +import java.util.List; + +import org.apache.felix.scr.Component; import org.apache.felix.scr.ScrService; -import org.apache.karaf.shell.console.AbstractAction; +import org.apache.felix.service.command.CommandSession; +import org.apache.karaf.scr.command.ScrCommandConstants; +import org.apache.karaf.shell.commands.Option; +import org.apache.karaf.shell.console.CommandSessionHolder; +import org.apache.karaf.shell.console.SubShellAction; +import org.apache.karaf.shell.console.completer.ArgumentCompleter; import org.fusesource.jansi.Ansi; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class ScrActionSupport extends AbstractAction { +public abstract class ScrActionSupport extends SubShellAction { + @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; + + public static final String SHOW_ALL_OPTION = "-s"; + public static final String SHOW_ALL_ALIAS = "--show-hidden"; + protected final Logger logger = LoggerFactory.getLogger(getClass().getName()); private ScrService scrService; @Override - protected Object doExecute() throws Exception { + public Object doExecute() throws Exception { + super.doExecute(); if (scrService == null) { String msg = "ScrService is unavailable"; System.out.println(msg); @@ -42,12 +59,52 @@ public abstract class ScrActionSupport extends AbstractAction { return null; } - protected abstract Object doScrAction(ScrService scrService) - throws Exception; + protected abstract Object doScrAction(ScrService scrService) throws Exception; + + protected boolean isActionable(Component component) { + boolean answer = true; + + return answer; + } + + public static boolean showHiddenComponent(Component component) { + boolean answer = false; + + // First look to see if the show all options is there. + // If it is we set showAllFlag to true so the next + // section will skip + CommandSession commandSession = CommandSessionHolder.getSession(); + ArgumentCompleter.ArgumentList list = (ArgumentCompleter.ArgumentList)commandSession.get(ArgumentCompleter.ARGUMENTS_LIST); + if (list != null && list.getArguments() != null && list.getArguments().length > 0) { + List arguments = Arrays.asList(list.getArguments()); + if (arguments.contains(ScrActionSupport.SHOW_ALL_OPTION) || arguments.contains(ScrActionSupport.SHOW_ALL_ALIAS)) { + answer = true; + } + } + + return answer; + } + + @SuppressWarnings("rawtypes") + public static boolean isHiddenComponent(Component component) { + boolean answer = false; + + Hashtable properties = (Hashtable)component.getProperties(); + if (properties != null && properties.containsKey(ScrCommandConstants.HIDDEN_COMPONENT_KEY)) { + String value = (String)properties.get(ScrCommandConstants.HIDDEN_COMPONENT_KEY); + // If the value is false show the hidden + // then someone wants us to display the name + // of a hidden component + if (value != null && value.equals("true")) { + answer = true; + } + } + + return answer; + } public String getBoldString(String value) { - return Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(value) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF).toString(); + return Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(value).a(Ansi.Attribute.INTENSITY_BOLD_OFF).toString(); } public String buildRightPadBracketDisplay(String s, int max) { @@ -81,8 +138,7 @@ public abstract class ScrActionSupport extends AbstractAction { /** * Sets the scrService Object for this ScrActionSupport instance. * - * @param scrService - * the scrService to set + * @param scrService the scrService to set */ public void setScrService(ScrService scrService) { this.scrService = scrService; diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ActivateCompleter.java b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ActivateCompleter.java index 5c1ea64..fe2c91f 100644 --- a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ActivateCompleter.java +++ b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ActivateCompleter.java @@ -21,15 +21,14 @@ import org.apache.felix.scr.Component; public class ActivateCompleter extends ScrCompleterSupport { /** - * Overrides the super method noted below. See super documentation - * for details. + * Overrides the super method noted below. See super documentation for + * details. * * @see org.apache.karaf.scr.command.completer.ScrCompleterSupport#availableComponent(org.apache.felix.scr.Component) */ @Override public boolean availableComponent(Component component) throws Exception { - return (component != null - && component.getState() == Component.STATE_DISABLED); + return (component != null && component.getState() == Component.STATE_DISABLED); } } diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/DeactivateCompleter.java b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/DeactivateCompleter.java index 93e4c96..66fc7e3 100644 --- a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/DeactivateCompleter.java +++ b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/DeactivateCompleter.java @@ -21,18 +21,14 @@ import org.apache.felix.scr.Component; public class DeactivateCompleter extends ScrCompleterSupport { /** - * Overrides the super method noted below. See super documentation - * for details. + * Overrides the super method noted below. See super documentation for + * details. * * @see org.apache.karaf.scr.command.completer.ScrCompleterSupport#availableComponent(org.apache.felix.scr.Component) */ @Override public boolean availableComponent(Component component) throws Exception { - return (component != null - && ( - component.getState() == Component.STATE_ACTIVE || - component.getState() == Component.STATE_REGISTERED || - component.getState() == Component.STATE_UNSATISFIED)); + return (component != null && (component.getState() == Component.STATE_ACTIVE)); } } diff --git a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ScrCompleterSupport.java b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ScrCompleterSupport.java index 5f943a0..23db1fb 100755 --- a/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ScrCompleterSupport.java +++ b/scr/command/src/main/java/org/apache/karaf/scr/command/completer/ScrCompleterSupport.java @@ -20,17 +20,22 @@ import java.util.List; import org.apache.felix.scr.Component; import org.apache.felix.scr.ScrService; +import org.apache.karaf.scr.command.action.ScrActionSupport; import org.apache.karaf.shell.console.Completer; import org.apache.karaf.shell.console.completer.StringsCompleter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public abstract class ScrCompleterSupport implements Completer { + protected final transient Logger logger = LoggerFactory.getLogger(ScrCompleterSupport.class); + private ScrService scrService; /** * Overrides the super method noted below. See super documentation for * details. - * + * * @see org.apache.karaf.shell.console.Completer#complete(java.lang.String, * int, java.util.List) */ @@ -38,23 +43,37 @@ public abstract class ScrCompleterSupport implements Completer { StringsCompleter delegate = new StringsCompleter(); try { for (Component component : scrService.getComponents()) { + if (logger.isDebugEnabled()) { + logger.debug("Component Name to work on: " + component.getName()); + } + if (ScrActionSupport.showHiddenComponent(component)) { + // We display all because we are overridden + if (availableComponent(component)) { + delegate.getStrings().add(component.getName()); + } + } else { + if (ScrActionSupport.isHiddenComponent(component)) { + // do nothing + } else { + // We aren't hidden so print it if (availableComponent(component)) { delegate.getStrings().add(component.getName()); } } + } + } } catch (Exception e) { - // Ignore + logger.warn("Exception completing the command request: " + e.getLocalizedMessage()); } return delegate.complete(buffer, cursor, candidates); } - public abstract boolean availableComponent(Component component) - throws Exception; + public abstract boolean availableComponent(Component component) throws Exception; /** * Get the scrService Object associated with this instance of * ScrCompleterSupport. - * + * * @return the scrService */ public ScrService getScrService() { @@ -63,7 +82,7 @@ public abstract class ScrCompleterSupport implements Completer { /** * Sets the scrService Object for this ScrCompleterSupport instance. - * + * * @param scrService the scrService to set */ public void setScrService(ScrService scrService) { diff --git a/scr/management/src/main/java/org/apache/karaf/scr/management/ScrServiceMBean.java b/scr/management/src/main/java/org/apache/karaf/scr/management/ScrServiceMBean.java index 12392f5..87d12a4 100644 --- a/scr/management/src/main/java/org/apache/karaf/scr/management/ScrServiceMBean.java +++ b/scr/management/src/main/java/org/apache/karaf/scr/management/ScrServiceMBean.java @@ -16,14 +16,51 @@ */ package org.apache.karaf.scr.management; +/** + * The management interface for SCR Components. + */ public interface ScrServiceMBean { + + + /** + * Presents a {@ String} array of components currently registered with the SCR. + * + * @return String[] + * @throws Exception + */ String[] listComponents() throws Exception; + /** + * Verifies if the named component is currently in an ACTIVE state. + * + * @param componentName the components name + * @return true if ACTIVE, otherwise false + * @throws Exception + */ boolean isComponentActive(String componentName) throws Exception; + /** + * Returns the named components state + * + * @param componentName the components name + * @return + * @throws Exception + */ int componentState(String componentName) throws Exception; + /** + * Activates a component that is currently in a DISABLED state. + * + * @param componentName the components name + * @throws Exception + */ void activateComponent(String componentName) throws Exception; + /** + * Disables a component that is not in an ACTIVE state. + * + * @param componentName the components name + * @throws Exception + */ void deactiveateComponent(String componentName) throws Exception; } diff --git a/scr/management/src/main/java/org/apache/karaf/scr/management/internal/ScrServiceMBeanImpl.java b/scr/management/src/main/java/org/apache/karaf/scr/management/internal/ScrServiceMBeanImpl.java index 667676b..f233975 100644 --- a/scr/management/src/main/java/org/apache/karaf/scr/management/internal/ScrServiceMBeanImpl.java +++ b/scr/management/src/main/java/org/apache/karaf/scr/management/internal/ScrServiceMBeanImpl.java @@ -16,6 +16,10 @@ */ package org.apache.karaf.scr.management.internal; +import aQute.bnd.annotation.component.Activate; +import aQute.bnd.annotation.component.Deactivate; +import aQute.bnd.annotation.component.Reference; + import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; @@ -26,21 +30,17 @@ import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import javax.management.StandardMBean; -import aQute.bnd.annotation.component.Activate; -import aQute.bnd.annotation.component.Deactivate; -import aQute.bnd.annotation.component.Reference; - import org.apache.felix.scr.Component; import org.apache.felix.scr.ScrService; import org.apache.karaf.scr.management.ScrServiceMBean; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @aQute.bnd.annotation.component.Component( - name = ScrServiceMBeanImpl.COMPONENT_NAME, - enabled = true, - immediate = true) + name = ScrServiceMBeanImpl.COMPONENT_NAME, + enabled = true, + immediate = true, + properties={ "hidden.component=true"}) public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBean { public static final String OBJECT_NAME = "org.apache.karaf:type=scr,name=" + System.getProperty("karaf.name", "root"); @@ -67,7 +67,8 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea } /** - * Service component activation call back. Called when all dependencies are satisfied. + * Service component activation call back. Called when all dependencies are + * satisfied. * * @throws Exception */ @@ -89,8 +90,8 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea } /** - * Service component deactivation call back. Called after the component is in an active - * state when any dependencies become unsatisfied. + * Service component deactivation call back. Called after the component is + * in an active state when any dependencies become unsatisfied. * * @throws Exception */ @@ -107,6 +108,12 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea } } + /* + * @see org.apache.karaf.management.mbeans.scr.ScrServiceMBean#listComponents() + * + * @return + * @throws Exception + */ public String[] listComponents() throws Exception { Component[] components = safe(scrService.getComponents()); String[] componentNames = new String[components.length]; @@ -116,10 +123,24 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea return componentNames; } + /* + * @see org.apache.karaf.management.mbeans.scr.ScrServiceMBean#isComponentActive(java.lang.String) + * + * @param componentName + * @return + * @throws Exception + */ public boolean isComponentActive(String componentName) throws Exception { return (componentState(componentName) == Component.STATE_ACTIVE)?true:false; } + /* + * @see org.apache.karaf.management.mbeans.scr.ScrServiceMBean#componentState(java.lang.String) + * + * @param componentName + * @return + * @throws Exception + */ public int componentState(String componentName) throws Exception { int state = -1; final Component component = findComponent(componentName); @@ -130,6 +151,12 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea return state; } + /* + * @see org.apache.karaf.management.mbeans.scr.ScrServiceMBean#activateComponent(java.lang.String) + * + * @param componentName + * @throws Exception + */ public void activateComponent(String componentName) throws Exception { final Component component = findComponent(componentName); if(component != null) @@ -138,6 +165,12 @@ public class ScrServiceMBeanImpl extends StandardMBean implements ScrServiceMBea LOGGER.warn("No component found for name: " + componentName); } + /* + * @see org.apache.karaf.management.mbeans.scr.ScrServiceMBean#deactiveateComponent(java.lang.String) + * + * @param componentName + * @throws Exception + */ public void deactiveateComponent(String componentName) throws Exception { final Component component = findComponent(componentName); if(component != null) -- 1.7.7.3