Skip to content

Commit

Permalink
Capabilities skeleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
gd-tmagrys committed Jul 8, 2015
1 parent 3358960 commit dfed1fa
Show file tree
Hide file tree
Showing 20 changed files with 599 additions and 416 deletions.
16 changes: 14 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
<parent>
<groupId>org.sonatype.nexus.plugins</groupId>
<artifactId>nexus-plugins</artifactId>
<version>2.11.2-06</version>
<version>2.11.3-01</version>
</parent>

<properties>
<pluginName>${project.name}</pluginName>
<pluginDescription>Facilitates push-replication to remote proxy repositories.</pluginDescription>
<pluginDescription>Facilitates push-replication to remote proxy repositories (6).</pluginDescription>
<nexus.version>${project.parent.version}</nexus.version>
</properties>

Expand All @@ -67,6 +67,13 @@
<version>${nexus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonatype.nexus.plugins</groupId>
<artifactId>nexus-capabilities-plugin</artifactId>
<type>${nexus-plugin.type}</type>
<version>${nexus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-plugin-testsupport</artifactId>
Expand Down Expand Up @@ -136,6 +143,11 @@
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-client-core</artifactId>
Expand Down
5 changes: 4 additions & 1 deletion replication-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ requestsQueueSize and requestsSendingThreadsCount attributes define thread pool
requests from master to peers asynchronously.
The default values are: 500 for requestsQueueSize and 1 for requestsSendingThreadsCount
-->
<configurations myUrl="http://localhost:8081/nexus" requestsQueueSize="500" requestsSendingThreadsCount="1" queueDumpFileName="/tmp/nexus-replication-plugin-queue-backup">
<configurations myUrl="http://localhost:8081/nexus"
requestsQueueSize="500"
requestsSendingThreadsCount="1"
queueDumpFileName="/tmp/nexus-replication-plugin-queue-backup">
<servers>
<server>
<url>http://localhost:8083/nexus</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.griddynamics.cd.nrp.internal.capabilities;

import javax.inject.Named;

import org.sonatype.nexus.plugins.capabilities.CapabilityRegistry;
import org.sonatype.nexus.plugins.capabilities.support.CapabilityBooterSupport;

import org.eclipse.sisu.EagerSingleton;

import java.util.Collections;

@Named
@EagerSingleton
public class ReplicationPluginCapabilitiesBooter
extends CapabilityBooterSupport
{

@Override
protected void boot(final CapabilityRegistry registry)
throws Exception
{
maybeAddCapability(
registry,
ReplicationPluginCapabilityDescriptor.TYPE,
true, // enabled
null, // no notes
Collections.<String,String>emptyMap());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.griddynamics.cd.nrp.internal.capabilities;

import com.griddynamics.cd.nrp.internal.model.config.ReplicationPluginConfigurationStorage;
import com.griddynamics.cd.nrp.internal.uploading.ArtifactUpdateApiClient;
import com.griddynamics.cd.nrp.internal.uploading.impl.factories.JerseyClientFactory;
import org.jetbrains.annotations.NonNls;
import org.sonatype.nexus.capability.support.CapabilitySupport;
import org.sonatype.nexus.plugins.capabilities.Condition;
import org.sonatype.nexus.plugins.capabilities.Evaluable;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

@Named(ReplicationPluginCapabilityDescriptor.TYPE_ID)
public class ReplicationPluginCapability
extends CapabilitySupport<ReplicationPluginCapabilityConfiguration> {
@NonNls
public static final String NL = System.getProperty("line.separator");

private final ReplicationPluginConfigurationStorage gridRegistry;
private final ArtifactUpdateApiClient artifactUpdateApiClient;
private final JerseyClientFactory jerseyClientFactory;

@Inject
public ReplicationPluginCapability(final ReplicationPluginConfigurationStorage replicationPluginConfigurationStorage,
final ArtifactUpdateApiClient artifactUpdateApiClient,
final JerseyClientFactory jerseyClientFactory) {
this.gridRegistry = checkNotNull(replicationPluginConfigurationStorage);
this.artifactUpdateApiClient = artifactUpdateApiClient;
this.jerseyClientFactory = jerseyClientFactory;
}

@Override
protected ReplicationPluginCapabilityConfiguration createConfig(final Map<String, String> properties) throws Exception {
Map<String, String> newProperties = new HashMap<>(properties);
return new ReplicationPluginCapabilityConfiguration(newProperties);
}

@Override
public void configure(final ReplicationPluginCapabilityConfiguration config){
gridRegistry.setMasterServerURLPrefix(config.getMasterServerURLPrefix());
gridRegistry.setRequestQueueSize(config.getRequestQueueSize());
gridRegistry.setRequestSendingThreadCount(config.getRequestSendingThreadCount());
gridRegistry.setRequestQueueDumpFileName(config.getRequestQueueDumpFileName());
gridRegistry.setServers(config.getNexusServers());
jerseyClientFactory.onActivate();
artifactUpdateApiClient.onActivate();
}

@Override
public Condition activationCondition() {
return conditions().capabilities().evaluable(
new Evaluable() {
@Override
public boolean isSatisfied() {
return true;
}

@Override
public String explainSatisfied() {
return "\"createrepo\" and \"mergerepo\" are available";
}

@Override
public String explainUnsatisfied() {
return "";
}
}
);
}

@Override
protected String renderStatus() {
return "Sample Nexus replication plugin status";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.griddynamics.cd.nrp.internal.capabilities;

import com.google.common.collect.Maps;
import com.griddynamics.cd.nrp.internal.model.config.ReplicationPluginConfigurationStorage;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReplicationPluginCapabilityConfiguration {

public static final String MASTER_SERVER_URL_PREFIX = "myUrl";
public static final String REQUESTS_QUEUE_SIZE = "requestsQueueSize";
public static final String REQUESTS_SENDING_THREADS_COUNT = "requestsSendingThreadsCount";
public static final String QUEUE_DUMP_FILE_NAME = "queueDumpFileName";
public static final String SERVERS = "servers";

Logger logger =
LoggerFactory.getLogger(ReplicationPluginCapabilityConfiguration.class);


private final String masterServerURLPrefix;
private final int requestQueueSize;
private final int requestSendingThreadCount;
private final String requestQueueDumpFileName;
private final Set<ReplicationPluginConfigurationStorage.NexusServer> nexusServers;

public ReplicationPluginCapabilityConfiguration(final Map<String, String> properties) {
this.masterServerURLPrefix = properties.get(MASTER_SERVER_URL_PREFIX);
this.requestQueueSize = Integer.parseInt(properties.get(REQUESTS_QUEUE_SIZE));
this.requestSendingThreadCount = Integer.parseInt(properties.get(REQUESTS_SENDING_THREADS_COUNT));
this.requestQueueDumpFileName = properties.get(QUEUE_DUMP_FILE_NAME);
this.nexusServers = new LinkedHashSet<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
List<Map> servers = (List<Map>) objectMapper.readValue(properties.get(SERVERS), Map.class).get("servers");
for (Map server : servers) {
this.nexusServers.add(
new ReplicationPluginConfigurationStorage.NexusServer(
(String) server.get("url"),
(String) server.get("user"),
(String) server.get("password")
)
);
}

} catch (Exception e) {
logger.error(e.getMessage(),e);
}
}

public Map<String, String> asMap() {
final Map<String, String> props = Maps.newHashMap();
props.put(MASTER_SERVER_URL_PREFIX, masterServerURLPrefix);
props.put(REQUESTS_QUEUE_SIZE, String.valueOf(requestQueueSize));
props.put(REQUESTS_SENDING_THREADS_COUNT, String.valueOf(requestSendingThreadCount));
props.put(QUEUE_DUMP_FILE_NAME, requestQueueDumpFileName);
return props;
}

public String getMasterServerURLPrefix() {
return masterServerURLPrefix;
}

public int getRequestQueueSize() {
return requestQueueSize;
}

public int getRequestSendingThreadCount() {
return requestSendingThreadCount;
}

public String getRequestQueueDumpFileName() {
return requestQueueDumpFileName;
}

public Set<ReplicationPluginConfigurationStorage.NexusServer> getNexusServers() {
return nexusServers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.griddynamics.cd.nrp.internal.capabilities;

import com.google.common.collect.Lists;
import org.sonatype.nexus.capability.support.CapabilityDescriptorSupport;
import org.sonatype.nexus.formfields.FormField;
import org.sonatype.nexus.formfields.NumberTextFormField;
import org.sonatype.nexus.formfields.StringTextFormField;
import org.sonatype.nexus.formfields.TextAreaFormField;
import org.sonatype.nexus.plugins.capabilities.*;
import org.sonatype.nexus.plugins.capabilities.support.validator.Validators;
import org.sonatype.sisu.goodies.i18n.I18N;
import org.sonatype.sisu.goodies.i18n.MessageBundle;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.List;
import java.util.Set;

import static org.sonatype.nexus.plugins.capabilities.CapabilityType.capabilityType;
import static org.sonatype.nexus.plugins.capabilities.Tag.categoryTag;
import static org.sonatype.nexus.plugins.capabilities.Tag.tags;

@Singleton
@Named(ReplicationPluginCapabilityDescriptor.TYPE_ID)
public class ReplicationPluginCapabilityDescriptor
extends CapabilityDescriptorSupport
implements Taggable {
public static final String TYPE_ID = "nexus-replication-plugin";

public static final CapabilityType TYPE = capabilityType(TYPE_ID);

private static interface Messages
extends MessageBundle {
@DefaultMessage("Nexus Replication Plugin Configuration")
String name();

@DefaultMessage("Master server's URL prefix")
String masterServerURLPrefixLabel();

@DefaultMessage("Master server's URL prefix (e.g. http://localhost:8081/nexus")
String masterServerURLPrefixHelp();

@DefaultMessage("Request queue size")
String requestQueueSizeLabel();

@DefaultMessage("Request queue size (default 500)")
String requestQueueSizeHelp();

@DefaultMessage("Request sending thread count")
String requestSendingThreadCountLabel();

@DefaultMessage("Request sending thread count (default 1)")
String requestSendingThreadCountHelp();

@DefaultMessage("Request queue dump file")
String requestQueueDumpFileNameLabel();

@DefaultMessage("Request queue dump file (default /tmp/nexus-replication-plugin-queue-backup)")
String requestQueueDumpFileNameHelp();

@DefaultMessage("Slave servers (in JSON)")
String serversLabel();

@DefaultMessage("Slave servers in JSON, like: {\"servers\":[{\"url\":\"http://localhost:8083/nexus\",\"user\":\"admin\",\"password\":\"admin123\"},{\"url\":\"http://localhost:8082/nexus\",\"user\":\"admin\",\"password\":\"admin123\"}]}")
String serversHelp();

}

private static final Messages messages = I18N.create(Messages.class);

private final Validators validators;

private final List<FormField> formFields;

@Inject
public ReplicationPluginCapabilityDescriptor(final Validators validators) {
this.validators = validators;

this.formFields = Lists.<FormField>newArrayList(
new StringTextFormField(
ReplicationPluginCapabilityConfiguration.MASTER_SERVER_URL_PREFIX,
messages.masterServerURLPrefixLabel(),
messages.masterServerURLPrefixHelp(),
FormField.OPTIONAL
).withInitialValue("http://localhost:8081/nexus"),
new NumberTextFormField(
ReplicationPluginCapabilityConfiguration.REQUESTS_QUEUE_SIZE,
messages.requestQueueSizeLabel(),
messages.requestQueueSizeHelp(),
FormField.OPTIONAL
).withInitialValue(500),
new NumberTextFormField(
ReplicationPluginCapabilityConfiguration.REQUESTS_SENDING_THREADS_COUNT,
messages.requestSendingThreadCountLabel(),
messages.requestSendingThreadCountHelp(),
FormField.OPTIONAL
).withInitialValue(1),
new StringTextFormField(
ReplicationPluginCapabilityConfiguration.QUEUE_DUMP_FILE_NAME,
messages.requestQueueDumpFileNameLabel(),
messages.requestQueueDumpFileNameHelp(),
FormField.OPTIONAL
).withInitialValue("/tmp/nexus-replication-plugin-queue-backup"),
new TextAreaFormField(
ReplicationPluginCapabilityConfiguration.SERVERS,
messages.serversLabel(),
messages.serversHelp(),
FormField.OPTIONAL
).withInitialValue("{\"servers\":[{\"url\":\"http://localhost:8083/nexus\",\"user\":\"admin\",\"password\":\"admin123\"},{\"url\":\"http://localhost:8082/nexus\",\"user\":\"admin\",\"password\":\"admin123\"}]}")
);
}

@Override
public Validator validator() {
return validators.logical().and(
validators.capability().uniquePer(TYPE)
);
}

@Override
public Validator validator(final CapabilityIdentity id) {
return validators.logical().and(
validators.capability().uniquePerExcluding(id, TYPE)
);
}

@Override
public CapabilityType type() {
return TYPE;
}

@Override
public String name() {
return messages.name();
}

@Override
public List<FormField> formFields() {
return formFields;
}

@Override
public Set<Tag> getTags() {
return tags(categoryTag("Grid"));
}
}
Loading

0 comments on commit dfed1fa

Please sign in to comment.