Add deserializers for ErrorCode and Exception. This makes the serializable entity a POJO.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162801 82a268e6-3cf1-43bd-a215-b396298e98cfmaster
parent
2d80258f83
commit
3adf77e8bd
@ -0,0 +1,43 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
||||
|
||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode;
|
||||
|
||||
/**
|
||||
* Don't forget to comment!
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
final class ErrorCodeDeserializer {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private ErrorCodeDeserializer() {}
|
||||
|
||||
/**
|
||||
* The error code, if any
|
||||
* @return the error code or null
|
||||
*/
|
||||
public static ErrorCode deserialize(int id, String message) {
|
||||
if (id != 1) {
|
||||
return new ErrorCode() {
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
};
|
||||
} else
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.ErrorCode;
|
||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity;
|
||||
|
||||
/**
|
||||
* Don't forget to comment!
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
public class ErrorEntityManager {
|
||||
|
||||
private final SerializableErrorEntity entity;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public ErrorEntityManager(SerializableErrorEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
public boolean hasException() {
|
||||
return Objects.nonNull(this.entity.getExceptionClass());
|
||||
}
|
||||
|
||||
public <E extends Exception> E getException() {
|
||||
return ExceptionDeserializer.deserialize(this.entity.getExceptionClass(),this.entity.getMessage());
|
||||
}
|
||||
|
||||
public boolean hasErrorCode() {
|
||||
return this.entity.getId() != -1;
|
||||
}
|
||||
|
||||
public ErrorCode getErrorCode(){
|
||||
return ErrorCodeDeserializer.deserialize(this.entity.getId(), this.entity.getMessage());
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Deserializer for {@link Exception}.
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
class ExceptionDeserializer {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private ExceptionDeserializer() {}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E extends Exception> E deserialize(String exceptionClass, String message) {
|
||||
try {
|
||||
final Class<?>[] ctorParams = {String.class};
|
||||
return (E) Class.forName(exceptionClass).getConstructor(ctorParams).newInstance(message);
|
||||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
|
||||
/**
|
||||
* Don't forget to comment!
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
package org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer;
|
Loading…
Reference in New Issue