View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.support;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.config.EndpointsConfig;
17  import com.eviware.soapui.config.InterfaceConfig;
18  import com.eviware.soapui.impl.support.definition.support.AbstractDefinitionContext;
19  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.model.ModelItem;
22  import com.eviware.soapui.model.iface.Interface;
23  import com.eviware.soapui.model.iface.InterfaceListener;
24  import com.eviware.soapui.model.iface.Operation;
25  import com.eviware.soapui.model.iface.Request;
26  
27  import java.util.Arrays;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Set;
31  
32  public abstract class AbstractInterface<T extends InterfaceConfig> extends AbstractWsdlModelItem<T> implements Interface
33  {
34     private Set<InterfaceListener> interfaceListeners = new HashSet<InterfaceListener>();
35  
36     protected AbstractInterface(T config, ModelItem parent, String icon)
37     {
38        super(config, parent, icon);
39  
40        if (config.getEndpoints() == null)
41           config.addNewEndpoints();
42  
43        for (InterfaceListener listener : SoapUI.getListenerRegistry().getListeners(InterfaceListener.class))
44        {
45           addInterfaceListener(listener);
46        }
47  
48        if (!config.isSetDefinitionCache())
49           config.addNewDefinitionCache();
50     }
51  
52     public WsdlProject getProject()
53     {
54        return (WsdlProject) getParent();
55     }
56  
57     public T getConfig()
58     {
59        return super.getConfig();
60     }
61  
62     public List<? extends ModelItem> getChildren()
63     {
64        return getOperationList();
65     }
66  
67     public String[] getEndpoints()
68     {
69        EndpointsConfig endpoints = getConfig().getEndpoints();
70        List<String> endpointArray = endpoints.getEndpointList();
71        return endpointArray.toArray(new String[endpointArray.size()]);
72     }
73  
74     public void addEndpoint(String endpoint)
75     {
76        if (endpoint == null || endpoint.trim().length() == 0)
77           return;
78  
79        endpoint = endpoint.trim();
80        String[] endpoints = getEndpoints();
81  
82        // dont add the same endpoint twice
83        if (Arrays.asList(endpoints).contains(endpoint))
84           return;
85  
86        getConfig().getEndpoints().addNewEndpoint().setStringValue(endpoint);
87  
88        notifyPropertyChanged(ENDPOINT_PROPERTY, null, endpoint);
89     }
90  
91     public void changeEndpoint(String oldEndpoint, String newEndpoint)
92     {
93        if (oldEndpoint == null || oldEndpoint.trim().length() == 0)
94           return;
95        if (newEndpoint == null || newEndpoint.trim().length() == 0)
96           return;
97  
98        EndpointsConfig endpoints = getConfig().getEndpoints();
99  
100       for (int c = 0; c < endpoints.sizeOfEndpointArray(); c++)
101       {
102          if (endpoints.getEndpointArray(c).equals(oldEndpoint))
103          {
104             endpoints.setEndpointArray(c, newEndpoint);
105             notifyPropertyChanged(ENDPOINT_PROPERTY, oldEndpoint, newEndpoint);
106             break;
107          }
108       }
109    }
110 
111    public void removeEndpoint(String endpoint)
112    {
113       EndpointsConfig endpoints = getConfig().getEndpoints();
114 
115       for (int c = 0; c < endpoints.sizeOfEndpointArray(); c++)
116       {
117          if (endpoints.getEndpointArray(c).equals(endpoint))
118          {
119             endpoints.removeEndpoint(c);
120             notifyPropertyChanged(ENDPOINT_PROPERTY, endpoint, null);
121             break;
122          }
123       }
124    }
125 
126 
127    public void fireOperationAdded(Operation operation)
128    {
129       InterfaceListener[] a = interfaceListeners.toArray(new InterfaceListener[interfaceListeners.size()]);
130 
131       for (int c = 0; c < a.length; c++)
132       {
133          a[c].operationAdded(operation);
134       }
135    }
136 
137    public void fireOperationUpdated(Operation operation)
138    {
139       InterfaceListener[] a = interfaceListeners.toArray(new InterfaceListener[interfaceListeners.size()]);
140 
141       for (int c = 0; c < a.length; c++)
142       {
143          a[c].operationUpdated(operation);
144       }
145    }
146 
147    public void fireOperationRemoved(Operation operation)
148    {
149       InterfaceListener[] a = interfaceListeners.toArray(new InterfaceListener[interfaceListeners.size()]);
150 
151       for (int c = 0; c < a.length; c++)
152       {
153          a[c].operationRemoved(operation);
154       }
155    }
156 
157    public void fireRequestAdded(Request request)
158    {
159       InterfaceListener[] a = interfaceListeners.toArray(new InterfaceListener[interfaceListeners.size()]);
160 
161       for (int c = 0; c < a.length; c++)
162       {
163          a[c].requestAdded(request);
164       }
165    }
166 
167    public void fireRequestRemoved(Request request)
168    {
169       InterfaceListener[] a = interfaceListeners.toArray(new InterfaceListener[interfaceListeners.size()]);
170 
171       for (int c = 0; c < a.length; c++)
172       {
173          a[c].requestRemoved(request);
174       }
175    }
176 
177    public void addInterfaceListener(InterfaceListener listener)
178    {
179       interfaceListeners.add(listener);
180    }
181 
182    public void removeInterfaceListener(InterfaceListener listener)
183    {
184       interfaceListeners.remove(listener);
185    }
186 
187    @Override
188    public void release()
189    {
190       super.release();
191 
192       interfaceListeners.clear();
193    }
194 
195    public abstract AbstractDefinitionContext getDefinitionContext();
196 
197    /***
198     * Return the URL for the current definition (ie a WSDL or WADL url)
199     */
200 
201    public abstract String getDefinition();
202 
203    public abstract String getType();
204 
205    public abstract boolean isDefinitionShareble();
206 
207    public Operation [] getAllOperations()
208    {
209       return getOperationList().toArray( new Operation[getOperationCount()] );
210    }
211 }