diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__AsyncClient.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__AsyncClient.java new file mode 100644 index 0000000000..87cfb6c5b1 --- /dev/null +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__AsyncClient.java @@ -0,0 +1,59 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package ${package}; + +import java.util.SortedSet; +import java.util.concurrent.Future; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +import org.jclouds.http.filters.BasicAuthentication; +import org.jclouds.rest.annotations.Endpoint; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; +import ${package}.domain.Status; +import ${package}.functions.ParseStatusesFromJsonResponse; + +/** + * Provides asynchronous access to ${clientName} via their REST API. + *

+ * + * @see ${clientName}Client + * @see + * @author ${author} + */ +@Endpoint(${clientName}.class) +@RequestFilters(BasicAuthentication.class) +public interface ${clientName}AsyncClient { + + @GET + @ResponseParser(ParseStatusesFromJsonResponse.class) + @Path("/statuses/mentions.json") + Future> getMyMentions(); + +} diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__Client.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__Client.java index fe4224fbfb..19fb324367 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__Client.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__Client.java @@ -27,32 +27,22 @@ package ${package}; import java.util.SortedSet; -import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; -import javax.ws.rs.GET; -import javax.ws.rs.Path; - -import org.jclouds.http.filters.BasicAuthentication; -import org.jclouds.rest.annotations.Endpoint; -import org.jclouds.rest.annotations.RequestFilters; -import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.concurrent.Timeout; import ${package}.domain.Status; -import ${package}.functions.ParseStatusesFromJsonResponse; /** - * Provides access to ${clientName} via their REST API. + * Provides synchronous access to ${clientName}. *

* - * @see + * @see ${clientName}AsyncClient + * @see * @author ${author} */ -@Endpoint(${clientName}.class) -@RequestFilters(BasicAuthentication.class) +@Timeout(duration = 4, timeUnit = TimeUnit.SECONDS) public interface ${clientName}Client { - @GET - @ResponseParser(ParseStatusesFromJsonResponse.class) - @Path("/statuses/mentions.json") - Future> getMyMentions(); + SortedSet getMyMentions(); } diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextBuilder.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextBuilder.java index fdd4b9815b..f9e2abd97e 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextBuilder.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextBuilder.java @@ -44,10 +44,11 @@ import com.google.inject.TypeLiteral; * * @author ${author} */ -public class ${clientName}ContextBuilder extends RestContextBuilder<${clientName}Client> { +public class ${clientName}ContextBuilder extends RestContextBuilder<${clientName}AsyncClient, ${clientName}Client> { public ${clientName}ContextBuilder(Properties props) { - super(new TypeLiteral<${clientName}Client>() { + super(new TypeLiteral<${clientName}AsyncClient>() { + }, new TypeLiteral<${clientName}Client>() { }, props); checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_USER)); checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_PASSWORD)); diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextFactory.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextFactory.java index 332b0937b8..b52b474c2e 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextFactory.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/__clientName__ContextFactory.java @@ -47,16 +47,17 @@ import com.google.inject.Module; * @author ${author} * @see RestContext * @see ${clientName}Client + * @see ${clientName}AsyncClient */ public class ${clientName}ContextFactory { - public static RestContext<${clientName}Client> createContext(String user, String password, + public static RestContext<${clientName}AsyncClient, ${clientName}Client> createContext(String user, String password, Module... modules) { return new ${clientName}ContextBuilder(new ${clientName}PropertiesBuilder(user, password).build()) .withModules(modules).buildContext(); } - public static RestContext<${clientName}Client> createContext(Properties properties, Module... modules) { + public static RestContext<${clientName}AsyncClient, ${clientName}Client> createContext(Properties properties, Module... modules) { return new ${clientName}ContextBuilder(new ${clientName}PropertiesBuilder(properties).build()) .withModules(modules).buildContext(); } diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__ContextModule.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__ContextModule.java index 8e4df2440d..1ffbdcbd2d 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__ContextModule.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__ContextModule.java @@ -35,6 +35,7 @@ import org.jclouds.lifecycle.Closer; import org.jclouds.rest.RestContext; import org.jclouds.rest.internal.RestContextImpl; import ${package}.${clientName}; +import ${package}.${clientName}AsyncClient; import ${package}.${clientName}Client; import ${package}.reference.${clientName}Constants; @@ -54,9 +55,9 @@ public class ${clientName}ContextModule extends AbstractModule { @Provides @Singleton - RestContext<${clientName}Client> provideContext(Closer closer, ${clientName}Client defaultApi, - @${clientName} URI endPoint, @Named(${clientName}Constants.PROPERTY_${ucaseClientName}_USER) String account) { - return new RestContextImpl<${clientName}Client>(closer, defaultApi, endPoint, account); + RestContext<${clientName}AsyncClient, ${clientName}Client> provideContext(Closer closer, ${clientName}AsyncClient asyncApi, + ${clientName}Client syncApi, @${clientName} URI endPoint, @Named(${clientName}Constants.PROPERTY_${ucaseClientName}_USER) String account) { + return new RestContextImpl<${clientName}AsyncClient, ${clientName}Client>(closer, asyncApi, syncApi, endPoint, account); } } \ No newline at end of file diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__RestClientModule.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__RestClientModule.java index d8025d78cb..82ce19ebfe 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__RestClientModule.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/main/java/config/__clientName__RestClientModule.java @@ -30,12 +30,15 @@ import java.net.URI; import javax.inject.Named; import javax.inject.Singleton; +import org.jclouds.concurrent.internal.SyncProxy; import org.jclouds.http.RequiresHttp; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.RestClientFactory; + import ${package}.${clientName}; import ${package}.${clientName}Client; +import ${package}.${clientName}AsyncClient; import ${package}.reference.${clientName}Constants; import com.google.inject.AbstractModule; @@ -67,10 +70,17 @@ public class ${clientName}RestClientModule extends AbstractModule { @Provides @Singleton - protected ${clientName}Client provideClient(RestClientFactory factory) { - return factory.create(${clientName}Client.class); + protected ${clientName}AsyncClient provideClient(RestClientFactory factory) { + return factory.create(${clientName}AsyncClient.class); } + @Provides + @Singleton + public ${clientName}Client provideClient(${clientName}AsyncClient client) throws IllegalArgumentException, + SecurityException, NoSuchMethodException { + return SyncProxy.create(${clientName}Client.class, client); + } + @Provides @Singleton @${clientName} diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientTest.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__AsyncClientTest.java similarity index 85% rename from archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientTest.java rename to archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__AsyncClientTest.java index eea8e4b9f3..69ef87a926 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientTest.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__AsyncClientTest.java @@ -51,18 +51,18 @@ import com.google.inject.Provides; import com.google.inject.TypeLiteral; /** - * Tests behavior of {@code ${clientName}Client} + * Tests annotation parsing of {@code ${clientName}AsyncClient} * * @author ${author} */ -@Test(groups = "unit", testName = "${lcaseClientName}.${clientName}ClientTest") -public class ${clientName}ClientTest extends RestClientTest<${clientName}Client> { +@Test(groups = "unit", testName = "${lcaseClientName}.${clientName}AsyncClientTest") +public class ${clientName}AsyncClientTest extends RestClientTest<${clientName}AsyncClient> { public void testGetMyMentions() throws SecurityException, NoSuchMethodException, IOException { - Method method = ${clientName}Client.class.getMethod(""<-- (remove '"') TODO: insert test method name"); - GeneratedHttpRequest<${clientName}Client> httpMethod = processor.createRequest(method); + Method method = ${clientName}AsyncClient.class.getMethod("TODO: insert test method name"); + GeneratedHttpRequest<${clientName}AsyncClient> httpMethod = processor.createRequest(method); - assertRequestLineEquals(httpMethod, ""<-- (remove '"') TODO: insert expected request"); + assertRequestLineEquals(httpMethod, "TODO: insert expected request"); assertHeadersEqual(httpMethod, ""); assertEntityEquals(httpMethod, null); @@ -74,14 +74,14 @@ public class ${clientName}ClientTest extends RestClientTest<${clientName}Client> } @Override - protected void checkFilters(GeneratedHttpRequest<${clientName}Client> httpMethod) { + protected void checkFilters(GeneratedHttpRequest<${clientName}AsyncClient> httpMethod) { assertEquals(httpMethod.getFilters().size(), 1); assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class); } @Override - protected TypeLiteral> createTypeLiteral() { - return new TypeLiteral>() { + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { }; } diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientLiveTest.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientLiveTest.java index 8f7b238b1e..f1e6e11140 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientLiveTest.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ClientLiveTest.java @@ -30,9 +30,6 @@ package ${package}; import static com.google.common.base.Preconditions.checkNotNull; import java.util.SortedSet; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import ${package}.domain.Status; @@ -50,7 +47,7 @@ public class ${clientName}ClientLiveTest { private ${clientName}Client connection; @BeforeGroups(groups = { "live" }) - public void setupClient() throws InterruptedException, ExecutionException, TimeoutException { + public void setupClient() { String user = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user"); String password = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key"); @@ -59,8 +56,8 @@ public class ${clientName}ClientLiveTest { } @Test - public void testGetMyMentions() throws Exception { - SortedSet response = connection.getMyMentions().get(1, TimeUnit.SECONDS); + public void testGetMyMentions() { + SortedSet response = connection.getMyMentions(); assert (response.size() > 0); } diff --git a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ContextBuilderTest.java b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ContextBuilderTest.java index d4c07618c4..20c204e851 100644 --- a/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ContextBuilderTest.java +++ b/archetypes/json-client-archetype/src/main/resources/archetype-resources/src/test/java/__clientName__ContextBuilderTest.java @@ -65,7 +65,7 @@ public class ${clientName}ContextBuilderTest { } public void testBuildContext() { - RestContext<${clientName}Client> context = newBuilder().buildContext(); + RestContext<${clientName}AsyncClient, ${clientName}Client> context = newBuilder().buildContext(); assertEquals(context.getClass(), RestContextImpl.class); assertEquals(context.getAccount(), "user"); assertEquals(context.getEndPoint(), URI.create("${clientEndpoint}")); @@ -73,7 +73,7 @@ public class ${clientName}ContextBuilderTest { public void testBuildInjector() { Injector i = newBuilder().buildInjector(); - assert i.getInstance(Key.get(new TypeLiteral>() { + assert i.getInstance(Key.get(new TypeLiteral>() { })) != null; // TODO: test all things taken from context assert i.getInstance(BasicAuthentication.class) != null; }