mirror of https://github.com/apache/jclouds.git
updated glesys to 2.1 style type adapters
This commit is contained in:
parent
23ce04fc91
commit
fad1034427
|
@ -18,19 +18,20 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.glesys.config;
|
package org.jclouds.glesys.config;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.glesys.domain.ServerState;
|
||||||
|
import org.jclouds.glesys.domain.ServerUptime;
|
||||||
|
import org.jclouds.glesys.functions.internal.GleSYSTypeAdapters;
|
||||||
|
import org.jclouds.glesys.functions.internal.GlesysDateAdapter;
|
||||||
|
import org.jclouds.json.config.GsonModule.DateAdapter;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
import org.jclouds.glesys.domain.ServerState;
|
|
||||||
import org.jclouds.glesys.domain.ServerUptime;
|
|
||||||
import org.jclouds.glesys.functions.internal.CustomDeserializers;
|
|
||||||
import org.jclouds.glesys.functions.internal.GlesysDateAdapter;
|
|
||||||
import org.jclouds.json.config.GsonModule;
|
|
||||||
import org.jclouds.json.config.GsonModule.DateAdapter;
|
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
|
@ -40,10 +41,8 @@ public class GleSYSParserModule extends AbstractModule {
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
public Map<Type, Object> provideCustomAdapterBindings() {
|
public Map<Type, Object> provideCustomAdapterBindings() {
|
||||||
return ImmutableMap.<Type, Object>of(
|
return ImmutableMap.<Type, Object> of(ServerState.class, new GleSYSTypeAdapters.ServerStateAdapter(),
|
||||||
ServerState.class, new CustomDeserializers.ServerStateAdapter(),
|
ServerUptime.class, new GleSYSTypeAdapters.ServerUptimeAdapter());
|
||||||
ServerUptime.class, new CustomDeserializers.ServerUptimeAdaptor()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
package org.jclouds.glesys.functions.internal;
|
|
||||||
|
|
||||||
import com.google.gson.JsonDeserializationContext;
|
|
||||||
import com.google.gson.JsonDeserializer;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonParseException;
|
|
||||||
import org.jclouds.glesys.domain.ServerState;
|
|
||||||
import org.jclouds.glesys.domain.ServerUptime;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Adam Lowe
|
|
||||||
*/
|
|
||||||
public class CustomDeserializers {
|
|
||||||
|
|
||||||
public static class ServerStateAdapter implements JsonDeserializer<ServerState> {
|
|
||||||
@Override
|
|
||||||
public ServerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
|
|
||||||
throws JsonParseException {
|
|
||||||
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
|
|
||||||
return ServerState.fromValue(toParse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ServerUptimeAdaptor implements JsonDeserializer<ServerUptime> {
|
|
||||||
@Override
|
|
||||||
public ServerUptime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
|
|
||||||
throws JsonParseException {
|
|
||||||
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
|
|
||||||
return ServerUptime.fromValue(toParse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package org.jclouds.glesys.functions.internal;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.jclouds.glesys.domain.ServerState;
|
||||||
|
import org.jclouds.glesys.domain.ServerUptime;
|
||||||
|
|
||||||
|
import com.google.gson.TypeAdapter;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Adam Lowe
|
||||||
|
*/
|
||||||
|
public class GleSYSTypeAdapters {
|
||||||
|
|
||||||
|
public static class ServerStateAdapter extends TypeAdapter<ServerState> {
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter writer, ServerState value) throws IOException {
|
||||||
|
writer.value(value.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerState read(JsonReader reader) throws IOException {
|
||||||
|
return ServerState.fromValue(reader.nextString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ServerUptimeAdapter extends TypeAdapter<ServerUptime> {
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter writer, ServerUptime value) throws IOException {
|
||||||
|
writer.value(value.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServerUptime read(JsonReader reader) throws IOException {
|
||||||
|
return ServerUptime.fromValue(reader.nextString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,39 +1,42 @@
|
||||||
package org.jclouds.glesys.functions.internal;
|
package org.jclouds.glesys.functions.internal;
|
||||||
|
|
||||||
import com.google.gson.*;
|
import java.io.IOException;
|
||||||
import org.jclouds.json.config.GsonModule;
|
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import org.jclouds.json.config.GsonModule;
|
||||||
|
|
||||||
|
import com.google.common.base.Throwables;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parser for Glesys Date formats
|
* Parser for Glesys Date formats
|
||||||
*
|
*
|
||||||
* @author Adam Lowe
|
* @author Adam Lowe
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class GlesysDateAdapter implements GsonModule.DateAdapter {
|
public class GlesysDateAdapter extends GsonModule.DateAdapter {
|
||||||
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
public void write(JsonWriter writer, Date value) throws IOException {
|
||||||
synchronized (dateFormat) {
|
synchronized (dateFormat) {
|
||||||
return new JsonPrimitive(dateFormat.format(src));
|
writer.value(dateFormat.format(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
public Date read(JsonReader reader) throws IOException {
|
||||||
throws JsonParseException {
|
String toParse = reader.nextString();
|
||||||
String toParse = json.getAsJsonPrimitive().getAsString();
|
|
||||||
try {
|
try {
|
||||||
synchronized (dateFormat) {
|
synchronized (dateFormat) {
|
||||||
return dateFormat.parse(toParse);
|
return dateFormat.parse(toParse);
|
||||||
}
|
}
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new RuntimeException(e);
|
throw Throwables.propagate(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue