Add the handler list support.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@963383 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/webconsole-plugin/src/main/java/org/apache/felix/ipojo/webconsole/IPOJOPlugin.java b/ipojo/webconsole-plugin/src/main/java/org/apache/felix/ipojo/webconsole/IPOJOPlugin.java
index 915b653..a37bc93 100644
--- a/ipojo/webconsole-plugin/src/main/java/org/apache/felix/ipojo/webconsole/IPOJOPlugin.java
+++ b/ipojo/webconsole-plugin/src/main/java/org/apache/felix/ipojo/webconsole/IPOJOPlugin.java
@@ -37,6 +37,8 @@
 
     private final String INSTANCES;
     private final String FACTORIES;
+    private final String HANDLERS;
+
     
     /**
      * Label used by the web console.
@@ -74,7 +76,7 @@
     public IPOJOPlugin() {
         INSTANCES = readTemplateFile(this.getClass(), "/res/instances.html" );
         FACTORIES = readTemplateFile(this.getClass(), "/res/factories.html" );
-
+        HANDLERS = readTemplateFile(this.getClass(), "/res/handlers.html" );
     }
     
     private final String readTemplateFile(final Class clazz, final String templateFile)
@@ -132,7 +134,9 @@
             response.getWriter().print( INSTANCES );
         } else if (view.equals("factories")) {
             response.getWriter().print( FACTORIES );
-        }                
+        } else if (view.equals("handlers")) {
+            response.getWriter().print( HANDLERS );
+        }             
     }
     
     private void renderAllInstances(PrintWriter pw) {
@@ -164,7 +168,7 @@
             JSONObject resp = new JSONObject();
             resp.put("count", m_factories.size());
             resp.put("valid_count", getValidFactoriesCount());
-            resp.put("invalid_count", getValidFactoriesCount());
+            resp.put("invalid_count", getInvalidFactoriesCount());
             
             JSONArray factories = new JSONArray();
             for (Factory factory : m_factories) {
@@ -192,6 +196,43 @@
         }
     }
     
+    private void renderAllHandlers(PrintWriter pw) {
+        try {
+            JSONObject resp = new JSONObject();
+            resp.put("count", m_handlers.size());
+            resp.put("valid_count", getValidHandlersCount());
+            resp.put("invalid_count", getInvalidHandlersCount());
+            
+            JSONArray factories = new JSONArray();
+            for (HandlerFactory factory : m_handlers) {
+                String version = factory.getVersion();
+                String name = factory.getHandlerName();
+                
+                String state = getFactoryState(factory.getState());
+                String bundle = factory.getBundleContext().getBundle().getSymbolicName()
+                    + " (" + factory.getBundleContext().getBundle().getBundleId() + ")";
+                JSONObject fact = new JSONObject();
+                fact.put("name", name);
+                if (version != null) {
+                    fact.put("version", version);
+                }
+                fact.put("bundle", bundle);
+                fact.put("state", state);
+                fact.put("type", factory.getType());
+                if (! factory.getMissingHandlers().isEmpty()) {
+                    fact.put("missing", factory.getMissingHandlers().toString());
+                }
+                factories.put(fact);
+            }
+            resp.put("data", factories);
+            
+            pw.print(resp.toString());
+        } catch (JSONException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+    
     @Override
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response) throws ServletException, IOException {
@@ -222,8 +263,7 @@
             }
             
             if (reqInfo.handlers) {
-                //TODO
-                return;
+                this.renderAllHandlers(response.getWriter());
             }
             // nothing more to do
             return;
@@ -287,6 +327,26 @@
         return i;
     }
     
+    private int getValidHandlersCount() {
+        int i = 0;
+        for (Factory a : m_handlers) { // Cannot be null, an empty list is returned.
+            if (a.getState() == Factory.VALID) {
+                i ++;
+            }
+        }
+        return i;
+    }
+    
+    private int getInvalidHandlersCount() {
+        int i = 0;
+        for (Factory a : m_handlers) { // Cannot be null, an empty list is returned.
+            if (a.getState() == Factory.INVALID) {
+                i ++;
+            }
+        }
+        return i;
+    }
+    
     /**
      * Gets the instance state as a String.
      * @param state the state.
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/handlers.html b/ipojo/webconsole-plugin/src/main/resources/res/handlers.html
new file mode 100644
index 0000000..b93da53
--- /dev/null
+++ b/ipojo/webconsole-plugin/src/main/resources/res/handlers.html
@@ -0,0 +1,47 @@
+<script type="text/javascript" src="${pluginRoot}/res/ui/handler.js"></script>
+
+
+<!-- status line -->
+<p class="statline">&nbsp;</p>
+
+<!-- top header -->
+<form method="post" enctype="multipart/form-data" action="">
+	<div class="ui-widget-header ui-corner-top buttonGroup">
+		<button class="instancesButton" type="button">Instances</button>
+		<button class="factoriesButton" type="button">Factories</button>
+		<button class="handlersButton" type="button">Handlers</button>
+	</div>
+</form>
+
+<table id="plugin_table" class="tablesorter nicetable noauto">
+	<thead>
+		<tr>
+			<th class="col_Name">Handler Name</th>
+            <th class="col_Type">Handler Type</th>
+			<th class="col_Bundle">Bundle</th>
+			<th class="col_State">State</th>
+            <th class="col_Missing">Missing Handlers</th>
+		</tr>
+	</thead>
+	<tbody>
+		<tr><!-- template -->
+			<td class="name">&nbsp;	</td>
+            <td class="type">&nbsp; </td>
+			<td class="bundle">&nbsp;</td>
+			<td class="state">&nbsp;</td>
+            <td class="missing">&nbsp; </td>
+		</tr>
+	</tbody>
+</table>
+
+<!-- bottom header -->
+<form method="post" enctype="multipart/form-data" action="">
+    <div class="ui-widget-header ui-corner-bottom buttonGroup">
+        <button class="instancesButton" type="button">Instances</button>
+        <button class="factoriesButton" type="button">Factories</button>
+        <button class="handlersButton" type="button">Handlers</button>
+    </div>
+</form>
+
+<!-- status line -->
+<p class="statline">&nbsp;</p>
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js b/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js
index 67f03e6..50b8ba3 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js
@@ -57,7 +57,7 @@
 }
 
 function loadHandlersData() {
-    console.log("Load handlers data"); 
+    window.location=window.location.pathname + "?view=handlers"
 }
 
 var tableBody = false;
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/ui/handler.js b/ipojo/webconsole-plugin/src/main/resources/res/ui/handler.js
new file mode 100644
index 0000000..cddbd60
--- /dev/null
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/handler.js
@@ -0,0 +1,95 @@
+/*
+ * 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.
+ */
+function renderHandlersData(handlers)  {
+    $(".statline").html(getHandlersStatLine(handlers));
+    tableBody.empty();
+    for ( var idx in handlers.data ) {
+        handlersEntry( handlers.data[idx] );
+    }
+    $("#plugin_table").trigger("update");
+}
+
+function getHandlersStatLine(handlers) {
+    return handlers.count + " handlers in total, "
+        + handlers.valid_count + " valid handlers, "
+        + handlers.invalid_count + " invalid handlers.";
+}
+
+function handlersEntry(handler) {
+    var name = handler.name;
+    var state = handler.state;
+    var bundle = handler.bundle;
+    var type = handler.type;
+
+    console.log("Create entry : " + handler);
+
+    var _ = tableEntryTemplate.clone().appendTo(tableBody).attr('id', 'handler-' + handler.name);
+
+    _.find('td.name').text(name);
+    _.find('td.type').text(type);
+    _.find('td.bundle').text(bundle);
+    _.find('td.state').text(state);
+    if (handler.missing) {
+        _.find('td.missing').html(handler.missing);    
+    } else {
+        _.find('td.missing').html('<i>No missing handlers</i>');
+    }
+    
+}
+
+
+function loadHandlersData() {
+    console.log("Load handlers data");
+	$.get(pluginRoot + "/handlers.json", null, function(data) {
+		renderHandlersData(data);
+	}, "json");	
+}
+
+function loadInstancesData() {
+    window.location=window.location.pathname + "?view=instances"
+}
+
+function loadFactoriesData() {
+    window.location=window.location.pathname + "?view=factories"
+}
+
+var tableBody = false;
+var tableEntryTemplate = false;
+
+$(document).ready(function(){
+	tableBody = $('#plugin_table tbody');
+	tableEntryTemplate = tableBody.find('tr').clone();
+
+    loadHandlersData();
+	
+    $(".instancesButton").click(loadInstancesData);
+    $(".factoriesButton").click(loadFactoriesData);
+    $(".handlersButton").click(loadHandlersData);
+
+	var extractMethod = function(node) {
+		var link = node.getElementsByTagName("a");
+		if ( link && link.length == 1 ) {
+			return link[0].innerHTML;
+		}
+		return node.innerHTML;
+	};
+	$("#plugin_table").tablesorter({
+		sortList: [[1,0]],
+		textExtraction:extractMethod
+	});
+});
+
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js b/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js
index 6504308..0fea36e 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js
@@ -54,7 +54,7 @@
 }
 
 function loadHandlersData() {
-    console.log("Load handlers data"); 
+    window.location=window.location.pathname + "?view=handlers"
 }
 
 var tableBody = false;