You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by br...@apache.org on 2013/05/08 16:46:43 UTC

svn commit: r1480307 - in /ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent: deployment/ impl/ logging/

Author: bramk
Date: Wed May  8 14:46:43 2013
New Revision: 1480307

URL: http://svn.apache.org/r1480307
Log:
ACE-347 Added eventlogger and startup trigger so targets register with the server

Added:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerFactory.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerImpl.java
Modified:
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentCheckTaskFactory.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogImpl.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogSyncTask.java

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentCheckTaskFactory.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentCheckTaskFactory.java?rev=1480307&r1=1480306&r2=1480307&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentCheckTaskFactory.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/deployment/DeploymentCheckTaskFactory.java Wed May  8 14:46:43 2013
@@ -28,6 +28,7 @@ import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.event.EventAdmin;
 import org.osgi.service.log.LogService;
 
 /**
@@ -48,6 +49,7 @@ public class DeploymentCheckTaskFactory 
             .setInterface(Runnable.class.getName(), properties)
             .setImplementation(new DeploymentCheckTask())
             .add(manager.createServiceDependency().setService(DeploymentService.class, getAgentFilter(configuration, null)).setRequired(true))
+            .add(manager.createServiceDependency().setService(EventAdmin.class).setRequired(false))
             .add(manager.createServiceDependency().setService(LogService.class).setRequired(false));
     }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties?rev=1480307&r1=1480306&r2=1480307&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties Wed May  8 14:46:43 2013
@@ -15,6 +15,7 @@ system.factories= org.apache.ace.agent.i
    	org.apache.ace.agent.logging.LogFactory,\
 	org.apache.ace.agent.logging.LogStoreFactory,\
 	org.apache.ace.agent.logging.LogSyncTaskFactory,\
+	org.apache.ace.agent.logging.EventLoggerFactory,\
 	org.apache.ace.agent.deployment.DeploymentServiceFactory,\
 	org.apache.ace.agent.deployment.DeploymentAdminDeployerFactory,\
 	org.apache.ace.agent.deployment.DeploymentCheckTaskFactory,\

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerFactory.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerFactory.java?rev=1480307&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerFactory.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerFactory.java Wed May  8 14:46:43 2013
@@ -0,0 +1,78 @@
+/*
+ * 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.ace.agent.logging;
+
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.ace.agent.spi.ComponentFactoryBase;
+import org.apache.ace.log.Log;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.event.EventHandler;
+import org.osgi.service.log.LogService;
+
+/**
+ * Creates event logging service components with a {@link EventLoggerImpl} implementation for every configured store.
+ * 
+ */
+public class EventLoggerFactory extends ComponentFactoryBase {
+
+    /*
+     * FIXME This implementation effectively creates an event logger for each agent/store. At this point there is only
+     * 'auditlog' and these events are mostly hard wired. In the future This should probably be configurable to allow
+     * users to specify which types of events/topics should be logged where.
+     */
+
+    public static final String LOG_STORES = "logstores";
+    public static final String LOG_NAME = "name";
+
+    @Override
+    public Set<Component> createComponents(BundleContext context, DependencyManager manager, LogService logService, Dictionary<String, String> configuration) {
+
+        Set<Component> components = new HashSet<Component>();
+        String value = configuration.get(LOG_STORES);
+        String[] stores = value.split(",");
+        for (String store : stores) {
+            components.add(createEventLoggerComponent(context, manager, logService, configuration, store.trim()));
+        }
+        return components;
+    }
+
+    private Component createEventLoggerComponent(BundleContext context, DependencyManager manager, LogService logService, Dictionary<String, String> configuration, String store) {
+
+        Properties properties = getAgentproperties(configuration);
+        properties.put("name", store);
+        properties.put(EventConstants.EVENT_TOPIC, EventLoggerImpl.TOPICS_INTEREST);
+
+        return manager.createComponent()
+            .setInterface(new String[] { EventHandler.class.getName() }, properties)
+            .setImplementation(new EventLoggerImpl())
+            .add(manager.createServiceDependency()
+                .setService(Log.class, getAgentFilter(configuration, "(name=" + store + ")"))
+                .setRequired(true))
+            .add(manager.createServiceDependency()
+                .setService(LogService.class)
+                .setRequired(false));
+    }
+}

Added: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerImpl.java?rev=1480307&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerImpl.java (added)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/EventLoggerImpl.java Wed May  8 14:46:43 2013
@@ -0,0 +1,237 @@
+/*
+ * 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.ace.agent.logging;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Properties;
+
+import org.apache.ace.log.AuditEvent;
+import org.apache.ace.log.Log;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+import org.osgi.service.deploymentadmin.DeploymentAdmin;
+import org.osgi.service.deploymentadmin.DeploymentPackage;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventHandler;
+
+/**
+ * Service component that listens for
+ * 
+ */
+public class EventLoggerImpl implements BundleListener, FrameworkListener, EventHandler {
+
+    /*
+     * FIXME This is a simplified quick copy and paste of org.apache.ace.log.listener.* without caching and async. I
+     * think that is OK. However we need to revisit all logging/monitoring and this logic should probably be made
+     * configurable split up is separate components.
+     * 
+     * @see EvenLoggerFactory as well
+     */
+
+    public static final String[] TOPICS_INTEREST = new String[] { "org/osgi/service/deployment/*", "org/apache/ace/deployment/*" };
+    
+    public static final String TOPIC_INSTALL = "org/osgi/service/deployment/INSTALL";
+    public static final String TOPIC_UNINSTALL = "org/osgi/service/deployment/UNINSTALL";
+    public static final String TOPIC_COMPLETE = "org/osgi/service/deployment/COMPLETE";
+    public static final String TOPIC_DEPLOYMENTPACKAGE_INSTALL = "org/apache/ace/deployment/INSTALL";
+
+    // injected
+    private volatile BundleContext m_context;
+    private volatile Log m_log;
+
+    public void start() throws Exception {
+        m_context.addFrameworkListener(this);
+        m_context.addBundleListener(this);
+    }
+
+    public void stop() throws Exception {
+        m_context.removeFrameworkListener(this);
+        m_context.removeBundleListener(this);
+    }
+
+    @Override
+    public void handleEvent(Event event) {
+        int eventType = AuditEvent.DEPLOYMENTADMIN_BASE;
+        Dictionary<String, String> props = new Hashtable<String, String>();
+
+        String topic = event.getTopic();
+
+        if (topic.equals(TOPIC_DEPLOYMENTPACKAGE_INSTALL)) {
+            String url = (String) event.getProperty("deploymentpackage.url");
+            String version = (String) event.getProperty("deploymentpackage.version");
+            eventType = AuditEvent.DEPLOYMENTCONTROL_INSTALL;
+            props.put(AuditEvent.KEY_VERSION, version);
+            props.put(AuditEvent.KEY_NAME, url);
+        }
+        else if (topic.equals(TOPIC_INSTALL)) {
+            String deplPackName = (String) event.getProperty("deploymentpackage.name");
+            eventType = AuditEvent.DEPLOYMENTADMIN_INSTALL;
+            props.put(AuditEvent.KEY_NAME, deplPackName);
+        }
+
+        else if (topic.equals(TOPIC_UNINSTALL)) {
+            String deplPackName = (String) event.getProperty("deploymentpackage.name");
+            eventType = AuditEvent.DEPLOYMENTADMIN_UNINSTALL;
+            props.put(AuditEvent.KEY_NAME, deplPackName);
+        }
+        else if (topic.equals(TOPIC_COMPLETE)) {
+            String deplPackName = (String) event.getProperty("deploymentpackage.name");
+            // to retrieve the version, DeploymentAdmin has to be used
+            ServiceReference ref = m_context.getServiceReference(DeploymentAdmin.class.getName());
+            if (ref != null) {
+                DeploymentAdmin deplAdmin = (DeploymentAdmin) m_context.getService(ref);
+                if (deplAdmin != null) {
+                    DeploymentPackage dp = deplAdmin.getDeploymentPackage(deplPackName);
+                    if (dp != null) {
+                        Version version = dp.getVersion();
+                        if (version != null) {
+                            props.put(AuditEvent.KEY_VERSION, version.toString());
+                        }
+                    }
+                    // after use, release the service as is it not needed anymore
+                    m_context.ungetService(ref);
+                }
+            }
+            eventType = AuditEvent.DEPLOYMENTADMIN_COMPLETE;
+            props.put(AuditEvent.KEY_NAME, deplPackName);
+            Boolean success = (Boolean) event.getProperty("successful");
+            props.put(AuditEvent.KEY_SUCCESS, success.toString());
+        }
+        m_log.log(eventType, props);
+    }
+
+    @Override
+    public void frameworkEvent(FrameworkEvent event) {
+        int eventType = AuditEvent.FRAMEWORK_BASE;
+        Properties props = new Properties();
+        Bundle bundle = event.getBundle();
+
+        if (bundle != null) {
+            props.put(AuditEvent.KEY_ID, Long.toString(bundle.getBundleId()));
+        }
+
+        String msg = null;
+        String type = null;
+        Throwable exception = event.getThrowable();
+        if (exception != null) {
+            msg = exception.getMessage();
+            type = exception.getClass().getName();
+        }
+
+        switch (event.getType()) {
+            case FrameworkEvent.INFO:
+                eventType = AuditEvent.FRAMEWORK_INFO;
+                if (msg != null) {
+                    props.put(AuditEvent.KEY_MSG, msg);
+                }
+                if (type != null) {
+                    props.put(AuditEvent.KEY_TYPE, type);
+                }
+                break;
+            case FrameworkEvent.WARNING:
+                eventType = AuditEvent.FRAMEWORK_WARNING;
+                if (msg != null) {
+                    props.put(AuditEvent.KEY_MSG, msg);
+                }
+                if (type != null) {
+                    props.put(AuditEvent.KEY_TYPE, type);
+                }
+                break;
+            case FrameworkEvent.ERROR:
+                eventType = AuditEvent.FRAMEWORK_ERROR;
+                if (msg != null) {
+                    props.put(AuditEvent.KEY_MSG, msg);
+                }
+                if (type != null) {
+                    props.put(AuditEvent.KEY_TYPE, type);
+                }
+                break;
+            case FrameworkEvent.PACKAGES_REFRESHED:
+                eventType = AuditEvent.FRAMEWORK_REFRESH;
+                break;
+            case FrameworkEvent.STARTED:
+                eventType = AuditEvent.FRAMEWORK_STARTED;
+                break;
+            case FrameworkEvent.STARTLEVEL_CHANGED:
+                eventType = AuditEvent.FRAMEWORK_STARTLEVEL;
+                break;
+        }
+        m_log.log(eventType, props);
+    }
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        int eventType = AuditEvent.BUNDLE_BASE;
+        Properties props = new Properties();
+        Bundle bundle = event.getBundle();
+        props.put(AuditEvent.KEY_ID, Long.toString(bundle.getBundleId()));
+
+        switch (event.getType()) {
+            case BundleEvent.INSTALLED:
+                eventType = AuditEvent.BUNDLE_INSTALLED;
+                if (bundle.getSymbolicName() != null) {
+                    props.put(AuditEvent.KEY_NAME, bundle.getSymbolicName());
+                }
+                String version = (String) bundle.getHeaders().get(Constants.BUNDLE_VERSION);
+                if (version != null) {
+                    props.put(AuditEvent.KEY_VERSION, version);
+                }
+                props.put(AuditEvent.KEY_LOCATION, bundle.getLocation());
+                break;
+            case BundleEvent.RESOLVED:
+                eventType = AuditEvent.BUNDLE_RESOLVED;
+                break;
+            case BundleEvent.STARTED:
+                eventType = AuditEvent.BUNDLE_STARTED;
+                break;
+            case BundleEvent.STOPPED:
+                eventType = AuditEvent.BUNDLE_STOPPED;
+                break;
+            case BundleEvent.UNRESOLVED:
+                eventType = AuditEvent.BUNDLE_UNRESOLVED;
+                break;
+            case BundleEvent.UPDATED:
+                eventType = AuditEvent.BUNDLE_UPDATED;
+                version = (String) bundle.getHeaders().get(Constants.BUNDLE_VERSION);
+                if (version != null) {
+                    props.put(AuditEvent.KEY_VERSION, version);
+                }
+                props.put(AuditEvent.KEY_LOCATION, bundle.getLocation());
+                break;
+            case BundleEvent.UNINSTALLED:
+                eventType = AuditEvent.BUNDLE_UNINSTALLED;
+                break;
+            case BundleEvent.STARTING:
+                eventType = AuditEvent.BUNDLE_STARTING;
+                break;
+            case BundleEvent.STOPPING:
+                eventType = AuditEvent.BUNDLE_STOPPING;
+                break;
+        }
+        m_log.log(eventType, props);
+    }
+}

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogImpl.java?rev=1480307&r1=1480306&r2=1480307&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogImpl.java Wed May  8 14:46:43 2013
@@ -20,17 +20,29 @@ package org.apache.ace.agent.logging;
 
 import java.io.IOException;
 import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.ace.log.AuditEvent;
 import org.apache.ace.log.Log;
 import org.apache.ace.log.LogEvent;
 import org.apache.ace.log.target.store.LogStore;
 import org.osgi.service.log.LogService;
 
-//FIXME This is a of the org.apache.ace.log it is private and may be better located here.
+//FIXME This is a modified copy of the org.apache.ace.log it is private and may be better located here.
 
 public class LogImpl implements Log {
+
     private volatile LogStore m_store;
     private volatile LogService m_log;
 
+    public void start() {
+
+        // FIXME This is mostly useful to trigger a logsync so the agent registers with the server.
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(AuditEvent.KEY_MSG, "Log started");
+        log(AuditEvent.FRAMEWORK_INFO, props);
+    }
+
     public void log(int type, Dictionary properties) {
         try {
             m_store.put(type, properties);
@@ -44,4 +56,4 @@ public class LogImpl implements Log {
             m_log.log(LogService.LOG_WARNING, "Could not store event: " + (new LogEvent("", 0, 0, 0, type, properties)).toRepresentation(), e);
         }
     }
-}
\ No newline at end of file
+}

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogSyncTask.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogSyncTask.java?rev=1480307&r1=1480306&r2=1480307&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogSyncTask.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogSyncTask.java Wed May  8 14:46:43 2013
@@ -70,7 +70,6 @@ public class LogSyncTask implements Runn
      */
     public void run() {
         URL host = m_discovery.discover();
-
         if (host == null) {
             // expected if there's no discovered
             // ps or relay server