ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1491702 [6/6] - in /ace/sandbox/bramk: cnf/localrepo/ cnf/localrepo/org.apache.ace.log.api/ cnf/localrepo/org.apache.ace.log.target.store.impl/ cnf/localrepo/org.apache.ace.log.target/ cnf/localrepo/org.apache.ace.range.api/ cnf/localrepo/...
Date Tue, 11 Jun 2013 07:56:27 GMT
Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConfigurationHelper.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConfigurationHelper.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConfigurationHelper.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/ConfigurationHelper.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+import org.apache.ace.agent.Constants;
+import org.apache.ace.agent.spi.ComponentFactory;
+
+/**
+ * Handles management agent configuration system properties and subsequently calls
+ * {@link AgentFactoryImpl#updated(String, Dictionary)} for every agent specific configuration.
+ * 
+ */
+public class ConfigurationHelper {
+
+    private static final String DEFAULTS_RESOURCE = "org/apache/ace/agent/impl/agent-defaults.properties";
+    private final Map<String, String> m_configuration;
+
+    private final String[] m_agentIds;
+    private final Map<String, Map<String, String>> m_agentConfigurations;
+    private final ComponentFactory[] m_componentFactories;
+
+    public ConfigurationHelper(Map<String, String> configuration) throws Exception {
+
+        m_configuration = loadDefaultConfiguration();
+        m_configuration.putAll(configuration);
+        m_agentIds = loadAgentIds();
+        m_componentFactories = loadComponentFactories();
+        m_agentConfigurations = new HashMap<String, Map<String, String>>();
+        for (String agentId : m_agentIds) {
+            m_agentConfigurations.put(agentId, loadAgentConfiguration(agentId));
+        }
+    }
+
+    public String[] getAgentIds() {
+        return m_agentIds;
+    }
+
+    public ComponentFactory[] getComponentFactories() throws Exception {
+        return m_componentFactories;
+    }
+
+    public Map<String, String> getComponentFactoriesConfiguration() throws Exception {
+        // FIXME should pass static config only
+        return m_configuration;
+    }
+
+    public Map<String, String> getAgentConfiguration(String agentId) throws Exception {
+        return m_agentConfigurations.get(agentId);
+    }
+
+    private String[] loadAgentIds() {
+        List<String> agentList = getStringListProperty(m_configuration, Constants.CONFIG_AGENTS_KEY);
+        return agentList.toArray(new String[agentList.size()]);
+    }
+
+    private ComponentFactory[] loadComponentFactories() throws Exception {
+        List<String> componentFactoryList = getStringListProperty(m_configuration, Constants.CONFIG_FACTORIES_KEY);
+        ComponentFactory[] componentFactories = new ComponentFactory[componentFactoryList.size()];
+        int i = 0;
+        for (String componentFactory : componentFactoryList) {
+            componentFactories[i++] = loadComponentFactory(componentFactory);
+        }
+        return componentFactories;
+    }
+
+    private Map<String, String> loadAgentConfiguration(String agentId) throws Exception {
+
+        String agentPrefix = agentId + ".";
+        Map<String, String> agentConfiguration = new HashMap<String, String>();
+        // first map all global properties
+        for (Entry<String, String> entry : m_configuration.entrySet()) {
+            if (!entry.getKey().startsWith(agentPrefix)) {
+                agentConfiguration.put(entry.getKey(), entry.getValue());
+            }
+        }
+        // overwrite with agent specific properties
+        for (Entry<String, String> entry : m_configuration.entrySet()) {
+            if (entry.getKey().startsWith(agentPrefix)) {
+                agentConfiguration.put(entry.getKey().replaceFirst(agentPrefix, ""), entry.getValue());
+            }
+        }
+        agentConfiguration.put("agent", agentId);
+        return agentConfiguration;
+    }
+
+    private List<String> getStringListProperty(Map<String, String> configuration, String key) {
+        List<String> values = new ArrayList<String>();
+        String value = (String) m_configuration.get(key);
+        if (value != null && !value.equals("")) {
+            String[] parts = value.split(",");
+            for (String part : parts) {
+                values.add(part.trim());
+            }
+        }
+        return values;
+    }
+
+    private Map<String, String> loadDefaultConfiguration() throws IOException {
+        Properties properties = new Properties();
+        ClassLoader classloader = getClass().getClassLoader();
+        InputStream inStream = classloader.getResourceAsStream(DEFAULTS_RESOURCE);
+        if (inStream != null) {
+            try {
+                properties.load(inStream);
+            }
+            finally {
+                inStream.close();
+            }
+        }
+        Map<String, String> configuration = new HashMap<String, String>();
+        for (Object key : properties.keySet()) {
+            configuration.put((String) key, (String) properties.getProperty((String) key));
+        }
+        return configuration;
+    }
+
+    private ComponentFactory loadComponentFactory(String componentFactoryName) throws Exception {
+        try {
+            Class<?> clazz = AgentFactoryImpl.class.getClassLoader().loadClass(componentFactoryName);
+            if (!ComponentFactory.class.isAssignableFrom(clazz)) {
+                throw new Exception("Factory class does not implement ComponentFactory interface: " + componentFactoryName);
+            }
+            try {
+                Object instance = clazz.newInstance();
+                return (ComponentFactory) instance;
+            }
+            catch (InstantiationException e) {
+                throw new Exception("Factory class does not have a default constructor: " + componentFactoryName);
+            }
+            catch (IllegalAccessException e) {
+                throw new Exception("Factory class does not have a default constructor: " + componentFactoryName);
+            }
+        }
+        catch (Exception e) {
+            throw new Exception("Factory class not found: " + componentFactoryName);
+        }
+    }
+
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LogLevels.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LogLevels.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LogLevels.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/LogLevels.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,37 @@
+package org.apache.ace.agent.impl;
+
+import org.osgi.service.log.LogService;
+
+public enum LogLevels {
+    DEBUG(LogService.LOG_DEBUG),
+    INFO(LogService.LOG_INFO),
+    WARNING(LogService.LOG_WARNING),
+    ERROR(LogService.LOG_ERROR),
+    UNKNOWN(-1);
+
+    public static LogLevels fromCode(int code) {
+        switch (code) {
+            case LogService.LOG_DEBUG:
+                return DEBUG;
+            case LogService.LOG_INFO:
+                return INFO;
+            case LogService.LOG_WARNING:
+                return WARNING;
+            case LogService.LOG_ERROR:
+                return ERROR;
+            default:
+                return UNKNOWN;
+        }
+    }
+
+    private final int m_code;
+
+    private LogLevels(int code) {
+        m_code = code;
+    }
+
+    public int getCode() {
+        return m_code;
+    }
+
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/impl/agent-defaults.properties Tue Jun 11 07:56:25 2013
@@ -0,0 +1,22 @@
+
+#
+# Apache ACE agent defaults
+# 
+
+#
+# Component factories
+#
+# Used to instantiate the the agent service components. These are instantiated by the management 
+# agent factory when a new agent is configured and may be specified per agent. 
+# All classes MUST be on the (system)bundle class-path.
+#
+system.factories= \
+   	org.apache.ace.agent.connection.DefaultConnectionHandlerFactory,\
+   	org.apache.ace.agent.deployment.DefaultDeploymentHandlerFactory,\
+   	org.apache.ace.agent.discovery.DefaultDiscoveryHandlerFactory,\
+   	org.apache.ace.agent.download.DefaultDownloadHandlerFactory,\
+    org.apache.ace.agent.identification.DefaultIdentificationHandlerFactory, \
+   	org.apache.ace.agent.update.DefaultUpdateHandlerFactory,\
+   	org.apache.ace.agent.versions.DefaultVersionsHandlerFactory,\
+   	org.apache.ace.agent.logging.DefaultLoggingHandlerFactory,\
+   	org.apache.ace.agent.eventlogging.DefaultEventLoggerFactory
\ No newline at end of file

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.logging;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.ConnectionHandler;
+import org.apache.ace.agent.spi.DiscoveryHandler;
+import org.apache.ace.agent.spi.IdentificationHandler;
+import org.apache.ace.agent.spi.LoggingHandler;
+import org.apache.ace.log.LogDescriptor;
+import org.apache.ace.log.LogEvent;
+import org.apache.ace.log.target.store.LogStore;
+import org.apache.ace.range.RangeIterator;
+import org.apache.ace.range.SortedRangeSet;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+
+public class DefaultLoggingHandler implements LoggingHandler {
+
+    public String LOG_STORES = "logstores";
+
+    private static final String COMMAND_QUERY = "query";
+    private static final String COMMAND_SEND = "send";
+    private static final String PARAMETER_TARGETID = "tid";
+    private static final String PARAMETER_LOGID = "logid";
+
+    private final Map<String, LogStoreImpl> m_logStores = new HashMap<String, LogStoreImpl>();
+
+    private volatile DependencyManager m_manager;
+    private volatile Component m_component;
+    private volatile AgentContext m_context;
+
+    private volatile IdentificationHandler m_identification;
+    private volatile DiscoveryHandler m_discovery;
+    private volatile ConnectionHandler m_connection;
+
+    public void init() throws Exception {
+
+        m_component
+            .add(m_manager.createServiceDependency()
+                .setService(IdentificationHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(DiscoveryHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(ConnectionHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true)
+            );
+
+        File logsDir = new File(m_context.getWorkDir(), "logs");
+        if (!logsDir.exists() && !logsDir.mkdir()) {
+            throw new IllegalStateException("Unable to access log dir " + logsDir.getAbsolutePath());
+        }
+        String value = m_context.getConfig().get(LOG_STORES);
+        String[] stores = value.split(",");
+        if (stores.length != 0) {
+            for (int i = 0; i < stores.length; i++) {
+                LogStoreImpl logStoreImpl = new LogStoreImpl(logsDir);
+                logStoreImpl.start();
+                m_logStores.put(stores[i], logStoreImpl);
+            }
+        }
+    }
+
+    @Override
+    public void sync() throws Exception {
+        syncLogs();
+    }
+
+    @Override
+    public void log(String name, int eventType, Dictionary<String, String> props) throws Exception {
+        m_logStores.get(name).put(eventType, props);
+    }
+
+    public void syncLogs() throws Exception {
+        for (Entry<String, LogStoreImpl> entry : m_logStores.entrySet()) {
+            syncLog(entry.getKey(), entry.getValue());
+        }
+    }
+
+    private void syncLog(String storeName, LogStore logStore) throws Exception {
+        for (Long logId : logStore.getLogIDs()) {
+            syncLogId(storeName, logStore, logId);
+        }
+    }
+
+    private void syncLogId(String storeName, LogStore logStore, long logId) throws Exception {
+
+        long highestLocal = logStore.getHighestID(logId);
+        if (highestLocal == 0) {
+            return;
+        }
+
+        SortedRangeSet localRange = new SortedRangeSet("1-" + highestLocal);
+        SortedRangeSet remoteRange = getRemoteStoreRange(storeName, logId);
+        SortedRangeSet deltaRange = remoteRange.diffDest(localRange);
+        RangeIterator rangeIterator = deltaRange.iterator();
+        if (rangeIterator.hasNext()) {
+            long lowest = rangeIterator.next();
+            long highest = deltaRange.getHigh() < localRange.getHigh() ? deltaRange.getHigh() : localRange.getHigh();
+            List<LogEvent> logEvents = logStore.get(logId, lowest, highest);
+            syncLogIdEvents(logStore, storeName, logId, logEvents);
+        }
+    }
+
+    private void syncLogIdEvents(LogStore logStore, String storeName, long logId, List<LogEvent> logEvents) throws Exception {
+        HttpURLConnection connection = null;
+        BufferedWriter writer = null;
+        try {
+            connection = m_connection.getConnection(new URL(m_discovery.getServerURL(), storeName + "/" + COMMAND_SEND));
+            connection.setDoOutput(true);
+            if (connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).setChunkedStreamingMode(8192);
+            }
+            writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
+            for (LogEvent logEvent : logEvents) {
+                LogEvent event = new LogEvent(m_identification.getIdentification(), logEvent);
+
+                // getAgentContext().getLogService().log(LogService.LOG_DEBUG, "Synchronizing " + storeName + "-" +
+                // logId + ": event " + event.toRepresentation());
+                writer.write(event.toRepresentation() + "\n");
+            }
+            writer.flush();
+            connection.getContent();
+        }
+        finally {
+            try {
+                if (writer != null) {
+                    writer.close();
+                }
+            }
+            finally {
+                if (connection != null) {
+                    connection.disconnect();
+                }
+            }
+        }
+    }
+
+    private SortedRangeSet getRemoteStoreRange(String storeName, long logId) throws Exception {
+        HttpURLConnection connection = null;
+        BufferedReader reader = null;
+        try {
+            URL url = new URL(m_discovery.getServerURL(), storeName + "/" + COMMAND_QUERY + "?" + PARAMETER_TARGETID + "=" + m_identification.getIdentification() + "&" + PARAMETER_LOGID + "=" + logId);
+            connection = m_connection.getConnection(url);
+            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String rangeString = reader.readLine();
+            return new LogDescriptor(rangeString).getRangeSet();
+        }
+        catch (MalformedURLException e) {
+            throw new IOException(e);
+        }
+        finally {
+            try {
+                if (reader != null) {
+                    reader.close();
+                }
+            }
+            finally {
+                if (connection != null) {
+                    connection.disconnect();
+                }
+            }
+        }
+    }
+
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandlerFactory.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandlerFactory.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandlerFactory.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/DefaultLoggingHandlerFactory.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.logging;
+
+import java.util.Map;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.ComponentFactory;
+import org.apache.ace.agent.spi.LoggingHandler;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ * 
+ */
+public class DefaultLoggingHandlerFactory implements ComponentFactory {
+
+    @Override
+    public Component createComponent(BundleContext context, DependencyManager manager, LogService logService, Map<String, String> configuration) throws Exception {
+
+        return manager.createAdapterService(AgentContext.class, null)
+            .setInterface(LoggingHandler.class.getName(), null)
+            .setImplementation(DefaultLoggingHandler.class);
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogStoreImpl.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogStoreImpl.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogStoreImpl.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/logging/LogStoreImpl.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,558 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.logging;
+
+import java.io.EOFException;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+
+import org.apache.ace.log.LogEvent;
+import org.apache.ace.log.target.store.LogStore;
+import org.osgi.service.log.LogService;
+
+//FIXME This is a of the org.apache.ace.log it is private and may be better located here.
+
+/**
+ * This class provides an implementation of the LogStore service. It tries to
+ * repair broken log files to make them readable again. However, this might lead
+ * to loss of data. Additionally, a new file is used when an error is detected.
+ */
+public class LogStoreImpl implements LogStore {
+    volatile LogService m_log;
+
+    // The current store
+    private Store m_store = null;
+    private final File m_baseDir;
+    private long m_highest;
+
+    /**
+     * Create new instance using the specified directory as root directory.
+     * 
+     * @param baseDir
+     *            Root directory to use for storage.
+     */
+    public LogStoreImpl(File baseDir) {
+        m_baseDir = new File(baseDir, "store");
+    }
+
+    /**
+     * Init the current store.
+     * 
+     * @throws java.io.IOException
+     */
+    protected synchronized void start() throws IOException {
+        if (!m_baseDir.isDirectory() && !m_baseDir.mkdirs()) {
+            throw new IllegalArgumentException("Need valid dir");
+        }
+        long current = -1;
+        File[] files = (File[]) notNull(m_baseDir.listFiles());
+        for (int i = 0; i < files.length; i++) {
+            long id = Long.parseLong(files[i].getName());
+            current = Math.max(id, current);
+        }
+        try {
+            if (current == -1) {
+                m_store = newStore();
+            } else {
+                m_store = createStore(current);
+                try {
+                    m_store.init();
+                } 
+                catch (IOException ex) {
+                    handleException(m_store, ex);
+                }
+            }
+        } 
+        catch (IOException ex) {
+            // We should be able to recover from the error.
+            m_log.log(LogService.LOG_ERROR, "Exception during log store init",
+                    ex);
+        }
+    }
+
+    /**
+     * Close the current store.
+     * 
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    protected synchronized void stop() throws IOException {
+        m_store.close();
+        m_store = null;
+    }
+
+    /**
+     * Create a store object for a new log.
+     * 
+     * @return a store object for a new log.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    protected Store newStore() throws IOException {
+        long id = System.currentTimeMillis();
+
+        while (!(new File(m_baseDir, String.valueOf(id))).createNewFile()) {
+            id++;
+        }
+
+        return new Store(new File(m_baseDir, String.valueOf(id)), id);
+    }
+
+    /**
+     * Create a store object for the given log. This should not be used to
+     * create a new log.
+     * 
+     * @param id
+     *            the id of the log.
+     * @return a new store object for the given log.
+     * @throws java.io.IOException
+     *             in case of an IO error.
+     */
+    protected Store createStore(long id) throws IOException {
+        return new Store(new File(m_baseDir, String.valueOf(id)), id);
+    }
+
+    /**
+     * Get the entries in the given range from the given log.
+     * 
+     * @param logID
+     *            the id of the log.
+     * @param from
+     *            the lower bound of the range.
+     * @param to
+     *            the upper bound of the range.
+     * @return a list of entries from the given log in the given range.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    public synchronized List get(long logID, long from, long to)
+            throws IOException {
+        Store store = getLog(logID);
+        List result = new ArrayList();
+        try {
+            if (store.getCurrent() > from) {
+                store.reset();
+            }
+
+            while (store.hasNext()) {
+                long eventID = store.readCurrentID();
+                if ((eventID >= from) && (eventID <= to)) {
+                    result.add(new LogEvent(new String(store.read())));
+                } else {
+                    store.skip();
+                }
+            }
+        } 
+        catch (Exception ex) {
+            handleException(store, ex);
+        } 
+        finally {
+            closeIfNeeded(store);
+        }
+        return result;
+    }
+
+    /**
+     * Try to repair the given store, log the given exception and rethrow it. In
+     * case the store is the current log switch to a new one if possible.
+     * 
+     * @param store
+     *            the store to repair/close.
+     * @param exception
+     *            the exception to log and rethrow.
+     * @throws java.io.IOException
+     *             the given exception if it is an IOException else the message
+     *             of the given exception wrapped in an IOException.
+     */
+    protected void handleException(Store store, Exception exception)
+            throws IOException {
+        m_log.log(LogService.LOG_WARNING, "Exception accessing the log: "
+                + store.getId(), exception);
+        if (store == m_store) {
+            m_store = newStore();
+        }
+
+        try {
+            store.truncate();
+        } 
+        catch (IOException ex) {
+            m_log.log(LogService.LOG_WARNING, "Exception during truncate: "
+                    + store.getId(), ex);
+        }
+        try {
+            store.close();
+        } 
+        catch (IOException ex) {
+            // Not much we can do
+        }
+        if (exception instanceof IOException) {
+            throw (IOException) exception;
+        }
+        throw new IOException("Unable to read log entry: "
+                + exception.getMessage());
+    }
+
+    /**
+     * Get all entries of the given log.
+     * 
+     * @param logID
+     *            the id of the log.
+     * @return a list of all entries in this log.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    public List get(long logID) throws IOException {
+        return get(logID, 0, Long.MAX_VALUE);
+    }
+
+    /**
+     * Get the current log ids.
+     * 
+     * @return the ids of the current logs.
+     */
+    public long[] getLogIDs() throws IOException {
+        File[] files = (File[]) notNull(m_baseDir.listFiles());
+        long[] result = new long[files.length];
+        for (int i = 0; i < files.length; i++) {
+            result[i] = Long.parseLong(files[i].getName());
+        }
+        return result;
+    }
+
+    /**
+     * Create and add a LogEvent to the current log.
+     * 
+     * @param type
+     *            the type the event.
+     * @param props
+     *            the properties of the event.
+     * @return the new event.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    public synchronized LogEvent put(int type, Dictionary props) throws IOException {
+        try {
+            LogEvent result = new LogEvent(null, m_store.getId(), getNextID(), System.currentTimeMillis(), type, props);
+            m_store.append(result.getID(), result.toRepresentation().getBytes());
+            return result;
+        } 
+        catch (IOException ex) {
+            handleException(m_store, ex);
+        }
+        return null;
+    }
+
+    /**
+     * Get the highest entry id of the given log.
+     * 
+     * @param logID
+     *            the id of the log.
+     * @return the id of the highest entry.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    public synchronized long getHighestID(long logID) throws IOException {
+        Store store = getLog(logID);
+        try {
+            if (m_highest == 0) {
+                store.init();
+                return (m_highest = store.getCurrent());
+            } else {
+                return m_highest;
+            }
+        } 
+        catch (IOException ex) {
+            handleException(store, ex);
+        } 
+        finally {
+            closeIfNeeded(store);
+        }
+        return -1;
+    }
+
+    /**
+     * Close the given store if it is not the current store. IO errors are
+     * ignored.
+     * 
+     * @param store
+     *            the store to close.
+     */
+    protected void closeIfNeeded(Store store) {
+        if (store != m_store) {
+            try {
+                store.close();
+            } 
+            catch (IOException ex) {
+                // Not much we can do;
+            }
+        }
+    }
+
+    /**
+     * Get a Store object for the log of the given logid.
+     * 
+     * @param logID
+     *            the id for which to return (and possibly create) a store.
+     * @return either a new or the current Store object.
+     * @throws java.io.IOException
+     *             in case of any IO error.
+     */
+    protected Store getLog(long logID) throws IOException {
+        if (m_store.getId() == logID) {
+            return m_store;
+        }
+        return createStore(logID);
+    }
+
+    /**
+     * Get the next id for the current store.
+     * 
+     * @return the next free log id of the current store.
+     * @throws java.io.IOException
+     */
+    protected long getNextID() throws IOException {
+        return (m_highest = getHighestID(m_store.m_id) + 1);
+    }
+
+    /*
+     * throw IOException in case the target is null else return the target.
+     */
+    private Object notNull(Object target) throws IOException {
+        if (target == null) {
+            throw new IOException(
+                    "Unknown IO error while trying to access the store.");
+        }
+        return target;
+    }
+
+    /**
+     * The general idea is to provide easy access to a file of records. It
+     * supports iterating over records both by skipping and by reading.
+     * Furthermore, files can be truncated. Most methods will make an effort to
+     * reset to the last good record in case of an error -- hence, a call to
+     * truncate after an IOException might make the store readable again.
+     */
+    class Store {
+        private final RandomAccessFile m_store;
+        private final long m_id;
+        private long m_current;
+
+        /**
+         * 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.
+         */
+        Store(File store, long id) throws IOException {
+            m_store = new RandomAccessFile(store, "rwd");
+            m_id = id;
+        }
+
+        /**
+         * Get the id of the current record.
+         * 
+         * @return the idea of the current record.
+         */
+        public long getCurrent() throws IOException {
+            long pos = m_store.getFilePointer();
+            if (m_store.length() == 0) {
+                return 0;
+            }
+            long result = 0;
+            try {
+                m_store.seek(m_current);
+                result = readCurrentID();
+                m_store.seek(pos);
+            } 
+            catch (IOException ex) {
+                handle(pos, ex);
+            }
+            return result;
+        }
+
+        /**
+         * Get the log id of this store.
+         * 
+         * @return the log id of this store.
+         */
+        public long getId() {
+            return m_id;
+        }
+
+        /**
+         * Reset the store to the beginning of the records
+         * 
+         * @throws java.io.IOException
+         *             in case of an IO error.
+         */
+        public void reset() throws IOException {
+            m_store.seek(0);
+            m_current = 0;
+        }
+
+        /**
+         * 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.
+         */
+        public boolean hasNext() throws IOException {
+            return m_store.getFilePointer() < m_store.length();
+        }
+
+        public byte[] read() throws IOException {
+            long pos = m_store.getFilePointer();
+            try {
+                if (pos < m_store.length()) {
+                    long current = m_store.getFilePointer();
+                    long id = m_store.readLong();
+                    int next = m_store.readInt();
+                    byte[] entry = new byte[next];
+                    m_store.readFully(entry);
+                    m_current = current;
+                    return entry;
+                }
+            } 
+            catch (IOException ex) {
+                handle(pos, ex);
+            }
+            return null;
+        }
+
+        public long readCurrentID() throws IOException {
+            long pos = m_store.getFilePointer();
+            try {
+                if (pos < m_store.length()) {
+                    long id = m_store.readLong();
+                    m_store.seek(pos);
+                    return id;
+                }
+            } 
+            catch (IOException ex) {
+                handle(pos, ex);
+            }
+            return -1;
+        }
+
+        /**
+         * 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.
+         */
+        public void init() throws IOException {
+            reset();
+            try {
+                while (true) {
+                    skip();
+                }
+            } 
+            catch (EOFException ex) {
+                // done
+            }
+        }
+
+        /**
+         * 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.
+         */
+        public void skip() throws IOException {
+            long pos = m_store.getFilePointer();
+            try {
+                long id = m_store.readLong();
+                int next = m_store.readInt();
+                if (m_store.length() < next + m_store.getFilePointer()) {
+                    throw new IOException("Unexpected end of file");
+                }
+                m_store.seek(m_store.getFilePointer() + next);
+                m_current = pos;
+                pos = m_store.getFilePointer();
+            } 
+            catch (IOException ex) {
+                handle(pos, ex);
+            }
+        }
+
+        /**
+         * 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.
+         */
+        public void append(long id, byte[] entry) throws IOException {
+            long pos = m_store.getFilePointer();
+            try {
+                m_store.seek(m_store.length());
+                long current = m_store.getFilePointer();
+                m_store.writeLong(id);
+                m_store.writeInt(entry.length);
+                m_store.write(entry);
+                m_store.seek(pos);
+            } 
+            catch (IOException ex) {
+                handle(pos, ex);
+            }
+        }
+
+        /**
+         * Try to truncate the store at the current record.
+         * 
+         * @throws java.io.IOException
+         *             in case of any IO error.
+         */
+        public void truncate() throws IOException {
+            m_store.setLength(m_store.getFilePointer());
+        }
+
+        /**
+         * Release any resources.
+         * 
+         * @throws java.io.IOException
+         *             in case of any IO error.
+         */
+        public void close() throws IOException {
+            m_store.close();
+        }
+
+        private void handle(long pos, IOException exception) throws IOException {
+            try {
+                m_store.seek(pos);
+            } 
+            catch (IOException ex) {
+                m_log.log(LogService.LOG_WARNING, "Exception during seek!", ex);
+            }
+            throw exception;
+        }
+    }
+}
\ No newline at end of file

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/packageinfo
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/packageinfo?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/packageinfo (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/packageinfo Tue Jun 11 07:56:25 2013
@@ -0,0 +1 @@
+version 1.0

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentContext.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentContext.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentContext.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentContext.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.io.File;
+import java.util.Map;
+import java.util.concurrent.ScheduledExecutorService;
+
+/**
+ * Service interface that gets published in the service registry for every configured agent. Components must monitor and
+ * adapt to this whiteboard.
+ */
+public interface AgentContext {
+
+    /**
+     * Returns the internal identifier
+     * 
+     * @return The id
+     */
+    String getAgentId();
+
+    /**
+     * Return the agent specific configuration.
+     * 
+     * @return The config
+     */
+    Map<String, String> getConfig();
+
+    /**
+     * Returns the agent specific workdir.
+     * 
+     * @return The workdir
+     */
+    File getWorkDir();
+
+    /**
+     * Returns the dedicated executor .
+     * 
+     * @return The executor
+     */
+    ScheduledExecutorService getExecutor();
+
+    void log(int level, String message);
+
+    void log(int level, String message, Throwable cause);
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/AgentHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+/**
+ * Parent interface for a requires agent components.
+ */
+public interface AgentHandler {
+
+    /**
+     * Executes handler specific logic that. This method is called be the agent at the configured sync interval.
+     * 
+     * @throws Exception
+     *             On failure
+     */
+    void sync() throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ComponentFactory.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ComponentFactory.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ComponentFactory.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ComponentFactory.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.util.Map;
+
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ * Service provider interface for Agent component factories.
+ * 
+ */
+public interface ComponentFactory {
+
+    /**
+     * Returns a component for the specified agent configuration.
+     * 
+     * @param context
+     *            The Bundle Context
+     * @param manager
+     *            The Dependency manager
+     * @param logService
+     *            The Log Service
+     * @param configuration
+     *            The agent configuration
+     * @return A component, or <code>null</code>
+     * @throws Exception
+     *             If there is a fatal problem
+     */
+    Component createComponent(BundleContext context, DependencyManager manager, LogService logService, Map<String, String> configuration) throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ConnectionHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ConnectionHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ConnectionHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/ConnectionHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * Service provider interface for components that create connections. Every {@link Agent} requires one.
+ * 
+ */
+public interface ConnectionHandler extends AgentHandler {
+
+    /**
+     * Opens a connection for the specified URL.
+     * 
+     * @param url The URL
+     * @return The connection
+     * @throws Exception on failure
+     */
+    HttpURLConnection getConnection(URL url) throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DeploymentHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DeploymentHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DeploymentHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DeploymentHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.io.File;
+import java.util.SortedSet;
+
+import org.apache.ace.agent.Agent;
+import org.osgi.framework.Version;
+import org.osgi.service.deploymentadmin.DeploymentPackage;
+
+/**
+ * Service provider interface for components that handle the local deployment. Every {@link Agent} requires one.
+ * 
+ */
+public interface DeploymentHandler extends AgentHandler {
+
+    /**
+     * Returns the sorted set of locally installed versions.
+     * 
+     * @return The sorted set
+     */
+    SortedSet<Version> getVersions();
+
+    /**
+     * Install a local {@link DeploymentPackage} from file.
+     * 
+     * @param file The source
+     * @throws Exception On failure
+     */
+    void installPackage(File file) throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DiscoveryHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DiscoveryHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DiscoveryHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DiscoveryHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.net.URL;
+
+import org.apache.ace.agent.Agent;
+
+/**
+ * Service provider interface for components that provide the server URL. Every {@link Agent} requires one.
+ *
+ */
+public interface DiscoveryHandler extends AgentHandler {
+
+    URL getServerURL();
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DownloadHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DownloadHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DownloadHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/DownloadHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.io.File;
+
+import org.apache.ace.agent.Agent;
+import org.osgi.framework.Version;
+
+/**
+ * Service provider interface for components that can download packages. Every {@link Agent} requires one.
+ *
+ */
+public interface DownloadHandler extends AgentHandler {
+
+    File downloadPackage(Version from, Version to) throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/IdentificationHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/IdentificationHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/IdentificationHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/IdentificationHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import org.apache.ace.agent.Agent;
+
+/**
+ * Service provider interface for components that handle identification. Every {@link Agent} requires one.
+ * 
+ */
+public interface IdentificationHandler extends AgentHandler {
+
+    /**
+     * Return the identification string.
+     * 
+     * @return The identification
+     */
+    String getIdentification();
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/LoggingHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/LoggingHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/LoggingHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/LoggingHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.util.Dictionary;
+
+import org.apache.ace.agent.Agent;
+
+/**
+ * Service provider interface for components that handle logging. Every {@link Agent} requires one.
+ * 
+ */
+public interface LoggingHandler extends AgentHandler {
+
+    /**
+     * Log an event to a named log.
+     * 
+     * @param string
+     *            The log name
+     * @param eventType
+     *            The event type
+     * @param props
+     *            The event payload
+     * @throws Exception
+     *             On failure
+     */
+    void log(String string, int eventType, Dictionary<String, String> props) throws Exception;
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/UpdateHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/UpdateHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/UpdateHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/UpdateHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import org.apache.ace.agent.Agent;
+
+/**
+ * Service provider interface for components that handle update logic. Every {@link Agent} requires one.
+ * 
+ */
+public interface UpdateHandler extends AgentHandler {
+
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/VersionsHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/VersionsHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/VersionsHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/VersionsHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.spi;
+
+import java.util.SortedSet;
+
+import org.apache.ace.agent.Agent;
+import org.osgi.framework.Version;
+
+/**
+ * Service provider interface for components that provide access to remote versions. Every {@link Agent} requires one.
+ * 
+ */
+public interface VersionsHandler extends AgentHandler {
+
+    SortedSet<Version> getVersions();
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/packageinfo
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/packageinfo?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/packageinfo (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/spi/packageinfo Tue Jun 11 07:56:25 2013
@@ -0,0 +1 @@
+version 1.0
\ No newline at end of file

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.update;
+
+import java.io.File;
+import java.util.SortedSet;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.DeploymentHandler;
+import org.apache.ace.agent.spi.DownloadHandler;
+import org.apache.ace.agent.spi.UpdateHandler;
+import org.apache.ace.agent.spi.VersionsHandler;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.Version;
+import org.osgi.service.log.LogService;
+
+public class DefaultUpdateHandler implements UpdateHandler {
+
+    private volatile DependencyManager m_manager;
+    private volatile Component m_component;
+    private volatile AgentContext m_context;
+
+    private volatile DeploymentHandler m_deployment;
+    private volatile VersionsHandler m_versions;
+    private volatile DownloadHandler m_download;
+
+    public void init() throws Exception {
+        m_component
+            .add(m_manager.createServiceDependency()
+                .setService(DeploymentHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(VersionsHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(DownloadHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true)
+            );
+    }
+
+    @Override
+    public void sync() throws Exception {
+        Version localVersion = highestVersion(m_deployment.getVersions());
+        Version remoteVersion = highestVersion(m_versions.getVersions());
+        if (remoteVersion.compareTo(localVersion) > 0) {
+            m_context.log(LogService.LOG_INFO, "Updating to new version: " + localVersion + " -> " + remoteVersion);
+            File download = m_download.downloadPackage(localVersion, remoteVersion);
+            m_deployment.installPackage(download);
+            m_context.log(LogService.LOG_INFO, "Update completed");
+        }
+    }
+
+    public static Version highestVersion(SortedSet<Version> versions) {
+        if (versions == null || versions.isEmpty()) {
+            return Version.emptyVersion;
+        }
+        return versions.last();
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandlerFactory.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandlerFactory.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandlerFactory.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/update/DefaultUpdateHandlerFactory.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.update;
+
+import java.util.Map;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.ComponentFactory;
+import org.apache.ace.agent.spi.UpdateHandler;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ * 
+ */
+public class DefaultUpdateHandlerFactory implements ComponentFactory {
+
+    @Override
+    public Component createComponent(BundleContext context, DependencyManager manager, LogService logService, Map<String, String> configuration) throws Exception {
+
+        return manager.createAdapterService(AgentContext.class, null)
+            .setInterface(UpdateHandler.class.getName(), null)
+            .setImplementation(DefaultUpdateHandler.class);
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandler.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandler.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandler.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandler.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.versions;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.ConnectionHandler;
+import org.apache.ace.agent.spi.DiscoveryHandler;
+import org.apache.ace.agent.spi.IdentificationHandler;
+import org.apache.ace.agent.spi.VersionsHandler;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.Version;
+
+public class DefaultVersionsHandler implements VersionsHandler {
+
+    private SortedSet<Version> m_versions;
+
+    private volatile DependencyManager m_manager;
+    private volatile Component m_component;
+    private volatile AgentContext m_context;
+
+    private volatile DiscoveryHandler m_discovery;
+    private volatile IdentificationHandler m_identification;
+    private volatile ConnectionHandler m_connection;
+
+    public void init() {
+        m_component
+            .add(m_manager.createServiceDependency()
+                .setService(IdentificationHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(DiscoveryHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true))
+            .add(m_manager.createServiceDependency()
+                .setService(ConnectionHandler.class, "(agent=" + m_context.getAgentId() + ")")
+                .setRequired(true)
+            );
+    }
+
+    @Override
+    public void sync() throws Exception {
+        syncVersions();
+    }
+
+    @Override
+    public SortedSet<Version> getVersions() {
+        return m_versions;
+    }
+
+    private void syncVersions() throws Exception {
+
+        URL endpoint = getEndpoint(m_discovery.getServerURL(), m_identification.getIdentification());
+        HttpURLConnection connection = null;
+        BufferedReader reader = null;
+
+        try {
+            connection = m_connection.getConnection(endpoint);
+            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            SortedSet<Version> versions = new TreeSet<Version>();
+            String versionString;
+            while ((versionString = reader.readLine()) != null) {
+                try {
+                    Version version = Version.parseVersion(versionString);
+                    if (version != Version.emptyVersion) {
+                        versions.add(version);
+                    }
+                }
+                catch (IllegalArgumentException e) {
+                    throw new IOException(e);
+                }
+            }
+            m_versions = versions;
+        }
+        finally {
+            if (reader != null) {
+                reader.close();
+            }
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+
+    private URL getEndpoint(URL serverURL, String identification) throws Exception {
+        return new URL(serverURL, "deployment/" + identification + "/versions/");
+    }
+}

Added: ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandlerFactory.java
URL: http://svn.apache.org/viewvc/ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandlerFactory.java?rev=1491702&view=auto
==============================================================================
--- ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandlerFactory.java (added)
+++ ace/sandbox/bramk/org.apache.ace.agent/src/org/apache/ace/agent/versions/DefaultVersionsHandlerFactory.java Tue Jun 11 07:56:25 2013
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.agent.versions;
+
+import java.util.Map;
+
+import org.apache.ace.agent.spi.AgentContext;
+import org.apache.ace.agent.spi.ComponentFactory;
+import org.apache.ace.agent.spi.VersionsHandler;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.log.LogService;
+
+/**
+ * 
+ */
+public class DefaultVersionsHandlerFactory implements ComponentFactory {
+
+    @Override
+    public Component createComponent(BundleContext context, DependencyManager manager, LogService logService, Map<String, String> configuration) throws Exception {
+
+        return manager.createAdapterService(AgentContext.class, null)
+            .setInterface(VersionsHandler.class.getName(), null)
+            .setImplementation(DefaultVersionsHandler.class);
+    }
+}



Mime
View raw message