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 4110ff6
Show file tree
Hide file tree
Showing 18 changed files with 575 additions and 367 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,135 @@
package com.griddynamics.cd.nrp.internal;

import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

@Named(value = "replicationPluginConfigurationStorage")
@Singleton
public class ReplicationPluginConfigurationStorage {

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

private final AtomicReference<String> masterServerURLPrefix = new AtomicReference<>();
private final AtomicReference<Integer> requestQueueSize = new AtomicReference<>();
private final AtomicReference<Integer> requestSendingThreadCount = new AtomicReference<>();
private final AtomicReference<String> requestQueueDumpFileName = new AtomicReference<>();;
private final AtomicReference<Set<NexusServer>> servers = new AtomicReference<>();

@Inject
public ReplicationPluginConfigurationStorage() {
servers.set(new LinkedHashSet<NexusServer>());
}

public String getMasterServerURLPrefix() {
String retVal = masterServerURLPrefix.get();
if(retVal == null){
throw new RuntimeException("Replication plugin configuration not ready yet!");
}
return retVal;
}

public void setMasterServerURLPrefix(String masterServerURLPrefix) {
this.masterServerURLPrefix.set(masterServerURLPrefix);
logger.info(toString());
}

public String getRequestQueueDumpFileName() {
String retVal = requestQueueDumpFileName.get();
if(retVal == null){
throw new RuntimeException("Replication plugin configuration not ready yet!");
}
return retVal;
}

public void setRequestQueueDumpFileName(String requestQueueDumpFileName) {
this.requestQueueDumpFileName.set(requestQueueDumpFileName);
logger.info(toString());
}

public Set<NexusServer> getServers() {
Set<NexusServer> retVal = servers.get();
if(retVal == null){
throw new RuntimeException("Replication plugin configuration not ready yet!");
}
return retVal;
}

public void setServers(Set<NexusServer> nexusServers) {
servers.get().clear();
servers.get().addAll(nexusServers);
logger.info(toString());
}

public int getRequestSendingThreadCount() {
Integer retVal = requestSendingThreadCount.get();
if(retVal == null){
throw new RuntimeException("Replication plugin configuration not ready yet!");
}
return retVal;
}

public void setRequestSendingThreadCount(int requestSendingThreadCount) {
this.requestSendingThreadCount.set(requestSendingThreadCount);
logger.info(toString());
}

public int getRequestQueueSize() {
Integer retVal = requestQueueSize.get();
if(retVal == null){
throw new RuntimeException("Replication plugin configuration not ready yet!");
}
return retVal;
}

public void setRequestQueueSize(int requestQueueSize) {
this.requestQueueSize.set(requestQueueSize);
logger.info(toString());
}

@Override
public String toString() {
return "ReplicationPluginConfigurationStorage{" +
"masterServerURLPrefix='" + masterServerURLPrefix + '\'' +
", requestQueueSize=" + requestQueueSize.get() +
", requestSendingThreadCount=" + requestSendingThreadCount.get() +
", requestQueueDumpFileName='" + requestQueueDumpFileName + '\'' +
", servers=" + servers +
'}';
}

@NoArgsConstructor
@RequiredArgsConstructor
@ToString
public static class NexusServer {
@NonNull
private String url;
@NonNull
private String user;
@NonNull
private String password;

public String getUrl() {
return url;
}

public String getUser() {
return user;
}

public String getPassword() {
return password;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.griddynamics.cd.nrp.internal.capabilities;

import javax.inject.Named;

import com.google.common.collect.ImmutableMap;
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 GridCapabilitiesBooter
extends CapabilityBooterSupport
{

@Override
protected void boot(final CapabilityRegistry registry)
throws Exception
{
maybeAddCapability(
registry,
GridCapabilityDescriptor.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.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(GridCapabilityDescriptor.TYPE_ID)
public class GridCapability
extends CapabilitySupport<GridCapabilityConfiguration> {
@NonNls
public static final String NL = System.getProperty("line.separator");

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

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

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

@Override
public void configure(final GridCapabilityConfiguration 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.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 GridCapabilityConfiguration {

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(GridCapabilityConfiguration.class);


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

public GridCapabilityConfiguration(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;
}
}
Loading

0 comments on commit 4110ff6

Please sign in to comment.