You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2008/10/23 18:44:08 UTC

svn commit: r707413 - in /felix/trunk/webconsole/src/main: java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java resources/res/ui/events.js

Author: cziegeler
Date: Thu Oct 23 09:44:07 2008
New Revision: 707413

URL: http://svn.apache.org/viewvc?rev=707413&view=rev
Log:
FELIX-790 - Add initial support to display OSGi events.

Added:
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java   (with props)
    felix/trunk/webconsole/src/main/resources/res/ui/events.js   (with props)

Added: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java?rev=707413&view=auto
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java (added)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java Thu Oct 23 09:44:07 2008
@@ -0,0 +1,175 @@
+/*
+ * 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.felix.webconsole.internal.misc;
+
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.webconsole.internal.BaseWebConsolePlugin;
+import org.apache.felix.webconsole.internal.Util;
+import org.apache.felix.webconsole.internal.servlet.OsgiManager;
+import org.json.JSONException;
+import org.json.JSONWriter;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.event.*;
+
+
+/**
+ * @scr.component metatype="false"
+ * @scr.service interface="javax.servlet.Servlet"
+ * @scr.service interface="org.osgi.service.event.EventHandler"
+ * @scr.property name="event.topics" value="*"
+ * @scr.property name="felix.webconsole.label" valueRef="LABEL"
+ */
+public class EventAdminServlet
+extends BaseWebConsolePlugin
+implements EventHandler
+{
+
+    public static final String LABEL = "events";
+
+    public static final String TITLE = "Event Admin";
+
+    /** Number of events to be displayed. */
+    private int maxSize = 50;
+
+    protected final List events = new ArrayList();
+
+    public String getLabel()
+    {
+        return LABEL;
+    }
+
+
+    /**
+     * @see org.apache.felix.webconsole.AbstractWebConsolePlugin#getTitle()
+     */
+    public String getTitle()
+    {
+        return TITLE;
+    }
+
+    /**
+     * Activate this component.
+     */
+    protected void activate(ComponentContext context)
+    {
+        this.activate(context.getBundleContext());
+        this.events.clear();
+    }
+
+    /**
+     * Deactivate this component.
+     */
+    protected void deactivate(ComponentContext context)
+    {
+        this.deactivate();
+        this.events.clear();
+    }
+
+    /**
+     * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
+     */
+    public void handleEvent(Event event)
+    {
+        // we add everything which is not a log event
+        if ( !event.getTopic().startsWith("org/osgi/service/log") )
+        {
+            this.events.add(event);
+            if ( events.size() > this.maxSize )
+            {
+                events.remove(1);
+            }
+        }
+    }
+
+    protected void renderContent( HttpServletRequest request, HttpServletResponse response )
+    throws ServletException, IOException
+    {
+
+        PrintWriter pw = response.getWriter();
+
+        String appRoot = ( String ) request.getAttribute( OsgiManager.ATTR_APP_ROOT );
+        pw.println( "<script src='" + appRoot + "/res/ui/events.js' language='JavaScript'></script>" );
+
+        pw.println("<h1>Events</h1>");
+        final EventAdmin admin = (EventAdmin) this.getService(EventAdmin.class.getName());
+        if ( admin == null ) {
+            pw.println("<p><em>Event Admin is not installed.</em></p>");
+        }
+
+        Util.startScript( pw );
+        pw.println( "var eventListData = " );
+        JSONWriter jw = new JSONWriter( pw );
+        try
+        {
+            jw.object();
+
+            jw.key( "data" );
+
+            jw.array();
+
+            for(int i=0; i<events.size(); i++)
+            {
+                eventJson( jw, (Event)events.get(i), i );
+            }
+
+            jw.endArray();
+
+            jw.endObject();
+
+        }
+        catch ( JSONException je )
+        {
+            throw new IOException( je.toString() );
+        }
+
+        pw.println( ";" );
+        pw.println( "renderEvents( eventListData );" );
+        Util.endScript( pw );
+    }
+
+    private void eventJson( JSONWriter jw, Event e, int index)
+    throws JSONException
+    {
+        jw.object();
+        jw.key( "id" );
+        jw.value( String.valueOf(index) );
+        jw.key( "topic" );
+        jw.value( e.getTopic());
+        jw.key( "properties" );
+        jw.object();
+        final String[] names = e.getPropertyNames();
+        if ( names != null && names.length > 0 ) {
+            for(int i=0;i<names.length;i++) {
+                jw.key(names[i]);
+                jw.value(e.getProperty(names[i]).toString());
+            }
+        }
+        jw.endObject();
+
+        jw.endObject();
+    }
+
+}

Propchange: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/EventAdminServlet.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: felix/trunk/webconsole/src/main/resources/res/ui/events.js
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/resources/res/ui/events.js?rev=707413&view=auto
==============================================================================
--- felix/trunk/webconsole/src/main/resources/res/ui/events.js (added)
+++ felix/trunk/webconsole/src/main/resources/res/ui/events.js Thu Oct 23 09:44:07 2008
@@ -0,0 +1,117 @@
+/*
+ * 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 header( /* int */ columns )
+{
+    document.write( "<table class='content' cellpadding='0' cellspacing='0' width='100%'>" );
+
+    document.write( "<tr class='content'>" );
+    document.write( "<td colspan='" + columns + "' class='content'>&nbsp;</th>" );
+    document.write( "</tr>" );
+
+    document.write( "<tr class='content'>" );
+    document.write( "<th class='content' width='20%'>Topic</th>" );
+    document.write( "<th class='content'>Properties</th>" );
+    document.write( "</tr>" );
+
+}
+
+
+function error( /* int */ columns, /* String */ message )
+{
+    document.write( "<tr class='content'>" );
+    document.write( "<td class='content'>&nbsp;</td>" );
+    document.write( "<td class='content' colspan='" + (columns - 1) + "'>" + message + "</td>" );
+    document.write( "</tr>" );
+}
+
+
+function data( /* Array of Object */ dataArray )
+{
+    // render components
+    if (dataArray.length == 1)
+    {
+        entry( dataArray[0], true );
+    }
+    else {
+        for ( var idx in dataArray )
+        {
+            entry( dataArray[idx] );
+        }
+    }
+}
+
+
+function footer( /* int */ columns )
+{
+    document.write( "<tr class='content'>" );
+    document.write( "<td colspan='" + columns + "' class='content'>&nbsp;</th>" );
+    document.write( "</tr>" );
+
+    document.write( "</table>" );
+}
+
+
+function entry( /* Object */ dataEntry, /* boolean */ singleEntry )
+{
+    var trElement = tr( null, { id: "entry" + dataEntry.id } );
+    entryInternal( trElement,  dataEntry, singleEntry );
+    document.write( serialize( trElement ) );
+}
+
+
+function entryInternal( /* Element */ parent, /* Object */ dataEntry, /* boolean */ singleEntry )
+{
+
+    var id = dataEntry.id;
+    var topic = dataEntry.topic;
+    var properties = dataEntry.properties;
+
+    parent.appendChild( td( "content", { width: "20%"}, [ text( topic ) ] ) );
+
+    var tableE = createElement("table");
+    var bodyE = createElement("tbody");
+    tableE.appendChild(bodyE);
+
+    for( var p in dataEntry.properties ) {
+    	bodyE.appendChild(tr(null, null, [td(null, null, [text(p + " = " + dataEntry.properties[p])] )]));
+    }
+    
+    parent.appendChild( td( "content", null, [tableE] ) );
+}
+
+
+
+function renderEvents( /* Array of Data Objects */ bundleData )
+{
+
+    // topic and properties
+    var columns = 2;
+    
+    header( columns );
+
+    if (bundleData.error)
+    {
+        error( columns, bundleData.error );
+    }
+    else
+    {
+        data ( bundleData.data );
+    }
+
+    footer( columns );
+}

Propchange: felix/trunk/webconsole/src/main/resources/res/ui/events.js
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/webconsole/src/main/resources/res/ui/events.js
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: felix/trunk/webconsole/src/main/resources/res/ui/events.js
------------------------------------------------------------------------------
    svn:mime-type = text/plain