incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1556500 - in /sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent: ReplicationAgentConfigurationManager.java impl/DefaultReplicationAgentConfigurationManager.java
Date Wed, 08 Jan 2014 11:29:02 GMT
Author: bdelacretaz
Date: Wed Jan  8 11:29:02 2014
New Revision: 1556500

URL: http://svn.apache.org/r1556500
Log:
SLING-3300 -  API to create replication agent (configuration) - contributed by Tommaso Teofili,
thanks!

Modified:
    sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/ReplicationAgentConfigurationManager.java
    sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/impl/DefaultReplicationAgentConfigurationManager.java

Modified: sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/ReplicationAgentConfigurationManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/ReplicationAgentConfigurationManager.java?rev=1556500&r1=1556499&r2=1556500&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/ReplicationAgentConfigurationManager.java
(original)
+++ sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/ReplicationAgentConfigurationManager.java
Wed Jan  8 11:29:02 2014
@@ -46,4 +46,11 @@ public interface ReplicationAgentConfigu
     ReplicationAgentConfiguration updateConfiguration(ReplicationAgent agent,
                                                       Map<String, Object> properties)
throws AgentConfigurationException;
 
+    /**
+     * creates a configuration for a {@link ReplicationAgent}
+     *
+     * @param properties the configuration of the agent to create
+     * @throws AgentConfigurationException
+     */
+    void createAgentConfiguration(Map<String, Object> properties) throws AgentConfigurationException;
 }

Modified: sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/impl/DefaultReplicationAgentConfigurationManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/impl/DefaultReplicationAgentConfigurationManager.java?rev=1556500&r1=1556499&r2=1556500&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/impl/DefaultReplicationAgentConfigurationManager.java
(original)
+++ sling/trunk/contrib/extensions/replication/src/main/java/org/apache/sling/replication/agent/impl/DefaultReplicationAgentConfigurationManager.java
Wed Jan  8 11:29:02 2014
@@ -18,21 +18,21 @@
  */
 package org.apache.sling.replication.agent.impl;
 
+import java.util.Arrays;
 import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.Map;
-
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.apache.sling.replication.agent.AgentConfigurationException;
 import org.apache.sling.replication.agent.ReplicationAgent;
 import org.apache.sling.replication.agent.ReplicationAgentConfiguration;
 import org.apache.sling.replication.agent.ReplicationAgentConfigurationManager;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Default implementation of {@link ReplicationAgentConfigurationManager}
@@ -40,7 +40,7 @@ import org.apache.sling.replication.agen
 @Component(immediate = true)
 @Service(value = ReplicationAgentConfigurationManager.class)
 public class DefaultReplicationAgentConfigurationManager implements
-                ReplicationAgentConfigurationManager {
+        ReplicationAgentConfigurationManager {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -48,7 +48,7 @@ public class DefaultReplicationAgentConf
     private ConfigurationAdmin configAdmin;
 
     public ReplicationAgentConfiguration getConfiguration(ReplicationAgent replicationAgent)
-                    throws AgentConfigurationException {
+            throws AgentConfigurationException {
         if (log.isInfoEnabled()) {
             log.info("retrieving configuration for agent {}", replicationAgent);
         }
@@ -72,24 +72,24 @@ public class DefaultReplicationAgentConf
         String filter = "(name=" + replicationAgent.getName() + ")";
         Configuration[] configurations = configAdmin.listConfigurations(filter);
         if (configurations == null) {
-          throw new Exception("no configuration found");
+            throw new Exception("no configuration found");
         } else if (configurations.length == 1) {
             if (log.isInfoEnabled()) {
                 log.info("found configuration {} for agent {}", configurations[0],
-                                replicationAgent.getName());
+                        replicationAgent.getName());
             }
             return configurations[0];
         } else {
             if (log.isErrorEnabled()) {
                 log.error("{} configurations for agent {} found", configurations.length,
-                                replicationAgent.getName());
+                        replicationAgent.getName());
             }
             throw new Exception("too many configurations found");
         }
     }
 
     public ReplicationAgentConfiguration updateConfiguration(ReplicationAgent replicationAgent,
-                    Map<String, Object> updateProperties) throws AgentConfigurationException
{
+                                                             Map<String, Object> updateProperties)
throws AgentConfigurationException {
         try {
             Configuration configuration = getOsgiConfiguration(replicationAgent);
             @SuppressWarnings("unchecked")
@@ -112,4 +112,44 @@ public class DefaultReplicationAgentConf
 
     }
 
+    public void createAgentConfiguration(Map<String, Object> properties) throws AgentConfigurationException
{
+
+        Object name = properties.get("name");
+        if (name != null) {
+            try {
+                Configuration configuration = configAdmin.createFactoryConfiguration(ReplicationAgentServiceFactory.SERVICE_PID
+ "-" + parseString(name));
+                @SuppressWarnings("unchecked")
+                Dictionary<String, Object> configurationProperties = new Hashtable<String,
Object>();
+
+                for (Map.Entry<String, Object> entry : properties.entrySet()) {
+                    String key = entry.getKey();
+                    if (key.startsWith("X-replication-")) {
+                        key = key.substring(0, 14);
+                    }
+                    String value = parseString(entry.getValue());
+                    configurationProperties.put(key, value);
+                }
+                configuration.update(configurationProperties);
+            } catch (Exception e) {
+                if (log.isErrorEnabled()) {
+                    log.error("cannot create agent {} ", name);
+                }
+                throw new AgentConfigurationException(e);
+            }
+        } else {
+            throw new AgentConfigurationException("a (unique) name is needed in order to
create an agent");
+        }
+    }
+
+    private String parseString(Object object) {
+        String value;
+        if (object instanceof String[]) {
+            String arrayString = Arrays.toString((String[]) object);
+            value = arrayString.substring(1, arrayString.length() - 1);
+        } else {
+            value = String.valueOf(object);
+        }
+        return value;
+    }
+
 }



Mime
View raw message