net.unit8.waitt.mojo.component.DefaultServerProvider Maven / Gradle / Ivy
package net.unit8.waitt.mojo.component;
import net.unit8.waitt.api.EmbeddedServer;
import net.unit8.waitt.api.configuration.Server;
import net.unit8.waitt.mojo.configuration.ServerSpec;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;
/**
* A provider of a server.
*
* @author kawasima
*/
public class DefaultServerProvider implements ServerProvider {
private static final Logger LOG = Logger.getLogger(DefaultServerProvider.class.getName());
@Requirement
private Prompter prompter;
@Component
protected ArtifactResolver artifactResolver;
@Component
protected RepositorySystem repositorySystem;
@Override
public ServerSpec getServer(Server server, ClassRealm parentRealm) {
Artifact artifact = repositorySystem.createArtifact(server.getGroupId(), server.getArtifactId(), server.getVersion(), "jar");
ClassRealm serverRealm = artifactResolver.resolve(artifact, parentRealm);
ServiceLoader serviceLoaders = ServiceLoader.load(EmbeddedServer.class, serverRealm);
Iterator iter = serviceLoaders.iterator();
if (!iter.hasNext()) {
throw new IllegalArgumentException("Embedded server is not found.");
}
return new ServerSpec(iter.next(), serverRealm);
}
@Override
public ServerSpec selectServer(List servers, ClassRealm parentRealm, boolean interactive)
throws MojoFailureException {
if (servers == null || servers.isEmpty()) {
throw new MojoFailureException(servers, "No settings for server.",
"server not found");
}
List serverSpecs = new ArrayList();
for (Server server : servers) {
ServerSpec serverSpec = getServer(server, parentRealm);
serverSpecs.add(serverSpec);
}
if (interactive && serverSpecs.size() > 1) {
try {
prompter.showMessage("Detect multiple servers...\n");
List possibleValues = new ArrayList();
for (int i=0; i