View Javadoc

1   /*
2    * Copyright 2005-2010 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 [email protected] MarshallingMethodEndpointAdapter} that supports [email protected] GenericMarshaller} and [email protected]
30   * GenericUnmarshaller}. More specifically, this adapter is aware of the [email protected] Method#getGenericParameterTypes()} and
31   * [email protected] Method#getGenericReturnType()}.
32   * <p/>
33   * Prefer to use this adapter rather than the plain [email protected] MarshallingMethodEndpointAdapter} in combination with Java 5
34   * marshallers, such as the [email protected] Jaxb2Marshaller}.
35   *
36   * @author Arjen Poutsma
37   * @since 1.0.2
38   * @deprecated as of Spring Web Services 2.0, in favor of [email protected] DefaultMethodEndpointAdapter} and [email protected]
39   *             org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor
40   *             MarshallingPayloadMethodProcessor}.
41   */
42  @Deprecated
43  public class GenericMarshallingMethodEndpointAdapter extends MarshallingMethodEndpointAdapter {
44  
45      /**
46       * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code>. The [email protected] Marshaller} and [email protected]
47       * Unmarshaller} must be injected using properties.
48       *
49       * @see #setMarshaller(org.springframework.oxm.Marshaller)
50       * @see #setUnmarshaller(org.springframework.oxm.Unmarshaller)
51       */
52      public GenericMarshallingMethodEndpointAdapter() {
53      }
54  
55      /**
56       * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code> with the given marshaller. If the given [email protected]
57       * Marshaller} also implements the [email protected] Unmarshaller} interface, it is used for both marshalling and
58       * unmarshalling. Otherwise, an exception is thrown.
59       * <p/>
60       * Note that all [email protected] Marshaller} implementations in Spring-WS also implement the [email protected] Unmarshaller} interface,
61       * so that you can safely use this constructor.
62       *
63       * @param marshaller object used as marshaller and unmarshaller
64       * @throws IllegalArgumentException when <code>marshaller</code> does not implement the [email protected] Unmarshaller}
65       *                                  interface
66       */
67      public GenericMarshallingMethodEndpointAdapter(Marshaller marshaller) {
68          super(marshaller);
69      }
70  
71      /**
72       * Creates a new <code>GenericMarshallingMethodEndpointAdapter</code> with the given marshaller and unmarshaller.
73       *
74       * @param marshaller   the marshaller to use
75       * @param unmarshaller the unmarshaller to use
76       */
77      public GenericMarshallingMethodEndpointAdapter(Marshaller marshaller, Unmarshaller unmarshaller) {
78          super(marshaller, unmarshaller);
79      }
80  
81      @Override
82      protected boolean supportsInternal(MethodEndpoint methodEndpoint) {
83          Method method = methodEndpoint.getMethod();
84          return supportsReturnType(method) && supportsParameters(method);
85      }
86  
87      private boolean supportsReturnType(Method method) {
88          if (Void.TYPE.equals(method.getReturnType())) {
89              return true;
90          }
91          else {
92              if (getMarshaller() instanceof GenericMarshaller) {
93                  return ((GenericMarshaller) getMarshaller()).supports(method.getGenericReturnType());
94              }
95              else {
96                  return getMarshaller().supports(method.getReturnType());
97              }
98          }
99      }
100 
101     private boolean supportsParameters(Method method) {
102         if (method.getParameterTypes().length != 1) {
103             return false;
104         }
105         else if (getUnmarshaller() instanceof GenericUnmarshaller) {
106             GenericUnmarshaller genericUnmarshaller = (GenericUnmarshaller) getUnmarshaller();
107             return genericUnmarshaller.supports(method.getGenericParameterTypes()[0]);
108         }
109         else {
110             return getUnmarshaller().supports(method.getParameterTypes()[0]);
111         }
112     }
113 }