001: package net.sf.crispy.impl;
002:
003: import java.lang.reflect.Method;
004: import java.math.BigDecimal;
005: import java.util.Date;
006: import java.util.Hashtable;
007: import java.util.Iterator;
008: import java.util.Map;
009:
010: import javax.xml.namespace.QName;
011: import javax.xml.rpc.Call;
012: import javax.xml.rpc.ParameterMode;
013: import javax.xml.rpc.Service;
014: import javax.xml.rpc.ServiceFactory;
015: import javax.xml.rpc.encoding.TypeMapping;
016: import javax.xml.rpc.encoding.XMLType;
017:
018: import net.sf.crispy.Executor;
019: import net.sf.crispy.InvocationStrategy;
020: import net.sf.crispy.Property;
021: import net.sf.crispy.impl.jaxrpc.TypeMappingFactory;
022: import net.sf.crispy.strategy.UrlPlusClassNameInvocationStrategy;
023: import net.sf.crispy.util.Util;
024:
025: /**
026: * Executor for the JAX-RPC standard (Web-Services).
027: *
028: * @author Linke
029: *
030: */
031: public class JaxRpcExecutor extends Executor {
032:
033: public static final String DEFAULT_SERVICE_FACTORY = "org.apache.axis.client.ServiceFactory";
034: public final static String DEFAULT_URL_AND_PORT = "http://localhost:9080/axis/services";
035:
036: private String serviceFactory = DEFAULT_SERVICE_FACTORY;
037: private Service service = null;
038: private Call call = null;
039: // private InvocationStrategy invocationMapping = null;
040:
041: private static Map paramMapper = intParamMap();
042:
043: public synchronized static Map intParamMap() {
044: // if (paramMapper == null) {
045: paramMapper = new Hashtable();
046:
047: paramMapper.put(Boolean.class, XMLType.XSD_BOOLEAN);
048: paramMapper.put(Byte.class, XMLType.XSD_BYTE);
049: paramMapper.put(Short.class, XMLType.XSD_SHORT);
050: paramMapper.put(Integer.class, XMLType.XSD_INTEGER);
051: paramMapper.put(Long.class, XMLType.XSD_LONG);
052: paramMapper.put(Float.class, XMLType.XSD_FLOAT);
053: paramMapper.put(Double.class, XMLType.XSD_DOUBLE);
054: paramMapper.put(BigDecimal.class, XMLType.XSD_DECIMAL);
055: paramMapper.put(Date.class, XMLType.XSD_DATETIME);
056: paramMapper.put(String.class, XMLType.XSD_STRING);
057: paramMapper.put(Character.class, XMLType.XSD_STRING);
058:
059: // --- primitive Datentypen ---
060: paramMapper.put(boolean.class, XMLType.XSD_BOOLEAN);
061: paramMapper.put(byte.class, XMLType.XSD_BYTE);
062: paramMapper.put(short.class, XMLType.XSD_SHORT);
063: paramMapper.put(int.class, XMLType.XSD_INT);
064: paramMapper.put(long.class, XMLType.XSD_LONG);
065: paramMapper.put(float.class, XMLType.XSD_FLOAT);
066: paramMapper.put(double.class, XMLType.XSD_DOUBLE);
067: paramMapper.put(char.class, XMLType.XSD_STRING);
068: // }
069: return paramMapper;
070: }
071:
072: public String getDefaultUrlAndPort() {
073: return DEFAULT_URL_AND_PORT;
074: }
075:
076: public InvocationStrategy getDefaultInvocationStrategy() {
077: return new UrlPlusClassNameInvocationStrategy();
078: }
079:
080: public static QName getXMLTypeByClass(Class pvClass) {
081: QName lvRetQName = (QName) paramMapper.get(pvClass);
082: if (lvRetQName == null) {
083: lvRetQName = Util.createQNameByClass(pvClass);
084: paramMapper.put(pvClass, lvRetQName);
085: }
086: return lvRetQName;
087: }
088:
089: public Object execute(Class pvProxyClass, Object pvProxy,
090: Method pvMethod, Object[] pvArgs) throws Exception {
091: if (service == null) {
092: serviceFactory = getProperties().getProperty(
093: ServiceFactory.SERVICEFACTORY_PROPERTY,
094: DEFAULT_SERVICE_FACTORY);
095: System.setProperty(ServiceFactory.SERVICEFACTORY_PROPERTY,
096: serviceFactory);
097: service = (Service) ServiceFactory.newInstance()
098: .createService(null);
099:
100: // --- register Serializer and Deserializer ---
101: TypeMapping lvTypeMapping = service
102: .getTypeMappingRegistry().getDefaultTypeMapping();
103:
104: Map lvSorterMap = Util.getAllPropertiesByPrefixAndSort(
105: getProperties(),
106: TypeMappingFactory.PROPERTY_TYPE_MAPPING_FACTORY);
107: Iterator it = lvSorterMap.values().iterator();
108: while (it.hasNext()) {
109: String lvValue = (String) it.next();
110: String lvValues[] = lvValue.split(",");
111: Class lvTypeMappingFactoryClass = Class
112: .forName(lvValues[0]);
113: TypeMappingFactory lvMappingFactory = (TypeMappingFactory) lvTypeMappingFactoryClass
114: .newInstance();
115: lvMappingFactory.setParameter(lvValues);
116:
117: if (lvTypeMapping.isRegistered(lvMappingFactory
118: .getJavaType(), lvMappingFactory.getXmlType()) == false) {
119: lvTypeMapping.register(lvMappingFactory
120: .getJavaType(), lvMappingFactory
121: .getXmlType(), lvMappingFactory
122: .getSerializerFactory(), lvMappingFactory
123: .getDeserializerFactory());
124:
125: // paramMapper.put(lvMappingFactory.getJavaType(),lvMappingFactory.getXmlType());
126: }
127:
128: }
129:
130: call = service.createCall();
131:
132: String lvUser = getProperties().getProperty(
133: Property.SECURITY_USER);
134: String lvPassWd = getProperties().getProperty(
135: Property.SECURITY_PASSWD);
136: if ((lvUser != null) && (lvPassWd != null)) {
137: call.setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY,
138: lvUser);
139: call.setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY,
140: lvPassWd);
141: }
142:
143: String lvUrl = getInvocationStrategy().toString();
144: if (log.isDebugEnabled()) {
145: log.debug("Invoke with URL: " + lvUrl);
146: }
147: // System.out.println("URL: " + lvUrl);
148: call.setTargetEndpointAddress(lvUrl);
149: }
150:
151: call.removeAllParameters();
152: call.setOperationName(new QName(pvMethod.getName()));
153:
154: // if (pvMethod != null && pvMethod.getParameterTypes() != null) {
155: for (int i = 0; i < pvMethod.getParameterTypes().length; i++) {
156: Class lvArgClass = pvMethod.getParameterTypes()[i];
157: if (lvArgClass.equals(Object.class)) {
158: lvArgClass = pvArgs[i].getClass();
159: }
160: QName lvParamXmlType = getXMLTypeByClass(lvArgClass);
161: call.addParameter("arg" + i, lvParamXmlType, lvArgClass,
162: ParameterMode.IN);
163: }
164: // }
165:
166: Class lvReturnType = pvMethod.getReturnType();
167: QName lvXmlReturnType = getXMLTypeByClass(lvReturnType);
168: call.setReturnType(lvXmlReturnType);
169:
170: Object result = null;
171: try {
172: result = call.invoke(pvArgs);
173: } catch (Exception e) {
174: Throwable lvThrowable = Util.findDeepestThrowable(e);
175: throw (Exception) lvThrowable;
176: }
177:
178: return result;
179: }
180:
181: }
|