Issue 129: readying ant plugin for multiple cloud providers

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2388 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-09 02:39:56 +00:00
parent 6aeb907620
commit 572fe270c4
5 changed files with 121 additions and 25 deletions

View File

@ -0,0 +1,59 @@
package org.jclouds.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
import java.util.Properties;
import javax.inject.Inject;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpPropertiesBuilder;
import org.jclouds.rest.RestContextBuilder;
import com.google.inject.Module;
/**
*
* @author Adrian Cole
*/
public class ComputeServiceFactory {
private final Properties properties;
@Inject
public ComputeServiceFactory(Properties properties) {
this.properties = properties;
}
public ComputeService create(URI provider, Module... modules) {
return create(provider, Credentials.parse(provider), modules);
}
@SuppressWarnings("unchecked")
public ComputeService create(URI provider, Credentials creds, Module... modules) {
String hint = checkNotNull(provider.getHost(), "host");
String account = checkNotNull(creds.account, "account");
String key = creds.key;
String propertiesBuilderKey = String.format("%s.propertiesbuilder", hint);
String propertiesBuilderClassName = checkNotNull(
properties.getProperty(propertiesBuilderKey), propertiesBuilderKey);
String contextBuilderKey = String.format("%s.contextbuilder", hint);
String contextBuilderClassName = checkNotNull(properties.getProperty(contextBuilderKey),
contextBuilderKey);
try {
Class<HttpPropertiesBuilder> propertiesBuilderClass = (Class<HttpPropertiesBuilder>) Class
.forName(propertiesBuilderClassName);
Class<RestContextBuilder<?, ?>> contextBuilderClass = (Class<RestContextBuilder<?, ?>>) Class
.forName(contextBuilderClassName);
HttpPropertiesBuilder builder = propertiesBuilderClass.getConstructor(String.class,
String.class).newInstance(account, key);
return contextBuilderClass.getConstructor(Properties.class).newInstance(builder.build())
.withModules(modules).buildInjector().getInstance(ComputeService.class);
} catch (Exception e) {
throw new RuntimeException("error instantiating " + contextBuilderClassName, e);
}
}
}

View File

@ -1,2 +1,2 @@
rimuhosting.contextBuilder=org.jclouds.rimuhosting.miro.RimuHostingContextBuilder
rimuhosting.propertiesBuilder=org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder
rimuhosting.contextbuilder=org.jclouds.rimuhosting.miro.RimuHostingContextBuilder
rimuhosting.propertiesbuilder=org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder

View File

@ -24,7 +24,7 @@
-->
<project name="ex6" default="demo" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<artifact:localRepository id="jclouds.repository" path="c:\\Users\\Ivan\\.m2\\repository" />
<artifact:localRepository id="jclouds.repository" path="${user.home}/.m2/repository" />
<artifact:dependencies pathId="jclouds.classpath">
<dependency groupId="org.jclouds" artifactId="jclouds-ant-plugin" version="1.0-SNAPSHOT" />
<localRepository refid="jclouds.repository" />
@ -32,9 +32,8 @@
<typedef name="compute" classname="org.jclouds.tools.ant.ComputeTask" classpathref="jclouds.classpath" />
<target name="demo">
<compute action="create"/>
<compute action="create" provider="compute://accountkey:accountkey@rimuhosting" />
</target>
</project>

View File

@ -31,11 +31,12 @@ import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.jclouds.compute.ComputeService;
import org.jclouds.rimuhosting.miro.RimuHostingContextBuilder;
import org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder;
import org.jclouds.compute.ComputeServiceFactory;
import org.jclouds.http.HttpUtils;
import com.google.common.base.Function;
import com.google.common.collect.MapMaker;
import com.google.common.io.Resources;
import com.google.inject.Injector;
/**
* @author Ivan Meredith
@ -44,7 +45,7 @@ public class ComputeTask extends Task {
private final Map<URI, ComputeService> computeMap;
public ComputeTask() throws IOException {
this(null);//TODO MapMaker
this(buildComputeMap(loadDefaultProperties()));
}
static Properties loadDefaultProperties() throws IOException {
@ -54,27 +55,35 @@ public class ComputeTask extends Task {
return properties;
}
static Map<URI, ComputeService> buildComputeMap(final Properties props) {
return new MapMaker().makeComputingMap(new Function<URI, ComputeService>() {
@Override
public ComputeService apply(URI from) {
return new ComputeServiceFactory(props).create(from);
}
});
}
public ComputeTask(Map<URI, ComputeService> computeMap) {
this.computeMap = computeMap;
}
private final String ACTION_CREATE = "create";
private String provider;
private String action;
private ServerElement serverElement;
public void execute() throws BuildException {
if (ACTION_CREATE.equalsIgnoreCase(action)) {
if (getServerElement() != null) {
Injector injector = new RimuHostingContextBuilder(new RimuHostingPropertiesBuilder(
"test", "Test").relaxSSLHostname().build()).buildInjector();
ComputeService computeService = injector.getInstance(ComputeService.class);
ComputeService computeService = computeMap.get(HttpUtils.createUri(provider));
log("hello");
computeService.createServerAndWait("test.com", "MIRO1B", "lenny");
}
}
}
public String getAction() {
return action;
@ -91,4 +100,12 @@ public class ComputeTask extends Task {
public void setServerElement(ServerElement serverElement) {
this.serverElement = serverElement;
}
public void setProvider(String provider) {
this.provider = provider;
}
public String getProvider() {
return provider;
}
}

View File

@ -0,0 +1,21 @@
package org.jclouds.tools.ant;
import java.io.IOException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "compute.ComputeTaskTest")
public class ComputeTaskTest {
private ComputeTask task;
private ServerElement serverElement;
@BeforeTest
protected void setUp() throws IOException {
this.task = new ComputeTask();
}
}