001: /*
002: * Copyright 2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package samples.jms.stub;
017:
018: import samples.jms.stub.xmltoday_delayed_quotes.*;
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.utils.Options;
021: import org.apache.axis.transport.jms.JMSTransport;
022: import org.apache.axis.transport.jms.SimpleJMSListener;
023: import java.util.HashMap;
024:
025: import junit.framework.AssertionFailedError;
026: import junit.framework.TestCase;
027:
028: import java.rmi.RemoteException;
029:
030: import javax.xml.rpc.ServiceException;
031: import javax.xml.rpc.Stub;
032:
033: /**
034: * Demonstrates use of wsdl2java-generated static stubs to invoke JMS endpoints.
035: *
036: * The JMS listener is an intermediary that receives the JMS service request and
037: * invokes the actual stock quote service over HTTP.
038: *
039: * @author Ray Chun (rchun@sonicsoftware.com)
040: */
041:
042: public class JMSURLStubTest extends TestCase {
043: public JMSURLStubTest(String name) {
044: super (name);
045: }
046:
047: public static Float getQuote(String ticker) throws AxisFault {
048: float quote = -1.0F;
049:
050: GetQuoteServiceLocator locator = new GetQuoteServiceLocator();
051: GetQuote getQuote;
052:
053: try {
054: getQuote = locator.getGetQuote();
055: } catch (ServiceException e) {
056: throw new AxisFault("JAX-RPC ServiceException caught: ", e);
057: }
058: assertTrue("getQuote is null", getQuote != null);
059:
060: try {
061: quote = getQuote.getQuote(ticker);
062: System.out.println("quote: " + quote);
063:
064: // close matching connectors
065: // note: this is optional, as all connectors will be closed upon exit
066: String endptAddr = locator.getGetQuoteAddress();
067: JMSTransport.closeMatchingJMSConnectors(endptAddr, null,
068: null);
069: } catch (RemoteException e) {
070: throw new AxisFault("Remote Exception caught: ", e);
071: }
072: return new Float(quote);
073: }
074:
075: public static void printUsage() {
076: System.out
077: .println("JMSURLStubTest: Tests JMS transport by obtaining stock quote using wsdl2java-generated stub classes");
078: System.out
079: .println(" Usage: JMSURLStubTest <symbol 1> <symbol 2> <symbol 3> ...");
080: System.out.println(" Opts: -? this message");
081: System.out.println();
082: System.out
083: .println(" -c connection factory properties filename");
084: System.out.println(" -d destination");
085: System.out
086: .println(" -t topic [absence of -t indicates queue]");
087: System.out.println();
088: System.out.println(" -u username");
089: System.out.println(" -w password");
090: System.out.println();
091: System.out.println(" -s single-threaded listener");
092: System.out
093: .println(" [absence of option => multithreaded]");
094:
095: System.exit(1);
096: }
097:
098: /**
099: * Conn args are still required to set up the JMS listener
100: */
101: public static void main(String[] args) throws Exception {
102: Options opts = new Options(args);
103:
104: // first check if we should print usage
105: if ((opts.isFlagSet('?') > 0) || (opts.isFlagSet('h') > 0))
106: printUsage();
107:
108: String username = opts.getUser();
109: String password = opts.getPassword();
110:
111: HashMap connectorMap = SimpleJMSListener
112: .createConnectorMap(opts);
113: HashMap cfMap = SimpleJMSListener.createCFMap(opts);
114: String destination = opts.isValueSet('d');
115:
116: args = opts.getRemainingArgs();
117: if (args == null || args.length == 0)
118: printUsage();
119:
120: // create the jms listener
121: SimpleJMSListener listener = new SimpleJMSListener(
122: connectorMap, cfMap, destination, username, password,
123: false);
124: listener.start();
125:
126: JMSURLStubTest stubTest = new JMSURLStubTest(
127: "JMS URL static stub test");
128:
129: for (int i = 0; i < args.length; i++) {
130: try {
131: Float quote = stubTest.getQuote(args[i]);
132: System.out.println(args[i] + ": " + quote);
133: } catch (AxisFault af) {
134: System.out.println(af.dumpToString());
135: }
136: }
137:
138: listener.shutdown();
139:
140: System.exit(1);
141: }
142: }
|