Add factory detail (work in progress).
Add the ipojo.css file and load it.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@963387 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 a37bc93..5053938 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
@@ -5,6 +5,8 @@
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import javax.servlet.ServletException;
@@ -22,7 +24,6 @@
 import org.apache.felix.ipojo.architecture.Architecture;
 import org.apache.felix.webconsole.AbstractWebConsolePlugin;
 import org.apache.felix.webconsole.DefaultVariableResolver;
-import org.apache.felix.webconsole.SimpleWebConsolePlugin;
 import org.apache.felix.webconsole.WebConsoleUtil;
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -33,11 +34,12 @@
 @Instantiate
 public class IPOJOPlugin extends AbstractWebConsolePlugin {
     
-    private static final String CSS[] = { "/res/ui/bundles.css" };
+    private static final String CSS[] = { "/res/ui/bundles.css" , "/iPOJO_2/res/ui/ipojo.css" }; // TODO Change
 
     private final String INSTANCES;
     private final String FACTORIES;
     private final String HANDLERS;
+    private final String FACTORY_DETAILS;
 
     
     /**
@@ -53,7 +55,7 @@
     private String m_title = "iPOJO_2";  // TODO CHANGE
     
     @ServiceProperty(name= "felix.webconsole.css")
-    private String m_css = CSS[0];
+    protected String[] m_css = CSS;
 
     /**
      * List of available Architecture service.
@@ -77,38 +79,30 @@
         INSTANCES = readTemplateFile(this.getClass(), "/res/instances.html" );
         FACTORIES = readTemplateFile(this.getClass(), "/res/factories.html" );
         HANDLERS = readTemplateFile(this.getClass(), "/res/handlers.html" );
+        FACTORY_DETAILS = readTemplateFile(this.getClass(), "/res/factory.html" );
     }
     
-    private final String readTemplateFile(final Class clazz, final String templateFile)
-    {
-        InputStream templateStream = getClass().getResourceAsStream(templateFile);
-        if (templateStream != null)
-        {
+    private final String readTemplateFile(final Class clazz,
+            final String templateFile) {
+        InputStream templateStream = getClass().getResourceAsStream(
+                templateFile);
+        if (templateStream != null) {
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             byte[] data = new byte[1024];
-            try
-            {
+            try {
                 int len = 0;
-                while ((len = templateStream.read(data)) > 0)
-                {
+                while ((len = templateStream.read(data)) > 0) {
                     baos.write(data, 0, len);
                 }
                 return baos.toString("UTF-8");
-            }
-            catch (IOException e)
-            {
+            } catch (IOException e) {
                 // don't use new Exception(message, cause) because cause is 1.4+
                 throw new RuntimeException("readTemplateFile: Error loading "
-                    + templateFile + ": " + e);
-            }
-            finally
-            {
-                try
-                {
+                        + templateFile + ": " + e);
+            } finally {
+                try {
                     templateStream.close();
-                }
-                catch (IOException e)
-                {
+                } catch (IOException e) {
                     /* ignore */
                 }
 
@@ -116,8 +110,8 @@
         }
 
         // template file does not exist, return an empty string
-        log("readTemplateFile: File '" + templateFile + "' not found through class "
-            + clazz);
+        log("readTemplateFile: File '" + templateFile
+                + "' not found through class " + clazz);
         return "";
     }
 
@@ -128,15 +122,28 @@
         final RequestInfo reqInfo = getRequestInfo(request);
         // prepare variables
         DefaultVariableResolver vars = ( ( DefaultVariableResolver ) WebConsoleUtil.getVariableResolver( request ) );
-        String view = request.getParameter("view");
         
-        if (view == null || view.equals("instances")) {
-            response.getWriter().print( INSTANCES );
-        } else if (view.equals("factories")) {
-            response.getWriter().print( FACTORIES );
-        } else if (view.equals("handlers")) {
+        System.out.println("Render content for " + request.getPathInfo());
+        
+        if (reqInfo.instances) {
+            if (reqInfo.name == null) {
+                response.getWriter().print( INSTANCES );
+            } else {
+                // TODO
+            }
+        } else if (reqInfo.factories) {
+            if (reqInfo.name == null) {
+                response.getWriter().print( FACTORIES );
+            } else {
+                vars.put("name", reqInfo.name);
+                response.getWriter().print( FACTORY_DETAILS );
+            }
+        } else if (reqInfo.handlers) {
             response.getWriter().print( HANDLERS );
-        }             
+        } else {
+            // Default
+            response.getWriter().print( INSTANCES );
+        }
     }
     
     private void renderAllInstances(PrintWriter pw) {
@@ -233,6 +240,72 @@
         }
     }
     
+    private void renderFactoryDetail(PrintWriter pw, String name) {
+        System.out.println("Render factory detail for " + name);
+        // Find the factory
+        Factory factory = null;
+        for (Factory fact : m_factories) {
+            if (fact.getName().equals(name)) {
+                factory = fact;
+            }
+        }
+        
+        if (factory == null) {
+            // TODO Error management
+            System.err.println("factory " + name + "  not found");
+            return;
+        }
+        
+        try {
+            JSONObject resp = new JSONObject();
+            resp.put("count", m_factories.size());
+            resp.put("valid_count", getValidFactoriesCount());
+            resp.put("invalid_count", getInvalidFactoriesCount());
+            
+            // Factory object
+            JSONObject data = new JSONObject();
+            data.put("name", factory.getName());
+            data.put("state", getFactoryState(factory.getState()));
+            
+            String bundle = factory.getBundleContext().getBundle().getSymbolicName()
+            + " (" + factory.getBundleContext().getBundle().getBundleId() + ")";
+            data.put("bundle", bundle);
+            
+            if (factory.getComponentDescription().getprovidedServiceSpecification().length != 0) {
+                JSONArray services = new JSONArray
+                    (Arrays.asList(factory.getComponentDescription().getprovidedServiceSpecification()));
+                data.put("services", services);
+            }
+            
+            if (! factory.getRequiredHandlers().isEmpty()) {
+                JSONArray req = new JSONArray
+                    (factory.getRequiredHandlers());
+                data.put("requiredHandlers", req);
+            }
+            
+            if (! factory.getMissingHandlers().isEmpty()) {
+                JSONArray req = new JSONArray
+                    (factory.getMissingHandlers());
+                data.put("missingHandlers", req);
+            }
+            
+            List instances = getInstanceList(name);
+            if (! instances.isEmpty()) {
+                JSONArray req = new JSONArray(instances);
+                data.put("instances", req);
+            }
+            
+            data.put("architecture", factory.getDescription().toString());
+            resp.put("data", data);
+            
+            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 {
@@ -257,7 +330,8 @@
                     this.renderAllFactories(response.getWriter());
                     return;
                 } else {
-                    // TODO
+                    System.out.println("Render details for " + reqInfo.name);
+                    this.renderFactoryDetail(response.getWriter(), reqInfo.name);
                     return;
                 }
             }
@@ -383,6 +457,22 @@
         }
     }
     
+    /**
+     * Gets the instance list created by the given factory.
+     * @param factory the factory name
+     * @return the list containing the created instances (name)
+     */
+    private List getInstanceList(String factory) {
+        List list = new ArrayList();
+        for (Architecture arch : m_archs) { // Cannot be null, an empty list is returned.
+            String n = arch.getInstanceDescription().getComponentDescription().getName();
+            if (factory.equals(n)) {
+                list.add(arch.getInstanceDescription().getName());
+            }
+        }
+        return list;
+    }
+    
     private final class RequestInfo {
         public final String extension;
         public final String path;
@@ -408,15 +498,18 @@
 
             if (info.startsWith("/")) {
                 path = info.substring(1);
+                System.out.println("Info " + info);
+
                 instances = path.startsWith("instances");
                 factories = path.startsWith("factories");
                 handlers = path.startsWith("handlers");
                
+                System.out.println("Path " + path);
                 
                 if (instances  && path.startsWith("instances/")) {
-                    name = path.substring(0, "instances".length() + 1);
+                    name = path.substring("instances".length() + 1);
                 } else if (factories  && path.startsWith("factories/")) {
-                    name = path.substring(0, "factories".length() + 1);
+                    name = path.substring("factories".length() + 1);
                 } else {
                     name = null;
                 }
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/factories.html b/ipojo/webconsole-plugin/src/main/resources/res/factories.html
index 16a186a..f49ed6e 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/factories.html
+++ b/ipojo/webconsole-plugin/src/main/resources/res/factories.html
@@ -1,5 +1,10 @@
 <script type="text/javascript" src="${pluginRoot}/res/ui/factory.js"></script>
-
+<script>
+    var root_url = '${pluginRoot}';
+    var instances_url = '${pluginRoot}' + '/instances';
+    var factories_url = '${pluginRoot}' + '/factories';
+    var handlers_url = '${pluginRoot}' + '/handlers';
+</script>
 
 <!-- status line -->
 <p class="statline">&nbsp;</p>
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/handlers.html b/ipojo/webconsole-plugin/src/main/resources/res/handlers.html
index b93da53..b5edb5f 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/handlers.html
+++ b/ipojo/webconsole-plugin/src/main/resources/res/handlers.html
@@ -1,5 +1,10 @@
 <script type="text/javascript" src="${pluginRoot}/res/ui/handler.js"></script>
-
+<script>
+    var root_url = '${pluginRoot}';
+    var instances_url = '${pluginRoot}' + '/instances';
+    var factories_url = '${pluginRoot}' + '/factories';
+    var handlers_url = '${pluginRoot}' + '/handlers';
+</script>
 
 <!-- status line -->
 <p class="statline">&nbsp;</p>
diff --git a/ipojo/webconsole-plugin/src/main/resources/res/instances.html b/ipojo/webconsole-plugin/src/main/resources/res/instances.html
index 42c3cc0..1c83905 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/instances.html
+++ b/ipojo/webconsole-plugin/src/main/resources/res/instances.html
@@ -1,4 +1,10 @@
 <script type="text/javascript" src="${pluginRoot}/res/ui/ipojo.js"></script>
+<script>
+    var root_url = '${pluginRoot}';
+    var instances_url = '${pluginRoot}' + '/instances';
+    var factories_url = '${pluginRoot}' + '/factories';
+    var handlers_url = '${pluginRoot}' + '/handlers';
+</script>
 
 <!-- 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 50b8ba3..8fc76cf 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/factory.js
@@ -38,7 +38,7 @@
 
     var _ = tableEntryTemplate.clone().appendTo(tableBody).attr('id', 'factory-' + factory.name);
 
-    _.find('td.name').html('<a href="' + window.location.pathname + '/factories/' + name + '">' + name + '</a>');
+    _.find('td.name').html('<a href="' + factories_url + '/' + name + '">' + name + '</a>');
     _.find('td.bundle').text(bundle);
     _.find('td.state').text(state);
 }
@@ -53,11 +53,11 @@
 
 function loadInstancesData() {
     console.log("Go to instances"); 
-    window.location=window.location.pathname + "?view=instances"
+    window.location = instances_url;
 }
 
 function loadHandlersData() {
-    window.location=window.location.pathname + "?view=handlers"
+    window.location = handlers_url;
 }
 
 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
index cddbd60..6298146 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/ui/handler.js
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/handler.js
@@ -60,11 +60,11 @@
 }
 
 function loadInstancesData() {
-    window.location=window.location.pathname + "?view=instances"
+    window.location = instances_url;
 }
 
 function loadFactoriesData() {
-    window.location=window.location.pathname + "?view=factories"
+    window.location = factories_url;
 }
 
 var tableBody = false;
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 0fea36e..f59f15f 100644
--- a/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js
+++ b/ipojo/webconsole-plugin/src/main/resources/res/ui/ipojo.js
@@ -49,12 +49,11 @@
 }
 
 function loadFactoriesData() {
-    console.log("Go to factories data");
-    window.location=window.location.pathname + '?view=factories'; 
+    window.location = factories_url; 
 }
 
 function loadHandlersData() {
-    window.location=window.location.pathname + "?view=handlers"
+    window.location = handlers_url;
 }
 
 var tableBody = false;