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/08/28 14:28:02 UTC

svn commit: r1518177 [3/4] - in /ace/trunk: org.apache.ace.agent.itest/ org.apache.ace.agent.itest/conf/ org.apache.ace.agent.itest/src/org/apache/ace/agent/itest/ org.apache.ace.agent.launcher/ org.apache.ace.agent.launcher/src/org/apache/ace/agent/la...

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventLoggerImpl.java Wed Aug 28 12:28:00 2013
@@ -22,7 +22,7 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.ace.agent.AgentControl;
+import org.apache.ace.agent.EventListener;
 import org.apache.ace.agent.FeedbackChannel;
 import org.apache.ace.log.AuditEvent;
 import org.osgi.framework.Bundle;
@@ -32,99 +32,132 @@ 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
  * 
  */
-// TODO quick copy & paste & simplify from org.apache.ace.log.listener.*
-// TODO Which event types to log must be configurable
-// TODO split into separate listeners
-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 class EventLoggerImpl extends ComponentBase implements BundleListener, FrameworkListener, EventListener {
 
     public static final String EVENTLOGGER_FEEDBACKCHANNEL = "auditlog";
-
-    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";
 
     private final BundleContext m_bundleContext;
-    private final AgentControl m_agentControl;
+    private volatile boolean m_isStarted = false;
 
-    public EventLoggerImpl(AgentControl agentControl, BundleContext bundleContext) {
-        m_agentControl = agentControl;
+    public EventLoggerImpl(BundleContext bundleContext) {
+        super("auditlogger");
         m_bundleContext = bundleContext;
     }
 
     @Override
-    public void handleEvent(Event event) {
-        int eventType = AuditEvent.DEPLOYMENTADMIN_BASE;
-        Map<String, String> props = new HashMap<String, String>();
+    protected void onStart() throws Exception {
+        getEventsHandler().addListener(this);
+        m_bundleContext.addBundleListener(this);
+        m_bundleContext.addFrameworkListener(this);
+        m_isStarted = true;
+    }
 
-        String topic = event.getTopic();
+    @Override
+    protected void onStop() throws Exception {
+        m_isStarted = false;
+        getEventsHandler().removeListener(this);
+        m_bundleContext.removeBundleListener(this);
+        m_bundleContext.removeFrameworkListener(this);
+    }
 
-        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);
+    @Override
+    public void handle(String topic, Map<String, String> payload) {
+        if (!m_isStarted) {
+            return;
         }
-        else if (topic.equals(TOPIC_INSTALL)) {
-            String deplPackName = (String) event.getProperty("deploymentpackage.name");
+
+        int eventType = AuditEvent.DEPLOYMENTADMIN_BASE;
+        Map<String, String> props = new HashMap<String, String>();
+
+        if (topic.equals(TOPIC_INSTALL)) {
+            String deplPackName = payload.get("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");
+            String deplPackName = payload.get("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_bundleContext.getServiceReference(DeploymentAdmin.class.getName());
-            if (ref != null) {
-                DeploymentAdmin deplAdmin = (DeploymentAdmin) m_bundleContext.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_bundleContext.ungetService(ref);
-                }
-            }
             eventType = AuditEvent.DEPLOYMENTADMIN_COMPLETE;
-            props.put(AuditEvent.KEY_NAME, deplPackName);
-            props.put(AuditEvent.KEY_SUCCESS, (String) event.getProperty("successful"));
+            props.put(AuditEvent.KEY_NAME, payload.get("deploymentpackage.name"));
+            props.put(AuditEvent.KEY_VERSION, getDeploymentHandler().getInstalledVersion().toString());
+            props.put(AuditEvent.KEY_SUCCESS, payload.get("successful"));
+        }
+        writeAuditEvent(eventType, props);
+    }
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        if (!m_isStarted) {
+            return;
+        }
+
+        int eventType = AuditEvent.BUNDLE_BASE;
+        Map<String, String> props = new HashMap<String, String>();
+        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;
         }
-        writeEvent(eventType, props);
+        writeAuditEvent(eventType, props);
     }
 
     @Override
     public void frameworkEvent(FrameworkEvent event) {
+        if (!m_isStarted) {
+            return;
+        }
         int eventType = AuditEvent.FRAMEWORK_BASE;
         Map<String, String> props = new HashMap<String, String>();
         Bundle bundle = event.getBundle();
@@ -179,65 +212,12 @@ public class EventLoggerImpl implements 
                 eventType = AuditEvent.FRAMEWORK_STARTLEVEL;
                 break;
         }
-        writeEvent(eventType, props);
-    }
-
-    @Override
-    public void bundleChanged(BundleEvent event) {
-        int eventType = AuditEvent.BUNDLE_BASE;
-        Map<String, String> props = new HashMap<String, String>();
-        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;
-        }
-        writeEvent(eventType, props);
+        writeAuditEvent(eventType, props);
     }
 
-    private void writeEvent(int eventType, Map<String, String> payload) {
+    private void writeAuditEvent(int eventType, Map<String, String> payload) {
         try {
-            FeedbackChannel channel = m_agentControl.getFeedbackHandler()
-                .getChannel(EVENTLOGGER_FEEDBACKCHANNEL);
+            FeedbackChannel channel = getFeedbackHandler().getChannel(EVENTLOGGER_FEEDBACKCHANNEL);
             if (channel != null) {
                 channel.write(eventType, payload);
             }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventsHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventsHandlerImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventsHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/EventsHandlerImpl.java Wed Aug 28 12:28:00 2013
@@ -18,60 +18,102 @@
  */
 package org.apache.ace.agent.impl;
 
-import java.util.Dictionary;
-import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
+import java.util.concurrent.CopyOnWriteArrayList;
 
-import org.osgi.service.event.Event;
-import org.osgi.service.event.EventHandler;
+import org.apache.ace.agent.EventListener;
+import org.apache.ace.agent.EventsHandler;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
 
 /**
- * InternalEvents that posts events to internal handlers and external admins.
+ * Default thread-safe {@link EventsHandler} implementation that tracks external {@link EventListener.class} services.
+ * Agent handles can manages their own listeners directly using {@link #addListener(EventListener)} and
+ * {@link #removeListener(EventListener)}.
  */
-public class EventsHandlerImpl implements EventsHandler {
+public class EventsHandlerImpl extends ComponentBase implements EventsHandler {
 
-    private final Map<EventHandler, String[]> m_eventHandlers = new HashMap<EventHandler, String[]>();
+    private final List<EventListener> m_listeners = new CopyOnWriteArrayList<EventListener>();
+    private final BundleContext m_bundleContext;
 
-    public void postEvent(String topic, Dictionary<String, String> payload) {
-        Event event = new Event(topic, payload);
-        postEvent(event);
-    }
+    private ServiceTracker m_tracker;
+
+    public EventsHandlerImpl(BundleContext bundleContext) throws Exception {
+        super("events");
+        m_bundleContext = bundleContext;
+        Filter listenerFilter = m_bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + EventListener.class.getName() + ")");
+        m_tracker = new ServiceTracker(m_bundleContext, listenerFilter, new ServiceTrackerCustomizer() {
+
+            @Override
+            public Object addingService(ServiceReference reference) {
+                Object service = m_bundleContext.getService(reference);
+                addListener((EventListener) service);
+                return service;
+            }
 
-    public void postEvent(Event event) {
-        sendInternal(event);
-        sendExternal(event);
+            @Override
+            public void removedService(ServiceReference reference, Object service) {
+                removeListener((EventListener) service);
+            }
+
+            @Override
+            public void modifiedService(ServiceReference reference, Object service) {
+            }
+        });
     }
 
-    void registerHandler(EventHandler eventHandler, String[] topics) {
-        synchronized (m_eventHandlers) {
-            m_eventHandlers.put(eventHandler, topics);
-        }
+    @Override
+    protected void onStart() throws Exception {
+        m_tracker.open();
     }
 
-    void unregisterHandler(EventHandler eventHandler) {
-        synchronized (m_eventHandlers) {
-            m_eventHandlers.remove(eventHandler);
-        }
+    @Override
+    protected void onStop() throws Exception {
+        m_tracker.close();
+        m_listeners.clear();
     }
 
-    private void sendInternal(Event event) {
-        String topic = event.getTopic();
-        synchronized (m_eventHandlers) {
-            for (Entry<EventHandler, String[]> entry : m_eventHandlers.entrySet()) {
-                for (String interest : entry.getValue()) {
-                    if ((interest.endsWith("*") && topic.startsWith(interest.substring(0, interest.length() - 1))
-                    || topic.equals(interest))) {
-                        entry.getKey().handleEvent(event);
-                        break;
+    @Override
+    public void postEvent(final String topic, final Map<String, String> payload) {
+        for (final EventListener listener : m_listeners) {
+            getExecutorService().submit(new Runnable() {
+                @Override
+                public void run() {
+                    try {
+                        listener.handle(topic, payload);
+                    }
+                    catch (Exception e) {
+                        logWarning("Exception while posting event", e);
                     }
                 }
+            });
+        }
+    }
+
+    @Override
+    public void sendEvent(final String topic, final Map<String, String> payload) {
+        for (final EventListener listener : m_listeners) {
+            try {
+                listener.handle(topic, payload);
+            }
+            catch (Exception e) {
+                logWarning("Exception while sending event", e);
             }
         }
     }
 
-    private void sendExternal(Event event) {
-        // TODO this requires looking for all service references and invoking any found admins using reflection
+    @Override
+    public void addListener(EventListener listener) {
+        m_listeners.add(listener);
     }
 
+    @Override
+    public void removeListener(EventListener listener) {
+        m_listeners.remove(listener);
+    }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackChannelImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackChannelImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackChannelImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackChannelImpl.java Wed Aug 28 12:28:00 2013
@@ -29,7 +29,6 @@ import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.RandomAccessFile;
 import java.io.Writer;
-import java.net.ConnectException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -45,7 +44,9 @@ import java.util.TreeSet;
 
 import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.ConnectionHandler;
+import org.apache.ace.agent.DiscoveryHandler;
 import org.apache.ace.agent.FeedbackChannel;
+import org.apache.ace.agent.IdentificationHandler;
 import org.apache.ace.agent.RetryAfterException;
 import org.apache.ace.log.LogDescriptor;
 import org.apache.ace.log.LogEvent;
@@ -93,16 +94,18 @@ public class FeedbackChannelImpl impleme
     public synchronized void sendFeedback() throws RetryAfterException, IOException {
         String identification = getIdentification();
         URL serverURL = getServerURL();
-        if (identification == null || serverURL == null)
+        if (identification == null || serverURL == null) {
             return;
+        }
         URLConnection sendConnection = null;
         Writer writer = null;
         try {
             URL sendURL = new URL(serverURL, m_name + "/" + COMMAND_SEND);
             sendConnection = getConnectionHandler().getConnection(sendURL);
             sendConnection.setDoOutput(true);
-            if (sendConnection instanceof HttpURLConnection)
+            if (sendConnection instanceof HttpURLConnection) {
                 ((HttpURLConnection) sendConnection).setChunkedStreamingMode(8192);
+            }
             writer = new BufferedWriter(new OutputStreamWriter(sendConnection.getOutputStream()));
             SortedSet<Long> storeIDs = getStoreIDs();
             for (Long storeID : storeIDs) {
@@ -111,14 +114,9 @@ public class FeedbackChannelImpl impleme
                 synchronizeStore(storeID, queryConnection.getInputStream(), writer);
             }
             writer.flush();
+            ConnectionUtil.checkConnectionResponse(sendConnection);
             sendConnection.getContent();
         }
-        catch (ConnectException e) {
-            e.printStackTrace();
-        }
-        catch (IOException e) {
-            e.printStackTrace();
-        }
         finally {
             if (writer != null)
                 writer.close();
@@ -347,15 +345,15 @@ public class FeedbackChannelImpl impleme
     }
 
     private ConnectionHandler getConnectionHandler() {
-        return m_agentContext.getConnectionHandler();
+        return m_agentContext.getHandler(ConnectionHandler.class);
     }
 
     private String getIdentification() {
-        return m_agentContext.getIdentificationHandler().getAgentId();
+        return m_agentContext.getHandler(IdentificationHandler.class).getAgentId();
     }
 
     private URL getServerURL() {
-        return m_agentContext.getDiscoveryHandler().getServerUrl();
+        return m_agentContext.getHandler(DiscoveryHandler.class).getServerUrl();
     }
 
     // bridging to log api
@@ -381,12 +379,9 @@ public class FeedbackChannelImpl impleme
         /**
          * Create a new File based Store.
          * 
-         * @param store
-         *            the file to use as backend.
-         * @param id
-         *            the log id of the store
-         * @throws java.io.IOException
-         *             in case the file is not rw.
+         * @param store the file to use as backend.
+         * @param id the log id of the store
+         * @throws java.io.IOException in case the file is not rw.
          */
         Store(File store, long id) throws IOException {
             m_store = new RandomAccessFile(store, "rwd");
@@ -427,8 +422,7 @@ public class FeedbackChannelImpl impleme
         /**
          * Reset the store to the beginning of the records
          * 
-         * @throws java.io.IOException
-         *             in case of an IO error.
+         * @throws java.io.IOException in case of an IO error.
          */
         public void reset() throws IOException {
             m_store.seek(0);
@@ -439,8 +433,7 @@ public class FeedbackChannelImpl impleme
          * Determine whether there are any records left based on the current postion.
          * 
          * @return <code>true</code> if there are still records to be read.
-         * @throws java.io.IOException
-         *             in case of an IO error.
+         * @throws java.io.IOException in case of an IO error.
          */
         public boolean hasNext() throws IOException {
             return m_store.getFilePointer() < m_store.length();
@@ -483,8 +476,7 @@ public class FeedbackChannelImpl impleme
         /**
          * Make sure the store is readable. As a result, the store is at the end of the records.
          * 
-         * @throws java.io.IOException
-         *             in case of any IO error.
+         * @throws java.io.IOException in case of any IO error.
          */
         public void init() throws IOException {
             reset();
@@ -501,8 +493,7 @@ public class FeedbackChannelImpl impleme
         /**
          * Skip the next record if there is any.
          * 
-         * @throws java.io.IOException
-         *             in case of any IO error or if there is no record left.
+         * @throws java.io.IOException in case of any IO error or if there is no record left.
          */
         public void skip() throws IOException {
             long pos = m_store.getFilePointer();
@@ -524,10 +515,8 @@ public class FeedbackChannelImpl impleme
         /**
          * Store the given record data as the next record.
          * 
-         * @param entry
-         *            the data of the record to store.
-         * @throws java.io.IOException
-         *             in case of any IO error.
+         * @param entry the data of the record to store.
+         * @throws java.io.IOException in case of any IO error.
          */
         public void append(long id, byte[] entry) throws IOException {
             long pos = m_store.getFilePointer();
@@ -547,8 +536,7 @@ public class FeedbackChannelImpl impleme
         /**
          * Try to truncate the store at the current record.
          * 
-         * @throws java.io.IOException
-         *             in case of any IO error.
+         * @throws java.io.IOException in case of any IO error.
          */
         public void truncate() throws IOException {
             m_store.setLength(m_store.getFilePointer());
@@ -557,8 +545,7 @@ public class FeedbackChannelImpl impleme
         /**
          * Release any resources.
          * 
-         * @throws java.io.IOException
-         *             in case of any IO error.
+         * @throws java.io.IOException in case of any IO error.
          */
         public void close() throws IOException {
             m_store.close();

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/FeedbackHandlerImpl.java Wed Aug 28 12:28:00 2013
@@ -18,6 +18,8 @@
  */
 package org.apache.ace.agent.impl;
 
+import static org.apache.ace.agent.AgentConstants.CONFIG_FEEDBACK_CHANNELS;
+
 import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
@@ -33,22 +35,12 @@ import org.apache.ace.agent.FeedbackHand
  */
 public class FeedbackHandlerImpl extends ComponentBase implements FeedbackHandler {
 
-    public static final String COMPONENT_IDENTIFIER = "feedback";
-    public static final String CONFIG_KEY_BASE = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "." + COMPONENT_IDENTIFIER;
-
-    /**
-     * Configuration key for the default discovery handler. The value must be a comma-separated list of valid base
-     * server URLs.
-     */
-    public static final String CONFIG_KEY_CHANNELS = CONFIG_KEY_BASE + ".channels";
-    public static final String CONFIG_DEFAULT_CHANNELS = "auditlog";
-
-    private Map<String, FeedbackChannelImpl> m_channels = new HashMap<String, FeedbackChannelImpl>();
+    private final Map<String, FeedbackChannelImpl> m_channels = new HashMap<String, FeedbackChannelImpl>();
     private Set<String> m_channelNames;
     private String m_channelNamesConfig;
 
     public FeedbackHandlerImpl() {
-        super(COMPONENT_IDENTIFIER);
+        super("feedback");
     }
 
     @Override
@@ -82,14 +74,14 @@ public class FeedbackHandlerImpl extends
     }
 
     private void ensureChannels() throws IOException {
-        String channelNamesConfig = getAgentContext().getConfigurationHandler().get(CONFIG_KEY_CHANNELS, CONFIG_DEFAULT_CHANNELS);
+        String channelNamesConfig = getConfigurationHandler().get(CONFIG_FEEDBACK_CHANNELS, "auditlog");
         if (m_channelNamesConfig != null && m_channelNamesConfig.equals(channelNamesConfig)) {
             return;
         }
 
         m_channelNamesConfig = channelNamesConfig;
         m_channelNames = Collections.unmodifiableSet(getConfigurationValues(channelNamesConfig));
-        m_channels = new HashMap<String, FeedbackChannelImpl>();
+        m_channels.clear();
         for (String channelName : m_channelNames) {
             m_channels.put(channelName, new FeedbackChannelImpl(getAgentContext(), channelName));
         }
@@ -98,17 +90,17 @@ public class FeedbackHandlerImpl extends
     private void clearChannels() {
         m_channelNamesConfig = null;
         m_channelNames = null;
-        m_channels = null;
+        m_channels.clear();
     }
 
     // TODO move to util or configurationhandler
     private static Set<String> getConfigurationValues(String value) {
         Set<String> trimmedValues = new HashSet<String>();
-        if(value != null){
+        if (value != null) {
             String[] rawValues = value.split(",");
             for (String rawValue : rawValues) {
                 trimmedValues.add(rawValue.trim());
-            }            
+            }
         }
         return trimmedValues;
     }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/IdentificationHandlerImpl.java Wed Aug 28 12:28:00 2013
@@ -18,7 +18,8 @@
  */
 package org.apache.ace.agent.impl;
 
-import org.apache.ace.agent.ConfigurationHandler;
+import static org.apache.ace.agent.AgentConstants.CONFIG_IDENTIFICATION_AGENTID;
+
 import org.apache.ace.agent.IdentificationHandler;
 
 /**
@@ -28,24 +29,15 @@ import org.apache.ace.agent.Identificati
  */
 public class IdentificationHandlerImpl extends ComponentBase implements IdentificationHandler {
 
-    public static final String COMPONENT_IDENTIFIER = "identification";
-    public static final String CONFIG_KEY_BASE = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "." + COMPONENT_IDENTIFIER;
-
     public IdentificationHandlerImpl() {
-        super(COMPONENT_IDENTIFIER);
+        super("identification");
     }
 
-    /**
-     * Configuration key for the default identification handler. The value must be a single file-system and URL safe
-     * string.
-     */
-    public static final String CONFIG_KEY_IDENTIFICATION = CONFIG_KEY_BASE + ".agentId";
     public static final String CONFIG_DEFAULT_AGENTID = "defaultTargetID";
 
     @Override
     public String getAgentId() {
-        ConfigurationHandler configurationHandler = getAgentContext().getConfigurationHandler();
-        String configValue = configurationHandler.get(CONFIG_KEY_IDENTIFICATION, CONFIG_DEFAULT_AGENTID);
+        String configValue = getConfigurationHandler().get(CONFIG_IDENTIFICATION_AGENTID, "defaultTargetID");
         return configValue;
     }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LoggingHandlerImpl.java Wed Aug 28 12:28:00 2013
@@ -18,54 +18,74 @@
  */
 package org.apache.ace.agent.impl;
 
+import static org.apache.ace.agent.AgentConstants.CONFIG_LOGGING_LEVEL;
+
 import java.util.Date;
 
-import org.osgi.service.log.LogService;
+import org.apache.ace.agent.LoggingHandler;
 
 /**
- * Internal logger that writes to system out for now. It minimizes work until it is determined the loglevel is loggable.
+ * Default thread-safe {@link LoggingHandler} implementation that logs messages to {@link System.out} .
  */
-public class LoggingHandlerImpl implements LoggingHandler {
-
-    private final int m_level;
+public class LoggingHandlerImpl extends ComponentBase implements LoggingHandler {
 
-    public LoggingHandlerImpl(int level) {
-        m_level = level;
-    }
-
-    private void log(String level, String component, String message, Throwable exception, Object... args) {
-        if (args.length > 0)
-            message = String.format(message, args);
-        String line = String.format("[%s] %TT (%s) %s", level, new Date(), component, message);
-        System.out.println(line);
-        if (exception != null)
-            exception.printStackTrace(System.out);
+    public LoggingHandlerImpl() {
+        super("logging");
     }
 
     @Override
     public void logDebug(String component, String message, Throwable exception, Object... args) {
-        if (m_level < LogService.LOG_DEBUG)
-            return;
-        log("DEBUG", component, message, exception, args);
+        Levels level = getLogLevel();
+        if (level == Levels.DEBUG) {
+            log(Levels.DEBUG.name(), component, message, exception, args);
+        }
     }
 
     @Override
     public void logInfo(String component, String message, Throwable exception, Object... args) {
-        if (m_level < LogService.LOG_INFO)
-            return;
-        log("INFO", component, message, exception, args);
+        Levels level = getLogLevel();
+        if (level == Levels.DEBUG || level == Levels.INFO) {
+            log(Levels.INFO.name(), component, message, exception, args);
+        }
     }
 
     @Override
     public void logWarning(String component, String message, Throwable exception, Object... args) {
-        if (m_level < LogService.LOG_WARNING)
-            return;
-        log("WARNING", component, message, exception, args);
+        Levels level = getLogLevel();
+        if (level == Levels.DEBUG || level == Levels.INFO || level == Levels.WARNING) {
+            log(Levels.WARNING.name(), component, message, exception, args);
+        }
     }
 
     @Override
     public void logError(String component, String message, Throwable exception, Object... args) {
-        log("ERROR", component, message, exception, args);
+        log(Levels.ERROR.name(), component, message, exception, args);
     }
 
+    private void log(String level, String component, String message, Throwable exception, Object... args) {
+        if (args.length > 0) {
+            message = String.format(message, args);
+        }
+        String line = String.format("[%s] %TT (%s) %s", level, new Date(), component, message);
+        System.out.println(line);
+        if (exception != null) {
+            exception.printStackTrace(System.out);
+        }
+    }
+
+    // TODO performance; replace with configuration events
+    private Levels getLogLevel() {
+        String config = getConfigurationHandler().get(CONFIG_LOGGING_LEVEL, Levels.INFO.name());
+        return fromName(config);
+    }
+
+    private static Levels fromName(String name) {
+        name = name.toUpperCase().trim();
+        try {
+            return Levels.valueOf(name.toUpperCase().trim());
+        }
+        catch (Exception e) {
+            return Levels.ERROR;
+        }
+    }
 }

Modified: ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java (original)
+++ ace/trunk/org.apache.ace.agent/src/org/apache/ace/agent/impl/UpdateHandlerBase.java Wed Aug 28 12:28:00 2013
@@ -45,7 +45,7 @@ public class UpdateHandlerBase extends C
         BufferedReader reader = null;
         try {
             connection = getConnection(endpoint);
-            // TODO handle problems and retries
+            ConnectionUtil.checkConnectionResponse(connection);
             reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
             String versionString;
             while ((versionString = reader.readLine()) != null) {
@@ -115,16 +115,16 @@ public class UpdateHandlerBase extends C
     }
 
     protected DownloadHandle getDownloadHandle(URL packageURL) {
-        return getAgentContext().getDownloadHandler().getHandle(packageURL);
+        return getDownloadHandler().getHandle(packageURL);
     }
 
     protected String getIdentification() {
-        return getAgentContext().getIdentificationHandler().getAgentId();
+        return getIdentificationHandler().getAgentId();
     }
 
     protected URL getServerURL() throws RetryAfterException {
         // FIXME not sure if this is the proper place
-        URL serverURL = getAgentContext().getDiscoveryHandler().getServerUrl();
+        URL serverURL = getDiscoveryHandler().getServerUrl();
         if (serverURL == null) {
             throw new RetryAfterException(10);
         }
@@ -132,6 +132,6 @@ public class UpdateHandlerBase extends C
     }
 
     private URLConnection getConnection(URL url) throws IOException {
-        return getAgentContext().getConnectionHandler().getConnection(url);
+        return getConnectionHandler().getConnection(url);
     }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConfigurationHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConfigurationHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConfigurationHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConfigurationHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -18,14 +18,12 @@
  */
 package org.apache.ace.agent.impl;
 
-import static org.easymock.EasyMock.expect;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
-import java.io.File;
 import java.lang.reflect.Method;
 
-import org.apache.ace.agent.AgentContext;
+import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.testng.annotations.AfterMethod;
@@ -37,20 +35,17 @@ import org.testng.annotations.Test;
  */
 public class ConfigurationHandlerImplTest extends BaseAgentTest {
 
-    private AgentContext m_agentContext;
+    private AgentContextImpl m_agentContextImpl;
 
     @BeforeMethod
     public void setUpAgain(Method method) throws Exception {
-        File methodDir = new File(new File(getWorkDir(), ConfigurationHandlerImplTest.class.getName()), method.getName());
-        methodDir.mkdirs();
-        cleanDir(methodDir);
-        m_agentContext = addTestMock(AgentContext.class);
-        expect(m_agentContext.getWorkDir()).andReturn(methodDir).anyTimes();
+        m_agentContextImpl = mockAgentContext(method.getName());
         replayTestMocks();
     }
 
     @AfterMethod
     public void tearDownAgain(Method method) throws Exception {
+        m_agentContextImpl.stop();
         verifyTestMocks();
         clearTestMocks();
     }
@@ -59,16 +54,21 @@ public class ConfigurationHandlerImplTes
     public void testConfigClean() throws Exception {
 
         ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(0, configurationHandler.keySet().size());
         assertEquals(configurationHandler.get("key1", "default1"), "default1");
 
         // should be persisted
-
         configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(0, configurationHandler.keySet().size());
@@ -78,14 +78,17 @@ public class ConfigurationHandlerImplTes
     @Test
     public void testConfigSystemProps() throws Exception {
 
-        String systemKey1 = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "key1";
-        String systemKey2 = ConfigurationHandlerImpl.CONFIG_KEY_NAMESPACE + "key2";
+        String systemKey1 = AgentConstants.CONFIG_KEY_NAMESPACE + "key1";
+        String systemKey2 = AgentConstants.CONFIG_KEY_NAMESPACE + "key2";
 
         System.setProperty(systemKey1, "value1");
         System.setProperty(systemKey2, "value2");
 
         ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(2, configurationHandler.keySet().size());
@@ -97,8 +100,10 @@ public class ConfigurationHandlerImplTes
         System.clearProperty(systemKey1);
         System.clearProperty(systemKey2);
 
-        configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl());
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(2, configurationHandler.keySet().size());
@@ -113,8 +118,10 @@ public class ConfigurationHandlerImplTes
         configurationHandler.put(systemKey1, "newvalue1");
         configurationHandler.put(systemKey2, "newvalue2");
 
-        configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl());
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(2, configurationHandler.keySet().size());
@@ -125,11 +132,13 @@ public class ConfigurationHandlerImplTes
 
         System.setProperty(systemKey1, "valueX");
         System.setProperty(systemKey2, "valueY");
-        System.setProperty(systemKey1 + ConfigurationHandlerImpl.CONFIG_KEY_RETAIN, "true");
-        System.setProperty(systemKey2 + ConfigurationHandlerImpl.CONFIG_KEY_RETAIN, "true");
+        System.setProperty(systemKey1 + AgentConstants.CONFIG_KEY_RETAIN, "true");
+        System.setProperty(systemKey2 + AgentConstants.CONFIG_KEY_RETAIN, "true");
 
-        configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl());
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         assertNotNull(configurationHandler.keySet());
         assertEquals(2, configurationHandler.keySet().size());
@@ -141,7 +150,10 @@ public class ConfigurationHandlerImplTes
     public void testConfigBooleanProps() throws Exception {
 
         ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         configurationHandler.putBoolean("boolean1", true);
         configurationHandler.putBoolean("boolean2", false);
@@ -157,7 +169,10 @@ public class ConfigurationHandlerImplTes
     public void testConfigLongProps() throws Exception {
 
         ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl();
-        startHandler(configurationHandler, m_agentContext);
+        m_agentContextImpl.stop();
+        m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContextImpl.start();
+        configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
 
         configurationHandler.putLong("long1", 42);
         configurationHandler.putLong("long2", 4l);

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/ConnectionHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -36,7 +36,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.xml.bind.DatatypeConverter;
 
-import org.apache.ace.agent.AgentContext;
+import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
@@ -68,7 +68,6 @@ public class ConnectionHandlerImplTest e
                 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Requires Basic Auth");
             resp.setStatus(HttpServletResponse.SC_OK);
         }
-
     }
 
     private TestWebServer m_webServer;
@@ -76,12 +75,10 @@ public class ConnectionHandlerImplTest e
     private String m_pass = "Mantle";
     private URL m_basicAuthURL;
 
-    private AgentContext m_agentContext;
-    private ConfigurationHandler m_configurationHandler;
-    private ConnectionHandler m_connectionHandler;
+    private AgentContextImpl m_agentContext;
 
     @BeforeTest
-    public void setUpAgain() throws Exception {
+    public void setUpOnceAgain() throws Exception {
 
         int port = 8880;
         m_basicAuthURL = new URL("http://localhost:" + port + "/basicauth");
@@ -89,40 +86,42 @@ public class ConnectionHandlerImplTest e
         m_webServer.addServlet(new BasicAuthServlet(m_user, m_pass), "/basicauth/*");
         m_webServer.start();
 
-        m_configurationHandler = addTestMock(ConfigurationHandler.class);
-        m_agentContext = addTestMock(AgentContext.class);
-        expect(m_agentContext.getConfigurationHandler()).andReturn(m_configurationHandler).anyTimes();
-        replayTestMocks();
+        m_agentContext = mockAgentContext();
+        m_agentContext.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
 
-        m_connectionHandler = new ConnectionHandlerImpl();
-        startHandler(m_connectionHandler, m_agentContext);
+        replayTestMocks();
+        m_agentContext.start();
     }
 
     @AfterTest
-    public void tearDownAgain() throws Exception {
-        stopHandler(m_connectionHandler);
+    public void tearDownOnceAgain() throws Exception {
+        m_agentContext.stop();
         m_webServer.stop();
         verifyTestMocks();
+        clearTestMocks();
     }
 
     @Test
     public void testBasicAuthFORBIDDEN() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(notNull(String.class), anyObject(String.class))).andReturn(null).anyTimes();
-        replay(m_configurationHandler);
-        HttpURLConnection connection = (HttpURLConnection) m_connectionHandler.getConnection(m_basicAuthURL);
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(notNull(String.class), anyObject(String.class))).andReturn(null).anyTimes();
+        replay(configurationHandler);
+        HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL);
         assertEquals(connection.getResponseCode(), HttpServletResponse.SC_FORBIDDEN);
-
     }
 
     @Test
     public void testBasicAuthOK() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class))).andReturn("BASIC").anyTimes();
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHUSER), anyObject(String.class))).andReturn(m_user).anyTimes();
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHPASS), anyObject(String.class))).andReturn(m_pass).anyTimes();
-        replay(m_configurationHandler);
-        HttpURLConnection connection = (HttpURLConnection) m_connectionHandler.getConnection(m_basicAuthURL);
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class))).andReturn("BASIC").anyTimes();
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_USERNAME), anyObject(String.class))).andReturn(m_user).anyTimes();
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_PASSWORD), anyObject(String.class))).andReturn(m_pass).anyTimes();
+        replay(configurationHandler);
+        HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL);
         assertEquals(connection.getResponseCode(), HttpServletResponse.SC_OK);
     }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/CustomControllerTest.java Wed Aug 28 12:28:00 2013
@@ -30,7 +30,6 @@ import java.net.URL;
 import java.util.SortedSet;
 import java.util.TreeSet;
 
-import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.AgentControl;
 import org.apache.ace.agent.DeploymentHandler;
 import org.apache.ace.agent.DownloadHandle;
@@ -48,7 +47,6 @@ import org.testng.annotations.Test;
  */
 public class CustomControllerTest extends BaseAgentTest {
 
-    AgentControl m_agentControl;
     Version m_version1 = Version.parseVersion("1.0.0");
     Version m_version2 = Version.parseVersion("2.0.0");
     Version m_version3 = Version.parseVersion("3.0.0");
@@ -59,6 +57,9 @@ public class CustomControllerTest extend
     URL m_dummyFileUrl;
     InputStream m_dummyInputStream;
 
+    AgentControl m_agentControl;
+    AgentContextImpl m_agentContext;
+
     @BeforeTest
     public void setUpOnceAgain() throws Exception {
 
@@ -69,7 +70,7 @@ public class CustomControllerTest extend
         m_dummyFile = File.createTempFile("mock", ".txt");
         m_dummyFile.deleteOnExit();
         m_dummyFileUrl = m_dummyFile.toURI().toURL();
-        
+
         m_workDir = new File(m_dummyFile.getParentFile(), "test-" + System.currentTimeMillis());
         m_workDir.mkdir();
     }
@@ -94,19 +95,20 @@ public class CustomControllerTest extend
         deploymentHandler.deployPackage(notNull(InputStream.class));
         expectLastCall().once();
 
-        AgentContext agentContext = addTestMock(AgentContext.class);
-        expect(agentContext.getDeploymentHandler()).andReturn(deploymentHandler).anyTimes();
-        expect(agentContext.getWorkDir()).andReturn(m_workDir).anyTimes();
-
+        m_agentContext = mockAgentContext();
+        m_agentContext.setHandler(DeploymentHandler.class, deploymentHandler);
         replayTestMocks();
-        m_agentControl = new AgentControlImpl(agentContext);
+        
+        m_agentContext.start();
+        m_agentControl = new AgentControlImpl(m_agentContext);
     }
 
     @AfterMethod
     public void tearDownOnceAgain() throws Exception {
+        m_dummyInputStream.close();
+        m_agentContext.stop();
         verifyTestMocks();
         clearTestMocks();
-        m_dummyInputStream.close();
     }
 
     @Test

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DeploymentHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -46,7 +46,6 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.ace.agent.AgentConstants;
-import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.DeploymentHandler;
@@ -106,6 +105,10 @@ public class DeploymentHandlerImplTest e
         }
     }
 
+    int port = 8881;
+    String identification = "agent";
+    URL serverURL = null;
+
     private TestWebServer m_webserver;
     private File m_200file;
     private Version m_version1 = Version.parseVersion("1.0.0");
@@ -113,15 +116,12 @@ public class DeploymentHandlerImplTest e
     private Version m_version3 = Version.parseVersion("3.0.0");
     long m_remotePackageSize = 0l;
 
-    private DeploymentHandler m_deploymentHandler;
+    private AgentContextImpl m_agentContext;
 
     @BeforeTest
     public void setUpOnceAgain() throws Exception {
 
-        int port = 8881;
-        String identification = "agent";
-        URL serverURL = new URL("http://localhost:" + port + "/");
-
+        serverURL = new URL("http://localhost:" + port + "/");
         m_webserver = new TestWebServer(port, "/", "generated");
         m_webserver.start();
 
@@ -168,42 +168,40 @@ public class DeploymentHandlerImplTest e
         expect(deploymentAdmin.installDeploymentPackage(notNull(InputStream.class)
             )).andReturn(deploymentPackage3).once();
 
-        AgentContext agentContext = addTestMock(AgentContext.class);
-        expect(agentContext.getIdentificationHandler()).andReturn(identificationHandler).anyTimes();
-        expect(agentContext.getDiscoveryHandler()).andReturn(discoveryHandler).anyTimes();
-        expect(agentContext.getConfigurationHandler()).andReturn(configurationHandler).anyTimes();
-
-        ConnectionHandler connectionHandler = new ConnectionHandlerImpl();
-        expect(agentContext.getConnectionHandler()).andReturn(connectionHandler).anyTimes();
-
+        m_agentContext = mockAgentContext();
+        m_agentContext.setHandler(IdentificationHandler.class, identificationHandler);
+        m_agentContext.setHandler(DiscoveryHandler.class, discoveryHandler);
+        m_agentContext.setHandler(ConfigurationHandler.class, configurationHandler);
+        m_agentContext.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
+        m_agentContext.setHandler(DeploymentHandler.class, new DeploymentHandlerImpl(deploymentAdmin));
         replayTestMocks();
-
-        m_deploymentHandler = new DeploymentHandlerImpl(deploymentAdmin);
-        startHandler(connectionHandler, agentContext);
-        startHandler(m_deploymentHandler, agentContext);
+        m_agentContext.start();
     }
 
     @AfterTest
     public void tearDownOnceAgain() throws Exception {
-        stopHandler(m_deploymentHandler);
-        verifyTestMocks();
         m_webserver.stop();
+        m_agentContext.stop();
+        verifyTestMocks();
+        clearTestMocks();
     }
 
     @Test
     public void testCurrentVersion() throws Exception {
-        Version current = m_deploymentHandler.getInstalledVersion();
+        DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class);
+        Version current = deploymentHandler.getInstalledVersion();
         assertNotNull(current);
         assertEquals(current, m_version2);
     }
 
     @Test
     public void testAvailableVersions() throws Exception {
+        DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class);
         SortedSet<Version> expected = new TreeSet<Version>();
         expected.add(m_version1);
         expected.add(m_version2);
         expected.add(m_version3);
-        SortedSet<Version> available = m_deploymentHandler.getAvailableVersions();
+        SortedSet<Version> available = deploymentHandler.getAvailableVersions();
         assertNotNull(available);
         assertFalse(available.isEmpty());
         assertEquals(available, expected);
@@ -211,15 +209,17 @@ public class DeploymentHandlerImplTest e
 
     @Test
     public void testPackageSize() throws Exception {
-        long packageSize = m_deploymentHandler.getPackageSize(m_version1, true);
+        DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class);
+        long packageSize = deploymentHandler.getPackageSize(m_version1, true);
         assertEquals(packageSize, m_remotePackageSize);
     }
 
     @Test
     public void testDeployPackage() throws Exception {
-        InputStream inputStream = m_deploymentHandler.getInputStream(m_version3, true);
+        DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class);
+        InputStream inputStream = deploymentHandler.getInputStream(m_version3, true);
         try {
-            m_deploymentHandler.deployPackage(inputStream);
+            deploymentHandler.deployPackage(inputStream);
         }
         finally {
             inputStream.close();

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DiscoveryHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -23,19 +23,18 @@ import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.resetToNice;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
 import java.net.URL;
 
+import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.ConnectionHandler;
 import org.apache.ace.agent.DiscoveryHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
 import org.apache.ace.agent.testutil.TestWebServer;
-import org.osgi.service.log.LogService;
 import org.testng.annotations.AfterTest;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.Test;
@@ -49,123 +48,142 @@ public class DiscoveryHandlerImplTest ex
     private TestWebServer m_webServer;
     private URL m_availableURL;
     private URL m_unavailableURL;
-    private DiscoveryHandler m_discoveryHandler;
-    private ConfigurationHandler m_configurationHandler;
-    private ConnectionHandler m_connectionHandler;
 
-    @BeforeTest
-    public void setUpAgain() throws Exception {
+    private AgentContext m_agentContext;
+    private AgentContextImpl m_agentContextImpl;
 
+    @BeforeTest
+    public void setUpOnceAgain() throws Exception {
         m_webServer = new TestWebServer(PORT, "/", "generated");
         m_webServer.start();
         m_availableURL = new URL("http://localhost:" + PORT);
         m_unavailableURL = new URL("http://localhost:9999");
 
-        AgentContext agentContext = addTestMock(AgentContext.class);
-
-        LogService logService = addTestMock(LogService.class);
-        resetToNice(logService);
-
-        m_discoveryHandler = new DiscoveryHandlerImpl();
-        m_connectionHandler = new ConnectionHandlerImpl();
-        m_configurationHandler = addTestMock(ConfigurationHandler.class);
-
-        expect(agentContext.getConfigurationHandler()).andReturn(m_configurationHandler).anyTimes();
-        expect(agentContext.getConnectionHandler()).andReturn(m_connectionHandler).anyTimes();
-
+        m_agentContextImpl = mockAgentContext();
+        m_agentContext = m_agentContextImpl;
+        m_agentContextImpl.setHandler(DiscoveryHandler.class, new DiscoveryHandlerImpl());
+        m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl());
         replayTestMocks();
-        startHandler(m_connectionHandler, agentContext);
-        startHandler(m_discoveryHandler, agentContext);
+        m_agentContextImpl.start();
     }
 
     @AfterTest
-    public void tearDownAgain() throws Exception {
-        stopHandler(m_connectionHandler);
-        stopHandler(m_discoveryHandler);
-        verifyTestMocks();
+    public void tearDownOnceAgain() throws Exception {
         m_webServer.stop();
+
+        m_agentContextImpl.stop();
+        verifyTestMocks();
+        clearTestMocks();
     }
 
     @Test
     public void testAvailableURL() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_availableURL.toExternalForm()).anyTimes();
-        replay(m_configurationHandler);
-        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertEquals(discoveryHandler.getServerUrl(), m_availableURL);
     }
 
     @Test
     public void testUnavailableURL_unavailable() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_unavailableURL.toExternalForm()).anyTimes();
-        replay(m_configurationHandler);
-        assertNull(m_discoveryHandler.getServerUrl());
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertNull(discoveryHandler.getServerUrl());
     }
 
     @Test
     public void testUnavailableAfterConfigUpdate() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_availableURL.toExternalForm()).once();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_unavailableURL.toExternalForm()).once();
-        replay(m_configurationHandler);
-        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
-        assertNull(m_discoveryHandler.getServerUrl());
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertEquals(discoveryHandler.getServerUrl(), m_availableURL);
+        assertNull(discoveryHandler.getServerUrl());
     }
 
     @Test
     public void testAvailableAfterConfigUpdate() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_unavailableURL.toExternalForm()).once();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_availableURL.toExternalForm()).once();
-        replay(m_configurationHandler);
-        assertNull(m_discoveryHandler.getServerUrl());
-        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertNull(discoveryHandler.getServerUrl());
+        assertEquals(discoveryHandler.getServerUrl(), m_availableURL);
     }
 
     @Test
     public void testAvailableAfterUnavailableURL() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn(m_unavailableURL.toExternalForm() + "," + m_availableURL.toExternalForm()).once();
-        replay(m_configurationHandler);
-        assertEquals(m_discoveryHandler.getServerUrl(), m_availableURL);
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertEquals(discoveryHandler.getServerUrl(), m_availableURL);
     }
 
     @Test
     public void testEmptyURLConfig() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn("").once();
-        replay(m_configurationHandler);
-        assertNull(m_discoveryHandler.getServerUrl());
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertNull(discoveryHandler.getServerUrl());
     }
 
     @Test
     public void testBadURLConfig() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(ConnectionHandlerImpl.PROP_AUTHTYPE), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_CONNECTION_AUTHTYPE), anyObject(String.class)))
             .andReturn(null).anyTimes();
-        expect(m_configurationHandler.get(eq(DiscoveryHandlerImpl.CONFIG_KEY_SERVERURLS), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_DISCOVERY_SERVERURLS), anyObject(String.class)))
             .andReturn("foobar").once();
-        replay(m_configurationHandler);
-        assertNull(m_discoveryHandler.getServerUrl());
+        expect(configurationHandler.getBoolean(AgentConstants.CONFIG_DISCOVERY_CHECKING, false))
+            .andReturn(true).anyTimes();
+        replay(configurationHandler);
+        DiscoveryHandler discoveryHandler = m_agentContext.getHandler(DiscoveryHandler.class);
+        assertNull(discoveryHandler.getServerUrl());
     }
 }

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/DownloadHandlerTest.java Wed Aug 28 12:28:00 2013
@@ -18,7 +18,6 @@
  */
 package org.apache.ace.agent.impl;
 
-import static org.easymock.EasyMock.expect;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
@@ -47,9 +46,9 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.DownloadHandle;
-import org.apache.ace.agent.DownloadHandler;
-import org.apache.ace.agent.DownloadHandle.ResultListener;
 import org.apache.ace.agent.DownloadHandle.ProgressListener;
+import org.apache.ace.agent.DownloadHandle.ResultListener;
+import org.apache.ace.agent.DownloadHandler;
 import org.apache.ace.agent.DownloadResult;
 import org.apache.ace.agent.DownloadState;
 import org.apache.ace.agent.testutil.BaseAgentTest;
@@ -79,7 +78,6 @@ public class DownloadHandlerTest extends
         }
     }
 
-    private DownloadHandler m_downloadHandler;
     private TestWebServer m_webServer;
     private URL m_200url;
     private File m_200file;
@@ -88,6 +86,9 @@ public class DownloadHandlerTest extends
     private URL m_404url;
     private URL m_503url;
 
+    private AgentContextImpl m_agentContextImpl;
+    private AgentContext m_agentContext;
+
     @BeforeTest
     public void setUpOnceAgain() throws Exception {
 
@@ -110,35 +111,38 @@ public class DownloadHandlerTest extends
         m_webServer.addServlet(new TestErrorServlet(), "/error");
         m_webServer.start();
 
-        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+        m_agentContextImpl = mockAgentContext();
+        m_agentContext = m_agentContextImpl;
 
-        AgentContext agentContext = addTestMock(AgentContext.class);
-        expect(agentContext.getExecutorService()).andReturn(executorService).anyTimes();
+        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
+        m_agentContextImpl.setHandler(ScheduledExecutorService.class, executorService);
+        m_agentContextImpl.setHandler(DownloadHandler.class, new DownloadHandlerImpl());
 
+        m_agentContextImpl.start();
         replayTestMocks();
-        m_downloadHandler = new DownloadHandlerImpl();
-        startHandler(m_downloadHandler, agentContext);
     }
 
     @AfterTest
     public void tearDownOnceAgain() throws Exception {
-        stopHandler(m_downloadHandler);
-        verifyTestMocks();
+        m_agentContextImpl.stop();
         m_webServer.stop();
+        verifyTestMocks();
     }
 
     @Test
     public void testSuccessful_noresume_result() throws Exception {
-        final DownloadHandle handle = m_downloadHandler.getHandle(m_200url).start();
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+        final DownloadHandle handle = downloadHandler.getHandle(m_200url).start();
         final DownloadResult result = handle.result();
         assertSuccessFul(result, 200, m_200digest);
     }
 
     @Test
     public void testSuccessful_noresume_listener() throws Exception {
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
         final CountDownLatch latch = new CountDownLatch(1);
         final List<DownloadResult> holder = new ArrayList<DownloadResult>();
-        final DownloadHandle handle = m_downloadHandler.getHandle(m_200url)
+        final DownloadHandle handle = downloadHandler.getHandle(m_200url)
             .setCompletionListener(new ResultListener() {
                 @Override
                 public void completed(DownloadResult result) {
@@ -153,7 +157,8 @@ public class DownloadHandlerTest extends
 
     @Test
     public void testSuccessful_resume_result() throws Exception {
-        final DownloadHandle handle = m_downloadHandler.getHandle(m_200url);
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+        final DownloadHandle handle = downloadHandler.getHandle(m_200url);
         handle.setProgressListener(new ProgressListener() {
             @Override
             public void progress(long contentLength, long progress) {
@@ -167,7 +172,8 @@ public class DownloadHandlerTest extends
 
     @Test
     public void testFailedIO_nostatus_result() throws Exception {
-        DownloadHandle handle = m_downloadHandler.getHandle(m_200url, 2048);
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+        DownloadHandle handle = downloadHandler.getHandle(m_200url, 2048);
 
         DownloadResult result = ((DownloadHandleImpl) handle).start(DownloadCallableImpl.FAIL_OPENCONNECTION).result();
         assertFailed(result, 0);
@@ -195,13 +201,15 @@ public class DownloadHandlerTest extends
 
     @Test
     public void testFailed404_noresume_result() throws Exception {
-        final DownloadResult result = m_downloadHandler.getHandle(m_404url).start().result();
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+        final DownloadResult result = downloadHandler.getHandle(m_404url).start().result();
         assertFailed(result, 404);
     }
 
     @Test
     public void testFailed503_noresume_result() throws Exception {
-        DownloadResult result = m_downloadHandler.getHandle(m_503url).start().result();
+        DownloadHandler downloadHandler = m_agentContext.getHandler(DownloadHandler.class);
+        DownloadResult result = downloadHandler.getHandle(m_503url).start().result();
         assertFailed(result, 503);
         assertNotNull(result.getHeaders().get("Retry-After"), "Expected a Retry-After header from error servlet");
         assertNotNull(result.getHeaders().get("Retry-After").get(0), "Expected a Retry-After header from error servlet");

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/FeedbackHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -27,11 +27,10 @@ import static org.testng.Assert.assertNo
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
-import java.io.File;
 import java.lang.reflect.Method;
 import java.util.Set;
 
-import org.apache.ace.agent.AgentContext;
+import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.FeedbackHandler;
 import org.apache.ace.agent.testutil.BaseAgentTest;
@@ -44,38 +43,32 @@ import org.testng.annotations.Test;
  */
 public class FeedbackHandlerImplTest extends BaseAgentTest {
 
-    private AgentContext m_agentContext;
-    private ConfigurationHandler m_configurationHandler;
+    private AgentContextImpl m_agentContextImpl;
 
     @BeforeMethod
     public void setUpAgain(Method method) throws Exception {
-        File methodDir = new File(new File(getWorkDir(), FeedbackHandlerImplTest.class.getName()), method.getName());
-        methodDir.mkdirs();
-        cleanDir(methodDir);
-
-        m_agentContext = addTestMock(AgentContext.class);
-        m_configurationHandler = addTestMock(ConfigurationHandler.class);
-        expect(m_agentContext.getWorkDir()).andReturn(methodDir).anyTimes();
-        expect(m_agentContext.getConfigurationHandler()).andReturn(m_configurationHandler).anyTimes();
+        m_agentContextImpl = mockAgentContext(method.getName());
         replayTestMocks();
+        m_agentContextImpl.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl());
+        m_agentContextImpl.start();
     }
 
     @AfterMethod
     public void tearDownAgain(Method method) throws Exception {
+        m_agentContextImpl.stop();
         verifyTestMocks();
         clearTestMocks();
     }
 
     @Test
     public void testFeedbackChannelConfig() throws Exception {
+        ConfigurationHandler configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_FEEDBACK_CHANNELS),
+            anyObject(String.class))).andReturn("auditlog").anyTimes();
+        replay(configurationHandler);
 
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(FeedbackHandlerImpl.CONFIG_KEY_CHANNELS), anyObject(String.class))).andReturn("auditlog").anyTimes();
-        replay(m_configurationHandler);
-
-        FeedbackHandler feedbackHandler = new FeedbackHandlerImpl();
-        startHandler(feedbackHandler, m_agentContext);
-
+        FeedbackHandler feedbackHandler = m_agentContextImpl.getHandler(FeedbackHandler.class);
         Set<String> names = feedbackHandler.getChannelNames();
         assertNotNull(names);
         assertTrue(names.size() == 1);
@@ -83,9 +76,10 @@ public class FeedbackHandlerImplTest ext
         assertNotNull(feedbackHandler.getChannel("auditlog"));
         assertNull(feedbackHandler.getChannel("QQQ"));
 
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(FeedbackHandlerImpl.CONFIG_KEY_CHANNELS), anyObject(String.class))).andReturn("auditlog, customchannel").anyTimes();
-        replay(m_configurationHandler);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_FEEDBACK_CHANNELS),
+            anyObject(String.class))).andReturn("auditlog, customchannel").anyTimes();
+        replay(configurationHandler);
 
         names = feedbackHandler.getChannelNames();
         assertNotNull(names);

Modified: ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java?rev=1518177&r1=1518176&r2=1518177&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java (original)
+++ ace/trunk/org.apache.ace.agent/test/org/apache/ace/agent/impl/IdentificationHandlerImplTest.java Wed Aug 28 12:28:00 2013
@@ -26,6 +26,7 @@ import static org.easymock.EasyMock.rese
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
+import org.apache.ace.agent.AgentConstants;
 import org.apache.ace.agent.AgentContext;
 import org.apache.ace.agent.ConfigurationHandler;
 import org.apache.ace.agent.IdentificationHandler;
@@ -39,62 +40,68 @@ import org.testng.annotations.Test;
  */
 public class IdentificationHandlerImplTest extends BaseAgentTest {
 
-    private IdentificationHandler m_identificationHandler;
-    private ConfigurationHandler m_configurationHandler;
+    private AgentContextImpl m_agentContextImpl;
+    private AgentContext m_agentContext;
 
     @BeforeTest
     public void setUpAgain() throws Exception {
-        AgentContext agentContext = addTestMock(AgentContext.class);
-        m_identificationHandler = new IdentificationHandlerImpl();
-        m_configurationHandler = addTestMock(ConfigurationHandler.class);
-        expect(agentContext.getConfigurationHandler()).andReturn(m_configurationHandler).anyTimes();
+        m_agentContextImpl = mockAgentContext();
+        m_agentContext = m_agentContextImpl;
+        m_agentContextImpl.setHandler(IdentificationHandler.class, new IdentificationHandlerImpl());
+        m_agentContextImpl.start();
         replayTestMocks();
-
-        startHandler(m_identificationHandler, agentContext);
     }
 
     @AfterTest
     public void tearDownAgain() throws Exception {
-        stopHandler(m_identificationHandler);
+        m_agentContextImpl.stop();
         verifyTestMocks();
     }
 
     @Test
     public void testAvailableIdentification() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(IdentificationHandlerImpl.CONFIG_KEY_IDENTIFICATION), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class)))
             .andReturn("qqq").once();
-        replay(m_configurationHandler);
-        assertEquals(m_identificationHandler.getAgentId(), "qqq");
+        replay(configurationHandler);
+        IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class);
+        assertEquals(identificationHandler.getAgentId(), "qqq");
     }
 
     @Test
     public void testUpdatedIdentification() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(IdentificationHandlerImpl.CONFIG_KEY_IDENTIFICATION), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class)))
             .andReturn("qqq").once();
-        expect(m_configurationHandler.get(eq(IdentificationHandlerImpl.CONFIG_KEY_IDENTIFICATION), anyObject(String.class)))
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class)))
             .andReturn("yyy").once();
-        replay(m_configurationHandler);
-        assertEquals(m_identificationHandler.getAgentId(), "qqq");
-        assertEquals(m_identificationHandler.getAgentId(), "yyy");
+        replay(configurationHandler);
+        IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class);
+        assertEquals(identificationHandler.getAgentId(), "qqq");
+        assertEquals(identificationHandler.getAgentId(), "yyy");
     }
 
     @Test
     public void testNoIdentification() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(IdentificationHandlerImpl.CONFIG_KEY_IDENTIFICATION), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class)))
             .andReturn(null).once();
-        replay(m_configurationHandler);
-        assertNull(m_identificationHandler.getAgentId());
+        replay(configurationHandler);
+        IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class);
+        assertNull(identificationHandler.getAgentId());
     }
 
     @Test
     public void testEmptyIdentification() throws Exception {
-        reset(m_configurationHandler);
-        expect(m_configurationHandler.get(eq(IdentificationHandlerImpl.CONFIG_KEY_IDENTIFICATION), anyObject(String.class)))
+        ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class);
+        reset(configurationHandler);
+        expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class)))
             .andReturn(null).once();
-        replay(m_configurationHandler);
-        assertNull(m_identificationHandler.getAgentId());
+        replay(configurationHandler);
+        IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class);
+        assertNull(identificationHandler.getAgentId());
     }
 }