Repository: karaf-cellar
Updated Branches:
  refs/heads/master 010d7abff -> 5379aef34


[KARAF-3051] Refactoring of the obr module to use Karaf 3.x syntax


Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/5379aef3
Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/5379aef3
Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/5379aef3

Branch: refs/heads/master
Commit: 5379aef3499c01092a5fa9afc165af6341862322
Parents: 010d7ab
Author: Jean-Baptiste Onofré <[email protected]>
Authored: Thu Jun 19 07:11:02 2014 +0200
Committer: Jean-Baptiste Onofré <[email protected]>
Committed: Thu Jun 19 07:11:02 2014 +0200

----------------------------------------------------------------------
 obr/pom.xml                                     |  9 ++++---
 .../karaf/cellar/obr/ClusterObrBundleEvent.java | 16 ++++++++----
 .../org/apache/karaf/cellar/obr/Constants.java  |  2 --
 .../karaf/cellar/obr/ObrBundleEventHandler.java | 15 +++++------
 .../cellar/obr/management/CellarOBRMBean.java   | 17 ++++++++++---
 .../management/internal/CellarOBRMBeanImpl.java | 15 ++++++-----
 .../cellar/obr/shell/ObrDeployCommand.java      |  7 +++---
 .../karaf/cellar/obr/shell/ObrListCommand.java  | 26 +++++++++++---------
 8 files changed, 66 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/pom.xml
----------------------------------------------------------------------
diff --git a/obr/pom.xml b/obr/pom.xml
index 132476d..23e5fde 100644
--- a/obr/pom.xml
+++ b/obr/pom.xml
@@ -62,14 +62,17 @@
     </properties>
 
     <dependencies>
-
-        <!-- Cellar Core -->
+        <!-- Core dependencies -->
         <dependency>
             <groupId>org.apache.karaf.cellar</groupId>
             <artifactId>org.apache.karaf.cellar.core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.table</artifactId>
+        </dependency>
 
-        <!-- OBR -->
+        <!-- OBR dependency -->
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.bundlerepository</artifactId>

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/ClusterObrBundleEvent.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/ClusterObrBundleEvent.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/ClusterObrBundleEvent.java
index 4222ed5..4d6d61d 100644
--- a/obr/src/main/java/org/apache/karaf/cellar/obr/ClusterObrBundleEvent.java
+++ b/obr/src/main/java/org/apache/karaf/cellar/obr/ClusterObrBundleEvent.java
@@ -21,20 +21,26 @@ import org.apache.karaf.cellar.core.event.Event;
 public class ClusterObrBundleEvent extends Event {
 
     private String bundleId;
-    private int type;
+    private Boolean start;
+    private Boolean deployOptional;
 
-    public ClusterObrBundleEvent(String bundleId, int type) {
+    public ClusterObrBundleEvent(String bundleId, boolean start, boolean 
deployOptional) {
         super(bundleId);
         this.bundleId = bundleId;
-        this.type = type;
+        this.start = start;
+        this.deployOptional = deployOptional;
     }
 
     public String getBundleId() {
         return this.bundleId;
     }
 
-    public int getType() {
-        return this.type;
+    public boolean getStart() {
+        return this.start;
+    }
+
+    public boolean getDeployOptional() {
+        return this.deployOptional;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/Constants.java
----------------------------------------------------------------------
diff --git a/obr/src/main/java/org/apache/karaf/cellar/obr/Constants.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/Constants.java
index 18cddff..9a170ff 100644
--- a/obr/src/main/java/org/apache/karaf/cellar/obr/Constants.java
+++ b/obr/src/main/java/org/apache/karaf/cellar/obr/Constants.java
@@ -27,6 +27,4 @@ public class Constants {
     public static final int URL_ADD_EVENT_TYPE = 1;
     public static final int URL_REMOVE_EVENT_TYPE = 0;
 
-    public static final int BUNDLE_START_EVENT_TYPE = 1;
-
 }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/ObrBundleEventHandler.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/ObrBundleEventHandler.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/ObrBundleEventHandler.java
index 3e06b31..0d7f137 100644
--- a/obr/src/main/java/org/apache/karaf/cellar/obr/ObrBundleEventHandler.java
+++ b/obr/src/main/java/org/apache/karaf/cellar/obr/ObrBundleEventHandler.java
@@ -132,22 +132,23 @@ public class ObrBundleEventHandler extends ObrSupport 
implements EventHandler<Cl
         }
 
         String bundleId = event.getBundleId();
+        boolean deployOptional = event.getDeployOptional();
+        boolean start = event.getStart();
         try {
             if (isAllowed(event.getSourceGroup(), 
Constants.BUNDLES_CONFIG_CATEGORY, bundleId, EventType.INBOUND)) {
                 Resolver resolver = obrService.resolver();
                 String[] target = getTarget(bundleId);
                 Resource resource = 
selectNewestVersion(searchRepository(target[0], target[1]));
-                if (resource != null) {
-                    resolver.add(resource);
-                } else {
+                if (resource == null) {
                     LOGGER.warn("CELLAR OBR: bundle {} unknown", target[0]);
+                    return;
                 }
+                resolver.add(resource);
+
                 if ((resolver.getAddedResources() != null) &&
                         (resolver.getAddedResources().length > 0)) {
-                    if (resolver.resolve()) {
-                        if (event.getType() == 
Constants.BUNDLE_START_EVENT_TYPE)
-                            resolver.deploy(Resolver.START);
-                        else resolver.deploy(0);
+                    if (resolver.resolve(deployOptional ? 0 : 
Resolver.NO_OPTIONAL_RESOURCES)) {
+                        resolver.deploy(start ? Resolver.START : 0);
                     }
                 } else {
                     Reason[] reqs = resolver.getUnsatisfiedRequirements();

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
index 915e99c..6e9cee0 100644
--- 
a/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
+++ 
b/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
@@ -28,7 +28,7 @@ public interface CellarOBRMBean {
      * @return the list of OBR URLs.
      * @throws Exception in case of retrieval failure.
      */
-    List<String> listUrls(String groupName) throws Exception;
+    List<String> getUrls(String groupName) throws Exception;
 
     /**
      * List the OBR bundles in a cluster group.
@@ -37,7 +37,7 @@ public interface CellarOBRMBean {
      * @return the list of OBR bundles.
      * @throws Exception in case of retrieval failure.
      */
-    TabularData listBundles(String groupName) throws Exception;
+    TabularData getBundles(String groupName) throws Exception;
 
     /**
      * Add an OBR URL in a cluster group.
@@ -64,6 +64,17 @@ public interface CellarOBRMBean {
      * @param bundleId the bundle ID.
      * @throws Exception in case of deploy failure.
      */
-    void deploy(String groupName, String bundleId) throws Exception;
+    void deployBundle(String groupName, String bundleId) throws Exception;
+
+    /**
+     * Deploy an OBR bundle in a cluster group.
+     *
+     * @param groupName the cluster group name.
+     * @param bundleId the bundle ID.
+     * @param start true to start the bundle, false else.
+     * @param deployOptional true to set the deployment as optional, false 
else.
+     * @throws Exception
+     */
+    void deployBundle(String groupName, String bundleId, boolean start, 
boolean deployOptional) throws Exception;
 
 }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
 
b/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
index 2a2ac69..f374ae0 100644
--- 
a/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
+++ 
b/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
@@ -50,7 +50,7 @@ public class CellarOBRMBeanImpl extends StandardMBean 
implements CellarOBRMBean
     }
 
     @Override
-    public List<String> listUrls(String groupName) throws Exception {
+    public List<String> getUrls(String groupName) throws Exception {
         // check if the group exists
         Group group = groupManager.findGroupByName(groupName);
         if (group == null) {
@@ -66,7 +66,7 @@ public class CellarOBRMBeanImpl extends StandardMBean 
implements CellarOBRMBean
     }
 
     @Override
-    public TabularData listBundles(String groupName) throws Exception {
+    public TabularData getBundles(String groupName) throws Exception {
         // check if the group exists
         Group group = groupManager.findGroupByName(groupName);
         if (group == null) {
@@ -186,7 +186,7 @@ public class CellarOBRMBeanImpl extends StandardMBean 
implements CellarOBRMBean
     }
 
     @Override
-    public void deploy(String groupName, String bundleId) throws Exception {
+    public void deployBundle(String groupName, String bundleId, boolean start, 
boolean deployOptional) throws Exception {
         // check if the group exists
         Group group = groupManager.findGroupByName(groupName);
         if (group == null) {
@@ -208,13 +208,16 @@ public class CellarOBRMBeanImpl extends StandardMBean 
implements CellarOBRMBean
         }
 
         // broadcast a cluster event
-        int type = 0;
-        ClusterObrBundleEvent event = new ClusterObrBundleEvent(bundleId, 
type);
-        event.setForce(true);
+        ClusterObrBundleEvent event = new ClusterObrBundleEvent(bundleId, 
start, deployOptional);
         event.setSourceGroup(group);
         eventProducer.produce(event);
     }
 
+    @Override
+    public void deployBundle(String groupName, String bundleId) throws 
Exception {
+        this.deployBundle(groupName, bundleId, false, false);
+    }
+
     public ClusterManager getClusterManager() {
         return clusterManager;
     }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrDeployCommand.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrDeployCommand.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrDeployCommand.java
index 7c28539..7c39b66 100644
--- a/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrDeployCommand.java
+++ b/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrDeployCommand.java
@@ -35,6 +35,9 @@ public class ObrDeployCommand extends ObrCommandSupport {
     @Option(name = "-s", aliases = { "--start" }, description = "Start the 
deployed bundles.", required = false, multiValued = false)
     boolean start = false;
 
+    @Option(name = "-d", aliases = { "--deployOptional" }, description = 
"Deploy optional bundles", required = false, multiValued = false)
+    boolean deployOptional = false;
+
     private EventProducer eventProducer;
 
     @Override
@@ -59,9 +62,7 @@ public class ObrDeployCommand extends ObrCommandSupport {
         }
 
         // broadcast a cluster event
-        int type = 0;
-        if (start) type = Constants.BUNDLE_START_EVENT_TYPE;
-        ClusterObrBundleEvent event = new ClusterObrBundleEvent(bundleId, 
type);
+        ClusterObrBundleEvent event = new ClusterObrBundleEvent(bundleId, 
start, deployOptional);
         event.setSourceGroup(group);
         eventProducer.produce(event);
 

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/5379aef3/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
----------------------------------------------------------------------
diff --git 
a/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java 
b/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
index 5cba73b..848ade1 100644
--- a/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
+++ b/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
@@ -20,6 +20,8 @@ import org.apache.karaf.cellar.obr.Constants;
 import org.apache.karaf.cellar.obr.ObrBundleInfo;
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.apache.karaf.shell.table.ShellTable;
 
 import java.util.Set;
 
@@ -29,6 +31,9 @@ public class ObrListCommand extends CellarCommandSupport {
     @Argument(index = 0, name = "group", description = "The cluster group 
name", required = true, multiValued = false)
     String groupName;
 
+    @Option(name = "--no-format", description = "Disable table rendered 
output", required = false, multiValued = false)
+    boolean noFormat;
+
     @Override
     public Object doExecute() {
         // check if the group exists
@@ -42,20 +47,17 @@ public class ObrListCommand extends CellarCommandSupport {
         try {
             
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
             Set<ObrBundleInfo> clusterBundles = 
clusterManager.getSet(Constants.BUNDLES_DISTRIBUTED_SET_NAME + 
Configurations.SEPARATOR + groupName);
-            int maxPName = 4;
-            int maxSName = 13;
-            int maxVersion = 7;
-            for (ObrBundleInfo bundle : clusterBundles) {
-                maxPName = Math.max(maxPName, 
emptyIfNull(bundle.getPresentationName()).length());
-                maxSName = Math.max(maxSName, 
emptyIfNull(bundle.getSymbolicName()).length());
-                maxVersion = Math.max(maxVersion, 
emptyIfNull(bundle.getVersion()).length());
-            }
-            String formatHeader = "  %-" + maxPName + "s  %-" + maxSName + "s  
 %-" + maxVersion + "s";
-            String formatLine = "[%-" + maxPName + "s] [%-" + maxSName + "s] 
[%-" + maxVersion + "s]";
-            System.out.println(String.format(formatHeader, "NAME", "SYMBOLIC 
NAME", "VERSION"));
+
+            ShellTable table = new ShellTable();
+            table.column("Name");
+            table.column("Symbolic Name");
+            table.column("Version");
+
             for (ObrBundleInfo bundle : clusterBundles) {
-                System.out.println(String.format(formatLine, 
emptyIfNull(bundle.getPresentationName()), 
emptyIfNull(bundle.getSymbolicName()), emptyIfNull(bundle.getVersion())));
+                
table.addRow().addContent(emptyIfNull(bundle.getPresentationName()), 
emptyIfNull(bundle.getSymbolicName()), emptyIfNull(bundle.getVersion()));
             }
+
+            table.print(System.out, !noFormat);
         } finally {
             Thread.currentThread().setContextClassLoader(originalClassLoader);
         }

Reply via email to