Index: scr/management/pom.xml =================================================================== --- scr/management/pom.xml (revision 1336829) +++ scr/management/pom.xml (working copy) @@ -58,11 +58,6 @@ bndlib provided - - org.apache.karaf.management - org.apache.karaf.management.server - provided - @@ -90,6 +85,12 @@ org.apache.karaf.scr.management + + !org.apache.karaf.scr.management, + javax.management, + javax.management.loading, + * + org.apache.karaf.scr.management.internal Index: scr/management/src/main/java/org/apache/karaf/scr/management/ScrMBean.java =================================================================== --- scr/management/src/main/java/org/apache/karaf/scr/management/ScrMBean.java (revision 1336829) +++ scr/management/src/main/java/org/apache/karaf/scr/management/ScrMBean.java (working copy) @@ -16,14 +16,70 @@ */ package org.apache.karaf.scr.management; -public interface ScrsMBean { +/** + * The ScrMBean is a set of operations used to perform monitor and control + * operations on components registered with the OSGi Service Component Runtime + * (SCR). + * + */ +public interface ScrMBean { + /** + * Returns a list of components currently registered with the SCR. + * + * @return a String array + * @throws Exception + */ String[] listComponents() throws Exception; - + + /** + * An operation used to determine whether or not an SCR registered component + * is currently in an active state. + * + * @param componentName + * The name of the component + * @return true if the components state is active, otherwise false + * @throws Exception + */ boolean isComponentActive(String componentName) throws Exception; + /** + * Returns the raw component state value. + * + * @param componentName + * The name of the component + * @return the int value of the components state + * @throws Exception + */ + int componentState(String componentName) throws Exception; + + /** + * Activates a component that is currently not in an active state and only + * if the components required dependencies are satisfied. + * + * @param componentName + * The name of the component + * @throws Exception + */ void activateComponent(String componentName) throws Exception; + /** + * Deactivates a component that is currently in an active state. + * + * @param componentName + * The name of the component + * @throws Exception + */ void deactiveateComponent(String componentName) throws Exception; + /** + * An operation used to that returns the list of services this component has + * a dependency on. + * + * @param componentName + * The name of the component + * @return true if the components state is active, otherwise false + * @throws Exception + */ + String[] serviceDependencies(String componentName) throws Exception; } Index: scr/management/src/main/java/org/apache/karaf/scr/management/ScrsMBean.java =================================================================== --- scr/management/src/main/java/org/apache/karaf/scr/management/ScrsMBean.java (revision 1336829) +++ scr/management/src/main/java/org/apache/karaf/scr/management/ScrsMBean.java (working copy) @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.karaf.scr.management; - -public interface ScrsMBean { - - String[] listComponents() throws Exception; - - boolean isComponentActive(String componentName) throws Exception; - - void activateComponent(String componentName) throws Exception; - - void deactiveateComponent(String componentName) throws Exception; - -} Index: scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scr.java =================================================================== --- scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scr.java (revision 1336829) +++ scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scr.java (working copy) @@ -16,29 +16,115 @@ */ package org.apache.karaf.scr.management.internal; +import java.lang.reflect.Array; +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 org.apache.felix.scr.Component; +import org.apache.felix.scr.Reference; import org.apache.felix.scr.ScrService; -import org.apache.karaf.scr.management.ScrsMBean; +import org.apache.karaf.scr.management.ScrMBean; -public class Scrs extends StandardMBean implements ScrsMBean { +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * Implementation of the ScrMBean interface. The Service Component Runtime (SCR) + * is used to instantiate and manage this class. + * + */ +@aQute.bnd.annotation.component.Component( + name = Scr.COMPONENT_NAME, + enabled = true, + immediate = true) +public class Scr extends StandardMBean implements ScrMBean { + + 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(Scr.class); + + private MBeanServer mBeanServer; + private ScrService scrService; + + private ReadWriteLock lock = new ReentrantReadWriteLock(); /** * Creates new Declarative Services mbean. * * @throws NotCompliantMBeanException */ - public Scrs(ScrService scrService) throws NotCompliantMBeanException { - super(ScrsMBean.class); - this.scrService = scrService; + public Scr() throws NotCompliantMBeanException { + super(ScrMBean.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); + 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)); + } + } catch (Exception e) { + LOGGER.error("Exception unregistering the SCR Management MBean: " + + e.getLocalizedMessage(), e); + } finally { + lock.writeLock().unlock(); + } + } + + /* + * @see org.apache.karaf.scr.management.ScrMBean#listComponents() + * + * @return + * @throws Exception + */ public String[] listComponents() throws Exception { - Component[] components = safe(scrService.getComponents()); + Component[] components = emptyIfNull(Component.class, scrService.getComponents()); String[] componentNames = new String[components.length]; for (int i = 0; i < componentNames.length; i++) { componentNames[i] = components[i].getName(); @@ -46,35 +132,140 @@ return componentNames; } + /* + * @see org.apache.karaf.scr.management.ScrMBean#isComponentActive(java.lang.String) + * + * @param componentName + * @return + * @throws Exception + */ public boolean isComponentActive(String componentName) throws Exception { - boolean state = false; - Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - state = (component.getState() == Component.STATE_ACTIVE)?true:false; - } + return (componentState(componentName) == Component.STATE_ACTIVE) ? true + : false; + } + + /* + * @see org.apache.karaf.scr.management.ScrMBean#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.scr.management.ScrMBean#activateComponent(java.lang.String) + * + * @param componentName + * @throws Exception + */ public void activateComponent(String componentName) throws Exception { - if (scrService.getComponents(componentName) != null) { - Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - component.enable(); - } + final Component component = findComponent(componentName); + if (component != null) + component.enable(); + else + LOGGER.warn("No component found for name: " + componentName); + } + + /* + * @see org.apache.karaf.scr.management.ScrMBean#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); + } + + /* + * @see org.apache.karaf.scr.management.ScrMBean#serviceDependencies(java.lang.String) + * + * @param componentName + * @return + * @throws Exception + */ + @Override + public String[] serviceDependencies(String componentName) throws Exception { + final Component component = findComponent(componentName); + Reference[] references = emptyIfNull(Reference.class, component.getReferences()); + String[] referenceNames = new String[references.length]; + for (int i = 0; i < references.length; ++i) { + referenceNames[i] = references[i].getName(); } + return referenceNames; } - public void deactiveateComponent(String componentName) throws Exception { + /** + * Helper method that performs the search for the named component. + * + * @param componentName + * @return a Component or null + */ + private Component findComponent(String componentName) { + Component answer = null; if (scrService.getComponents(componentName) != null) { Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - component.disable(); + for (Component component : emptyIfNull(Component.class, components)) { + answer = component; } } + return answer; } + + /** + * Helper method to verify that we only operate on an array of components + * when using an iterator. The ScrService will return null when no + * components are found which breaks a foreach loop so we at least have an + * array of ZERO to operate on. + * + * @param components + * @return a Component[] + */ + @SuppressWarnings("unchecked") + private T[] emptyIfNull(Class clazz, T[] objects) { + return objects == null ? (T[])Array.newInstance(clazz,0): objects; + } - private Component[] safe( Component[] components ) { - return components == null ? new Component[0] : components; + /** + * Declarative service setter for the MBeanServer. + * + * @param mBeanServer + * the mBeanServer to set + */ + @aQute.bnd.annotation.component.Reference(name="MBeanServer") + public void setmBeanServer(MBeanServer mBeanServer) { + this.mBeanServer = mBeanServer; } + public void unsetmBeanServer(MBeanServer mBeanServer) { + this.mBeanServer = null; + } + + /** + * Declarative service setter for the ScrService. + * + * @param scrService + * the scrService to set + */ + @aQute.bnd.annotation.component.Reference(name="ScrService") + public void setScrService(ScrService scrService) { + this.scrService = scrService; + } + + public void unsetScrService(ScrService scrService) { + this.scrService = null; + } + } Index: scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scrs.java =================================================================== --- scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scrs.java (revision 1336829) +++ scr/management/src/main/java/org/apache/karaf/scr/management/internal/Scrs.java (working copy) @@ -1,80 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.karaf.scr.management.internal; - -import javax.management.NotCompliantMBeanException; -import javax.management.StandardMBean; - -import org.apache.felix.scr.Component; -import org.apache.felix.scr.ScrService; -import org.apache.karaf.scr.management.ScrsMBean; - -public class Scrs extends StandardMBean implements ScrsMBean { - - private ScrService scrService; - - /** - * Creates new Declarative Services mbean. - * - * @throws NotCompliantMBeanException - */ - public Scrs(ScrService scrService) throws NotCompliantMBeanException { - super(ScrsMBean.class); - this.scrService = scrService; - } - - 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; - } - - public boolean isComponentActive(String componentName) throws Exception { - boolean state = false; - Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - state = (component.getState() == Component.STATE_ACTIVE)?true:false; - } - return state; - } - - public void activateComponent(String componentName) throws Exception { - if (scrService.getComponents(componentName) != null) { - Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - component.enable(); - } - } - } - - public void deactiveateComponent(String componentName) throws Exception { - if (scrService.getComponents(componentName) != null) { - Component[] components = scrService.getComponents(componentName); - for (Component component : safe(components)) { - component.disable(); - } - } - } - - private Component[] safe( Component[] components ) { - return components == null ? new Component[0] : components; - } - -} Index: scr/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml =================================================================== --- scr/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml (revision 1336829) +++ scr/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml (working copy) @@ -1,33 +0,0 @@ - - - - - - - - - - - - - - - - - - -