You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2011/10/12 22:13:32 UTC
svn commit: r1182569 - in
/karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles:
BundlesMBean.java internal/BundlesMBeanImpl.java
internal/BundlesSelector.java
Author: jbonofre
Date: Wed Oct 12 20:13:32 2011
New Revision: 1182569
URL: http://svn.apache.org/viewvc?rev=1182569&view=rev
Log:
[KARAF-930] Add bundles selector support in BundlesMBean allowing the usage of ID, ID range, name, and version to identify bundles
Added:
karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesSelector.java
Modified:
karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/BundlesMBean.java
karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesMBeanImpl.java
Modified: karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/BundlesMBean.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/BundlesMBean.java?rev=1182569&r1=1182568&r2=1182569&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/BundlesMBean.java (original)
+++ karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/BundlesMBean.java Wed Oct 12 20:13:32 2011
@@ -25,27 +25,27 @@ public interface BundlesMBean {
TabularData list() throws Exception;
- int getStartLevel(long bundleId) throws Exception;
- void setStartLevel(long bundleId, int bundleStartLevel) throws Exception;
+ int getStartLevel(String bundleId) throws Exception;
+ void setStartLevel(String bundleId, int bundleStartLevel) throws Exception;
void refresh() throws Exception;
- void refresh(long bundleId) throws Exception;
+ void refresh(String bundleId) throws Exception;
- void update(long bundleId) throws Exception;
- void update(long bundleId, String location) throws Exception;
+ void update(String bundleId) throws Exception;
+ void update(String bundleId, String location) throws Exception;
void resolve() throws Exception;
- void resolve(long bundleId) throws Exception;
+ void resolve(String bundleId) throws Exception;
- void restart(long bundleId) throws Exception;
+ void restart(String bundleId) throws Exception;
long install(String url) throws Exception;
long install(String url, boolean start) throws Exception;
- void start(long bundleId) throws Exception;
+ void start(String bundleId) throws Exception;
- void stop(long bundleId) throws Exception;
+ void stop(String bundleId) throws Exception;
- void uninstall(long bundleId) throws Exception;
+ void uninstall(String bundleId) throws Exception;
}
Modified: karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesMBeanImpl.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesMBeanImpl.java?rev=1182569&r1=1182568&r2=1182569&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesMBeanImpl.java (original)
+++ karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesMBeanImpl.java Wed Oct 12 20:13:32 2011
@@ -28,6 +28,7 @@ import javax.management.StandardMBean;
import javax.management.openmbean.*;
import java.io.InputStream;
import java.net.URL;
+import java.util.List;
/**
* Bundles MBean implementation.
@@ -50,7 +51,7 @@ public class BundlesMBeanImpl extends St
CompositeType bundleType = new CompositeType("Bundle", "OSGi Bundle",
new String[]{ "ID", "Name", "Version", "Start Level", "State" },
new String[]{ "ID of the Bundle", "Name of the Bundle", "Version of the Bundle", "Start Level of the Bundle", "Current State of the Bundle" },
- new OpenType[]{ SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.INTEGER });
+ new OpenType[]{ SimpleType.LONG, SimpleType.STRING, SimpleType.STRING, SimpleType.INTEGER, SimpleType.STRING });
TabularType tableType = new TabularType("Bundles", "Tables of all Bundles", bundleType, new String[]{ "ID" });
TabularData table = new TabularDataSupport(tableType);
@@ -61,9 +62,22 @@ public class BundlesMBeanImpl extends St
if (startLevel != null) {
bundleStartLevel = startLevel.getBundleStartLevel(bundles[i]);
}
+ int bundleState = bundles[i].getState();
+ String bundleStateString;
+ if (bundleState == Bundle.ACTIVE)
+ bundleStateString = "ACTIVE";
+ else if (bundleState == Bundle.INSTALLED)
+ bundleStateString = "INSTALLED";
+ else if (bundleState == Bundle.RESOLVED)
+ bundleStateString = "RESOLVED";
+ else if (bundleState == Bundle.STARTING)
+ bundleStateString = "STARTING";
+ else if (bundleState == Bundle.STOPPING)
+ bundleStateString = "STOPPING";
+ else bundleStateString = "UNKNOWN";
CompositeData data = new CompositeDataSupport(bundleType,
new String[]{ "ID", "Name", "Version", "Start Level", "State" },
- new Object[]{ bundles[i].getBundleId(), bundles[i].getSymbolicName(), bundles[i].getVersion().toString(), bundleStartLevel, bundles[i].getState() });
+ new Object[]{ bundles[i].getBundleId(), bundles[i].getSymbolicName(), bundles[i].getVersion().toString(), bundleStartLevel, bundleStateString });
table.put(data);
}
@@ -72,7 +86,7 @@ public class BundlesMBeanImpl extends St
return table;
}
- public int getStartLevel(long bundleId) throws Exception {
+ public int getStartLevel(String bundleId) throws Exception {
ServiceReference startLevelReference = bundleContext.getServiceReference(StartLevel.class.getName());
if (startLevelReference == null) {
throw new IllegalStateException("StartLevel service is not available");
@@ -81,12 +95,20 @@ public class BundlesMBeanImpl extends St
if (startLevel == null) {
throw new IllegalStateException("StartLevel service is not available");
}
- int bundleStartLevel = startLevel.getBundleStartLevel(bundleContext.getBundle(bundleId));
+
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ if (bundles.size() != 1) {
+ throw new IllegalArgumentException("Provided bundle Id doesn't return any bundle or more than one bundle selected");
+ }
+
+ int bundleStartLevel = startLevel.getBundleStartLevel(bundles.get(0));
bundleContext.ungetService(startLevelReference);
return bundleStartLevel;
}
- public void setStartLevel(long bundleId, int bundleStartLevel) throws Exception {
+ public void setStartLevel(String bundleId, int bundleStartLevel) throws Exception {
ServiceReference startLevelReference = bundleContext.getServiceReference(StartLevel.class.getName());
if (startLevelReference == null) {
throw new IllegalStateException("StartLevel service is not available");
@@ -95,7 +117,14 @@ public class BundlesMBeanImpl extends St
if (startLevel == null) {
throw new IllegalStateException("StartLevel service is not available");
}
- startLevel.setBundleStartLevel(bundleContext.getBundle(bundleId), bundleStartLevel);
+
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ for (Bundle bundle : bundles) {
+ startLevel.setBundleStartLevel(bundle, bundleStartLevel);
+ }
+
bundleContext.ungetService(startLevelReference);
}
@@ -112,7 +141,7 @@ public class BundlesMBeanImpl extends St
getBundleContext().ungetService(packageAdminReference);
}
- public void refresh(long bundleId) throws Exception {
+ public void refresh(String bundleId) throws Exception {
ServiceReference packageAdminReference = getBundleContext().getServiceReference(PackageAdmin.class.getName());
if (packageAdminReference == null) {
throw new IllegalStateException("PackageAdmin service is not available");
@@ -121,21 +150,37 @@ public class BundlesMBeanImpl extends St
if (packageAdmin == null) {
throw new IllegalStateException("PackageAdmin service is not available");
}
- packageAdmin.refreshPackages(new Bundle[]{ bundleContext.getBundle(bundleId) });
+
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ Bundle[] bundlesArray = new Bundle[bundles.size()];
+ packageAdmin.refreshPackages(bundles.toArray(bundlesArray));
+
getBundleContext().ungetService(packageAdminReference);
}
- public void update(long bundleId) throws Exception {
+ public void update(String bundleId) throws Exception {
update(bundleId, null);
}
- public void update(long bundleId, String location) throws Exception {
- if (location != null) {
- InputStream is = new URL(location).openStream();
- bundleContext.getBundle(bundleId).update(is);
- } else {
- bundleContext.getBundle(bundleId).update();
+ public void update(String bundleId, String location) throws Exception {
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ if (location == null) {
+ for (Bundle bundle : bundles) {
+ bundle.update();
+ }
+ return;
+ }
+
+ if (bundles.size() != 1) {
+ throw new IllegalArgumentException("Provided bundle Id doesn't return any bundle or more than one bundle selected");
}
+
+ InputStream is = new URL(location).openStream();
+ bundles.get(0).update(is);
}
public void resolve() throws Exception {
@@ -151,7 +196,7 @@ public class BundlesMBeanImpl extends St
getBundleContext().ungetService(packageAdminReference);
}
- public void resolve(long bundleId) throws Exception {
+ public void resolve(String bundleId) throws Exception {
ServiceReference packageAdminReference = getBundleContext().getServiceReference(PackageAdmin.class.getName());
if (packageAdminReference == null) {
throw new IllegalStateException("PackageAdmin service is not available");
@@ -160,13 +205,24 @@ public class BundlesMBeanImpl extends St
if (packageAdmin == null) {
throw new IllegalStateException("PackageAdmin service is not available");
}
- packageAdmin.resolveBundles(new Bundle[]{ bundleContext.getBundle(bundleId) });
+
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ Bundle[] bundlesArray = new Bundle[bundles.size()];
+ packageAdmin.resolveBundles(bundles.toArray(bundlesArray));
+
getBundleContext().ungetService(packageAdminReference);
}
- public void restart(long bundleId) throws Exception {
- bundleContext.getBundle(bundleId).stop();
- bundleContext.getBundle(bundleId).start();
+ public void restart(String bundleId) throws Exception {
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ for (Bundle bundle : bundles) {
+ bundle.stop();
+ bundle.start();
+ };
}
public long install(String url) throws Exception {
@@ -181,16 +237,31 @@ public class BundlesMBeanImpl extends St
return bundle.getBundleId();
}
- public void start(long bundleId) throws Exception {
- bundleContext.getBundle(bundleId).start();
+ public void start(String bundleId) throws Exception {
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ for (Bundle bundle : bundles) {
+ bundle.start();
+ }
}
- public void stop(long bundleId) throws Exception {
- bundleContext.getBundle(bundleId).stop();
+ public void stop(String bundleId) throws Exception {
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ for (Bundle bundle : bundles) {
+ bundle.stop();
+ }
}
- public void uninstall(long bundleId) throws Exception {
- bundleContext.getBundle(bundleId).uninstall();
+ public void uninstall(String bundleId) throws Exception {
+ BundlesSelector selector = new BundlesSelector(bundleContext);
+ List<Bundle> bundles = selector.selectBundles(bundleId);
+
+ for (Bundle bundle : bundles) {
+ bundle.uninstall();
+ }
}
public BundleContext getBundleContext() {
Added: karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesSelector.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesSelector.java?rev=1182569&view=auto
==============================================================================
--- karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesSelector.java (added)
+++ karaf/branches/karaf-2.2.x/management/mbeans/bundles/src/main/java/org/apache/karaf/management/mbeans/bundles/internal/BundlesSelector.java Wed Oct 12 20:13:32 2011
@@ -0,0 +1,169 @@
+/*
+ * 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.bundles.internal;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Bundles selector which is able to get a bundle by ID, ID range, name, and version.
+ */
+public class BundlesSelector {
+
+ private BundleContext bundleContext;
+
+ public BundlesSelector(BundleContext bundleContext) {
+ this.bundleContext = bundleContext;
+ }
+
+ /**
+ * Select bundles with ID, ID range, name, and version.
+ *
+ * @param bundleId the bundles ID for selection.
+ * @return the corresponding bundles found.
+ * @throws Exception in case of selection
+ */
+ public List<Bundle> selectBundles(String bundleId) throws Exception {
+ List<Bundle> bundles = new ArrayList<Bundle>();
+ if (bundleId != null && bundleId.length() > 0) {
+
+ // bundle ID is a number
+ Pattern pattern = Pattern.compile("^\\d+$");
+ Matcher matcher = pattern.matcher(bundleId);
+ if (matcher.find()) {
+ Bundle bundle = this.getBundleById(bundleId);
+ bundles.add(bundle);
+ return bundles;
+ }
+
+ // bundle is an ID range
+ pattern = Pattern.compile("^(\\d+)-(\\d+)$");
+ matcher = pattern.matcher(bundleId);
+ if (matcher.find()) {
+ int index = bundleId.indexOf('-');
+ long startId = Long.parseLong(bundleId.substring(0, index));
+ long stopId = Long.parseLong(bundleId.substring(index + 1));
+ if (startId < stopId) {
+ for (long i = startId; i <= stopId; i++) {
+ Bundle bundle = bundleContext.getBundle(i);
+ bundles.add(bundle);
+ }
+ }
+ return bundles;
+ }
+
+ // bundle ID is name/version
+ int index = bundleId.indexOf('/');
+ if (index != -1) {
+ // user has provided name and version
+ return this.getBundleByNameAndVersion(bundleId.substring(0, index), bundleId.substring(index + 1));
+ } else {
+ // user has provided only name
+ return this.getBundleByName(bundleId);
+ }
+
+
+ }
+ return bundles;
+ }
+
+ /**
+ * Get a bundle with the bundle ID.
+ *
+ * @param id the bundle ID.
+ * @return the corresponding bundle.
+ */
+ private Bundle getBundleById(String id) {
+ Bundle bundle = null;
+ try {
+ long idNumber = Long.parseLong(id);
+ bundle = bundleContext.getBundle(idNumber);
+ } catch (Exception e) {
+ // ignore
+ }
+ return bundle;
+ }
+
+ /**
+ * Get a bundles list with the name or symbolic name matching the pattern.
+ *
+ * @param name the bundle name or symbolic name pattern to match.
+ * @return the bundles list.
+ */
+ private List<Bundle> getBundleByName(String name) {
+ return getBundleByNameAndVersion(name, null);
+ }
+
+ /**
+ * Get a bundles list with the name or symbolic name matching the name pattern and version matching the version pattern.
+ *
+ * @param name the bundle name or symbolic name regex to match.
+ * @param version the bundle version regex to match.
+ * @return the bundles list.
+ */
+ private List<Bundle> getBundleByNameAndVersion(String name, String version) {
+ Bundle[] bundles = bundleContext.getBundles();
+
+ ArrayList<Bundle> result = new ArrayList<Bundle>();
+
+ Pattern namePattern = Pattern.compile(name);
+
+ for (int i = 0; i < bundles.length; i++) {
+
+ String bundleSymbolicName = bundles[i].getSymbolicName();
+ // skip bundles without Bundle-SymbolicName header
+ if (bundleSymbolicName == null) {
+ continue;
+ }
+
+ Matcher symbolicNameMatcher = namePattern.matcher(bundleSymbolicName);
+
+ Matcher nameMatcher = null;
+ String bundleName = (String) bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
+ if (bundleName != null) {
+ nameMatcher = namePattern.matcher(bundleName);
+ }
+
+ if (version != null) {
+ String bundleVersion = (String) bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
+ if (bundleVersion != null) {
+ boolean nameMatch = (nameMatcher != null && nameMatcher.find()) || symbolicNameMatcher.find();
+ if (nameMatch) {
+ Pattern versionPattern = Pattern.compile(version);
+ Matcher versionMatcher = versionPattern.matcher(bundleVersion);
+ if (versionMatcher.find()) {
+ result.add(bundles[i]);
+ }
+ }
+ }
+ } else {
+ boolean nameMatch = (nameMatcher != null && nameMatcher.find()) || symbolicNameMatcher.find();
+ if (nameMatch) {
+ result.add(bundles[i]);
+ }
+ }
+ }
+ return result;
+ }
+
+}