ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1516265 - in /ace/trunk/org.apache.ace.agent: bnd.bnd src/org/apache/ace/agent/AgentContext.java src/org/apache/ace/agent/impl/Activator.java src/org/apache/ace/agent/impl/AgentContextImpl.java
Date Wed, 21 Aug 2013 19:20:56 GMT
Author: bramk
Date: Wed Aug 21 19:20:55 2013
New Revision: 1516265

URL: http://svn.apache.org/r1516265
Log:
ACE-347 Internalized EventAdmin package

Modified:
    ace/trunk/org.apache.ace.agent/bnd.bnd
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentContext.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
    ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContextImpl.java

Modified: ace/trunk/org.apache.ace.agent/bnd.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/bnd.bnd?rev=1516265&r1=1516264&r2=1516265&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/bnd.bnd (original)
+++ ace/trunk/org.apache.ace.agent/bnd.bnd Wed Aug 21 19:20:55 2013
@@ -23,6 +23,7 @@ Private-Package: org.apache.ace.range,\
 	org.apache.felix.dm.tracker,\
 	org.osgi.service.metatype,\
 	org.osgi.service.cm,\
+	org.osgi.service.event,\
 	org.osgi.service.log,\
 	org.osgi.util.tracker
 
@@ -34,11 +35,9 @@ Import-Package:javax.net.ssl,\
 
 # This is a minimal set on purpose. Are you really sure the agent must 
 # expose another package? Probably not...
-# FIXME Decouple api from event admin as well
 Export-Package: org.apache.ace.agent,\
 	org.osgi.service.deploymentadmin;-split-package:=merge-last,\
-	org.osgi.service.deploymentadmin.spi;-split-package:=merge-last,\
-	org.osgi.service.event
+	org.osgi.service.deploymentadmin.spi;-split-package:=merge-last
 
 -buildpath: osgi.core;version=4.2,\
 	osgi.cmpn;version=4.2,\

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentContext.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentContext.java?rev=1516265&r1=1516264&r2=1516265&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentContext.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/AgentContext.java Wed Aug 21 19:20:55
2013
@@ -19,11 +19,10 @@
 package org.apache.ace.agent;
 
 import java.io.File;
+import java.util.Dictionary;
 import java.util.Formatter;
 import java.util.concurrent.ScheduledExecutorService;
 
-import org.osgi.service.event.EventAdmin;
-
 /**
  * Internal interface that provides access to handlers, supporting services and static configuration.
  * 
@@ -94,18 +93,20 @@ public interface AgentContext {
     ScheduledExecutorService getExecutorService();
 
     /**
-     * Return the event admin.
+     * Return the work directory.
      * 
-     * @return The service
+     * @return The directory
      */
-    EventAdmin getEventAdmin();
+    File getWorkDir();
 
     /**
-     * Return the work directory.
+     * Post an event to any eventAdmin services outside the agent available at this time.
There is no guarantee on
+     * delivery. Only string values are supported to avoid any potential class-loading issues.
      * 
-     * @return The directory
+     * @param topic The topic
+     * @param properties The payload
      */
-    File getWorkDir();
+    void postEvent(String topic, Dictionary<String, String> payload);
 
     /**
      * Log a debug message. If <code>args</code> are provided the message will
be processed as a format using the

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java?rev=1516265&r1=1516264&r2=1516265&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/Activator.java Wed Aug 21
19:20:55 2013
@@ -22,6 +22,7 @@ import static org.apache.ace.agent.impl.
 import static org.apache.ace.agent.impl.ReflectionUtil.invokeMethod;
 
 import java.util.Date;
+import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -54,7 +55,7 @@ import org.osgi.service.packageadmin.Pac
 public class Activator extends DependencyActivatorBase {
 
     // internal delegates
-    private final InternalEventAdmin m_internalEventAdmin = new InternalEventAdmin();
+    private final InternalEvents m_internalEvents = new InternalEvents();
     private final InternalLogger m_internalLogger = new InternalLogger(1);
 
     // managed state
@@ -65,10 +66,10 @@ public class Activator extends Dependenc
     private DeploymentAdmin m_internalDeploymentAdmin;
     private Component m_agentControlComponent = null;
     private EventLoggerImpl m_eventLoggerImpl;
+    private DefaultController m_defaultController;
 
     // injected services
     private volatile PackageAdmin m_packageAdmin;
-    private volatile EventAdmin m_externalEventAdmin;
 
     @Override
     public void init(BundleContext context, DependencyManager manager) throws Exception {
@@ -78,15 +79,14 @@ public class Activator extends Dependenc
         m_internalDeploymentAdmin = new DeploymentAdminImpl();
         configureField(m_internalDeploymentAdmin, BundleContext.class, context);
         configureField(m_internalDeploymentAdmin, PackageAdmin.class, null);
-        configureField(m_internalDeploymentAdmin, EventAdmin.class, m_internalEventAdmin);
+        configureField(m_internalDeploymentAdmin, EventAdmin.class, new InternalEventAdmin(m_internalEvents));
         configureField(m_internalDeploymentAdmin, LogService.class, new InternalLogService(m_internalLogger,
"deployment"));
 
-        m_agentContext = new AgentContextImpl(context.getDataFile(""), m_internalLogger);
+        m_agentContext = new AgentContextImpl(context.getDataFile(""), m_internalLogger,
m_internalEvents);
         m_agentControl = new AgentControlImpl(m_agentContext);
         m_agentUpdateHandler = new AgentUpdateHandlerImpl(context);
 
         configureField(m_agentContext, AgentControl.class, m_agentControl);
-        configureField(m_agentContext, EventAdmin.class, m_internalEventAdmin);
         configureField(m_agentContext, ConfigurationHandler.class, new ConfigurationHandlerImpl());
         configureField(m_agentContext, ConnectionHandler.class, new ConnectionHandlerImpl());
         configureField(m_agentContext, DeploymentHandler.class, new DeploymentHandlerImpl(m_internalDeploymentAdmin));
@@ -104,10 +104,7 @@ public class Activator extends Dependenc
             .setAutoConfig(Component.class, false)
             .add(createServiceDependency()
                 .setService(PackageAdmin.class).setRequired(true)
-                .setCallbacks(this, "packageAdminAdded", "packageAdminRemoved"))
-            .add(createServiceDependency()
-                .setService(EventAdmin.class).setRequired(false)
-                .setCallbacks(this, "eventAdminAdded", "eventAdminRemoved"));
+                .setCallbacks(this, "packageAdminAdded", "packageAdminRemoved"));
 
         // FIXME fake config
         if (Boolean.parseBoolean(System.getProperty("agent.identificationhandler.disabled")))
{
@@ -146,27 +143,10 @@ public class Activator extends Dependenc
         }
     }
 
-    synchronized void eventAdminAdded(EventAdmin eventAdmin) {
-        if (m_externalEventAdmin == null) {
-            m_externalEventAdmin = eventAdmin;
-            configureField(m_internalEventAdmin, EventAdmin.class, eventAdmin);
-        }
-    }
-
-    synchronized void eventAdminRemoved(EventAdmin eventAdmin) {
-        if (m_externalEventAdmin == eventAdmin) {
-            m_externalEventAdmin = null;
-            configureField(m_internalEventAdmin, EventAdmin.class, null);
-        }
-    }
-
-    private DefaultController m_defaultController;
-
     void startAgent() throws Exception {
 
         m_internalLogger.logInfo("activator", "Agent starting...", null);
 
-        m_internalLogger.logInfo("activator", "Agent starting...", null);
         invokeMethod(m_internalDeploymentAdmin, "start", new Class<?>[] {}, new Object[]
{});
         m_agentContext.start();
 
@@ -192,7 +172,7 @@ public class Activator extends Dependenc
             BundleContext bundleContext = getDependencyManager().getBundleContext();
             bundleContext.addBundleListener(m_eventLoggerImpl);
             bundleContext.addFrameworkListener(m_eventLoggerImpl);
-            m_internalEventAdmin.registerHandler(m_eventLoggerImpl, EventLoggerImpl.TOPICS_INTEREST);
+            m_internalEvents.registerHandler(m_eventLoggerImpl, EventLoggerImpl.TOPICS_INTEREST);
             m_internalLogger.logInfo("activator", "Audit logger started", null);
         }
         else {
@@ -220,7 +200,7 @@ public class Activator extends Dependenc
             BundleContext bundleContext = getDependencyManager().getBundleContext();
             bundleContext.removeFrameworkListener(m_eventLoggerImpl);
             bundleContext.removeBundleListener(m_eventLoggerImpl);
-            m_internalEventAdmin.unregisterHandler(m_eventLoggerImpl);
+            m_internalEvents.unregisterHandler(m_eventLoggerImpl);
         }
 
         m_agentContext.stop();
@@ -228,25 +208,33 @@ public class Activator extends Dependenc
         m_internalLogger.logInfo("activator", "Agent stopped", null);
     }
 
-    static class InternalEventAdmin implements EventAdmin {
+    /**
+     * InternalEvents that posts events to internal handlers and external admins.
+     */
+    static class InternalEvents {
 
         private final Map<EventHandler, String[]> m_eventHandlers = new HashMap<EventHandler,
String[]>();
-        private volatile EventAdmin m_eventAdmin;
 
-        @Override
+        public void postEvent(String topic, Dictionary<String, String> payload) {
+            Event event = new Event(topic, payload);
+            postEvent(event);
+        }
+
         public void postEvent(Event event) {
             sendInternal(event);
-            EventAdmin eventAdmin = m_eventAdmin;
-            if (eventAdmin != null)
-                eventAdmin.postEvent(event);
+            sendExternal(event);
         }
 
-        @Override
-        public void sendEvent(Event event) {
-            sendInternal(event);
-            EventAdmin eventAdmin = m_eventAdmin;
-            if (eventAdmin != null)
-                eventAdmin.sendEvent(event);
+        void registerHandler(EventHandler eventHandler, String[] topics) {
+            synchronized (m_eventHandlers) {
+                m_eventHandlers.put(eventHandler, topics);
+            }
+        }
+
+        void unregisterHandler(EventHandler eventHandler) {
+            synchronized (m_eventHandlers) {
+                m_eventHandlers.remove(eventHandler);
+            }
         }
 
         private void sendInternal(Event event) {
@@ -264,19 +252,38 @@ public class Activator extends Dependenc
             }
         }
 
-        void registerHandler(EventHandler eventHandler, String[] topics) {
-            synchronized (m_eventHandlers) {
-                m_eventHandlers.put(eventHandler, topics);
-            }
+        private void sendExternal(Event event) {
+            // TODO this requires looking for all service references and invoking any found
admins using reflection
         }
 
-        void unregisterHandler(EventHandler eventHandler) {
-            synchronized (m_eventHandlers) {
-                m_eventHandlers.remove(eventHandler);
-            }
+    }
+
+    /**
+     * Internal EventAdmin that delegates to actual InternalEvents. Used to inject into the
DeploymentAdmin only.
+     */
+    static class InternalEventAdmin implements EventAdmin {
+
+        private final InternalEvents m_events;
+
+        public InternalEventAdmin(InternalEvents events) {
+            m_events = events;
+        }
+
+        @Override
+        public void postEvent(Event event) {
+            m_events.postEvent(event);
+        }
+
+        @Override
+        public void sendEvent(Event event) {
+            m_events.postEvent(event);
         }
     }
 
+    /**
+     * Internal logger that writes to system out for now. It minimizes work until it is determined
the loglevel is
+     * loggable.
+     */
     static class InternalLogger {
 
         private final int m_level;
@@ -317,6 +324,9 @@ public class Activator extends Dependenc
         }
     }
 
+    /**
+     * Internal LogService that wraps delegates to actual InternalLogger. Used to inject
into the DeploymentAdmin only.
+     */
     static class InternalLogService implements LogService {
 
         private final InternalLogger m_logger;
@@ -361,6 +371,9 @@ public class Activator extends Dependenc
         }
     }
 
+    /**
+     * Internal thread factory that assigns recognizable names to the threads it creates
and sets them in daemon mode.
+     */
     static class InternalThreadFactory implements ThreadFactory {
 
         private static final String m_name = "ACE Agent worker (%s)";

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContextImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContextImpl.java?rev=1516265&r1=1516264&r2=1516265&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContextImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/AgentContextImpl.java Wed
Aug 21 19:20:55 2013
@@ -19,6 +19,7 @@
 package org.apache.ace.agent.impl;
 
 import java.io.File;
+import java.util.Dictionary;
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.ace.agent.AgentContext;
@@ -31,8 +32,8 @@ import org.apache.ace.agent.DeploymentHa
 import org.apache.ace.agent.DiscoveryHandler;
 import org.apache.ace.agent.DownloadHandler;
 import org.apache.ace.agent.IdentificationHandler;
+import org.apache.ace.agent.impl.Activator.InternalEvents;
 import org.apache.ace.agent.impl.Activator.InternalLogger;
-import org.osgi.service.event.EventAdmin;
 
 /**
  * Implementation of the internal agent context service.
@@ -51,13 +52,15 @@ public class AgentContextImpl implements
     private volatile AgentUpdateHandler m_agentUpdateHandler;
 
     private volatile ScheduledExecutorService m_executorService;
-    private volatile EventAdmin m_eventAdmin;
 
     private final InternalLogger m_logger;
+    private final InternalEvents m_events;
+    
     private final File m_workDir;
 
-    public AgentContextImpl(File workDir, InternalLogger logger) {
+    public AgentContextImpl(File workDir, InternalLogger logger, InternalEvents events) {
         m_logger = logger;
+        m_events = events;
         m_workDir = workDir;
     }
 
@@ -141,18 +144,18 @@ public class AgentContextImpl implements
     }
 
     @Override
-    public EventAdmin getEventAdmin() {
-        return m_eventAdmin;
+    public AgentControl getAgentControl() {
+        return m_agentControl;
     }
 
     @Override
-    public AgentControl getAgentControl() {
-        return m_agentControl;
+    public void postEvent(String topic, Dictionary<String, String> payload) {
+        m_events.postEvent(topic, payload);
     }
 
     @Override
     public void logDebug(String component, String message, Object... args) {
-        m_logger.logDebug(component, message, null,args);
+        m_logger.logDebug(component, message, null, args);
     }
 
     @Override
@@ -189,4 +192,5 @@ public class AgentContextImpl implements
     public void logError(String component, String message, Throwable exception, Object...
args) {
         m_logger.logDebug(component, message, exception, args);
     }
+
 }



Mime
View raw message