com.consol.citrus.rmi.model.RmiServiceResult Maven / Gradle / Ivy
/*
* Copyright 2006-2015 the original author or authors.
*
* Licensed 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 com.consol.citrus.rmi.model;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import javax.xml.bind.annotation.*;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
/**
* @author Christoph Deppisch
* @since 2.5
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"exception",
"object"
})
@XmlRootElement(name = "service-result")
public class RmiServiceResult {
@XmlElement
protected String exception;
@XmlElement
protected RmiServiceResult.Object object;
/**
* Gets the value of the exception property.
*
* @return
* possible exception is
* {@link String }
*
*/
public String getException() {
return exception;
}
/**
* Sets the value of the exception property.
*
* @param value
* allowed exception is
* {@link String }
*
*/
public void setException(String value) {
this.exception = value;
}
/**
* Gets the value of the object property.
*
* @return
* possible object is
* {@link RmiServiceResult.Object }
*
*/
public RmiServiceResult.Object getObject() {
return object;
}
/**
* Sets the value of the object property.
*
* @param value
* allowed object is
* {@link RmiServiceResult.Object }
*
*/
public void setObject(RmiServiceResult.Object value) {
this.object = value;
}
/**
* Gets this service result as object casted to target type if necessary.
* @return
*/
public java.lang.Object getResultObject(ApplicationContext applicationContext) {
if (object == null) {
return null;
}
if (object.getValueObject() != null) {
return object.getValueObject();
}
try {
Class argType = Class.forName(object.getType());
java.lang.Object value = null;
if (object.getValue() != null) {
value = object.getValue();
} else if (StringUtils.hasText(object.getRef()) && applicationContext != null) {
value = applicationContext.getBean(object.getRef());
}
if (value == null) {
return null;
} else if (argType.isInstance(value) || argType.isAssignableFrom(value.getClass())) {
return argType.cast(value);
} else if(Map.class.equals(argType)) {
String mapString = value.toString();
Properties props = new Properties();
try {
props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replace(", ", "\n")));
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to reconstruct service result object of type map", e);
}
Map map = new LinkedHashMap<>();
for (Map.Entry entry : props.entrySet()) {
map.put(entry.getKey().toString(), entry.getValue().toString());
}
return map;
} else {
try {
return new SimpleTypeConverter().convertIfNecessary(value, argType);
} catch (ConversionNotSupportedException e) {
if (String.class.equals(argType)) {
return value.toString();
}
throw e;
}
}
} catch (ClassNotFoundException e) {
throw new CitrusRuntimeException("Failed to construct service result object", e);
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class Object {
@XmlAttribute(name = "type")
protected String type = String.class.getName();
@XmlAttribute(name = "value")
protected String value;
@XmlAttribute(name = "ref")
protected String ref;
@XmlTransient
private java.lang.Object valueObject;
/**
* Gets the value of the type property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getType() {
return type;
}
/**
* Sets the value of the type property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setType(String value) {
this.type = value;
}
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}
/**
* Gets the value of the ref property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getRef() {
return ref;
}
/**
* Sets the value of the ref property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setRef(String value) {
this.ref = value;
}
public java.lang.Object getValueObject() {
return valueObject;
}
public void setValueObject(java.lang.Object valueObject) {
setType(valueObject.getClass().getName());
setValue(valueObject.toString());
this.valueObject = valueObject;
}
}
}