Skip to main content
summaryrefslogtreecommitdiffstats
blob: 504f1e5c6ec8f048859d0beed9adeac4dd7e1dd9 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
 * Copyright (c) 2004 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 Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.ui.widgets;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jst.j2ee.webservice.wsdd.Handler;
import org.eclipse.jst.j2ee.webservice.wsdd.PortComponent;
import org.eclipse.jst.j2ee.webservice.wsdd.WebServiceDescription;
import org.eclipse.jst.j2ee.webservice.wsdd.WsddFactory;
import org.eclipse.jst.j2ee.webservice.wsdd.WsddResource;
import org.eclipse.jst.j2ee.webservice.wsdd.internal.impl.WsddFactoryImpl;
import org.eclipse.jst.ws.internal.consumption.ui.widgets.object.HandlerTableItem;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.common.internal.emf.utilities.EtoolsCopyUtility;

/*
 * Provide a way to externalize the edited fields and create new handlers
 *  
 */
public class ServiceHandlersWidgetOutputCommand extends AbstractDataModelOperation
{

  private WsddResource wsddResource_;
  private Hashtable wsDescToHandlers_;
  private Hashtable serviceDescNameToDescObj_;

  public IStatus execute( IProgressMonitor monitor, IAdaptable adaptable )
  {
    IStatus status = Status.OK_STATUS;
    try {
      Enumeration e = wsDescToHandlers_.keys();
      while (e.hasMoreElements()) {

        Hashtable wsPortsTable = new Hashtable();
        Hashtable wsPortToHandlerTable = new Hashtable();

        String serviceDescName = (String) e.nextElement();
        // load PortName -> PortComponent table
        WebServiceDescription wsDescription_ = (WebServiceDescription) serviceDescNameToDescObj_.get(serviceDescName);
        if (wsDescription_ != null) {

          List wsPorts = wsDescription_.getPortComponents();
          for (int k = 0; k < wsPorts.size(); k++) {
            PortComponent port = (PortComponent) wsPorts.get(k);
            wsPortsTable.put(port.getPortComponentName(), port);
            wsPortToHandlerTable.put(port.getPortComponentName(), new ArrayList());
          }
        }

        List handlerTableItems = (List) wsDescToHandlers_.get(serviceDescName);
        if (handlerTableItems != null) {

          WsddFactory wsddFactory = new WsddFactoryImpl();

          //form port components tables first
          for (int i = 0; i < handlerTableItems.size(); i++) {

            HandlerTableItem hti = (HandlerTableItem) handlerTableItems.get(i);
            PortComponent wsPort = (PortComponent) hti.getPort();
            if (wsPort == null) {
              //find it
              if (wsPortsTable.get(hti.getPortName()) != null)
                wsPort = (PortComponent) wsPortsTable.get(hti.getPortName());
              else {
                // create it; should not get into here
                wsPort = wsddFactory.createPortComponent();
                wsPort.setPortComponentName(hti.getPortName());
                wsPortsTable.put(wsPort.getPortComponentName(), wsPort);
              }
            }
            wsPortToHandlerTable.put(wsPort.getPortComponentName(), new ArrayList());
          }

          // form Handler and ports table
          for (int i = 0; i < handlerTableItems.size(); i++) {

            HandlerTableItem hti = (HandlerTableItem) handlerTableItems.get(i);
            String portName = hti.getPortName();

            PortComponent port = (PortComponent) wsPortsTable.get(portName);
            if (port == null) return status;

            List existingHandlers = port.getHandlers();

            Object handler = hti.getHandler();
            if (!existingHandlers.contains(handler)) {
              // create it
              Handler newHandler = wsddFactory.createHandler();
              newHandler.setHandlerName(hti.getHandlerName());
              newHandler.setHandlerClass(hti.getHandlerClassName());
              ((List) wsPortToHandlerTable.get(portName)).add(newHandler);

            }
            else if (handler instanceof Handler) {
              // clone it
              EtoolsCopyUtility copyUtil = new EtoolsCopyUtility();
              copyUtil.setCopyAdapters(true);
              Handler clonedHandler = (Handler) copyUtil.copy((Handler) handler);
              ((List) wsPortToHandlerTable.get(portName)).add(clonedHandler);
            }
          }

        }

        // remove existing handlers from ports
        removeExistingHandlers(wsPortsTable, wsPortToHandlerTable);

        // add ports to wsDescription
        addPortsToDescriptions(wsPortsTable, wsDescription_);

        // add handlers to ports
        addHandlersToPorts(wsPortsTable, wsPortToHandlerTable);
      }
      if (wsddResource_ != null) {
        wsddResource_.save(new HashMap());
      }

    }
    catch (Exception e) {
      //status = new
      e.printStackTrace();
    }
    return status;
  }

  private void removeExistingHandlers(Hashtable portsTable, Hashtable portsToHandlersTable) {
    try {
      Enumeration ports = portsToHandlersTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        Object port = portsTable.get(portName);
        if (port != null && port instanceof PortComponent) {
          List handlers = ((PortComponent) port).getHandlers();
          handlers.clear();
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void addPortsToDescriptions(Hashtable portsTable, WebServiceDescription wsDescription_) {
    try {
      Enumeration ports = portsTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        // newly edited port
        Object port = portsTable.get(portName);
        
        // existing port
        List emfPorts = wsDescription_.getPortComponents();
        if (!emfPorts.contains(port) && port != null && port instanceof PortComponent) {
          emfPorts.add((PortComponent) port);
        }
      }
    }

    catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void addHandlersToPorts(Hashtable portsTable, Hashtable portsToHandlersTable) {
    try {
      Enumeration ports = portsToHandlersTable.keys();
      while (ports.hasMoreElements()) {
        String portName = (String) ports.nextElement();
        Object port = portsTable.get(portName);
        if (port != null && port instanceof PortComponent) {
          List handlers = (List) portsToHandlersTable.get(portName);

          List modelHandlers = ((PortComponent) port).getHandlers();
          modelHandlers.addAll(handlers);
        }
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * @param handlerTableItems
   *          The handlerTableItems to set.
   */
  public void setHandlersList(List handlerTableItems) {
  }

  public void setWsDescToHandlers(Hashtable wsDescToHandlers) {
    this.wsDescToHandlers_ = wsDescToHandlers;
  }

//  public void setWsDescription(WebServiceDescription wsDesc) {
//    this.wsDescription_ = wsDesc;
//  }

  public void setWsddResource(WsddResource wsddRes) {
    this.wsddResource_ = wsddRes;
  }

  public void setServiceDescNameToDescObj(Hashtable serviceDescNameToDescObj) {
    this.serviceDescNameToDescObj_ = serviceDescNameToDescObj;
  }
}

Back to the top