View Javadoc

1   /*
2    * The Seasar Software License, Version 1.1 Copyright (c) 2003-2004 The Seasar
3    * Project. All rights reserved. Redistribution and use in source and binary
4    * forms, with or without modification, are permitted provided that the
5    * following conditions are met: 1. Redistributions of source code must retain
6    * the above copyright notice, this list of conditions and the following
7    * disclaimer. 2. Redistributions in binary form must reproduce the above
8    * copyright notice, this list of conditions and the following disclaimer in the
9    * documentation and/or other materials provided with the distribution. 3. The
10   * end-user documentation included with the redistribution, if any, must include
11   * the following acknowledgement: "This product includes software developed by
12   * the Seasar Project (http://www.seasar.org/)." Alternately, this
13   * acknowledgement may appear in the software itself, if and wherever such
14   * third-party acknowledgements normally appear. 4. Neither the name "The Seasar
15   * Project" nor the names of its contributors may be used to endorse or promote
16   * products derived from this software without specific prior written permission
17   * of the Seasar Project. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED
18   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20   * EVENT SHALL THE SEASAR PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21   * INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24   * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING
25   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  package org.seasar.axis.client;
29  
30  import java.lang.reflect.Method;
31  import java.net.URL;
32  
33  import javax.xml.namespace.QName;
34  import javax.xml.rpc.Call;
35  
36  import org.aopalliance.intercept.MethodInvocation;
37  import org.apache.axis.client.Service;
38  import org.apache.axis.encoding.TypeMappingRegistry;
39  import org.apache.axis.enum.Use;
40  import org.seasar.axis.S2AxisConstants;
41  import org.seasar.axis.encoding.AutoRegisterTypeMappingImpl;
42  import org.seasar.framework.aop.interceptors.AbstractInterceptor;
43  import org.seasar.framework.util.MethodUtil;
44  
45  /***
46   * Javaインタフェースを通じてWebサービスを呼び出すためのインターセプタです。
47   * 
48   * @author koichik
49   */
50  public class DynamicInvocationInterceptor extends AbstractInterceptor {
51      //instance fields
52      protected final Service service;
53      protected final String endPointAddress;
54  
55      /***
56       * インスタンスを構築します。 <br>
57       * ビーンのタイプマッピングを自動化するようにサービスのタイプマッピングレジストリを設定します。
58       * 
59       * @param service
60       *            サービス
61       * @param endPointAddress
62       *            エンドポイントアドレス
63       */
64      public DynamicInvocationInterceptor(final Service service, final String endPointAddress) {
65          this.service = service;
66          this.endPointAddress = endPointAddress;
67  
68          final TypeMappingRegistry tmr = service.getEngine().getTypeMappingRegistry();
69          final AutoRegisterTypeMappingImpl autoTM = new AutoRegisterTypeMappingImpl(null);
70          tmr.register(Use.DEFAULT.getEncoding(), autoTM);
71      }
72  
73      /***
74       * インスタンスを構築します。
75       * 
76       * @param service
77       *            サービス
78       * @param url
79       *            エンドポイントアドレスのURL
80       */
81      public DynamicInvocationInterceptor(final Service service, final URL url) {
82          this(service, url.toString());
83      }
84  
85      /***
86       * ターゲットまたはサービスのメソッドを起動します。 <br>
87       * 呼び出されたメソッドがターゲットで実装されている場合はターゲットのメソッドを呼び出します。
88       * ターゲットで実装されていなければサービスのメソッドを呼び出します。
89       * 
90       * @param invocation
91       *            メソッド起動情報
92       */
93      public Object invoke(final MethodInvocation invocation) throws Throwable {
94          final Method method = invocation.getMethod();
95          if (!MethodUtil.isAbstract(method)) {
96              return invocation.proceed();
97          }
98  
99          final Call call = service.createCall();
100         call.setTargetEndpointAddress(endPointAddress);
101         call.setOperationName(new QName(S2AxisConstants.OPERATION_NAMESPACE_URI, method.getName()));
102         return call.invoke(invocation.getArguments());
103     }
104 }