1 57 58 package util; 59 60 64 65 import java.io.InputStream ; 66 import java.util.Properties ; 67 import java.util.StringTokenizer ; 68 69 import javax.jms.JMSException ; 70 import javax.jms.Message ; 71 import javax.jms.Queue ; 72 import javax.jms.QueueConnection ; 73 import javax.jms.QueueConnectionFactory ; 74 import javax.jms.QueueReceiver ; 75 import javax.jms.QueueSession ; 76 import javax.jms.Session ; 77 78 import org.apache.wsif.WSIFException; 79 import org.apache.wsif.base.WSIFDefaultCorrelationService; 80 import org.apache.wsif.providers.soap.apacheaxis.WSIFDynamicProvider_ApacheAxis; 81 import org.apache.wsif.providers.soap.apachesoap.WSIFDynamicProvider_ApacheSOAP; 82 import org.apache.wsif.spi.WSIFProvider; 83 import org.apache.wsif.util.WSIFCorrelationServiceLocator; 84 import org.apache.wsif.util.WSIFPluggableProviders; 85 import org.apache.wsif.util.WSIFProperties; 86 import org.apache.wsif.util.jms.JMSAsyncListener; 87 import org.apache.wsif.util.jms.NativeJMSRequestListener; 88 import org.apache.wsif.util.jms.WSIFJMSFinder; 89 import org.apache.wsif.util.jms.WSIFJMSFinderForJndi; 90 91 public class TestUtilities { 92 private static final String WSIF_TEST_PROPERTIES = "wsif.test.properties"; 93 private static final String WSIF_PATH = "wsif.path"; 94 private static final String WSIF_TEST_COMPONENTS = "wsif.test.components"; 95 private static final String WSIF_SOAP_SERVER = "wsif.soapserver"; 96 97 private static final String SOAP = "soap"; 98 private static final String AXIS = "axis"; 99 100 private static BridgeThread jmsAb = null; 101 private static BridgeThread jmsSq = null; 102 private static JMSAsyncListener asyncListener = null; 103 private static NativeJMSRequestListener nativeReqListener = null; 104 105 public static final int ADDRESSBOOK_LISTENER = 1; 106 public static final int STOCKQUOTE_LISTENER = 2; 107 public static final int ASYNC_LISTENER = 4; 108 public static final int NATIVEJMS_LISTENER = 8; 109 public static final int ALL_LISTENERS = 15; 110 111 public static final Class DEFAULT_SOAP_PROVIDER_CLASS = 112 WSIFDynamicProvider_ApacheAxis.class; 113 private static final String DEFAULT_SOAP_PROTOCOL = 114 "axis"; 115 public static final Class NON_DEFAULT_SOAP_PROVIDER_CLASS = 116 WSIFDynamicProvider_ApacheSOAP.class; 117 private static final String NON_DEFAULT_SOAP_PROTOCOL = 118 "soap"; 119 129 public static String getWsdlPath(String relativePath) { 130 String wsdlPath = getWsifProperty(WSIF_PATH); 131 if (wsdlPath == null) 132 wsdlPath = new String (""); 133 134 String slash = System.getProperty("file.separator"); 135 136 if (!wsdlPath.equals("") && !wsdlPath.endsWith(slash)) 137 wsdlPath += slash; 138 139 if (relativePath != null && relativePath.length() != 0) { 140 StringBuffer relativeBuff = new StringBuffer (relativePath); 141 for (int i = 0; i < relativeBuff.length(); i++) { 142 if (relativeBuff.charAt(i) == '\\' || relativeBuff.charAt(i) == '/') 143 relativeBuff.setCharAt(i, slash.charAt(0)); 144 } 145 146 wsdlPath += relativeBuff; 147 148 if (!wsdlPath.endsWith(slash)) 149 wsdlPath += slash; 150 } 151 return wsdlPath; 152 } 153 154 157 public static String getWsifProperty(String property) { 158 try { 159 Properties prop = new Properties (); 160 InputStream in = ClassLoader.getSystemResourceAsStream(WSIF_TEST_PROPERTIES); 161 prop.load(in); 162 return prop.getProperty(property); 163 } catch (Exception ignored) { 164 return null; 165 } 166 } 167 168 173 public static boolean areWeTesting(String component) { 174 String prop = getWsifProperty(WSIF_TEST_COMPONENTS); 175 if (prop == null) 176 return true; 178 StringTokenizer tokenizer = new StringTokenizer (prop, ","); 179 String token; 180 int eqPos; 181 while (tokenizer.hasMoreTokens()) { 182 token = tokenizer.nextToken(); 183 eqPos = token.indexOf('='); 184 if (eqPos == -1) 185 continue; 186 if (token.substring(0, eqPos).equals(component) 187 && token.substring(eqPos + 1).equals("off")) 188 return false; 189 } 190 return true; 191 } 192 193 197 public static String getSoapServer() { 198 String prop = getWsifProperty(WSIF_SOAP_SERVER); 199 if (prop == null) 200 return SOAP; 201 if (SOAP.equals(prop) || AXIS.equals(prop)) 202 return prop; 203 return SOAP; 204 } 205 206 public static boolean isJmsVerbose() { 207 String strVerbose = TestUtilities.getWsifProperty("wsif.jms.output"); 208 if ("verbose".equals(strVerbose)) 209 return true; 210 return false; 211 } 212 213 public static void setProviderForProtocol(String protocol) { 214 if ( protocol.equals( NON_DEFAULT_SOAP_PROTOCOL ) ) { 215 try { 216 WSIFPluggableProviders.overrideDefaultProvider( 217 "http://schemas.xmlsoap.org/wsdl/soap/", 218 (WSIFProvider) NON_DEFAULT_SOAP_PROVIDER_CLASS.newInstance() ); 219 } catch (Exception ex) { 220 ex.printStackTrace(); 221 } 222 } else { 223 resetDefaultProviders(); 224 } 225 } 226 227 public static void resetDefaultProviders() { 228 WSIFPluggableProviders.setAutoLoadProviders( false ); 229 WSIFPluggableProviders.setAutoLoadProviders( true ); 230 } 231 232 public static void setUpExtensionsAndProviders() { 233 resetDefaultProviders(); 238 } 239 248 public static void startListeners() { 249 startListeners(ALL_LISTENERS); 250 } 251 252 public static void startListeners(int which) { 253 if (TestUtilities.areWeTesting("jms")) { 254 if ((which & ADDRESSBOOK_LISTENER) > 0) { 255 jmsAb = new BridgeThread("AddressBook"); 256 jmsAb.start(); 257 } 258 259 if ((which & STOCKQUOTE_LISTENER) > 0) { 260 jmsSq = new BridgeThread("Stockquote"); 261 jmsSq.start(); 262 } 263 264 if ((which & NATIVEJMS_LISTENER) > 0) 265 try { 266 nativeReqListener = 267 new NativeJMSRequestListener( 268 TestUtilities.getWsifProperty( 269 "wsif.nativejms.requestq")); 270 } catch (WSIFException ex) { 271 ex.printStackTrace(); 272 } 273 } 274 275 if ((which & ASYNC_LISTENER) > 0 276 && TestUtilities.areWeTesting("async")) 277 try { 278 asyncListener = 279 new JMSAsyncListener( 280 TestUtilities.getWsifProperty("wsif.async.replytoq")); 281 } catch (Exception ex) { 282 ex.printStackTrace(); 283 } 284 } 285 286 293 public static void stopListeners() 294 { 295 if ( jmsAb != null ) jmsAb.interrupt(); 296 if ( jmsSq != null ) jmsSq.interrupt(); 297 if ( asyncListener != null ) asyncListener.stop(); 298 if ( nativeReqListener != null ) nativeReqListener.stop(); 299 ((WSIFDefaultCorrelationService)WSIFCorrelationServiceLocator. 300 getCorrelationService()).shutdown(); 301 } 302 303 public static Message getJMSAsyncResponse(String id, String queueName) 304 throws WSIFException, JMSException { 305 306 WSIFJMSFinder finder = 307 new WSIFJMSFinderForJndi( 308 null, 309 TestUtilities.getWsifProperty( 310 "wsif.jms2httpbridge.initialcontextfactory"), 311 TestUtilities.getWsifProperty( 312 "wsif.jms2httpbridge.jndiproviderurl"), 313 WSIFJMSFinder.STYLE_QUEUE, 314 TestUtilities.getWsifProperty( 315 "wsif.jms2httpbridge.jndiconnectionfactoryname"), 316 null, 317 null); 318 319 QueueConnectionFactory factory = finder.getFactory(); 320 QueueConnection connection = factory.createQueueConnection(); 321 connection.start(); 322 QueueSession session = 323 connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 324 Queue readQ = session.createQueue(queueName); 325 QueueReceiver receiver = 326 session.createReceiver(readQ, "JMSCorrelationID='" + id + "'"); 327 return receiver.receive(WSIFProperties.getAsyncTimeout()); 328 } 329 } 330 | Popular Tags |