added json to core parser module

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1631 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-07-16 22:18:28 +00:00
parent fbe7ad9f8e
commit d92f4e2750
6 changed files with 51 additions and 12 deletions

View File

@ -43,7 +43,7 @@ import org.jclouds.aws.s3.xml.config.S3ParserModule;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.config.SaxModule;
import org.jclouds.http.functions.config.ParserModule;
import org.joda.time.DateTime;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
@ -68,7 +68,7 @@ public class S3ParserTest extends PerformanceTest {
@BeforeTest
protected void setUpInjector() {
injector = Guice.createInjector(new SaxModule(), new S3ParserModule());
injector = Guice.createInjector(new ParserModule(), new S3ParserModule());
parserFactory = injector.getInstance(S3ParserFactory.class);
assert parserFactory != null;
}

View File

@ -28,7 +28,7 @@ import java.net.URI;
import org.jclouds.aws.s3.S3Connection;
import org.jclouds.aws.s3.internal.StubS3Connection;
import org.jclouds.cloud.ConfiguresCloudConnection;
import org.jclouds.http.functions.config.SaxModule;
import org.jclouds.http.functions.config.ParserModule;
import com.google.inject.AbstractModule;
@ -40,7 +40,7 @@ import com.google.inject.AbstractModule;
@ConfiguresCloudConnection
public class StubS3ConnectionModule extends AbstractModule {
protected void configure() {
install(new SaxModule());
install(new ParserModule());
bind(S3Connection.class).to(StubS3Connection.class);
bind(URI.class).toInstance(URI.create("http://localhost:8080"));
}

View File

@ -25,7 +25,7 @@ package org.jclouds.aws.s3.functions;
import org.jclouds.aws.s3.xml.S3ParserFactory;
import org.jclouds.aws.s3.xml.config.S3ParserModule;
import org.jclouds.http.functions.config.SaxModule;
import org.jclouds.http.functions.config.ParserModule;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
@ -39,7 +39,7 @@ public class BaseHandlerTest {
@BeforeTest
protected void setUpInjector() {
injector = Guice.createInjector(new S3ParserModule(), new SaxModule());
injector = Guice.createInjector(new S3ParserModule(), new ParserModule());
parserFactory = injector.getInstance(S3ParserFactory.class);
assert parserFactory != null;
}

View File

@ -25,7 +25,7 @@ package org.jclouds.aws.s3.functions;
import org.jclouds.aws.s3.xml.S3ParserFactory;
import org.jclouds.aws.s3.xml.config.S3ParserModule;
import org.jclouds.http.functions.config.SaxModule;
import org.jclouds.http.functions.config.ParserModule;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@ -44,7 +44,7 @@ public class S3ParserFactoryTest {
@BeforeTest
void setUpInjector() {
injector = Guice.createInjector(new S3ParserModule(), new SaxModule());
injector = Guice.createInjector(new S3ParserModule(), new ParserModule());
parserFactory = injector.getInstance(S3ParserFactory.class);
}

View File

@ -23,6 +23,10 @@
*/
package org.jclouds.http.functions.config;
import java.lang.reflect.Type;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
@ -31,20 +35,47 @@ import org.jclouds.http.functions.ParseSax;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryProvider;
/**
* // TODO: Adrian: Document this!
* Contains logic for parsing objects from Strings.
*
* @author Adrian Cole
*/
public class SaxModule extends AbstractModule {
public class ParserModule extends AbstractModule {
private final static TypeLiteral<ParseSax.Factory> parseSaxFactoryLiteral = new TypeLiteral<ParseSax.Factory>() {
};
static class InetAddressAdapter implements JsonSerializer<InetAddress>,
JsonDeserializer<InetAddress> {
public JsonElement serialize(InetAddress src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getHostAddress());
}
public InetAddress deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
try {
return InetAddress.getByName(json.getAsJsonPrimitive().getAsString());
} catch (UnknownHostException e) {
throw new JsonParseException(e);
}
}
}
@Provides
XMLReader provideXMLReader(SAXParserFactory factory) throws ParserConfigurationException,
SAXException {
@ -62,6 +93,14 @@ public class SaxModule extends AbstractModule {
return factory;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(InetAddress.class, new InetAddressAdapter());
return gson.create();
}
protected void configure() {
bind(parseSaxFactoryLiteral).toProvider(
FactoryProvider.newFactory(parseSaxFactoryLiteral, new TypeLiteral<ParseSax<?>>() {

View File

@ -27,7 +27,7 @@ import javax.ws.rs.ext.RuntimeDelegate;
import org.jclouds.http.TransformingHttpCommand;
import org.jclouds.http.TransformingHttpCommandImpl;
import org.jclouds.http.functions.config.SaxModule;
import org.jclouds.http.functions.config.ParserModule;
import org.jclouds.rest.JaxrsAnnotationProcessor;
import org.jclouds.rest.RestClientProxy;
import org.jclouds.rest.RuntimeDelegateImpl;
@ -46,7 +46,7 @@ public class JaxrsModule extends AbstractModule {
@Override
protected void configure() {
install(new SaxModule());
install(new ParserModule());
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
bind(RestClientProxy.RestClientProxyFactory.class).toProvider(
FactoryProvider.newFactory(RestClientProxy.RestClientProxyFactory.class, RestClientProxy.class));