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/03/29 10:37:20 UTC
svn commit: r1086518 -
/karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java
Author: jbonofre
Date: Tue Mar 29 08:37:19 2011
New Revision: 1086518
URL: http://svn.apache.org/viewvc?rev=1086518&view=rev
Log:
[KARAF-452] Support bnudle id ranges, symbolic name/version and regex in the bundles commands.
Modified:
karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java
Modified: karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java?rev=1086518&r1=1086517&r2=1086518&view=diff
==============================================================================
--- karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java (original)
+++ karaf/branches/karaf-2.2.x/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/BundlesCommand.java Tue Mar 29 08:37:19 2011
@@ -18,16 +18,19 @@ package org.apache.karaf.shell.osgi;
import java.util.ArrayList;
import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Option;
import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
public abstract class BundlesCommand extends OsgiCommandSupport {
- @Argument(index = 0, name = "ids", description = "The list of bundle IDs separated by whitespaces", required = true, multiValued = true)
- List<Long> ids;
+ @Argument(index = 0, name = "ids", description = "The list of bundle (identified by IDs or name or name/version) separated by whitespaces", required = true, multiValued = true)
+ List<String> ids;
@Option(name = "--force", aliases = {}, description = "Forces the command to execute", required = false, multiValued = false)
boolean force;
@@ -35,20 +38,131 @@ public abstract class BundlesCommand ext
protected Object doExecute() throws Exception {
List<Bundle> bundles = new ArrayList<Bundle>();
if (ids != null && !ids.isEmpty()) {
- for (long id : ids) {
- Bundle bundle = getBundleContext().getBundle(id);
- if (bundle == null) {
- System.err.println("Bundle ID" + id + " is invalid");
- } else {
- if (force || !Util.isASystemBundle(getBundleContext(), bundle) || Util.accessToSystemBundleIsAllowed(bundle.getBundleId(), session)) {
- bundles.add(bundle);
+ for (String id : ids) {
+
+ // id is a number
+ Pattern pattern = Pattern.compile("^\\d+$");
+ Matcher matcher = pattern.matcher(id);
+ if (matcher.find()) {
+ Bundle bundle = this.getBundleById(id);
+ this.addBundle(bundle, id, force, bundles);
+ continue;
+ }
+
+ // id is a number range
+ pattern = Pattern.compile("^(\\d+)-(\\d+)$");
+ matcher = pattern.matcher(id);
+ if (matcher.find()) {
+ int index = id.indexOf('-');
+ Long startId = Long.valueOf(id.substring(0, index));
+ Long endId = Long.valueOf(id.substring(index + 1));
+ if (startId < endId) {
+ for (long i = startId; i <= endId; i++) {
+ Bundle bundle = getBundleContext().getBundle(i);
+ this.addBundle(bundle, id, force, bundles);
+ }
}
+ continue;
}
+
+ Bundle bundle = null;
+ int index = id.indexOf('/');
+ List<Bundle> bundlesByName = null;
+ if (index != -1) {
+ // user has provided name and version
+ bundlesByName = this.getBundleByNameAndVersion(id.substring(0, index), id.substring(index + 1));
+ } else {
+ // user has provided only the name
+ bundlesByName = this.getBundleByName(id);
+ }
+ for (Bundle bundleByName : bundlesByName) {
+ this.addBundle(bundleByName, id, force, bundles);
+ }
+
}
}
doExecute(bundles);
return null;
}
+ private void addBundle(Bundle bundle, String id, boolean force, List bundles) throws Exception {
+ if (bundle == null) {
+ // if the bundle is null here, it's because we didn't find it
+ System.err.println("Bundle " + id + " is invalid");
+ } else {
+ if (force || !Util.isASystemBundle(getBundleContext(), bundle) || Util.accessToSystemBundleIsAllowed(bundle.getBundleId(), session)) {
+ bundles.add(bundle);
+ }
+ }
+ }
+
+ /**
+ * Get a bundle identified by an id number.
+ *
+ * @param id the id number.
+ * @return the bundle or null if not found.
+ */
+ private Bundle getBundleById(String id) {
+ Bundle bundle = null;
+ try {
+ long idNumber = Long.valueOf(id);
+ bundle = getBundleContext().getBundle(idNumber);
+ } catch (NumberFormatException nfe) {
+ // 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 this.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 = getBundleContext().getBundles();
+
+ ArrayList<Bundle> result = new ArrayList<Bundle>();
+
+ Pattern namePattern = Pattern.compile(name);
+
+ for (int i = 0; i < bundles.length; i++) {
+
+ String bundleName = (String) bundles[i].getHeaders().get(Constants.BUNDLE_NAME);
+ String bundleSymbolicName = bundles[i].getSymbolicName();
+
+ Matcher nameMatcher = namePattern.matcher(bundleName);
+ Matcher symbolicNameMatcher = namePattern.matcher(bundleSymbolicName);
+
+ if (version != null) {
+
+ Pattern versionPattern = Pattern.compile(version);
+
+ String bundleVersion = (String) bundles[i].getHeaders().get(Constants.BUNDLE_VERSION);
+ Matcher versionMatcher = versionPattern.matcher(bundleVersion);
+
+ if ((nameMatcher.find() || symbolicNameMatcher.find()) && versionMatcher.find()) {
+ result.add(bundles[i]);
+ }
+ } else {
+ if (nameMatcher.find() || symbolicNameMatcher.find()) {
+ result.add(bundles[i]);
+ }
+ }
+ }
+ return result;
+ }
+
protected abstract void doExecute(List<Bundle> bundles) throws Exception;
}