View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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 java.util.Arrays;
16  import java.util.HashSet;
17  import java.util.List;
18  import java.util.Set;
19  
20  import com.eviware.soapui.SoapUI;
21  import com.eviware.soapui.config.EndpointsConfig;
22  import com.eviware.soapui.config.InterfaceConfig;
23  import com.eviware.soapui.impl.support.definition.support.AbstractDefinitionContext;
24  import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
25  import com.eviware.soapui.impl.wsdl.WsdlProject;
26  import com.eviware.soapui.model.ModelItem;
27  import com.eviware.soapui.model.iface.Interface;
28  import com.eviware.soapui.model.iface.InterfaceListener;
29  import com.eviware.soapui.model.iface.Operation;
30  import com.eviware.soapui.model.iface.Request;
31  
32  public abstract class AbstractInterface<T extends InterfaceConfig> extends AbstractWsdlModelItem<T> implements
33  		Interface
34  {
35  	private Set<InterfaceListener> interfaceListeners = new HashSet<InterfaceListener>();
36  
37  	protected AbstractInterface( T config, ModelItem parent, String icon )
38  	{
39  		super( config, parent, icon );
40  
41  		if( config.getEndpoints() == null )
42  			config.addNewEndpoints();
43  
44  		for( InterfaceListener listener : SoapUI.getListenerRegistry().getListeners( InterfaceListener.class ) )
45  		{
46  			addInterfaceListener( listener );
47  		}
48  
49  		if( !config.isSetDefinitionCache() )
50  			config.addNewDefinitionCache();
51  	}
52  
53  	public WsdlProject getProject()
54  	{
55  		return ( WsdlProject )getParent();
56  	}
57  
58  	public T getConfig()
59  	{
60  		return super.getConfig();
61  	}
62  
63  	public List<? extends ModelItem> getChildren()
64  	{
65  		return getOperationList();
66  	}
67  
68  	public String[] getEndpoints()
69  	{
70  		EndpointsConfig endpoints = getConfig().getEndpoints();
71  		List<String> endpointArray = endpoints.getEndpointList();
72  		return endpointArray.toArray( new String[endpointArray.size()] );
73  	}
74  
75  	public void addEndpoint( String endpoint )
76  	{
77  		if( endpoint == null || endpoint.trim().length() == 0 )
78  			return;
79  
80  		endpoint = endpoint.trim();
81  		String[] endpoints = getEndpoints();
82  
83  		// dont add the same endpoint twice
84  		if( Arrays.asList( endpoints ).contains( endpoint ) )
85  			return;
86  
87  		getConfig().getEndpoints().addNewEndpoint().setStringValue( endpoint );
88  
89  		notifyPropertyChanged( ENDPOINT_PROPERTY, null, endpoint );
90  	}
91  
92  	public void changeEndpoint( String oldEndpoint, String newEndpoint )
93  	{
94  		if( oldEndpoint == null || oldEndpoint.trim().length() == 0 )
95  			return;
96  		if( newEndpoint == null || newEndpoint.trim().length() == 0 )
97  			return;
98  
99  		EndpointsConfig endpoints = getConfig().getEndpoints();
100 
101 		for( int c = 0; c < endpoints.sizeOfEndpointArray(); c++ )
102 		{
103 			if( endpoints.getEndpointArray( c ).equals( oldEndpoint ) )
104 			{
105 				endpoints.setEndpointArray( c, newEndpoint );
106 				notifyPropertyChanged( ENDPOINT_PROPERTY, oldEndpoint, newEndpoint );
107 				break;
108 			}
109 		}
110 	}
111 
112 	public void removeEndpoint( String endpoint )
113 	{
114 		EndpointsConfig endpoints = getConfig().getEndpoints();
115 
116 		for( int c = 0; c < endpoints.sizeOfEndpointArray(); c++ )
117 		{
118 			if( endpoints.getEndpointArray( c ).equals( endpoint ) )
119 			{
120 				endpoints.removeEndpoint( c );
121 				notifyPropertyChanged( ENDPOINT_PROPERTY, endpoint, null );
122 				break;
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 	@SuppressWarnings( "unchecked" )
196 	public abstract AbstractDefinitionContext getDefinitionContext();
197 
198 	/***
199 	 * Return the URL for the current definition (ie a WSDL or WADL url)
200 	 */
201 
202 	public abstract String getDefinition();
203 
204 	public abstract String getType();
205 
206 	public abstract boolean isDefinitionShareble();
207 
208 	public Operation[] getAllOperations()
209 	{
210 		return getOperationList().toArray( new Operation[getOperationCount()] );
211 	}
212 }