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  package com.eviware.soapui.impl.wsdl.actions.iface;
13  
14  import hermes.Domain;
15  import hermes.Hermes;
16  import hermes.HermesContext;
17  import hermes.JAXBHermesLoader;
18  import hermes.config.DestinationConfig;
19  
20  import java.io.IOException;
21  import java.net.MalformedURLException;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.naming.Context;
27  import javax.naming.NamingException;
28  
29  import com.eviware.soapui.SoapUI;
30  import com.eviware.soapui.impl.support.AbstractInterface;
31  import com.eviware.soapui.impl.wsdl.WsdlProject;
32  import com.eviware.soapui.impl.wsdl.submit.transports.jms.JMSEndpoint;
33  import com.eviware.soapui.impl.wsdl.submit.transports.jms.util.HermesUtils;
34  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
35  import com.eviware.soapui.support.UISupport;
36  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
37  import com.eviware.x.form.XForm;
38  import com.eviware.x.form.XFormDialog;
39  import com.eviware.x.form.XFormDialogBuilder;
40  import com.eviware.x.form.XFormFactory;
41  import com.eviware.x.form.XFormField;
42  import com.eviware.x.form.XFormFieldListener;
43  
44  public class AddJMSEndpointAction extends AbstractSoapUIAction<AbstractInterface<?>>
45  {
46  	public static final String SOAPUI_ACTION_ID = "AddJMSEndpointAction";
47  	private static final String SESSION = "Session";
48  	private static final String HERMES_CONFIG = "Hermes Config";
49  	private static final String SEND = "Send/Publish destination";
50  	private static final String RECEIVE = "Receive/Subscribe destination";
51  	private XForm mainForm;
52  	List<Destination> destinationNameList;
53  	
54  
55  	public AddJMSEndpointAction()
56  	{
57  		super("Add JMS endpoint", "Wizard for creating JMS endpoint");
58  	}
59  
60  	public void perform(AbstractInterface<?> iface, Object param)
61  	{
62  
63  		XFormDialog dialog = buildDialog(iface);
64  
65  		initValues(iface);
66  
67  		if (dialog.show())
68  		{
69  			String session = dialog.getValue(SESSION);
70  			int i = dialog.getValueIndex(SEND);
71  			if (i == -1)
72  			{
73  				UISupport.showErrorMessage("Not supported endpoint");
74  				return;
75  			}
76  			String send = destinationNameList.get(i).getDestinationName();
77  			int j = dialog.getValueIndex(RECEIVE);
78  			if (j == -1)
79  			{
80  				UISupport.showErrorMessage("Not supported endpoint");
81  				return;
82  			}
83  			String receive = destinationNameList.get(j).getDestinationName();
84  			if (JMSEndpoint.JMS_EMPTY_DESTIONATION.equals(send) && JMSEndpoint.JMS_EMPTY_DESTIONATION.equals(receive))
85  			{
86  				UISupport.showErrorMessage("Not supported endpoint");
87  				return;
88  			}
89  			
90  
91  			iface.addEndpoint(createEndpointString(session, send, receive));
92  		}
93  	}
94  
95  	private String createEndpointString(String session, String send, String receive)
96  	{
97  		StringBuilder sb = new StringBuilder(JMSEndpoint.JMS_ENDPIONT_PREFIX);
98  		sb.append(session + JMSEndpoint.JMS_ENDPOINT_SEPARATOR);
99  		sb.append(send);
100 		if (!JMSEndpoint.JMS_EMPTY_DESTIONATION.equals(receive))
101 			sb.append(JMSEndpoint.JMS_ENDPOINT_SEPARATOR + receive);
102 		return sb.toString();
103 	}
104 
105 	private String[] getSessionOptions(AbstractInterface<?> iface, String hermesConfigPath)
106 	{
107 
108 		List<Hermes> hermesList = new ArrayList<Hermes>();
109 		try
110 		{
111 			Context ctx = getHermesContext(iface, hermesConfigPath);
112 			JAXBHermesLoader loader = (JAXBHermesLoader) ctx.lookup(HermesContext.LOADER);
113 			hermesList = loader.load();
114 		}
115 		catch (Exception e)
116 		{
117 			SoapUI.logError(e);
118 			SoapUI.log.warn("no HermesJMS context!");
119 		}
120 		List<String> hermesSessionList = new ArrayList<String>();
121 		for (Hermes h : hermesList)
122 		{
123 			if (!h.getSessionConfig().getId().equals("<new>"))
124 				hermesSessionList.add(h.getSessionConfig().getId());
125 		}
126 		return hermesSessionList.toArray(new String[hermesSessionList.size()]);
127 	}
128 
129 	private void initValues(AbstractInterface<?> iface)
130 	{
131 		String hermesConfigPath = PropertyExpander.expandProperties(iface, iface.getProject().getHermesConfig());
132 		mainForm.getComponent(HERMES_CONFIG).setValue(hermesConfigPath);
133 
134 		String[] sessionOptions = getSessionOptions(iface, hermesConfigPath);
135 
136 		mainForm.setOptions(SESSION, sessionOptions);
137 		Context ctx = null;
138 		try
139 		{
140 			ctx = getHermesContext(iface, hermesConfigPath);
141 		}
142 		catch (Exception e)
143 		{
144 			SoapUI.log.info("no hermes context");
145 		}
146 		Hermes hermes = null;
147 		try
148 		{
149 			if (sessionOptions != null && sessionOptions.length > 0)
150 			{
151 				hermes = (Hermes) ctx.lookup(sessionOptions[0]);
152 			}
153 
154 			if (hermes != null)
155 			{
156 				updateDestinations(hermes);
157 			}
158 		}
159 		catch (NamingException e)
160 		{
161 			SoapUI.logError(e);
162 		}
163 
164 	}
165 
166 	private void updateDestinations(Hermes hermes)
167 	{
168 		destinationNameList = new ArrayList<Destination>();
169 		destinationNameList.add(new Destination(JMSEndpoint.JMS_EMPTY_DESTIONATION, Domain.UNKNOWN));
170 		extractDestinations(hermes, destinationNameList);
171 		mainForm.setOptions(SEND, destinationNameList.toArray());
172 		mainForm.setOptions(RECEIVE, destinationNameList.toArray());
173 	}
174 
175 	private Context getHermesContext(AbstractInterface<?> iface, String hermesConfigPath) throws MalformedURLException,
176 			NamingException, IOException
177 	{
178 		WsdlProject project = iface.getProject();
179 		HermesUtils.flushHermesCache();
180 		Context ctx = HermesUtils.hermesContext(project, hermesConfigPath);
181 		return ctx;
182 
183 	}
184 
185 	protected XFormDialog buildDialog(final AbstractInterface<?> iface)
186 	{
187 		if (iface == null)
188 			return null;
189 
190 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Add JMS endpoint");
191 
192 		mainForm = builder.createForm("Basic");
193 		mainForm.addTextField(HERMES_CONFIG, "choose folder where hermes-config.xml is", XForm.FieldType.FOLDER)
194 				.addFormFieldListener(new XFormFieldListener()
195 				{
196 					public void valueChanged(XFormField sourceField, String newValue, String oldValue)
197 					{
198 						if (!"".equals(newValue))
199 						{
200 							Hermes hermes = null;
201 							try
202 							{
203 								Context ctx = getHermesContext(iface, newValue);
204 								iface.getProject().setHermesConfig(newValue);
205 								String[] sessions = getSessionOptions(iface, newValue);
206 								mainForm.setOptions(SESSION, sessions);
207 								if (sessions != null && sessions.length > 0)
208 								{
209 									hermes = (Hermes) ctx.lookup(sessions[0]);
210 								}
211 							}
212 							catch (Exception e)
213 							{
214 								SoapUI.logError(e);
215 							}
216 							if (hermes != null)
217 							{
218 								updateDestinations(hermes);
219 							}
220 							else
221 							{
222 								mainForm.setOptions(SESSION, new String[] {});
223 								mainForm.setOptions(SEND, new String[] {});
224 								mainForm.setOptions(RECEIVE, new String[] {});
225 							}
226 						}
227 					}
228 				});
229 		mainForm.addComboBox(SESSION, new String[] {}, "Session name from HermesJMS").addFormFieldListener(
230 				new XFormFieldListener()
231 				{
232 
233 					public void valueChanged(XFormField sourceField, String newValue, String oldValue)
234 					{
235 						String hermesConfigPath = mainForm.getComponent(HERMES_CONFIG).getValue();
236 
237 						Hermes hermes = null;
238 						try
239 						{
240 							Context ctx = getHermesContext(iface, hermesConfigPath);
241 							hermes = (Hermes) ctx.lookup(newValue);
242 						}
243 						catch (Exception e)
244 						{
245 							SoapUI.logError(e);
246 						}
247 						if (hermes != null)
248 						{
249 							updateDestinations(hermes);
250 						}
251 						else
252 						{
253 							mainForm.setOptions(SEND, new String[] {});
254 							mainForm.setOptions(RECEIVE, new String[] {});
255 						}
256 					}
257 
258 				});
259 		mainForm.addComboBox(SEND, new String[] {}, "Queue/Topic  sending/publishing");
260 		mainForm.addComboBox(RECEIVE, new String[] {}, "Queue/Topic  receive/subscribe");
261 
262 		return builder
263 				.buildDialog(builder.buildOkCancelActions(), "create JMS endpoint by selecting proper values", null);
264 	}
265 
266 	private void extractDestinations(Hermes hermes, List<Destination> destinationList)
267 	{
268 		Iterator<?> hermesDestionations = hermes.getDestinations();
269 		while (hermesDestionations.hasNext())
270 		{
271 			DestinationConfig dest = (DestinationConfig) hermesDestionations.next();
272 			Destination temp = new Destination(dest.getName(), Domain.getDomain(dest.getDomain()));
273 			destinationList.add(temp);
274 		}
275 	}
276 
277 	private class Destination
278 	{
279 		public Destination(String destinationName, Domain domain)
280 		{
281 			this.domain = domain;
282 			if (destinationName.equals(JMSEndpoint.JMS_EMPTY_DESTIONATION) || destinationName.equals(""))
283 			{
284 				this.destinationName = destinationName;
285 			}
286 			else
287 			{
288 				if (domain.equals(Domain.QUEUE))
289 				{
290 					this.destinationName = JMSEndpoint.QUEUE_ENDPOINT_PREFIX + destinationName;
291 				}
292 				else
293 				{
294 					this.destinationName = JMSEndpoint.TOPIC_ENDPOINT_PREFIX + destinationName;
295 				}
296 			}
297 
298 		}
299 
300 		private String destinationName;
301 		private Domain domain;
302 
303 		public String getDestinationName()
304 		{
305 			return destinationName;
306 		}
307 
308 		public Domain getDomain()
309 		{
310 			return domain;
311 		}
312 
313 		public String toString()
314 		{
315 			return this.getDestinationName().replace(JMSEndpoint.QUEUE_ENDPOINT_PREFIX, "").replace(JMSEndpoint.TOPIC_ENDPOINT_PREFIX, "");
316 		}
317 	}
318 
319 	
320 }