diff --git a/core/src/main/java/org/jclouds/compute/ComputeServiceFactory.java b/core/src/main/java/org/jclouds/compute/ComputeServiceFactory.java new file mode 100644 index 0000000000..42136b4987 --- /dev/null +++ b/core/src/main/java/org/jclouds/compute/ComputeServiceFactory.java @@ -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 propertiesBuilderClass = (Class) Class + .forName(propertiesBuilderClassName); + Class> contextBuilderClass = (Class>) 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); + } + } +} diff --git a/core/src/main/resources/compute.properties b/core/src/main/resources/compute.properties index f2b1e8fbe4..476806f214 100644 --- a/core/src/main/resources/compute.properties +++ b/core/src/main/resources/compute.properties @@ -1,2 +1,2 @@ -rimuhosting.contextBuilder=org.jclouds.rimuhosting.miro.RimuHostingContextBuilder -rimuhosting.propertiesBuilder=org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder \ No newline at end of file +rimuhosting.contextbuilder=org.jclouds.rimuhosting.miro.RimuHostingContextBuilder +rimuhosting.propertiesbuilder=org.jclouds.rimuhosting.miro.RimuHostingPropertiesBuilder \ No newline at end of file diff --git a/tools/ant-plugin/build.xml b/tools/ant-plugin/build.xml index b279963d4a..05005a42cf 100644 --- a/tools/ant-plugin/build.xml +++ b/tools/ant-plugin/build.xml @@ -24,17 +24,16 @@ --> - - - - - + + + + + - - - + - - - \ No newline at end of file + + + + diff --git a/tools/ant-plugin/src/main/java/org/jclouds/tools/ant/ComputeTask.java b/tools/ant-plugin/src/main/java/org/jclouds/tools/ant/ComputeTask.java index 38614397a3..321c1811d5 100644 --- a/tools/ant-plugin/src/main/java/org/jclouds/tools/ant/ComputeTask.java +++ b/tools/ant-plugin/src/main/java/org/jclouds/tools/ant/ComputeTask.java @@ -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 computeMap; public ComputeTask() throws IOException { - this(null);//TODO MapMaker + this(buildComputeMap(loadDefaultProperties())); } static Properties loadDefaultProperties() throws IOException { @@ -54,25 +55,33 @@ public class ComputeTask extends Task { return properties; } + static Map buildComputeMap(final Properties props) { + return new MapMaker().makeComputingMap(new Function() { + + @Override + public ComputeService apply(URI from) { + return new ComputeServiceFactory(props).create(from); + } + + }); + + } + public ComputeTask(Map 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.createServerAndWait("test.com", "MIRO1B", "lenny"); - } + ComputeService computeService = computeMap.get(HttpUtils.createUri(provider)); + log("hello"); + computeService.createServerAndWait("test.com", "MIRO1B", "lenny"); } } @@ -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; + } } diff --git a/tools/ant-plugin/src/test/java/org/jclouds/tools/ant/ComputeTaskTest.java b/tools/ant-plugin/src/test/java/org/jclouds/tools/ant/ComputeTaskTest.java new file mode 100644 index 0000000000..001f7b0e48 --- /dev/null +++ b/tools/ant-plugin/src/test/java/org/jclouds/tools/ant/ComputeTaskTest.java @@ -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(); + + } +}