You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ma...@apache.org on 2012/03/05 23:00:24 UTC

svn commit: r1297245 - in /ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui: Activator.java LogViewerExtension.java

Author: marrs
Date: Mon Mar  5 22:00:23 2012
New Revision: 1297245

URL: http://svn.apache.org/viewvc?rev=1297245&view=rev
Log:
ACE-225 applied the patch

Modified:
    ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/Activator.java
    ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/LogViewerExtension.java

Modified: ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/Activator.java?rev=1297245&r1=1297244&r2=1297245&view=diff
==============================================================================
--- ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/Activator.java (original)
+++ ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/Activator.java Mon Mar  5 22:00:23 2012
@@ -20,34 +20,35 @@ package org.apache.ace.server.log.ui;
 
 import java.util.Properties;
 
-import org.apache.ace.client.repository.repository.DeploymentVersionRepository;
 import org.apache.ace.server.log.store.LogStore;
 import org.apache.ace.webui.UIExtensionFactory;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.service.log.LogService;
 
 public class Activator extends DependencyActivatorBase {
 
-	@Override
-	public void init(BundleContext context, DependencyManager manager)
-			throws Exception {
-		manager.add(createComponent().setInterface(
-                UIExtensionFactory.class.getName(), new Properties() {
-                    {
-                        put(UIExtensionFactory.EXTENSION_POINT_KEY, UIExtensionFactory.EXTENSION_POINT_VALUE_TARGET);
-                    }
-                }).setImplementation(LogViewerExtension.class)
-                .add(createServiceDependency()
-                		.setService(LogStore.class)
-                		.setRequired(true)));
-	}
-
-	@Override
-	public void destroy(BundleContext context, DependencyManager manager)
-			throws Exception {
-		// TODO Auto-generated method stub
-		
-	}
-
+    @Override
+    public void init(BundleContext context, DependencyManager manager) throws Exception {
+        Properties props = new Properties();
+        props.put(UIExtensionFactory.EXTENSION_POINT_KEY, UIExtensionFactory.EXTENSION_POINT_VALUE_TARGET);
+        props.put(Constants.SERVICE_RANKING, Integer.valueOf(10));
+        
+        manager.add(createComponent().setInterface(UIExtensionFactory.class.getName(), props)
+            .setImplementation(LogViewerExtension.class)
+            .add(createServiceDependency()
+                .setService(LogStore.class)
+                .setRequired(true))
+            .add(createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false))
+                );
+    }
+
+    @Override
+    public void destroy(BundleContext context, DependencyManager manager) throws Exception {
+        // Nop
+    }
 }

Modified: ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/LogViewerExtension.java
URL: http://svn.apache.org/viewvc/ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/LogViewerExtension.java?rev=1297245&r1=1297244&r2=1297245&view=diff
==============================================================================
--- ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/LogViewerExtension.java (original)
+++ ace/trunk/ace-server-log-ui/src/main/java/org/apache/ace/server/log/ui/LogViewerExtension.java Mon Mar  5 22:00:23 2012
@@ -18,16 +18,15 @@
  */
 package org.apache.ace.server.log.ui;
 
+import java.io.IOException;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.Date;
 import java.util.Dictionary;
 import java.util.Enumeration;
-import java.util.Iterator;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
 
 import org.apache.ace.client.repository.RepositoryObject;
 import org.apache.ace.client.repository.object.GatewayObject;
@@ -38,6 +37,7 @@ import org.apache.ace.log.LogEvent;
 import org.apache.ace.server.log.store.LogStore;
 import org.apache.ace.webui.NamedObject;
 import org.apache.ace.webui.UIExtensionFactory;
+import org.osgi.service.log.LogService;
 
 import com.vaadin.ui.Component;
 import com.vaadin.ui.Label;
@@ -45,76 +45,145 @@ import com.vaadin.ui.Table;
 import com.vaadin.ui.TextArea;
 import com.vaadin.ui.VerticalLayout;
 
+/**
+ * Provides a simple AuditLog viewer for targets.
+ */
 public class LogViewerExtension implements UIExtensionFactory {
+    
+    private static final String CAPTION = "LogViewer";
+
+    private static final String COL_TIME = "Time";
+    private static final String COL_TYPE = "Type";
+    private static final String COL_PROPERTIES = "Properties";
+    
+    private static final String FILL_AREA = "100%";
+    
     private volatile LogStore m_store;
+    private volatile LogService m_logService;
 
+    /** contains a mapping of event type to a string representation of that type. */
+    private final Map<Integer, String> m_eventTypeMapping = new HashMap<Integer, String>();
+
+    /**
+     * {@inheritDoc}
+     */
     public Component create(Map<String, Object> context) {
         RepositoryObject object = getRepositoryObjectFromContext(context);
+        if (object instanceof StatefulGatewayObject && !((StatefulGatewayObject) object).isRegistered()) {
+            VerticalLayout result = new VerticalLayout();
+            result.setCaption(CAPTION);
+            result.addComponent(new Label("This target is not yet registered, so it has no log."));
+            return result;
+        }
+
         Table table = new Table();
-        table.setWidth("100%");
-        table.setHeight("100%");
-        table.setCaption("LogViewer");
-        table.addContainerProperty("Time", Date.class, null);
-        table.addContainerProperty("Type", String.class, null);
-        table.addContainerProperty("Properties", TextArea.class, null);
-        table.setColumnExpandRatio("Properties", 1);
+        table.setWidth(FILL_AREA);
+        table.setHeight(FILL_AREA);
+        table.setCaption(CAPTION);
+        table.addContainerProperty(COL_TIME, Date.class, null);
+        table.addContainerProperty(COL_TYPE, String.class, null);
+        table.addContainerProperty(COL_PROPERTIES, TextArea.class, null);
+        table.setColumnExpandRatio(COL_PROPERTIES, 1);
         try {
-            if (object instanceof StatefulGatewayObject) {
-                StatefulGatewayObject statefulTarget = (StatefulGatewayObject) object;
-                if (statefulTarget.isRegistered()) {
-                    String id = object.getAttribute(GatewayObject.KEY_ID);
-                    List<LogDescriptor> desc = m_store.getDescriptors(id);
-                    if (desc != null) {
-                        for (LogDescriptor log : desc) {
-                            for (LogEvent event : m_store.get(log)) {
-                                Dictionary props = event.getProperties();
-                                Enumeration keys = props.keys();
-                                Set<String> keySet = new TreeSet<String>();
-                                while (keys.hasMoreElements()) {
-                                    keySet.add(keys.nextElement().toString());
-                                }
-                                Iterator<String> keyIter = keySet.iterator();
-                                String value = "";
-                                String propString = "";
-                                String prepend = "";
-                                while (keyIter.hasNext()) {
-                                    String key = keyIter.next();
-                                    value = props.get(key).toString();
-                                    propString += prepend + key + ": " + value;
-                                    prepend = "\n";
-                                }
-                                TextArea area = new TextArea("", propString);
-                                area.setWidth("100%");
-                                area.setRows(props.size());
-                                area.setWordwrap(false);
-                                area.setReadOnly(true);
-                                area.setImmediate(true);
-                                String type = Integer.toString(event.getType());
-                                for (Field f : AuditEvent.class.getFields()) {
-                                    if (((f.getModifiers() & Modifier.STATIC) == Modifier.STATIC) && (f.getType() == Integer.TYPE)) {
-                                        if (((Integer) f.get(null)).intValue() == event.getType()) {
-                                            type = f.getName();
-                                            break;
-                                        }
-                                    }
-                                }
-                                table.addItem(new Object[] { new Date(event.getTime()), type, area }, null);
-                            }
-                        }
-                    }
+            fillTable(object, table);
+        }
+        catch (IOException ex) {
+            m_logService.log(LogService.LOG_WARNING, "Log viewer failed!", ex);
+        }
+        return table;
+    }
+
+    /**
+     * Fills the table with all log entries for the given repository object.
+     * 
+     * @param object the repository object to get the log for, cannot be <code>null</code>;
+     * @param table the table to fill, cannot be <code>null</code>.
+     * @throws IOException in case of I/O problems accessing the log store.
+     */
+    private void fillTable(RepositoryObject object, Table table) throws IOException {
+        String id = object.getAttribute(GatewayObject.KEY_ID);
+        List<LogDescriptor> desc = m_store.getDescriptors(id);
+        if (desc != null) {
+            for (LogDescriptor log : desc) {
+                for (LogEvent event : m_store.get(log)) {
+                    table.addItem(
+                        new Object[] { new Date(event.getTime()), getEventType(event), getProperties(event) }, null);
+                }
+            }
+        }
+    }
+
+    /**
+     * Creates a {@link TextArea} with a dump of the given event's properties.
+     * 
+     * @param event the event to create a textarea for, cannot be <code>null</code>.
+     * @return a {@link TextArea} instance, never <code>null</code>.
+     */
+    private TextArea getProperties(LogEvent event) {
+        Dictionary props = event.getProperties();
+
+        TextArea area = new TextArea("", dumpProperties(props));
+        area.setWidth(FILL_AREA);
+        area.setRows(props.size());
+        area.setWordwrap(false);
+        area.setReadOnly(true);
+        area.setImmediate(true);
+        return area;
+    }
+
+    /**
+     * Dumps the given dictionary to a string by placing all key,value-pairs on a separate line.
+     * 
+     * @param dict the dictionary to dump, may be <code>null</code>.
+     * @return a string dump of all properties in the given dictionary, never <code>null</code>.
+     */
+    private String dumpProperties(Dictionary dict) {
+        StringBuilder sb = new StringBuilder();
+        if (dict != null) {
+            Enumeration keys = dict.keys();
+            while (keys.hasMoreElements()) {
+                String key = keys.nextElement().toString();
+                String value = dict.get(key).toString();
+
+                if (sb.length() > 0) {
+                    sb.append("\n");
                 }
-                else {
-                    VerticalLayout result = new VerticalLayout();
-                    result.setCaption("VerifyResolve");
-                    result.addComponent(new Label("This target is not yet registered, so it has no log."));
-                    return result;
+                sb.append(key).append(": ").append(value);
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * Returns a string representation of the given event's type.
+     * 
+     * @param event the event to get the type for, cannot be <code>null</code>.
+     * @return a string representation of the event's type, never <code>null</code>.
+     */
+    private String getEventType(LogEvent event) {
+        if (m_eventTypeMapping.isEmpty()) {
+            // Lazily create a mapping of value -> name of all event-types...
+            for (Field f : AuditEvent.class.getFields()) {
+                if (((f.getModifiers() & Modifier.STATIC) == Modifier.STATIC) && (f.getType() == Integer.TYPE)) {
+                    try {
+                        Integer value = (Integer) f.get(null);
+                        m_eventTypeMapping.put(value, f.getName());
+                    }
+                    catch (IllegalAccessException e) {
+                        // Should not happen, as all fields are public on an interface;
+                        // otherwise we simply ignore this field...
+                        m_logService.log(LogService.LOG_DEBUG, "Failed to access public field of interface?!", e);
+                    }
                 }
             }
         }
-        catch (Exception ex) {
-            ex.printStackTrace();
+
+        String type = m_eventTypeMapping.get(event.getType());
+        if (type == null) {
+            type = Integer.toString(event.getType());
         }
-        return table;
+
+        return type;
     }
 
     private RepositoryObject getRepositoryObjectFromContext(Map<String, Object> context) {
@@ -123,10 +192,10 @@ public class LogViewerExtension implemen
             throw new IllegalStateException("No context object found");
         }
         // It looks like there is some bug (or some other reason that escapes
-        // me)
-        // why ace is using either the object directly or wraps it in a
+        // me) why ace is using either the object directly or wraps it in a
         // NamedObject first.
         // Its unclear when it does which so for now we cater for both.
-        return ((RepositoryObject) (contextObject instanceof NamedObject ? ((NamedObject) contextObject).getObject() : contextObject));
+        return (contextObject instanceof NamedObject ? ((NamedObject) contextObject).getObject()
+            : (RepositoryObject) contextObject);
     }
 }