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.submit.transports.jms.util;
13  
14  import hermes.Hermes;
15  import hermes.HermesInitialContextFactory;
16  import hermes.JAXBHermesLoader;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.net.MalformedURLException;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import javax.naming.Context;
27  import javax.naming.InitialContext;
28  import javax.naming.NamingException;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.actions.SoapUIPreferencesAction;
32  import com.eviware.soapui.impl.wsdl.WsdlProject;
33  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
34  import com.eviware.soapui.settings.ToolsSettings;
35  import com.eviware.soapui.support.HermesJMSClasspathHacker;
36  import com.eviware.soapui.support.Tools;
37  import com.eviware.soapui.support.UISupport;
38  
39  public class HermesUtils
40  {
41  	private static boolean hermesJarsLoaded = false;
42  	private static Map<String, Context> contextMap = new HashMap<String, Context>();
43  	public static String HERMES_CONFIG_XML = "hermes-config.xml";
44  
45  	public static Context hermesContext( WsdlProject project ) throws NamingException, MalformedURLException,
46  			IOException
47  	{
48  		String expandedHermesConfigPath = PropertyExpander.expandProperties( project, project.getHermesConfig() );
49  		String key = project.getName() + expandedHermesConfigPath;
50  		return getHermes( key, expandedHermesConfigPath );
51  	}
52  
53  	public static Context hermesContext( WsdlProject project, String hermesConfigPath ) throws NamingException,
54  			MalformedURLException, IOException
55  	{
56  		String expandedHermesConfigPath = PropertyExpander.expandProperties( project, hermesConfigPath );
57  		String key = project.getName() + expandedHermesConfigPath;
58  		return getHermes( key, expandedHermesConfigPath );
59  	}
60  
61  	private static Context getHermes( String key, String hermesConfigPath ) throws IOException, MalformedURLException,
62  			NamingException
63  	{
64  		if( !hermesJarsLoaded )
65  		{
66  			addHermesJarsToClasspath();
67  			hermesJarsLoaded = true;
68  		}
69  
70  		if( contextMap.containsKey( key ) )
71  		{
72  			return contextMap.get( key );
73  		}
74  
75  		Properties props = new Properties();
76  		props.put( Context.INITIAL_CONTEXT_FACTORY, HermesInitialContextFactory.class.getName() );
77  		props.put( Context.PROVIDER_URL, hermesConfigPath + File.separator + HERMES_CONFIG_XML );
78  		props.put( "hermes.loader", JAXBHermesLoader.class.getName() );
79  
80  		Context ctx = new InitialContext( props );
81  		contextMap.put( key, ctx );
82  		return ctx;
83  	}
84  
85  	private static void addHermesJarsToClasspath() throws IOException, MalformedURLException
86  	{
87  		String hermesHome = SoapUI.getSettings().getString( ToolsSettings.HERMES_JMS, defaultHermesJMSPath() );
88  
89  		if( hermesHome == null || "".equals( hermesHome ) )
90  		{
91  			hermesHome = createHermesHomeSetting();
92  			if( hermesHome == null )
93  				throw new FileNotFoundException( "HermesJMS home not specified !!!" );
94  		}
95  
96  		String hermesLib = hermesHome + File.separator + "lib";
97  		File dir = new File( hermesLib );
98  
99  		String[] children = dir.list();
100 		for( String filename : children )
101 		{
102 		// fix for users using version of hermesJMS which still has cglib-2.1.3.jar in lib directory
103 			if( filename.equals( "cglib-2.1.3.jar" ) )
104 				continue;
105 			
106 			HermesJMSClasspathHacker.addFile( new File( dir, filename ) );
107 		}
108 
109 	}
110 
111 	public static void flushHermesCache()
112 	{
113 		contextMap.clear();
114 	}
115 
116 	private static String createHermesHomeSetting()
117 	{
118 		if( Tools.isEmpty( SoapUI.getSettings().getString( ToolsSettings.HERMES_JMS, defaultHermesJMSPath() ) ) )
119 		{
120 			UISupport.showErrorMessage( "HermesJMS Home must be set in global preferences" );
121 
122 			if( UISupport.getMainFrame() != null )
123 			{
124 				if( SoapUIPreferencesAction.getInstance().show( SoapUIPreferencesAction.INTEGRATED_TOOLS ) )
125 				{
126 					return SoapUI.getSettings().getString( ToolsSettings.HERMES_JMS, defaultHermesJMSPath() );
127 				}
128 			}
129 		}
130 		return null;
131 	}
132 
133 	public static String defaultHermesJMSPath()
134 	{
135 		try
136 		{
137 			String path = SoapUI.getSettings().getString( ToolsSettings.HERMES_JMS, null );
138 			if( path == null || "".equals( path ) )
139 			{
140 				String temp = System.getProperty( "soapui.home" ).substring( 0,
141 						System.getProperty( "soapui.home" ).lastIndexOf( "bin" ) - 1 );
142 				path = new File( temp + File.separator + "hermesJMS" ).getAbsolutePath().toString();
143 				SoapUI.log( "HermesJMS path: " + path );
144 			}
145 			setHermesJMSPath( path );
146 			return path;
147 		}
148 		catch( Exception e )
149 		{
150 			SoapUI.log( "No HermesJMS on default path %SOAPUI_HOME%/hermesJMS" );
151 			return null;
152 		}
153 
154 	}
155 
156 	public static void setHermesJMSPath( String path )
157 	{
158 		if( path != null )
159 			SoapUI.getSettings().setString( ToolsSettings.HERMES_JMS, path );
160 	}
161 
162 	/***
163 	 * @param project
164 	 * @param sessionName
165 	 * 
166 	 * @return hermes.Hermes
167 	 * 
168 	 * @throws NamingException
169 	 */
170 	public static Hermes getHermes( WsdlProject project, String sessionName ) throws NamingException
171 	{
172 		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
173 		try
174 		{
175 			Context ctx = hermesContext( project );
176 
177 			Hermes hermes = ( Hermes )ctx.lookup( sessionName );
178 			return hermes;
179 		}
180 		catch( NamingException ne )
181 		{
182 			UISupport
183 					.showErrorMessage( "Hermes configuration is not valid. Please check that 'Hermes Config' project property is set to path of proper hermes-config.xml file" );
184 			throw new NamingException( "Session name '" + sessionName
185 					+ "' does not exist in Hermes configuration or path to Hermes config ( " + project.getHermesConfig()
186 					+ " )is not valid !!!!" );
187 		}
188 		catch( MalformedURLException mue )
189 		{
190 			SoapUI.logError( mue );
191 		}
192 		catch( IOException ioe )
193 		{
194 			SoapUI.logError( ioe );
195 		}
196 		finally
197 		{
198 			Thread.currentThread().setContextClassLoader( contextClassLoader );
199 		}
200 		return null;
201 	}
202 }