2222import java .io .ObjectInputStream ;
2323import java .io .ObjectOutputStream ;
2424import java .io .Serializable ;
25+ import java .lang .reflect .Constructor ;
26+ import java .lang .reflect .InvocationTargetException ;
27+ import java .lang .reflect .Type ;
2528
2629import org .apache .commons .codec .binary .Base64 ;
2730import org .apache .log4j .Logger ;
2831
2932import com .google .gson .Gson ;
3033import com .google .gson .GsonBuilder ;
34+ import com .google .gson .JsonDeserializationContext ;
35+ import com .google .gson .JsonDeserializer ;
36+ import com .google .gson .JsonElement ;
37+ import com .google .gson .JsonObject ;
38+ import com .google .gson .JsonParseException ;
39+ import com .google .gson .JsonPrimitive ;
40+ import com .google .gson .JsonSerializationContext ;
41+ import com .google .gson .JsonSerializer ;
3142
3243import com .cloud .utils .exception .CloudRuntimeException ;
3344
@@ -43,6 +54,7 @@ public class JobSerializerHelper {
4354 GsonBuilder gsonBuilder = new GsonBuilder ();
4455 gsonBuilder .setVersion (1.5 );
4556 s_logger .debug ("Job GSON Builder initialized." );
57+ gsonBuilder .registerTypeAdapter (Class .class , new ClassTypeAdapter ());
4658 s_gson = gsonBuilder .create ();
4759 }
4860
@@ -124,4 +136,67 @@ public static Object fromObjectSerializedString(String base64EncodedString) {
124136 throw new CloudRuntimeException ("Unable to serialize: " + base64EncodedString , e );
125137 }
126138 }
139+
140+ public static class ClassTypeAdapter implements JsonSerializer <Class <?>>, JsonDeserializer <Class <?>> {
141+ @ Override
142+ public JsonElement serialize (Class <?> clazz , Type typeOfResponseObj , JsonSerializationContext ctx ) {
143+ return new JsonPrimitive (clazz .getName ());
144+ }
145+
146+ @ Override
147+ public Class <?> deserialize (JsonElement arg0 , Type arg1 , JsonDeserializationContext arg2 ) throws JsonParseException {
148+ String str = arg0 .getAsString ();
149+ try {
150+ return Class .forName (str );
151+ } catch (ClassNotFoundException e ) {
152+ throw new CloudRuntimeException ("Unable to find class " + str );
153+ }
154+ }
155+ }
156+
157+ public static class ThrowableTypeAdapter implements JsonSerializer <Throwable >, JsonDeserializer <Throwable > {
158+
159+ @ Override
160+ public Throwable deserialize (JsonElement json , Type type , JsonDeserializationContext ctx ) throws JsonParseException {
161+ JsonObject obj = (JsonObject )json ;
162+
163+ String className = obj .get ("class" ).getAsString ();
164+ try {
165+ Class <Throwable > clazz = (Class <Throwable >)Class .forName (className );
166+ Throwable cause = s_gson .fromJson (obj .get ("cause" ), Throwable .class );
167+ String msg = obj .get ("msg" ).getAsString ();
168+ Constructor <Throwable > constructor = clazz .getConstructor (String .class , Throwable .class );
169+ Throwable th = constructor .newInstance (msg , cause );
170+ return th ;
171+ } catch (ClassNotFoundException e ) {
172+ throw new JsonParseException ("Unable to find " + className );
173+ } catch (NoSuchMethodException e ) {
174+ throw new JsonParseException ("Unable to find constructor for " + className );
175+ } catch (SecurityException e ) {
176+ throw new JsonParseException ("Unable to get over security " + className );
177+ } catch (InstantiationException e ) {
178+ throw new JsonParseException ("Unable to instantiate " + className );
179+ } catch (IllegalAccessException e ) {
180+ throw new JsonParseException ("Illegal access to " + className , e );
181+ } catch (IllegalArgumentException e ) {
182+ throw new JsonParseException ("Illegal argument to " + className , e );
183+ } catch (InvocationTargetException e ) {
184+ throw new JsonParseException ("Cannot invoke " + className , e );
185+ }
186+ }
187+
188+ @ Override
189+ public JsonElement serialize (Throwable th , Type type , JsonSerializationContext ctx ) {
190+ JsonObject json = new JsonObject ();
191+
192+ json .add ("class" , new JsonPrimitive (th .getClass ().getName ()));
193+ json .add ("cause" , s_gson .toJsonTree (th .getCause ()));
194+ json .add ("msg" , new JsonPrimitive (th .getMessage ()));
195+ // json.add("stack", s_gson.toJsonTree(th.getStackTrace()));
196+
197+ return json ;
198+ }
199+
200+ }
201+
127202}
0 commit comments