001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package samples.userguide;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
024: import org.apache.axis2.Constants;
025: import org.apache.axis2.transport.http.HttpTransportProperties;
026: import org.apache.axis2.transport.http.HTTPConstants;
027: import org.apache.axis2.addressing.EndpointReference;
028: import org.apache.axis2.client.Options;
029: import org.apache.axis2.client.ServiceClient;
030: import org.apache.axis2.context.ConfigurationContext;
031: import org.apache.axis2.context.ConfigurationContextFactory;
032: import org.apache.rampart.RampartMessageData;
033: import org.apache.neethi.PolicyEngine;
034: import org.apache.neethi.Policy;
035: import org.apache.sandesha2.client.SandeshaClientConstants;
036: import org.apache.synapse.util.UUIDGenerator;
037: import samples.common.StockQuoteHandler;
038:
039: import java.net.URL;
040: import java.io.File;
041:
042: /**
043: * See build.xml for options
044: */
045: public class StockQuoteClient {
046:
047: private static String getProperty(String name, String def) {
048: String result = System.getProperty(name);
049: if (result == null || result.length() == 0) {
050: result = def;
051: }
052: return result;
053: }
054:
055: public static void main(String[] args) {
056:
057: try {
058: executeClient();
059:
060: if ("placeorder".equals(InnerStruct.MODE)) {
061: System.out.println("Order placed for "
062: + InnerStruct.QUANTITY + " shares of stock "
063: + InnerStruct.SYMBOL + " at a price of $ "
064: + InnerStruct.PRICE);
065: } else {
066: if ("customquote".equals(InnerStruct.MODE)) {
067: System.out
068: .println("Custom :: Stock price = $"
069: + StockQuoteHandler
070: .parseCustomQuoteResponse(InnerStruct.RESULT));
071: } else if ("quote".equals(InnerStruct.MODE)) {
072: System.out
073: .println("Standard :: Stock price = $"
074: + StockQuoteHandler
075: .parseStandardQuoteResponse(InnerStruct.RESULT));
076: } else if ("fullquote".equals(InnerStruct.MODE)) {
077: System.out
078: .println("Full :: Average price = $"
079: + StockQuoteHandler
080: .parseFullQuoteResponse(InnerStruct.RESULT));
081: } else if ("marketactivity".equals(InnerStruct.MODE)) {
082: System.out
083: .println("Activity :: Average price = $"
084: + StockQuoteHandler
085: .parseMarketActivityResponse(InnerStruct.RESULT));
086: }
087: }
088: } catch (Exception e) {
089: e.printStackTrace();
090: }
091: }
092:
093: public static OMElement executeTestClient() throws Exception {
094: executeClient();
095: return InnerStruct.RESULT;
096: }
097:
098: public static void executeClient() throws Exception {
099:
100: // defaults
101: String symbol = getProperty("symbol", "IBM");
102: String mode = getProperty("mode", "quote");
103: String addUrl = getProperty("addurl", null);
104: String trpUrl = getProperty("trpurl", null);
105: String prxUrl = getProperty("prxurl", null);
106: String repo = getProperty("repository", "client_repo");
107: String svcPolicy = getProperty("policy", null);
108: String rest = getProperty("rest", null);
109: String wsrm = getProperty("wsrm", null);
110: String itr = getProperty("itr", "1");
111:
112: double price = 0;
113: int quantity = 0;
114: ConfigurationContext configContext = null;
115:
116: Options options = new Options();
117: OMElement payload = null;
118: ServiceClient serviceClient = null;
119:
120: if (repo != null && !"null".equals(repo)) {
121: configContext = ConfigurationContextFactory
122: .createConfigurationContextFromFileSystem(repo,
123: repo + File.separator + "conf"
124: + File.separator + "axis2.xml");
125: serviceClient = new ServiceClient(configContext, null);
126: } else {
127: serviceClient = new ServiceClient();
128: }
129:
130: if ("customquote".equals(mode)) {
131: payload = StockQuoteHandler
132: .createCustomQuoteRequest(symbol);
133: options.setAction("urn:getQuote");
134: } else if ("fullquote".equals(mode)) {
135: payload = StockQuoteHandler.createFullQuoteRequest(symbol);
136: options.setAction("urn:getFullQuote");
137: } else if ("placeorder".equals(mode)) {
138: price = getRandom(100, 0.9, true);
139: quantity = (int) getRandom(10000, 1.0, true);
140: payload = StockQuoteHandler.createPlaceOrderRequest(price,
141: quantity, symbol);
142: options.setAction("urn:placeOrder");
143: } else if ("marketactivity".equals(mode)) {
144: payload = StockQuoteHandler.createMarketActivityRequest();
145: options.setAction("urn:getMarketActivity");
146: } else if ("quote".equals(mode)) {
147: payload = StockQuoteHandler.createStandardQuoteRequest(
148: symbol, Integer.parseInt(itr));
149: options.setAction("urn:getQuote");
150: }
151:
152: // set addressing, transport and proxy url
153: if (addUrl != null && !"null".equals(addUrl)) {
154: serviceClient.engageModule("addressing");
155: options.setTo(new EndpointReference(addUrl));
156: }
157: if (trpUrl != null && !"null".equals(trpUrl)) {
158: options.setProperty(Constants.Configuration.TRANSPORT_URL,
159: trpUrl);
160: }
161: if (prxUrl != null && !"null".equals(prxUrl)) {
162: HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties();
163: URL url = new URL(prxUrl);
164: proxyProperties.setProxyName(url.getHost());
165: proxyProperties.setProxyPort(url.getPort());
166: proxyProperties.setUserName("");
167: proxyProperties.setPassWord("");
168: proxyProperties.setDomain("");
169: options.setProperty(HTTPConstants.PROXY, proxyProperties);
170: }
171:
172: // apply any service policies if any
173: if (svcPolicy != null && !"null".equals(svcPolicy)
174: && svcPolicy.length() > 0) {
175: System.out.println("Using WS-Security");
176: serviceClient.engageModule("addressing");
177: serviceClient.engageModule("rampart");
178: options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,
179: loadPolicy(svcPolicy));
180: }
181:
182: if (Boolean.parseBoolean(rest)) {
183: System.out.println("Sending as REST");
184: options.setProperty(Constants.Configuration.ENABLE_REST,
185: Constants.VALUE_TRUE);
186: }
187: if (Boolean.parseBoolean(wsrm)) {
188: System.out.println("Using WS-RM");
189: serviceClient.engageModule("sandesha2");
190: options.setProperty("Sandesha2LastMessage", "true");
191: options.setProperty(
192: SandeshaClientConstants.OFFERED_SEQUENCE_ID,
193: UUIDGenerator.getUUID());
194: }
195:
196: serviceClient.setOptions(options);
197:
198: InnerStruct.MODE = mode;
199: InnerStruct.SYMBOL = symbol;
200: InnerStruct.PRICE = price;
201: InnerStruct.QUANTITY = quantity;
202:
203: if ("placeorder".equals(mode)) {
204: serviceClient.fireAndForget(payload);
205: Thread.sleep(5000);
206:
207: } else {
208: OMElement result = serviceClient.sendReceive(payload);
209: InnerStruct.RESULT = result;
210: if (Boolean.parseBoolean(wsrm)) {
211: // give some time for RM to terminate normally
212: Thread.sleep(5000);
213: if (configContext != null) {
214: configContext.getListenerManager().stop();
215: }
216: serviceClient.cleanup();
217: System.exit(0);
218: }
219: }
220:
221: try {
222: if (configContext != null) {
223: configContext.terminate();
224: }
225: // the above statement was used on reccomendation by Chamikara as I remember, but
226: // since using Axis2 1.3 - this causes some unexpected classloading issue on the
227: // Axis2 server side - which cannot be described. This using the below as suggested
228: // by Deepal
229: // serviceClient.cleanup();
230: } catch (Exception ignore) {
231: }
232: }
233:
234: private static class InnerStruct {
235: static String MODE = null;
236: static String SYMBOL = null;
237: static int QUANTITY = 0;
238: static double PRICE = 0;
239: static OMElement RESULT = null;
240: }
241:
242: private static Policy loadPolicy(String xmlPath) throws Exception {
243: StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
244: return PolicyEngine.getPolicy(builder.getDocumentElement());
245: }
246:
247: private static double getRandom(double base, double varience,
248: boolean onlypositive) {
249: double rand = Math.random();
250: return (base + ((rand > 0.5 ? 1 : -1) * varience * base * rand))
251: * (onlypositive ? 1 : (rand > 0.5 ? 1 : -1));
252: }
253:
254: }
|