1 /* 2 * Copyright 2007 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.ws.server.endpoint.adapter; 18 19 import java.lang.reflect.Method; 20 21 import org.springframework.oxm.GenericMarshaller; 22 import org.springframework.oxm.GenericUnmarshaller; 23 import org.springframework.oxm.Marshaller; 24 import org.springframework.oxm.Unmarshaller; 25 import org.springframework.oxm.jaxb.Jaxb2Marshaller; 26 import org.springframework.ws.server.endpoint.MethodEndpoint; 27 28 /** 29 * Subclass of {@link MarshallingMethodEndpointAdapter} that supports {@link GenericMarshaller} and {@link 30 * GenericUnmarshaller}. More specifically, this adapter is aware of the {@link Method#getGenericParameterTypes()} and 31 * {@link Method#getGenericReturnType()}. 32 * <p/> 33 * Prefer to use this adapter rather than the plain {@link MarshallingMethodEndpointAdapter} in combination with Java 5 34 * marshallers, such as the {@link Jaxb2Marshaller}. 35 * 36 * @author Arjen Poutsma 37 * @since 1.0.2 38 */ 39 public class GenericMarshallingMethodEndpointAdapter extends MarshallingMethodEndpointAdapter { 40 41 /** 42 * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code>. The {@link Marshaller} and {@link 43 * Unmarshaller} must be injected using properties. 44 * 45 * @see #setMarshaller(org.springframework.oxm.Marshaller) 46 * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller) 47 */ 48 public GenericMarshallingMethodEndpointAdapter() { 49 } 50 51 /** 52 * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code> with the given marshaller. If the given {@link 53 * Marshaller} also implements the {@link Unmarshaller} interface, it is used for both marshalling and 54 * unmarshalling. Otherwise, an exception is thrown. 55 * <p/> 56 * Note that all {@link Marshaller} implementations in Spring-WS also implement the {@link Unmarshaller} interface, 57 * so that you can safely use this constructor. 58 * 59 * @param marshaller object used as marshaller and unmarshaller 60 * @throws IllegalArgumentException when <code>marshaller</code> does not implement the {@link Unmarshaller} 61 * interface 62 */ 63 public GenericMarshallingMethodEndpointAdapter(Marshaller marshaller) { 64 super(marshaller); 65 } 66 67 /** 68 * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code> with the given marshaller and unmarshaller. 69 * 70 * @param marshaller the marshaller to use 71 * @param unmarshaller the unmarshaller to use 72 */ 73 public GenericMarshallingMethodEndpointAdapter(Marshaller marshaller, Unmarshaller unmarshaller) { 74 super(marshaller, unmarshaller); 75 } 76 77 protected boolean supportsInternal(MethodEndpoint methodEndpoint) { 78 Method method = methodEndpoint.getMethod(); 79 return supportsReturnType(method) && supportsParameters(method); 80 } 81 82 private boolean supportsReturnType(Method method) { 83 if (Void.TYPE.equals(method.getReturnType())) { 84 return true; 85 } 86 else { 87 if (getMarshaller() instanceof GenericMarshaller) { 88 return ((GenericMarshaller) getMarshaller()).supports(method.getGenericReturnType()); 89 } 90 else { 91 return getMarshaller().supports(method.getReturnType()); 92 } 93 } 94 } 95 96 private boolean supportsParameters(Method method) { 97 if (method.getParameterTypes().length != 1) { 98 return false; 99 } 100 else if (getUnmarshaller() instanceof GenericUnmarshaller) { 101 GenericUnmarshaller genericUnmarshaller = (GenericUnmarshaller) getUnmarshaller(); 102 return genericUnmarshaller.supports(method.getGenericParameterTypes()[0]); 103 } 104 else { 105 return getUnmarshaller().supports(method.getParameterTypes()[0]); 106 } 107 } 108 }