updated archetype to use new async model

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2297 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-17 06:33:59 +00:00
parent 09adeebe3d
commit f2410e5889
9 changed files with 102 additions and 43 deletions

View File

@ -0,0 +1,59 @@
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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.
* <p/>
*
* @see ${clientName}Client
* @see <a href="TODO: insert URL of client documentation" />
* @author ${author}
*/
@Endpoint(${clientName}.class)
@RequestFilters(BasicAuthentication.class)
public interface ${clientName}AsyncClient {
@GET
@ResponseParser(ParseStatusesFromJsonResponse.class)
@Path("/statuses/mentions.json")
Future<SortedSet<Status>> getMyMentions();
}

View File

@ -27,32 +27,22 @@
package ${package}; package ${package};
import java.util.SortedSet; import java.util.SortedSet;
import java.util.concurrent.Future; import java.util.concurrent.TimeUnit;
import javax.ws.rs.GET; import org.jclouds.concurrent.Timeout;
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}.domain.Status;
import ${package}.functions.ParseStatusesFromJsonResponse;
/** /**
* Provides access to ${clientName} via their REST API. * Provides synchronous access to ${clientName}.
* <p/> * <p/>
* *
* @see <a href="*/<-- (remove '*/') TODO: insert URL of client documentation" /> * @see ${clientName}AsyncClient
* @see <a href="TODO: insert URL of client documentation" />
* @author ${author} * @author ${author}
*/ */
@Endpoint(${clientName}.class) @Timeout(duration = 4, timeUnit = TimeUnit.SECONDS)
@RequestFilters(BasicAuthentication.class)
public interface ${clientName}Client { public interface ${clientName}Client {
@GET SortedSet<Status> getMyMentions();
@ResponseParser(ParseStatusesFromJsonResponse.class)
@Path("/statuses/mentions.json")
Future<SortedSet<Status>> getMyMentions();
} }

View File

@ -44,10 +44,11 @@ import com.google.inject.TypeLiteral;
* *
* @author ${author} * @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) { public ${clientName}ContextBuilder(Properties props) {
super(new TypeLiteral<${clientName}Client>() { super(new TypeLiteral<${clientName}AsyncClient>() {
}, new TypeLiteral<${clientName}Client>() {
}, props); }, props);
checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_USER)); checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_USER));
checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_PASSWORD)); checkNotNull(properties.getProperty(${clientName}Constants.PROPERTY_${ucaseClientName}_PASSWORD));

View File

@ -47,16 +47,17 @@ import com.google.inject.Module;
* @author ${author} * @author ${author}
* @see RestContext * @see RestContext
* @see ${clientName}Client * @see ${clientName}Client
* @see ${clientName}AsyncClient
*/ */
public class ${clientName}ContextFactory { 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) { Module... modules) {
return new ${clientName}ContextBuilder(new ${clientName}PropertiesBuilder(user, password).build()) return new ${clientName}ContextBuilder(new ${clientName}PropertiesBuilder(user, password).build())
.withModules(modules).buildContext(); .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()) return new ${clientName}ContextBuilder(new ${clientName}PropertiesBuilder(properties).build())
.withModules(modules).buildContext(); .withModules(modules).buildContext();
} }

View File

@ -35,6 +35,7 @@ import org.jclouds.lifecycle.Closer;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import org.jclouds.rest.internal.RestContextImpl; import org.jclouds.rest.internal.RestContextImpl;
import ${package}.${clientName}; import ${package}.${clientName};
import ${package}.${clientName}AsyncClient;
import ${package}.${clientName}Client; import ${package}.${clientName}Client;
import ${package}.reference.${clientName}Constants; import ${package}.reference.${clientName}Constants;
@ -54,9 +55,9 @@ public class ${clientName}ContextModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
RestContext<${clientName}Client> provideContext(Closer closer, ${clientName}Client defaultApi, RestContext<${clientName}AsyncClient, ${clientName}Client> provideContext(Closer closer, ${clientName}AsyncClient asyncApi,
@${clientName} URI endPoint, @Named(${clientName}Constants.PROPERTY_${ucaseClientName}_USER) String account) { ${clientName}Client syncApi, @${clientName} URI endPoint, @Named(${clientName}Constants.PROPERTY_${ucaseClientName}_USER) String account) {
return new RestContextImpl<${clientName}Client>(closer, defaultApi, endPoint, account); return new RestContextImpl<${clientName}AsyncClient, ${clientName}Client>(closer, asyncApi, syncApi, endPoint, account);
} }
} }

View File

@ -30,12 +30,15 @@ import java.net.URI;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.concurrent.internal.SyncProxy;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.RestClientFactory; import org.jclouds.rest.RestClientFactory;
import ${package}.${clientName}; import ${package}.${clientName};
import ${package}.${clientName}Client; import ${package}.${clientName}Client;
import ${package}.${clientName}AsyncClient;
import ${package}.reference.${clientName}Constants; import ${package}.reference.${clientName}Constants;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
@ -67,8 +70,15 @@ public class ${clientName}RestClientModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
protected ${clientName}Client provideClient(RestClientFactory factory) { protected ${clientName}AsyncClient provideClient(RestClientFactory factory) {
return factory.create(${clientName}Client.class); 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 @Provides

View File

@ -51,18 +51,18 @@ import com.google.inject.Provides;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
/** /**
* Tests behavior of {@code ${clientName}Client} * Tests annotation parsing of {@code ${clientName}AsyncClient}
* *
* @author ${author} * @author ${author}
*/ */
@Test(groups = "unit", testName = "${lcaseClientName}.${clientName}ClientTest") @Test(groups = "unit", testName = "${lcaseClientName}.${clientName}AsyncClientTest")
public class ${clientName}ClientTest extends RestClientTest<${clientName}Client> { public class ${clientName}AsyncClientTest extends RestClientTest<${clientName}AsyncClient> {
public void testGetMyMentions() throws SecurityException, NoSuchMethodException, IOException { public void testGetMyMentions() throws SecurityException, NoSuchMethodException, IOException {
Method method = ${clientName}Client.class.getMethod(""<-- (remove '"') TODO: insert test method name"); Method method = ${clientName}AsyncClient.class.getMethod("TODO: insert test method name");
GeneratedHttpRequest<${clientName}Client> httpMethod = processor.createRequest(method); GeneratedHttpRequest<${clientName}AsyncClient> httpMethod = processor.createRequest(method);
assertRequestLineEquals(httpMethod, ""<-- (remove '"') TODO: insert expected request"); assertRequestLineEquals(httpMethod, "TODO: insert expected request");
assertHeadersEqual(httpMethod, ""); assertHeadersEqual(httpMethod, "");
assertEntityEquals(httpMethod, null); assertEntityEquals(httpMethod, null);
@ -74,14 +74,14 @@ public class ${clientName}ClientTest extends RestClientTest<${clientName}Client>
} }
@Override @Override
protected void checkFilters(GeneratedHttpRequest<${clientName}Client> httpMethod) { protected void checkFilters(GeneratedHttpRequest<${clientName}AsyncClient> httpMethod) {
assertEquals(httpMethod.getFilters().size(), 1); assertEquals(httpMethod.getFilters().size(), 1);
assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class); assertEquals(httpMethod.getFilters().get(0).getClass(), BasicAuthentication.class);
} }
@Override @Override
protected TypeLiteral<RestAnnotationProcessor<${clientName}Client>> createTypeLiteral() { protected TypeLiteral<RestAnnotationProcessor<${clientName}AsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<${clientName}Client>>() { return new TypeLiteral<RestAnnotationProcessor<${clientName}AsyncClient>>() {
}; };
} }

View File

@ -30,9 +30,6 @@ package ${package};
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.SortedSet; 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 org.jclouds.logging.log4j.config.Log4JLoggingModule;
import ${package}.domain.Status; import ${package}.domain.Status;
@ -50,7 +47,7 @@ public class ${clientName}ClientLiveTest {
private ${clientName}Client connection; private ${clientName}Client connection;
@BeforeGroups(groups = { "live" }) @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 user = checkNotNull(System.getProperty("jclouds.test.user"), "jclouds.test.user");
String password = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key"); String password = checkNotNull(System.getProperty("jclouds.test.key"), "jclouds.test.key");
@ -59,8 +56,8 @@ public class ${clientName}ClientLiveTest {
} }
@Test @Test
public void testGetMyMentions() throws Exception { public void testGetMyMentions() {
SortedSet<Status> response = connection.getMyMentions().get(1, TimeUnit.SECONDS); SortedSet<Status> response = connection.getMyMentions();
assert (response.size() > 0); assert (response.size() > 0);
} }

View File

@ -65,7 +65,7 @@ public class ${clientName}ContextBuilderTest {
} }
public void testBuildContext() { public void testBuildContext() {
RestContext<${clientName}Client> context = newBuilder().buildContext(); RestContext<${clientName}AsyncClient, ${clientName}Client> context = newBuilder().buildContext();
assertEquals(context.getClass(), RestContextImpl.class); assertEquals(context.getClass(), RestContextImpl.class);
assertEquals(context.getAccount(), "user"); assertEquals(context.getAccount(), "user");
assertEquals(context.getEndPoint(), URI.create("${clientEndpoint}")); assertEquals(context.getEndPoint(), URI.create("${clientEndpoint}"));
@ -73,7 +73,7 @@ public class ${clientName}ContextBuilderTest {
public void testBuildInjector() { public void testBuildInjector() {
Injector i = newBuilder().buildInjector(); Injector i = newBuilder().buildInjector();
assert i.getInstance(Key.get(new TypeLiteral<RestContext<${clientName}Client>>() { assert i.getInstance(Key.get(new TypeLiteral<RestContext<${clientName}AsyncClient, ${clientName}Client>>() {
})) != null; // TODO: test all things taken from context })) != null; // TODO: test all things taken from context
assert i.getInstance(BasicAuthentication.class) != null; assert i.getInstance(BasicAuthentication.class) != null;
} }