### Eclipse Workspace Patch 1.0 #P karaf-trunk Index: scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java =================================================================== --- scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java (revision 1228952) +++ scr/command/src/main/java/org/apache/karaf/scr/command/action/ScrActionSupport.java (working copy) @@ -1,95 +1,91 @@ -/* - * 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.action; - -import org.apache.felix.scr.ScrService; -import org.apache.karaf.shell.console.AbstractAction; -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 { - - protected final Logger logger = LoggerFactory.getLogger(getClass().getName()); - - private ScrService scrService; - - @Override - protected Object doExecute() throws Exception { - if (scrService == null) { - String msg = "ScrService is unavailable"; - System.out.println(msg); - logger.warn(msg); - } else { - doScrAction(scrService); - } - return null; - } - - protected abstract Object doScrAction(ScrService scrService) - throws Exception; - - public String getPrettyString(String value, Ansi.Color color) { - return Ansi.ansi().fg(color).a(value).toString(); - } - - public String getPrettyBoldString(String value, Ansi.Color color) { - return Ansi.ansi().fg(color).a(Ansi.Attribute.INTENSITY_BOLD).a(value) - .a(Ansi.Attribute.INTENSITY_BOLD_OFF).toString(); - } - - public String buildRightPadBracketDisplay(String s, int max) { - return String.format("%1$-" + max + "s", s); - } - - public String buildLeftPadBracketDisplay(String s, int max) { - return String.format("%1$#" + max + "s", s); - } - - /** - * Get the bundleContext Object associated with this instance of - * ScrActionSupport. - * - * @return the bundleContext - */ - public BundleContext getBundleContext() { - return FrameworkUtil.getBundle(ListAction.class).getBundleContext(); - } - - /** - * Get the scrService Object associated with this instance of - * ScrActionSupport. - * - * @return the scrService - */ - public ScrService getScrService() { - return scrService; - } - - /** - * Sets the scrService Object for this ScrActionSupport instance. - * - * @param scrService - * the scrService to set - */ - public void setScrService(ScrService scrService) { - this.scrService = scrService; - } - -} +/* + * 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.action; + +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.AbstractAction; +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 { + + protected final Logger logger = LoggerFactory.getLogger(getClass().getName()); + + private ScrService scrService; + + @Override + protected Object doExecute() throws Exception { + if (scrService == null) { + String msg = "ScrService is unavailable"; + System.out.println(msg); + logger.warn(msg); + } else { + doScrAction(scrService); + } + return null; + } + + protected abstract Object doScrAction(ScrService scrService) + throws Exception; + + public String getBoldString(String value) { + return Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(value) + .a(Ansi.Attribute.INTENSITY_BOLD_OFF).toString(); + } + + public String buildRightPadBracketDisplay(String s, int max) { + return String.format("%1$-" + max + "s", s); + } + + public String buildLeftPadBracketDisplay(String s, int max) { + return String.format("%1$#" + max + "s", s); + } + + /** + * Get the bundleContext Object associated with this instance of + * ScrActionSupport. + * + * @return the bundleContext + */ + public BundleContext getBundleContext() { + return FrameworkUtil.getBundle(ListAction.class).getBundleContext(); + } + + /** + * Get the scrService Object associated with this instance of + * ScrActionSupport. + * + * @return the scrService + */ + public ScrService getScrService() { + return scrService; + } + + /** + * Sets the scrService Object for this ScrActionSupport instance. + * + * @param scrService + * the scrService to set + */ + public void setScrService(ScrService scrService) { + this.scrService = scrService; + } + +} Index: scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java =================================================================== --- scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java (revision 1228952) +++ scr/command/src/main/java/org/apache/karaf/scr/command/action/ListAction.java (working copy) @@ -1,59 +1,52 @@ -/* - * 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.action; - -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.fusesource.jansi.Ansi; - -/** - * Lists all the components currently installed. - */ -@Command( - scope = ScrCommandConstants.SCR_COMMAND, - name = ScrCommandConstants.LIST_FUNCTION, - description = "Displays a list of available components") -public class ListAction extends ScrActionSupport { - - @Override - protected Object doScrAction(ScrService scrService) throws Exception { - if(logger.isDebugEnabled()){ - logger.debug("Executing the List Action"); - } - System.out.println(getPrettyBoldString( - " ID State Component Name", - Ansi.Color.WHITE)); - 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(getPrettyString("[", Ansi.Color.WHITE) - + getPrettyString(id, Ansi.Color.YELLOW) - + getPrettyString("] [", Ansi.Color.WHITE) - + getPrettyString(state, Ansi.Color.YELLOW) - + getPrettyString("] ", Ansi.Color.WHITE) - + getPrettyString(name, Ansi.Color.YELLOW) - + Ansi.ansi().a(Ansi.Attribute.RESET).toString()); - } - return null; - } -} +/* + * 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.action; + +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.Command; + +/** + * Lists all the components currently installed. + */ +@Command( + scope = ScrCommandConstants.SCR_COMMAND, + name = ScrCommandConstants.LIST_FUNCTION, + description = "Displays a list of available components") +public class ListAction extends ScrActionSupport { + + @Override + protected Object doScrAction(ScrService scrService) throws Exception { + if(logger.isDebugEnabled()){ + logger.debug("Executing the List Action"); + } + 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); + } + return null; + } +} Index: scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java =================================================================== --- scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java (revision 1228952) +++ scr/command/src/main/java/org/apache/karaf/scr/command/action/DetailsAction.java (working copy) @@ -1,107 +1,103 @@ -/* - * 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.action; - -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.Reference; -import org.apache.felix.scr.ScrService; -import org.apache.karaf.scr.command.ScrCommandConstants; -import org.apache.karaf.scr.command.ScrUtils; -import org.fusesource.jansi.Ansi; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceReference; -import org.osgi.service.component.ComponentConstants; - -/** - * 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 { - - @Argument(index = 0, name = "name", description = "The name of the Component to display the detials of", required = true, multiValued = false) - String name; - - @Override - protected Object doScrAction(ScrService scrService) throws Exception { - if(logger.isDebugEnabled()){ - logger.debug("Executing the Details Action"); - } - System.out.println(getPrettyBoldString("Component Details", - Ansi.Color.WHITE)); - Component[] components = scrService.getComponents(name); - for (Component component : ScrUtils.emptyIfNull(Component.class, components)) { - printDetail(" Name : ", component.getName()); - printDetail(" State : ", ScrUtils.getState(component.getState())); - Reference[] references = component.getReferences(); - System.out.println(getPrettyBoldString("References", Ansi.Color.WHITE) - + Ansi.ansi().a(Ansi.Attribute.RESET).toString()); - - 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" )); - - // list bound services - 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 ) ); - - 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( ")" ); - } - printDetail(" Service Reference : ", b.toString()); - } - - if(ScrUtils.emptyIfNull(ServiceReference.class, boundRefs).length == 0) - { - printDetail(" Service Reference : ", "No Services bound"); - } - } - - } - - return null; - } - - - private void printDetail(String header, String value) { - System.out.print(getPrettyBoldString(header, Ansi.Color.WHITE)); - System.out.println(getPrettyString(value, Ansi.Color.YELLOW) - + Ansi.ansi().a(Ansi.Attribute.RESET).toString()); - } -} +/* + * 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.action; + +import org.apache.felix.scr.Component; +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.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.osgi.service.component.ComponentConstants; + +/** + * 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 { + + @Argument(index = 0, name = "name", description = "The name of the Component to display the detials of", required = true, multiValued = false) + String name; + + @SuppressWarnings("rawtypes") + @Override + protected Object doScrAction(ScrService scrService) throws Exception { + if(logger.isDebugEnabled()){ + logger.debug("Executing the Details Action"); + } + System.out.println(getBoldString("Component Details")); + Component[] components = scrService.getComponents(name); + for (Component component : ScrUtils.emptyIfNull(Component.class, components)) { + printDetail(" Name : ", component.getName()); + printDetail(" State : ", ScrUtils.getState(component.getState())); + Reference[] references = component.getReferences(); + System.out.println(getBoldString("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" )); + + // list bound services + 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 ) ); + + 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( ")" ); + } + printDetail(" Service Reference : ", b.toString()); + } + + if(ScrUtils.emptyIfNull(ServiceReference.class, boundRefs).length == 0) + { + printDetail(" Service Reference : ", "No Services bound"); + } + } + + } + + return null; + } + + + private void printDetail(String header, String value) { + System.out.println(getBoldString(header) + value); + } +}