com.consol.citrus.rmi.model.RmiServiceInvocation 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.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
/**
* @author Christoph Deppisch
* @since 2.5
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"remote",
"method",
"args"
})
@XmlRootElement(name = "service-invocation")
public class RmiServiceInvocation {
@XmlElement
protected String remote;
@XmlElement(required = true)
protected String method;
protected RmiServiceInvocation.Args args;
/**
* Static create method from target object and method definition.
* @return
*/
public static RmiServiceInvocation create(Object remoteTarget, Method method, Object[] args) {
RmiServiceInvocation serviceInvocation = new RmiServiceInvocation();
if (Proxy.isProxyClass(remoteTarget.getClass())) {
serviceInvocation.setRemote(method.getDeclaringClass().getName());
} else {
serviceInvocation.setRemote(remoteTarget.getClass().getName());
}
serviceInvocation.setMethod(method.getName());
if (args != null) {
serviceInvocation.setArgs(new RmiServiceInvocation.Args());
for (Object arg : args) {
MethodArg methodArg = new MethodArg();
methodArg.setValueObject(arg);
if (Map.class.isAssignableFrom(arg.getClass())) {
methodArg.setType(Map.class.getName());
} else if (List.class.isAssignableFrom(arg.getClass())) {
methodArg.setType(List.class.getName());
} else {
methodArg.setType(arg.getClass().getName());
}
serviceInvocation.getArgs().getArgs().add(methodArg);
}
}
return serviceInvocation;
}
/**
* Gets the argument types from list of args.
* @return
*/
public Class[] getArgTypes() {
List types = new ArrayList<>();
if (args != null) {
for (MethodArg arg : args.getArgs()) {
try {
types.add(Class.forName(arg.getType()));
} catch (ClassNotFoundException e) {
throw new CitrusRuntimeException("Failed to access method argument type", e);
}
}
}
return types.toArray(new Class[types.size()]);
}
/**
* Gets method args as objects. Automatically converts simple types and ready referenced beans.
* @return
*/
public Object[] getArgValues(ApplicationContext applicationContext) {
List