From 2e19dcb3fa377bcbb15d431a49ad7d403eee3a68 Mon Sep 17 00:00:00 2001 From: Scott England-Sullivan Date: Wed, 12 Sep 2012 08:34:22 -0500 Subject: [PATCH 1/2] KARAF-1753 Add SCR Capabilities to Karaf 2.3.x --- .../standard/src/main/resources/features.xml | 7 + management/mbeans/pom.xml | 1 + management/mbeans/scr/NOTICE | 48 +++++ management/mbeans/scr/pom.xml | 103 +++++++++ .../management/mbeans/scr/ScrServiceMBean.java | 66 ++++++ .../mbeans/scr/internal/ScrServiceMBeanImpl.java | 220 ++++++++++++++++++++ .../scr/src/main/resources/OSGI-INF/bundle.info | 20 ++ pom.xml | 12 + shell/pom.xml | 1 + shell/scr/NOTICE | 48 +++++ shell/scr/pom.xml | 99 +++++++++ .../karaf/shell/scr/ActivateCommandComponent.java | 80 +++++++ .../shell/scr/DeactivateCommandComponent.java | 80 +++++++ .../karaf/shell/scr/DetailsCommandComponent.java | 81 +++++++ .../karaf/shell/scr/ListCommandComponent.java | 75 +++++++ .../karaf/shell/scr/ScrCommandConstants.java | 37 ++++ .../apache/karaf/shell/scr/ScrCommandSupport.java | 119 +++++++++++ .../java/org/apache/karaf/shell/scr/ScrUtils.java | 75 +++++++ .../karaf/shell/scr/action/ActivateAction.java | 54 +++++ .../karaf/shell/scr/action/DeactivateAction.java | 55 +++++ .../karaf/shell/scr/action/DetailsAction.java | 111 ++++++++++ .../apache/karaf/shell/scr/action/ListAction.java | 63 ++++++ .../karaf/shell/scr/action/ScrActionSupport.java | 150 +++++++++++++ .../shell/scr/completer/ActivateCompleter.java | 34 +++ .../shell/scr/completer/DeactivateCompleter.java | 34 +++ .../shell/scr/completer/DetailsCompleter.java | 34 +++ .../shell/scr/completer/ScrCompleterSupport.java | 92 ++++++++ 27 files changed, 1799 insertions(+), 0 deletions(-) create mode 100644 management/mbeans/scr/NOTICE create mode 100644 management/mbeans/scr/pom.xml create mode 100644 management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/ScrServiceMBean.java create mode 100644 management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/internal/ScrServiceMBeanImpl.java create mode 100644 management/mbeans/scr/src/main/resources/OSGI-INF/bundle.info create mode 100644 shell/scr/NOTICE create mode 100644 shell/scr/pom.xml create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/ActivateCommandComponent.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/DeactivateCommandComponent.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/DetailsCommandComponent.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/ListCommandComponent.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandConstants.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandSupport.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrUtils.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ActivateAction.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DeactivateAction.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DetailsAction.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ListAction.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ScrActionSupport.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ActivateCompleter.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DeactivateCompleter.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DetailsCompleter.java create mode 100644 shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ScrCompleterSupport.java diff --git a/assemblies/features/standard/src/main/resources/features.xml b/assemblies/features/standard/src/main/resources/features.xml index aeb5c43..e9e5357 100644 --- a/assemblies/features/standard/src/main/resources/features.xml +++ b/assemblies/features/standard/src/main/resources/features.xml @@ -290,4 +290,11 @@ mvn:org.apache.karaf.jaas/org.apache.karaf.jaas.jasypt/${project.version} + + eventadmin + mvn:org.apache.felix/org.apache.felix.metatype/${felix.metatype.version} + mvn:org.apache.felix/org.apache.felix.scr/${felix.scr.version} + mvn:org.apache.karaf.shell/org.apache.karaf.shell.scr/${project.version} + mvn:org.apache.karaf.management.mbeans/org.apache.karaf.management.mbeans.scr/${project.version} + diff --git a/management/mbeans/pom.xml b/management/mbeans/pom.xml index e6f444e..feac9cc 100644 --- a/management/mbeans/pom.xml +++ b/management/mbeans/pom.xml @@ -42,6 +42,7 @@ dev log obr + scr web diff --git a/management/mbeans/scr/NOTICE b/management/mbeans/scr/NOTICE new file mode 100644 index 0000000..13c1292 --- /dev/null +++ b/management/mbeans/scr/NOTICE @@ -0,0 +1,48 @@ +Apache Karaf +Copyright 2010-2012 The Apache Software Foundation + + +I. Included Software + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). +Licensed under the Apache License 2.0. + +This product includes software written by +Antony Lesuisse. +Licensed under Public Domain. + + +II. Used Software + +This product uses software developed at +The OSGi Alliance (http://www.osgi.org/). +Copyright (c) OSGi Alliance (2000, 2010). +Licensed under the Apache License 2.0. + +This product uses software developed at +OPS4J (http://www.ops4j.org/). +Licensed under the Apache License 2.0. + +This product uses software developed at +FUSE Source (http://www.fusesource.org/). +Licensed under the Apache License 2.0. + +This product uses software developed at +Tanuki Software (http://www.tanukisoftware.com/). +Licensed under the Apache License 2.0. + +This product uses software developed at +JLine (http://jline.sourceforge.net). +Licensed under the BSD License. + +This product uses software developed at +SLF4J (http://www.slf4j.org/). +Licensed under the MIT License. + +This product includes software from http://www.json.org. +Copyright (c) 2002 JSON.org + + +III. License Summary +- Apache License 2.0 diff --git a/management/mbeans/scr/pom.xml b/management/mbeans/scr/pom.xml new file mode 100644 index 0000000..227a321 --- /dev/null +++ b/management/mbeans/scr/pom.xml @@ -0,0 +1,103 @@ + + + + + + 4.0.0 + + + org.apache.karaf.management.mbeans + mbeans + 2.3.0-SNAPSHOT + ../pom.xml + + + org.apache.karaf.management.mbeans.scr + bundle + Apache Karaf :: Management :: MBeans :: Services + This bundle provides MBeans to manipulate Service Components via JMX. + + + ${basedir}/../../etc/appended-resources + + + + + org.slf4j + slf4j-api + provided + + + org.osgi + org.osgi.core + provided + + + org.apache.felix + org.apache.felix.scr + provided + + + biz.aQute + bndlib + provided + + + org.apache.karaf.management + org.apache.karaf.management.server + provided + + + + + + + ${project.basedir}/src/main/resources + + **/* + + + + ${project.basedir}/src/main/resources + true + + **/*.info + + + + + + org.apache.felix + maven-bundle-plugin + + + + org.apache.karaf.management.mbeans.scr + + + org.apache.karaf.management.mbeans.scr.internal + + * + + + + + + + diff --git a/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/ScrServiceMBean.java b/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/ScrServiceMBean.java new file mode 100644 index 0000000..ba04820 --- /dev/null +++ b/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/ScrServiceMBean.java @@ -0,0 +1,66 @@ +/* + * 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.management.mbeans.scr; + +/** + * 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/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/internal/ScrServiceMBeanImpl.java b/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/internal/ScrServiceMBeanImpl.java new file mode 100644 index 0000000..362117e --- /dev/null +++ b/management/mbeans/scr/src/main/java/org/apache/karaf/management/mbeans/scr/internal/ScrServiceMBeanImpl.java @@ -0,0 +1,220 @@ +/* + * 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.management.mbeans.scr.internal; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import javax.management.MBeanServer; +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.management.mbeans.scr.ScrServiceMBean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@aQute.bnd.annotation.component.Component(name = ScrServiceMBeanImpl.COMPONENT_NAME, enabled = true, immediate = 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"); + + public static final String COMPONENT_NAME = "ScrServiceMBean"; + + public static final String COMPONENT_LABEL = "Apache Karaf SCR Service MBean"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ScrServiceMBeanImpl.class); + + private MBeanServer mBeanServer; + + private ScrService scrService; + + private ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** + * Creates new Declarative Services mbean. + * + * @throws NotCompliantMBeanException + */ + public ScrServiceMBeanImpl() throws NotCompliantMBeanException { + super(ScrServiceMBean.class); + } + + + /** + * Service component activation call back. Called when all dependencies are + * satisfied. + * + * @throws Exception + */ + @Activate + public void activate() throws Exception { + LOGGER.info("Activating the " + COMPONENT_LABEL); + Map mbeans = new HashMap(); + mbeans.put(this, "org.apache.karaf:type=scr,name=${karaf.name}"); + try { + lock.writeLock().lock(); + if (mBeanServer != null) { + mBeanServer.registerMBean(this, new ObjectName(OBJECT_NAME)); + } + } catch (Exception e) { + LOGGER.error("Exception registering the SCR Management MBean: " + e.getLocalizedMessage(), e); + } finally { + lock.writeLock().unlock(); + } + } + + + /** + * Service component deactivation call back. Called after the component is + * in an active state when any dependencies become unsatisfied. + * + * @throws Exception + */ + @Deactivate + public void deactivate() throws Exception { + LOGGER.info("Deactivating the " + COMPONENT_LABEL); + try { + lock.writeLock().lock(); + if (mBeanServer != null) { + mBeanServer.unregisterMBean(new ObjectName(OBJECT_NAME)); + } + } finally { + lock.writeLock().unlock(); + } + } + + /* + * @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]; + for (int i = 0; i < componentNames.length; i++) { + componentNames[i] = components[i].getName(); + } + 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); + if (component != null) + state = component.getState(); + else + LOGGER.warn("No component found for name: " + componentName); + 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) + component.enable(); + else + 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) + component.disable(); + else + LOGGER.warn("No component found for name: " + componentName); + } + + private Component findComponent(String componentName) { + Component answer = null; + if (scrService.getComponents(componentName) != null) { + Component[] components = scrService.getComponents(componentName); + for (Component component : safe(components)) { + answer = component; + } + } + return answer; + } + + private Component[] safe(Component[] components) { + return components == null ? new Component[0] : components; + } + + /** + * @param mBeanServer the mBeanServer to set + */ + @Reference + public void setmBeanServer(MBeanServer mBeanServer) { + this.mBeanServer = mBeanServer; + } + + public void unsetmBeanServer(MBeanServer mBeanServer) { + this.mBeanServer = null; + } + + /** + * @param scrService the scrService to set + */ + @Reference + public void setScrService(ScrService scrService) { + this.scrService = scrService; + } + + public void unsetScrService(ScrService scrService) { + this.scrService = null; + } + +} diff --git a/management/mbeans/scr/src/main/resources/OSGI-INF/bundle.info b/management/mbeans/scr/src/main/resources/OSGI-INF/bundle.info new file mode 100644 index 0000000..e8eba83 --- /dev/null +++ b/management/mbeans/scr/src/main/resources/OSGI-INF/bundle.info @@ -0,0 +1,20 @@ +\u001B[1mSYNOPSIS\u001B[0m + ${project.name} + + ${project.description} + + Maven URL: + \u001B[33mmvn:${project.groupId}/${project.artifactId}/${project.version}\u001B[0m + +\u001B[1mDESCRIPTION\u001B[0m + This bundle provide MBean to manage Declarative Service Components in Karaf features via JMX. + + Especially, it provides the ScrServiceMBean supporting the following operations: + \u001B[36mlistComponents()\u001B[0m Gets the list of the currently available components. + \u001B[36mactivateComponent(String)\u001B[0m Activates a component for the given component name. + \u001B[36mdeactiveateComponent(String)\u001B[0m Deactivates a component for the given component name. + \u001B[36misComponentActive(String)\u001B[0m Check to see if a given component is in an ACTIVE state. + + +\u001B[1mSEE ALSO\u001B[0m + \u001B[36mDeclarative Services\u001B[0m and \u001B[Remote Management via JMX\u001B[0m sections of the Karaf User Guide. \ No newline at end of file diff --git a/pom.xml b/pom.xml index b21ac9c..c79f373 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,7 @@ 1.0_6 3.3.1_1 + 1.50.0 2.2.2_1 1.8.3 1.6 @@ -132,6 +133,7 @@ 1.2.14 1.0.2 1.0.2 + 1.6.0 1.0.0 1.0.0 1.0.0 @@ -582,6 +584,16 @@ org.apache.felix + org.apache.felix.scr + ${felix.scr.version} + + + biz.aQute + bndlib + ${bndlib.version} + + + org.apache.felix maven-bundle-plugin ${felix.plugin.version} diff --git a/shell/pom.xml b/shell/pom.xml index b9fcf3a..b472402 100644 --- a/shell/pom.xml +++ b/shell/pom.xml @@ -44,6 +44,7 @@ ssh wrapper dev + scr web diff --git a/shell/scr/NOTICE b/shell/scr/NOTICE new file mode 100644 index 0000000..13c1292 --- /dev/null +++ b/shell/scr/NOTICE @@ -0,0 +1,48 @@ +Apache Karaf +Copyright 2010-2012 The Apache Software Foundation + + +I. Included Software + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). +Licensed under the Apache License 2.0. + +This product includes software written by +Antony Lesuisse. +Licensed under Public Domain. + + +II. Used Software + +This product uses software developed at +The OSGi Alliance (http://www.osgi.org/). +Copyright (c) OSGi Alliance (2000, 2010). +Licensed under the Apache License 2.0. + +This product uses software developed at +OPS4J (http://www.ops4j.org/). +Licensed under the Apache License 2.0. + +This product uses software developed at +FUSE Source (http://www.fusesource.org/). +Licensed under the Apache License 2.0. + +This product uses software developed at +Tanuki Software (http://www.tanukisoftware.com/). +Licensed under the Apache License 2.0. + +This product uses software developed at +JLine (http://jline.sourceforge.net). +Licensed under the BSD License. + +This product uses software developed at +SLF4J (http://www.slf4j.org/). +Licensed under the MIT License. + +This product includes software from http://www.json.org. +Copyright (c) 2002 JSON.org + + +III. License Summary +- Apache License 2.0 diff --git a/shell/scr/pom.xml b/shell/scr/pom.xml new file mode 100644 index 0000000..0bd77ec --- /dev/null +++ b/shell/scr/pom.xml @@ -0,0 +1,99 @@ + + + + + + 4.0.0 + + org.apache.karaf.shell + shell + 2.3.0-SNAPSHOT + ../pom.xml + + + org.apache.karaf.shell.scr + + bundle + Apache Karaf :: Shell :: SCR Commands + Provides the OSGi SCR commands + + + ${basedir}/../../etc/appended-resources + + + + + org.slf4j + slf4j-api + provided + + + org.apache.karaf.shell + org.apache.karaf.shell.console + provided + + + org.osgi + org.osgi.core + provided + + + org.apache.felix + org.apache.felix.scr + + + biz.aQute + bndlib + provided + + + + + + + ${project.basedir}/src/main/resources + + **/* + + + + ${project.basedir}/src/main/resources + true + + **/*.info + + + + + + org.apache.felix + maven-bundle-plugin + true + + + * + + + + + + diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/ActivateCommandComponent.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ActivateCommandComponent.java new file mode 100644 index 0000000..0122ab5 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ActivateCommandComponent.java @@ -0,0 +1,80 @@ +/* + * 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.scr; + +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 java.util.ArrayList; +import java.util.List; + +import org.apache.felix.gogo.commands.Action; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.CompletableFunction; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.scr.action.ActivateAction; +import org.apache.karaf.shell.scr.completer.ActivateCompleter; + +/** + * 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 getActionClass() { + return ActivateAction.class; + } + + @Override + public List> getCompleterClasses() { + List> completers = new ArrayList>(); + 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/DeactivateCommandComponent.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/DeactivateCommandComponent.java new file mode 100644 index 0000000..e86631d --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/DeactivateCommandComponent.java @@ -0,0 +1,80 @@ +/* + * 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.scr; + +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 java.util.ArrayList; +import java.util.List; + +import org.apache.felix.gogo.commands.Action; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.CompletableFunction; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.scr.action.DeactivateAction; +import org.apache.karaf.shell.scr.completer.DeactivateCompleter; + +/** + * 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 getActionClass() { + return DeactivateAction.class; + } + + @Override + public List> getCompleterClasses() { + List> completers = new ArrayList>(); + 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/DetailsCommandComponent.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/DetailsCommandComponent.java new file mode 100644 index 0000000..83e3f71 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/DetailsCommandComponent.java @@ -0,0 +1,81 @@ +/* + * 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.scr; + +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 java.util.ArrayList; +import java.util.List; + +import org.apache.felix.gogo.commands.Action; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.CompletableFunction; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.scr.action.DetailsAction; +import org.apache.karaf.shell.scr.completer.DetailsCompleter; + +/** + * 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 getActionClass() { + return DetailsAction.class; + } + + @Override + public List> getCompleterClasses() { + List> completers = new ArrayList>(); + 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/ListCommandComponent.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ListCommandComponent.java new file mode 100644 index 0000000..44e86b2 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ListCommandComponent.java @@ -0,0 +1,75 @@ +/* + * 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.scr; + +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 java.util.List; + +import org.apache.felix.gogo.commands.Action; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.CompletableFunction; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.scr.action.ListAction; + +/** + * 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 getActionClass() { + return ListAction.class; + } + + @Override + public List> 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandConstants.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandConstants.java new file mode 100644 index 0000000..44840ac --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandConstants.java @@ -0,0 +1,37 @@ +/* + * 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.scr; + +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"; + + public static final String LIST_FUNCTION = "list"; + + public static final String ACTIVATE_FUNCTION = "activate"; + + public static final String DEACTIVATE_FUNCTION = "deactivate"; + + public static final String DETAILS_FUNCTION = "details"; + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandSupport.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandSupport.java new file mode 100644 index 0000000..404fd09 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrCommandSupport.java @@ -0,0 +1,119 @@ +/* + * 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.scr; + +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.felix.gogo.commands.Action; +import org.apache.felix.gogo.commands.basic.AbstractCommand; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.CompletableFunction; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.scr.action.ScrActionSupport; +import org.apache.karaf.shell.scr.completer.ScrCompleterSupport; +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 getActionClass(); + + public abstract List> 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 getCompleters() { + List completers = null; + + if (getCompleterClasses() != null) { + try { + lock.readLock().lock(); + completers = new ArrayList(); + for (Class 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 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(); + } + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrUtils.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrUtils.java new file mode 100644 index 0000000..1d25030 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/ScrUtils.java @@ -0,0 +1,75 @@ +/* + * 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.scr; + +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; + } + + public static String getState(int componentState) { + String retVal = null; + + switch (componentState) { + case Component.STATE_ACTIVE: + retVal = "ACTIVE"; + break; + case Component.STATE_ACTIVATING: + retVal = "ACTIVATING"; + break; + case Component.STATE_DEACTIVATING: + retVal = "DEACTIVATING"; + break; + case Component.STATE_DISABLED: + retVal = "DISABLED"; + break; + case Component.STATE_DISABLING: + retVal = "DISABLING"; + break; + case Component.STATE_DISPOSED: + retVal = "DISPOSED"; + break; + case Component.STATE_DISPOSING: + retVal = "DISPOSING"; + break; + case Component.STATE_ENABLING: + retVal = "ENABLING"; + break; + case Component.STATE_FACTORY: + retVal = "FACTORY"; + break; + case Component.STATE_REGISTERED: + retVal = "REGISTERED"; + break; + case Component.STATE_UNSATISFIED: + retVal = "UNSATISFIED"; + break; + + default: + break; + } + + return retVal; + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ActivateAction.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ActivateAction.java new file mode 100644 index 0000000..2b9a4ed --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ActivateAction.java @@ -0,0 +1,54 @@ +/* + * 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.scr.action; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.scr.Component; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.scr.ScrCommandConstants; +import org.apache.karaf.shell.scr.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 { + + @Argument(index = 0, + name = "name", + description = "The name of the Component to activate ", + required = true, + multiValued = false) + String name; + + @Override + protected Object doScrAction(ScrService scrService) throws Exception { + if (logger.isDebugEnabled()) { + logger.debug("Activate Action"); + logger.debug(" Activating the Component: " + name); + } + Component[] components = scrService.getComponents(name); + for (Component component : ScrUtils.emptyIfNull(Component.class, components)) { + component.enable(); + } + return null; + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DeactivateAction.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DeactivateAction.java new file mode 100644 index 0000000..2f19f3d --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DeactivateAction.java @@ -0,0 +1,55 @@ +/* + * 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.scr.action; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.scr.Component; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.scr.ScrCommandConstants; + +/** + * Deactivates the given component by supplying its component name. + */ +@Command(scope = ScrCommandConstants.SCR_COMMAND, + name = ScrCommandConstants.DEACTIVATE_FUNCTION, + description = "Deactivates a Component for the given name") +public class DeactivateAction extends ScrActionSupport { + + @Argument(index = 0, + name = "name", + description = "The name of the Component to deactivate ", + required = true, + multiValued = false) + String name; + + @Override + protected Object doScrAction(ScrService scrService) throws Exception { + if (logger.isDebugEnabled()) { + logger.debug("Deactivate Action"); + logger.debug(" Deactivating the Component: " + name); + } + Component[] components = scrService.getComponents(name); + if (components != null && components.length > 0) { + for (int i = 0; i < components.length; i++) { + components[i].disable(); + } + } + return null; + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DetailsAction.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DetailsAction.java new file mode 100644 index 0000000..70905e0 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/DetailsAction.java @@ -0,0 +1,111 @@ +/* + * 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.scr.action; + +import java.util.Hashtable; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.scr.Component; +import org.apache.felix.scr.Reference; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.scr.ScrCommandConstants; +import org.apache.karaf.shell.scr.ScrUtils; +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())); + + Hashtable props = (Hashtable)component.getProperties(); + if (!props.isEmpty()) { + printDetail(" Properties : ", ScrUtils.getState(component.getState())); + 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)) { + 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ListAction.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ListAction.java new file mode 100644 index 0000000..3d34844 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ListAction.java @@ -0,0 +1,63 @@ +/* + * 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.scr.action; + +import org.apache.felix.gogo.commands.Command; +import org.apache.felix.scr.Component; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.scr.ScrCommandConstants; +import org.apache.karaf.shell.scr.ScrUtils; + +/** + * 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)) { + 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/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ScrActionSupport.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ScrActionSupport.java new file mode 100644 index 0000000..39f5e97 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/action/ScrActionSupport.java @@ -0,0 +1,150 @@ +/* + * 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.scr.action; + +import java.util.Arrays; +import java.util.Hashtable; +import java.util.List; + +import org.apache.felix.gogo.commands.Option; +import org.apache.felix.scr.Component; +import org.apache.felix.scr.ScrService; +import org.apache.felix.service.command.CommandSession; +import org.apache.karaf.shell.console.AbstractAction; +import org.apache.karaf.shell.console.completer.ArgumentCompleter; +import org.apache.karaf.shell.console.jline.CommandSessionHolder; +import org.apache.karaf.shell.scr.ScrCommandConstants; +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 { + + @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 { + 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; + + 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(); + } + + 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; + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ActivateCompleter.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ActivateCompleter.java new file mode 100644 index 0000000..6a926e9 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ActivateCompleter.java @@ -0,0 +1,34 @@ +/* + * 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.scr.completer; + +import org.apache.felix.scr.Component; + +public class ActivateCompleter extends ScrCompleterSupport { + + /** + * 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); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DeactivateCompleter.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DeactivateCompleter.java new file mode 100644 index 0000000..e839e2c --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DeactivateCompleter.java @@ -0,0 +1,34 @@ +/* + * 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.scr.completer; + +import org.apache.felix.scr.Component; + +public class DeactivateCompleter extends ScrCompleterSupport { + + /** + * 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)); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DetailsCompleter.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DetailsCompleter.java new file mode 100644 index 0000000..df6ca35 --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/DetailsCompleter.java @@ -0,0 +1,34 @@ +/* + * 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.scr.completer; + +import org.apache.felix.scr.Component; + +public class DetailsCompleter extends ScrCompleterSupport { + + /** + * Overrides the super method noted below. See super documentation for + * details. + * + * @see org.apache.karaf.shell.scr.completer.ScrCompleterSupport#availableComponent(org.apache.felix.scr.Component) + */ + @Override + public boolean availableComponent(Component component) throws Exception { + return (component != null); + } + +} diff --git a/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ScrCompleterSupport.java b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ScrCompleterSupport.java new file mode 100644 index 0000000..5ea621f --- /dev/null +++ b/shell/scr/src/main/java/org/apache/karaf/shell/scr/completer/ScrCompleterSupport.java @@ -0,0 +1,92 @@ +/* + * 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.scr.completer; + +import java.util.List; + +import org.apache.felix.scr.Component; +import org.apache.felix.scr.ScrService; +import org.apache.karaf.shell.console.Completer; +import org.apache.karaf.shell.console.completer.StringsCompleter; +import org.apache.karaf.shell.scr.action.ScrActionSupport; +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) + */ + public int complete(String buffer, int cursor, List candidates) { + 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) { + logger.warn("Exception completing the command request: " + e.getLocalizedMessage()); + } + return delegate.complete(buffer, cursor, candidates); + } + + public abstract boolean availableComponent(Component component) throws Exception; + + /** + * Get the scrService Object associated with this instance of + * ScrCompleterSupport. + * + * @return the scrService + */ + public ScrService getScrService() { + return scrService; + } + + /** + * Sets the scrService Object for this ScrCompleterSupport instance. + * + * @param scrService the scrService to set + */ + public void setScrService(ScrService scrService) { + this.scrService = scrService; + } + +} -- 1.7.7.3