FELIX-1457: refactor features management layer
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@801925 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/features/management/pom.xml b/karaf/features/management/pom.xml
new file mode 100644
index 0000000..6490511
--- /dev/null
+++ b/karaf/features/management/pom.xml
@@ -0,0 +1,118 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!--
+
+ 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.
+ -->
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.felix.karaf.features</groupId>
+ <artifactId>features</artifactId>
+ <version>1.2.0-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.felix.karaf.features</groupId>
+ <artifactId>org.apache.felix.karaf.features.management</artifactId>
+ <packaging>bundle</packaging>
+ <version>1.2.0-SNAPSHOT</version>
+ <name>Apache Felix Karaf :: Features Management</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix.karaf.features</groupId>
+ <artifactId>org.apache.felix.karaf.features.core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.bundlerepository</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix.karaf.gshell</groupId>
+ <artifactId>org.apache.felix.karaf.gshell.console</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix.karaf.gshell</groupId>
+ <artifactId>org.apache.felix.karaf.gshell.obr</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework.osgi</groupId>
+ <artifactId>spring-osgi-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicemix.bundles</groupId>
+ <artifactId>org.apache.servicemix.bundles.junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <configuration>
+ <instructions>
+ <Bundle-SymbolicName>${artifactId}</Bundle-SymbolicName>
+ <Export-Package>
+ ${artifactId}*;version=${version}
+ </Export-Package>
+ <Import-Package>
+ javax.management,
+ javax.management.loading,
+ org.osgi.service.command,
+ org.apache.felix.gogo.commands,
+ org.apache.felix.karaf.gshell.console,
+ !${artifactId}*,
+ *
+ </Import-Package>
+ <Private-Package>org.apache.felix.karaf.features.management.internal</Private-Package>
+ <_versionpolicy>${bnd.version.policy}</_versionpolicy>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/FeaturesServiceMBean.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/FeaturesServiceMBean.java
new file mode 100644
index 0000000..dac88da
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/FeaturesServiceMBean.java
@@ -0,0 +1,114 @@
+/*
+ * 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.felix.karaf.features.management;
+
+import javax.management.openmbean.TabularData;
+
+public interface FeaturesServiceMBean {
+
+ TabularData getFeatures() throws Exception;
+
+ TabularData getRepositories() throws Exception;
+
+ void addRepository(String url) throws Exception;
+
+ void removeRepositroy(String url) throws Exception;
+
+ void installFeature(String name) throws Exception;
+
+ void installFeature(String name, String version) throws Exception;
+
+ void uninstallFeature(String name) throws Exception;
+
+ void uninstallFeature(String name, String version) throws Exception;
+
+ String FEATURE_NAME = "Name";
+
+ String FEATURE_VERSION = "Version";
+
+ String FEATURE_DEPENDENCIES = "Dependencies";
+
+ String FEATURE_BUNDLES = "Bundles";
+
+ String FEATURE_CONFIGURATIONS = "Configurations";
+
+ String FEATURE_INSTALLED = "Installed";
+
+ String FEATURE_CONFIG_PID = "Pid";
+ String FEATURE_CONFIG_ELEMENTS = "Elements";
+ String FEATURE_CONFIG_ELEMENT_KEY = "Key";
+ String FEATURE_CONFIG_ELEMENT_VALUE = "Value";
+
+ /**
+ * The type of the event which is emitted for features events
+ */
+ String FEATURE_EVENT_TYPE = "org.apache.felix.karaf.features.featureEvent";
+
+ String FEATURE_EVENT_EVENT_TYPE = "Type";
+
+ String FEATURE_EVENT_EVENT_TYPE_INSTALLED = "Installed";
+
+ String FEATURE_EVENT_EVENT_TYPE_UNINSTALLED = "Uninstalled";
+
+ /**
+ * The item names in the CompositeData representing a feature
+ */
+ String[] FEATURE = { FEATURE_NAME, FEATURE_VERSION, FEATURE_DEPENDENCIES, FEATURE_BUNDLES,
+ FEATURE_CONFIGURATIONS, FEATURE_INSTALLED };
+
+ String[] FEATURE_IDENTIFIER = { FEATURE_NAME, FEATURE_VERSION };
+
+ String[] FEATURE_CONFIG = { FEATURE_CONFIG_PID, FEATURE_CONFIG_ELEMENTS };
+
+ String[] FEATURE_CONFIG_ELEMENT = { FEATURE_CONFIG_ELEMENT_KEY, FEATURE_CONFIG_ELEMENT_VALUE };
+
+ /**
+ * The item names in the CompositeData representing the event raised for
+ * feature events within the OSGi container by this bean
+ */
+ String[] FEATURE_EVENT = { FEATURE_NAME, FEATURE_VERSION, FEATURE_EVENT_EVENT_TYPE };
+
+
+ String REPOSITORY_URI = "Uri";
+
+ String REPOSITORY_REPOSITORIES = "Repositories";
+
+ String REPOSITORY_FEATURES = "Features";
+
+ /**
+ * The type of the event which is emitted for repositories events
+ */
+ String REPOSITORY_EVENT_TYPE = "org.apache.felix.karaf.features.repositoryEvent";
+
+ String REPOSITORY_EVENT_EVENT_TYPE = "Type";
+
+ String REPOSITORY_EVENT_EVENT_TYPE_ADDED = "Added";
+
+ String REPOSITORY_EVENT_EVENT_TYPE_REMOVED = "Removed";
+
+ /**
+ * The item names in the CompositeData representing a feature
+ */
+ String[] REPOSITORY = { REPOSITORY_URI, REPOSITORY_REPOSITORIES, REPOSITORY_FEATURES };
+
+ /**
+ * The item names in the CompositeData representing the event raised for
+ * feature events within the OSGi container by this bean
+ */
+ String[] REPOSITORY_EVENT = { REPOSITORY_URI, REPOSITORY_EVENT_EVENT_TYPE };
+
+}
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeature.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeature.java
new file mode 100644
index 0000000..f471282
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeature.java
@@ -0,0 +1,259 @@
+/*
+ * 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.felix.karaf.features.management.codec;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.ArrayType;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.TabularDataSupport;
+
+import org.apache.felix.karaf.features.Feature;
+import org.apache.felix.karaf.features.management.FeaturesServiceMBean;
+
+public class JmxFeature {
+
+ /**
+ * The CompositeType which represents a single feature
+ */
+ public final static CompositeType FEATURE;
+
+ /**
+ * The TabularType which represents a list of features
+ */
+ public final static TabularType FEATURE_TABLE;
+
+ public final static CompositeType FEATURE_IDENTIFIER;
+
+ public final static TabularType FEATURE_IDENTIFIER_TABLE;
+
+ public final static CompositeType FEATURE_CONFIG_ELEMENT;
+
+ public final static TabularType FEATURE_CONFIG_ELEMENT_TABLE;
+
+ public final static CompositeType FEATURE_CONFIG;
+
+ public final static TabularType FEATURE_CONFIG_TABLE;
+
+
+ private final CompositeData data;
+
+ public JmxFeature(Feature feature, boolean installed) {
+ try {
+ String[] itemNames = FeaturesServiceMBean.FEATURE;
+ Object[] itemValues = new Object[itemNames.length];
+ itemValues[0] = feature.getName();
+ itemValues[1] = feature.getVersion();
+ itemValues[2] = getFeatureIdentifierTable(feature.getDependencies());
+ itemValues[3] = feature.getBundles().toArray(new String[feature.getBundles().size()]);
+ itemValues[4] = getConfigTable(feature.getConfigurations());
+ itemValues[5] = installed;
+ data = new CompositeDataSupport(FEATURE, itemNames, itemValues);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Cannot form feature open data", e);
+ }
+ }
+
+ public CompositeData asCompositeData() {
+ return data;
+ }
+
+ public static TabularData tableFrom(Collection<JmxFeature> features) {
+ TabularDataSupport table = new TabularDataSupport(FEATURE_TABLE);
+ for (JmxFeature feature : features) {
+ table.put(feature.asCompositeData());
+ }
+ return table;
+ }
+
+ static TabularData getFeatureIdentifierTable(List<Feature> features) throws OpenDataException {
+ TabularDataSupport table = new TabularDataSupport(FEATURE_IDENTIFIER_TABLE);
+ for (Feature feature : features) {
+ String[] itemNames = new String[] { FeaturesServiceMBean.FEATURE_NAME, FeaturesServiceMBean.FEATURE_VERSION };
+ Object[] itemValues = new Object[] { feature.getName(), feature.getVersion() };
+ CompositeData ident = new CompositeDataSupport(FEATURE_IDENTIFIER, itemNames, itemValues);
+ table.put(ident);
+ }
+ return table;
+ }
+
+ static TabularData getConfigTable(Map<String, Map<String, String>> configs) throws OpenDataException {
+ TabularDataSupport table = new TabularDataSupport(FEATURE_CONFIG_TABLE);
+ for (Map.Entry<String, Map<String, String>> entry : configs.entrySet()) {
+ String[] itemNames = FeaturesServiceMBean.FEATURE_CONFIG;
+ Object[] itemValues = new Object[2];
+ itemValues[0] = entry.getKey();
+ itemValues[1] = getConfigElementTable(entry.getValue());
+ CompositeData config = new CompositeDataSupport(FEATURE_CONFIG, itemNames, itemValues);
+ table.put(config);
+ }
+ return table;
+ }
+
+ static TabularData getConfigElementTable(Map<String, String> config) throws OpenDataException {
+ TabularDataSupport table = new TabularDataSupport(FEATURE_CONFIG_ELEMENT_TABLE);
+ for (Map.Entry<String, String> entry : config.entrySet()) {
+ String[] itemNames = FeaturesServiceMBean.FEATURE_CONFIG_ELEMENT;
+ Object[] itemValues = { entry.getKey(), entry.getValue() };
+ CompositeData element = new CompositeDataSupport(FEATURE_CONFIG_ELEMENT, itemNames, itemValues);
+ table.put(element);
+ }
+ return table;
+ }
+
+
+ static {
+ FEATURE_IDENTIFIER = createFeatureIdentifierType();
+ FEATURE_IDENTIFIER_TABLE = createFeatureIdentifierTableType();
+ FEATURE_CONFIG_ELEMENT = createFeatureConfigElementType();
+ FEATURE_CONFIG_ELEMENT_TABLE = createFeatureConfigElementTableType();
+ FEATURE_CONFIG = createFeatureConfigType();
+ FEATURE_CONFIG_TABLE = createFeatureConfigTableType();
+ FEATURE = createFeatureType();
+ FEATURE_TABLE = createFeatureTableType();
+ }
+
+ private static CompositeType createFeatureIdentifierType() {
+ try {
+ String description = "This type identify a Karaf features";
+ String[] itemNames = FeaturesServiceMBean.FEATURE_IDENTIFIER;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = SimpleType.STRING;
+
+ itemDescriptions[0] = "The id of the feature";
+ itemDescriptions[1] = "The version of the feature";
+
+ return new CompositeType("FeatureIdentifier", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build featureIdentifier type", e);
+ }
+ }
+
+ private static TabularType createFeatureIdentifierTableType() {
+ try {
+ return new TabularType("Features", "The table of featureIdentifiers",
+ FEATURE_IDENTIFIER, new String[] { FeaturesServiceMBean.FEATURE_NAME, FeaturesServiceMBean.FEATURE_VERSION });
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build featureIdentifier table type", e);
+ }
+ }
+
+ private static CompositeType createFeatureConfigElementType() {
+ try {
+ String description = "This type encapsulates Karaf feature config element";
+ String[] itemNames = FeaturesServiceMBean.FEATURE_CONFIG_ELEMENT;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = SimpleType.STRING;
+
+ itemDescriptions[0] = "The key";
+ itemDescriptions[1] = "The value";
+
+ return new CompositeType("ConfigElement", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build configElement type", e);
+ }
+ }
+
+ private static TabularType createFeatureConfigElementTableType() {
+ try {
+ return new TabularType("ConfigElement", "The table of configurations elements",
+ FEATURE_CONFIG_ELEMENT, new String[] { FeaturesServiceMBean.FEATURE_CONFIG_ELEMENT_KEY});
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build feature table type", e);
+ }
+ }
+
+ private static CompositeType createFeatureConfigType() {
+ try {
+ String description = "This type encapsulates Karaf feature config";
+ String[] itemNames = FeaturesServiceMBean.FEATURE_CONFIG;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = FEATURE_CONFIG_ELEMENT_TABLE;
+
+ itemDescriptions[0] = "The PID of the config";
+ itemDescriptions[1] = "The configuration elements";
+
+ return new CompositeType("Config", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build configElement type", e);
+ }
+ }
+
+ private static TabularType createFeatureConfigTableType() {
+ try {
+ return new TabularType("Features", "The table of configurations",
+ FEATURE_CONFIG, new String[] { FeaturesServiceMBean.FEATURE_CONFIG_PID});
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build feature table type", e);
+ }
+ }
+
+ private static CompositeType createFeatureType() {
+ try {
+ String description = "This type encapsulates Karaf features";
+ String[] itemNames = FeaturesServiceMBean.FEATURE;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = SimpleType.STRING;
+ itemTypes[2] = FEATURE_IDENTIFIER_TABLE;
+ itemTypes[3] = new ArrayType(1, SimpleType.STRING);
+ itemTypes[4] = FEATURE_CONFIG_TABLE;
+ itemTypes[5] = SimpleType.BOOLEAN;
+
+ itemDescriptions[0] = "The name of the feature";
+ itemDescriptions[1] = "The version of the feature";
+ itemDescriptions[2] = "The feature dependencies";
+ itemDescriptions[3] = "The feature bundles";
+ itemDescriptions[4] = "The feature configurations";
+ itemDescriptions[5] = "Whether the feature is installed";
+
+ return new CompositeType("Feature", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build feature type", e);
+ }
+ }
+
+ private static TabularType createFeatureTableType() {
+ try {
+ return new TabularType("Features", "The table of all features",
+ FEATURE, new String[] { FeaturesServiceMBean.FEATURE_NAME, FeaturesServiceMBean.FEATURE_VERSION });
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build feature table type", e);
+ }
+ }
+
+}
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeatureEvent.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeatureEvent.java
new file mode 100644
index 0000000..03f024a
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxFeatureEvent.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.felix.karaf.features.management.codec;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+
+import org.apache.felix.karaf.features.FeatureEvent;
+import org.apache.felix.karaf.features.management.FeaturesServiceMBean;
+
+public class JmxFeatureEvent {
+
+ public static final CompositeType FEATURE_EVENT;
+
+ private final CompositeData data;
+
+ public JmxFeatureEvent(FeatureEvent event) {
+ try {
+ String[] itemNames = FeaturesServiceMBean.FEATURE_EVENT;
+ Object[] itemValues = new Object[itemNames.length];
+ itemValues[0] = event.getFeature().getName();
+ itemValues[1] = event.getFeature().getVersion();
+ switch (event.getType()) {
+ case FeatureInstalled: itemValues[2] = FeaturesServiceMBean.FEATURE_EVENT_EVENT_TYPE_INSTALLED; break;
+ case FeatureUninstalled: itemValues[2] = FeaturesServiceMBean.FEATURE_EVENT_EVENT_TYPE_UNINSTALLED; break;
+ default: throw new IllegalStateException("Unsupported event type: " + event.getType());
+ }
+ data = new CompositeDataSupport(FEATURE_EVENT, itemNames, itemValues);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Cannot form feature event open data", e);
+ }
+ }
+
+ public CompositeData asCompositeData() {
+ return data;
+ }
+
+ static {
+ FEATURE_EVENT = createFeatureEventType();
+ }
+
+ private static CompositeType createFeatureEventType() {
+ try {
+ String description = "This type identify a Karaf feature event";
+ String[] itemNames = FeaturesServiceMBean.FEATURE_EVENT;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = SimpleType.STRING;
+ itemTypes[2] = SimpleType.STRING;
+
+ itemDescriptions[0] = "The id of the feature";
+ itemDescriptions[1] = "The version of the feature";
+ itemDescriptions[2] = "The type of the event";
+
+ return new CompositeType("FeatureEvent", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build featureEvent type", e);
+ }
+ }
+}
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepository.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepository.java
new file mode 100644
index 0000000..c2897a1
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepository.java
@@ -0,0 +1,116 @@
+/*
+ * 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.felix.karaf.features.management.codec;
+
+import java.util.Collection;
+import java.util.Arrays;
+import java.net.URI;
+
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.TabularType;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.ArrayType;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.CompositeDataSupport;
+
+import org.apache.felix.karaf.features.Repository;
+import org.apache.felix.karaf.features.management.FeaturesServiceMBean;
+
+public class JmxRepository {
+
+ public final static CompositeType REPOSITORY;
+
+ public final static TabularType REPOSITORY_TABLE;
+
+ private final CompositeData data;
+
+ public JmxRepository(Repository repository) {
+ try {
+ String[] itemNames = FeaturesServiceMBean.REPOSITORY;
+ Object[] itemValues = new Object[itemNames.length];
+ itemValues[0] = repository.getURI().toString();
+ itemValues[1] = toStringArray(repository.getRepositories());
+ itemValues[2] = JmxFeature.getFeatureIdentifierTable(Arrays.asList(repository.getFeatures()));
+ data = new CompositeDataSupport(REPOSITORY, itemNames, itemValues);
+ } catch (Exception e) {
+ throw new IllegalStateException("Cannot form repository open data", e);
+ }
+ }
+
+ public CompositeData asCompositeData() {
+ return data;
+ }
+
+ public static TabularData tableFrom(Collection<JmxRepository> repositories) {
+ TabularDataSupport table = new TabularDataSupport(REPOSITORY_TABLE);
+ for (JmxRepository repository : repositories) {
+ table.put(repository.asCompositeData());
+ }
+ return table;
+ }
+
+ private static String[] toStringArray(URI[] uris) {
+ if (uris == null) {
+ return null;
+ }
+ String[] res = new String[uris.length];
+ for (int i = 0; i < res.length; i++) {
+ res[i] = uris[i].toString();
+ }
+ return res;
+ }
+
+ static {
+ REPOSITORY = createRepositoryType();
+ REPOSITORY_TABLE = createRepositoryTableType();
+ }
+
+ private static CompositeType createRepositoryType() {
+ try {
+ String description = "This type identify a Karaf repository";
+ String[] itemNames = FeaturesServiceMBean.REPOSITORY;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = new ArrayType(1, SimpleType.STRING);
+ itemTypes[2] = JmxFeature.FEATURE_IDENTIFIER_TABLE;
+
+ itemDescriptions[0] = "The uri of the repository";
+ itemDescriptions[1] = "The dependent repositories";
+ itemDescriptions[2] = "The list of included features";
+
+ return new CompositeType("Repository", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build repository type", e);
+ }
+ }
+
+ private static TabularType createRepositoryTableType() {
+ try {
+ return new TabularType("Features", "The table of repositories",
+ REPOSITORY, new String[] { FeaturesServiceMBean.REPOSITORY_URI });
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build repository table type", e);
+ }
+ }
+
+}
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepositoryEvent.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepositoryEvent.java
new file mode 100644
index 0000000..6e8954f
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/codec/JmxRepositoryEvent.java
@@ -0,0 +1,77 @@
+/*
+ * 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.felix.karaf.features.management.codec;
+
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+
+import org.apache.felix.karaf.features.RepositoryEvent;
+import org.apache.felix.karaf.features.management.FeaturesServiceMBean;
+
+public class JmxRepositoryEvent {
+
+ public static final CompositeType REPOSITORY_EVENT;
+
+ private final CompositeData data;
+
+ public JmxRepositoryEvent(RepositoryEvent event) {
+ try {
+ String[] itemNames = FeaturesServiceMBean.REPOSITORY_EVENT;
+ Object[] itemValues = new Object[itemNames.length];
+ itemValues[0] = event.getRepository().getURI().toString();
+ switch (event.getType()) {
+ case RepositoryAdded: itemValues[2] = FeaturesServiceMBean.REPOSITORY_EVENT_EVENT_TYPE_ADDED; break;
+ case RepositoryRemoved: itemValues[2] = FeaturesServiceMBean.REPOSITORY_EVENT_EVENT_TYPE_REMOVED; break;
+ default: throw new IllegalStateException("Unsupported event type: " + event.getType());
+ }
+ data = new CompositeDataSupport(REPOSITORY_EVENT, itemNames, itemValues);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Cannot form repository event open data", e);
+ }
+ }
+
+ public CompositeData asCompositeData() {
+ return data;
+ }
+
+ static {
+ REPOSITORY_EVENT = createRepositoryEventType();
+ }
+
+ private static CompositeType createRepositoryEventType() {
+ try {
+ String description = "This type identify a Karaf repository event";
+ String[] itemNames = FeaturesServiceMBean.REPOSITORY_EVENT;
+ OpenType[] itemTypes = new OpenType[itemNames.length];
+ String[] itemDescriptions = new String[itemNames.length];
+ itemTypes[0] = SimpleType.STRING;
+ itemTypes[1] = SimpleType.STRING;
+
+ itemDescriptions[0] = "The uri of the repository";
+ itemDescriptions[1] = "The type of event";
+
+ return new CompositeType("RepositoryEvent", description, itemNames,
+ itemDescriptions, itemTypes);
+ } catch (OpenDataException e) {
+ throw new IllegalStateException("Unable to build repositoryEvent type", e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/FeaturesServiceMBeanImpl.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/FeaturesServiceMBeanImpl.java
new file mode 100644
index 0000000..c7b37b4
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/FeaturesServiceMBeanImpl.java
@@ -0,0 +1,194 @@
+/*
+ * Licensed 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.felix.karaf.features.management.internal;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.net.URI;
+
+import javax.management.MBeanRegistration;
+import javax.management.MBeanServer;
+import javax.management.NotificationBroadcasterSupport;
+import javax.management.ObjectName;
+import javax.management.Notification;
+import javax.management.openmbean.TabularData;
+
+import org.apache.felix.karaf.features.management.FeaturesServiceMBean;
+import org.apache.felix.karaf.features.management.codec.JmxFeature;
+import org.apache.felix.karaf.features.management.codec.JmxFeatureEvent;
+import org.apache.felix.karaf.features.management.codec.JmxRepository;
+import org.apache.felix.karaf.features.management.codec.JmxRepositoryEvent;
+import org.apache.felix.karaf.features.FeaturesListener;
+import org.apache.felix.karaf.features.FeatureEvent;
+import org.apache.felix.karaf.features.RepositoryEvent;
+import org.apache.felix.karaf.features.FeaturesService;
+import org.apache.felix.karaf.features.Feature;
+import org.apache.felix.karaf.features.Repository;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ *
+ */
+
+public class FeaturesServiceMBeanImpl extends NotificationBroadcasterSupport
+ implements MBeanRegistration, FeaturesServiceMBean {
+
+ private ServiceRegistration registration;
+
+ private BundleContext bundleContext;
+
+ private ObjectName objectName;
+
+ private volatile long sequenceNumber = 0;
+
+ private MBeanServer server;
+
+ private FeaturesService featuresService;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.management.MBeanRegistration#preRegister(javax.manamement.MBeanServer, javax.management.ObjectName)
+ */
+ public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
+ objectName = name;
+ this.server = server;
+ return name;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
+ */
+ public void postRegister(Boolean registrationDone) {
+ registration = bundleContext.registerService(
+ FeaturesListener.class.getName(),
+ getFeaturesListener(),
+ new Hashtable());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.management.MBeanRegistration#preDeregister()
+ */
+ public void preDeregister() throws Exception {
+ registration.unregister();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.management.MBeanRegistration#postDeregister()
+ */
+ public void postDeregister() {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.felix.karaf.features.management.FeaturesServiceMBean#getFeatures()
+ */
+ public TabularData getFeatures() throws Exception {
+ try {
+ List<Feature> allFeatures = Arrays.asList(featuresService.listFeatures());
+ List<Feature> insFeatures = Arrays.asList(featuresService.listInstalledFeatures());
+ ArrayList<JmxFeature> features = new ArrayList<JmxFeature>();
+ for (Feature feature : allFeatures) {
+ features.add(new JmxFeature(feature, insFeatures.contains(feature)));
+ }
+ TabularData table = JmxFeature.tableFrom(features);
+ return table;
+ } catch (Throwable t) {
+ t.printStackTrace();
+ return null;
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.felix.karaf.features.management.FeaturesServiceMBean#getRepositories()
+ */
+ public TabularData getRepositories() throws Exception {
+ try {
+ List<Repository> allRepositories = Arrays.asList(featuresService.listRepositories());
+ ArrayList<JmxRepository> repositories = new ArrayList<JmxRepository>();
+ for (Repository repository : allRepositories) {
+ repositories.add(new JmxRepository(repository));
+ }
+ TabularData table = JmxRepository.tableFrom(repositories);
+ return table;
+ } catch (Throwable t) {
+ t.printStackTrace();
+ return null;
+ }
+ }
+
+ public void addRepository(String uri) throws Exception {
+ featuresService.addRepository(new URI(uri));
+ }
+
+ public void removeRepositroy(String uri) throws Exception {
+ featuresService.removeRepository(new URI(uri));
+ }
+
+ public void installFeature(String name) throws Exception {
+ featuresService.installFeature(name);
+ }
+
+ public void installFeature(String name, String version) throws Exception {
+ featuresService.installFeature(name, version);
+ }
+
+ public void uninstallFeature(String name) throws Exception {
+ featuresService.uninstallFeature(name);
+ }
+
+ public void uninstallFeature(String name, String version) throws Exception {
+ featuresService.uninstallFeature(name, version);
+ }
+
+ public void setBundleContext(BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ }
+
+ public void setFeaturesService(FeaturesService featuresService) {
+ this.featuresService = featuresService;
+ }
+
+ public FeaturesListener getFeaturesListener() {
+ return new FeaturesListener() {
+ public void featureEvent(FeatureEvent event) {
+ if (!event.isReplay()) {
+ Notification notification = new Notification(FEATURE_EVENT_TYPE, objectName, sequenceNumber++);
+ notification.setUserData(new JmxFeatureEvent(event).asCompositeData());
+ sendNotification(notification);
+ }
+ }
+ public void repositoryEvent(RepositoryEvent event) {
+ if (!event.isReplay()) {
+ Notification notification = new Notification(REPOSITORY_EVENT_TYPE, objectName, sequenceNumber++);
+ notification.setUserData(new JmxRepositoryEvent(event).asCompositeData());
+ sendNotification(notification);
+ }
+ }
+ };
+ }
+
+}
\ No newline at end of file
diff --git a/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java
new file mode 100644
index 0000000..004ad00
--- /dev/null
+++ b/karaf/features/management/src/main/java/org/apache/felix/karaf/features/management/internal/MBeanRegistrer.java
@@ -0,0 +1,72 @@
+/*
+ * 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.felix.karaf.features.management.internal;
+
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.management.MBeanRegistrationException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MalformedObjectNameException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.JMException;
+
+public class MBeanRegistrer {
+
+ private MBeanServer mbeanServer;
+
+ private Map<Object, String> mbeans;
+
+ public void setMbeans(Map<Object, String> mbeans) {
+ this.mbeans = mbeans;
+ }
+
+ public void registerMBeanServer(MBeanServer mbeanServer) throws JMException {
+ if (this.mbeanServer != mbeanServer) {
+ unregisterMBeans();
+ }
+ this.mbeanServer = mbeanServer;
+ registerMBeans();
+ }
+
+ public void unregisterMBeanServer(MBeanServer mbeanServer) throws JMException {
+ unregisterMBeans();
+ this.mbeanServer = null;
+ }
+
+ public void init() throws Exception {
+ registerMBeans();
+ }
+
+ protected void registerMBeans() throws JMException {
+ if (mbeanServer != null && mbeans != null) {
+ for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
+ mbeanServer.registerMBean(entry.getKey(), new ObjectName(entry.getValue()));
+ }
+ }
+ }
+
+ protected void unregisterMBeans() throws JMException {
+ if (mbeanServer != null && mbeans != null) {
+ for (Map.Entry<Object, String> entry : mbeans.entrySet()) {
+ mbeanServer.unregisterMBean(new ObjectName(entry.getValue()));
+ }
+ }
+ }
+}
diff --git a/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml b/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml
new file mode 100644
index 0000000..98f1041
--- /dev/null
+++ b/karaf/features/management/src/main/resources/OSGI-INF/blueprint/features-management.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:ext="http://geronimo.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
+
+ <reference id="featuresService" interface="org.apache.felix.karaf.features.FeaturesService" />
+
+ <reference id="mbeanServer" interface="javax.management.MBeanServer">
+ <reference-listener ref="mbeanRegister" bind-method="registerMBeanServer" unbind-method="unregisterMBeanServer" />
+ </reference>
+
+ <bean id="mbeanImpl" class="org.apache.felix.karaf.features.management.internal.FeaturesServiceMBeanImpl">
+ <property name="bundleContext" ref="blueprintBundleContext" />
+ <property name="featuresService" ref="featuresService" />
+ </bean>
+
+ <bean id="mbeanRegister" class="org.apache.felix.karaf.features.management.internal.MBeanRegistrer">
+ <property name="mbeans">
+ <map>
+ <entry>
+ <key>
+ <bean class="javax.management.StandardMBean">
+ <argument ref="mbeanImpl" />
+ <argument value="org.apache.felix.karaf.features.management.FeaturesServiceMBean"/>
+ </bean>
+ </key>
+ <value>org.apache.felix.karaf:service=features</value>
+ </entry>
+ </map>
+ </property>
+ </bean>
+
+</blueprint>