Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8b909382a599cdc4f61b5b05f1d74a0af20ed93e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.wsdl.traversal;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import javax.wsdl.Binding;
import javax.wsdl.BindingFault;
import javax.wsdl.BindingInput;
import javax.wsdl.BindingOperation;
import javax.wsdl.BindingOutput;
import javax.wsdl.Definition;
import javax.wsdl.Fault;
import javax.wsdl.Import;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Output;
import javax.wsdl.Part;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.Types;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.wsdl.extensions.soap.SOAPBody;
import javax.wsdl.extensions.soap.SOAPFault;
import javax.wsdl.extensions.soap.SOAPHeader;
import javax.wsdl.extensions.soap.SOAPHeaderFault;
import javax.wsdl.extensions.soap.SOAPOperation;

import org.w3c.dom.Element;

/**
 * The class adapts the given object with the implemented
 * <code>WSDLVisitor</code> methods.
 * 
 * @author Kulik
 */
public class VisitorAdaptor implements InvocationHandler
{

  // optimization hint
  static final private Class[] visitorClass = new Class[] { WSDLVisitor.class };

  /**
   * The array is used for determination of actual javax.wsdl class
   */
  private static Class[] wsdl =
    new Class[] {
      Part.class,
      Service.class,
      Types.class,
      Operation.class,
      Input.class,
      Output.class,
      Fault.class,
      Binding.class,
      BindingOperation.class,
      BindingInput.class,
      BindingOutput.class,
      BindingFault.class,
      Import.class,
      Element.class,
      Message.class,
      Port.class,
      PortType.class,
      Definition.class,
      ExtensibilityElement.class,
      SOAPBinding.class,
      SOAPBody.class,
      SOAPHeader.class,
      SOAPHeaderFault.class,
      SOAPFault.class,
      SOAPOperation.class };

  final private Object visitor;

  private Map methods = new HashMap();

  /**
   * Constructor.
   * @param o  a visitor object.
   */
  private VisitorAdaptor(Object o)
  {
    this.visitor = o;
  }

  /**
   * Adds method to method binding.
   * @param wsdlMethod    a WSDL method.
   * @param targetMethod  a  target method.
   */
  private void addMethodBinding(Method wsdlMethod, Method targetMethod)
  {
    methods.put(wsdlMethod, targetMethod);
  }

  /**
   * The method proxies all "visit(XXX)" methods to the corresponding
   * "visit(XXX)" methods of the target visitor object. 
   * @see java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
   */
  public Object invoke(Object proxy, Method m, Object[] params)
  {
    try
    {
      Method target = (Method) methods.get(m);
      // assert target != null
      return target.invoke(visitor, params);
    }
    catch (Throwable t)
    {
      t.printStackTrace();
    }
    return null;
  }

  /**
   * The method extract the short class name. 
   * @param c  a class.
   * @return the short class name.
   */
  static private String getName(Class c)
  {
    if (c != null)
    {
      String name = c.getName();
      return name.substring(name.lastIndexOf(".") + 1);
    }
    return null;
  }

  /**
   * The method generates proxy for the given visitor.
   * Proxy redirects all visit(XXX) callback to the implemented in visitor
   * object.
   * @param visitor  a vistor object.
   * @return a proxy for the given visitor.
   */
  public static WSDLTraversal adapt(Object visitor)
  {
    WSDLTraversal traversal = new WSDLTraversal();

    if (visitor == null)
      throw new IllegalArgumentException("Visitor object can not be NULL");

    VisitorAdaptor adaptor = new VisitorAdaptor(visitor);

    // check whether methods are implemented
    // get real class of the object
    Class c = visitor.getClass();
    // iterates through wsdl artifacts and looking for
    // void visit(XXX) implemented methods.  
    for (int i = 0; i < wsdl.length; i++)
      try
      {
        // if method is not found the exception will be thrown
        Method m =
          c.getMethod(
            "visit",
            new Class[] { wsdl[i], Object.class, WSDLTraversalContext.class });
        // register binding WSDLVisitor method -> target visitor method
        adaptor.addMethodBinding(
          WSDLVisitor.class.getMethod(
            "visit",
            new Class[] { wsdl[i], Object.class, WSDLTraversalContext.class }),
          m);
        // register visitXXX in WSDLTraversalBuilder
        m =
          WSDLTraversal.class.getMethod(
            "visit" + getName(wsdl[i]),
            new Class[] { boolean.class });
        m.invoke(traversal, new Object[] { Boolean.TRUE });
      }
      catch (Exception e)
      {
      }

    // construct the WSDLVisitor by using java.lang.reflect.Proxy
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    WSDLVisitor wsdlVisitor =
      (WSDLVisitor) Proxy.newProxyInstance(loader, visitorClass, adaptor);
    // traverse WSDL document
    traversal.setVisitor(wsdlVisitor);
    return traversal;
  }
}

Back to the top