Package org.apache.stonehenge.stocktrader.services

Source Code of org.apache.stonehenge.stocktrader.services.TradeOrderServiceClient

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.stonehenge.stocktrader.services;

import org.apache.stonehenge.stocktrader.BeanFactory;
import org.apache.stonehenge.stocktrader.CustomOrderBean;
import org.tempuri.OrderProcessor;
import org.tempuri.OrderProcessorService;
import traderorderhost.trade.SubmitOrder;

import javax.xml.ws.BindingProvider;
import java.io.IOException;
import java.util.Map;

import com.sun.xml.wss.XWSSConstants;

public class TradeOrderServiceClient {
    private static TradeOrderServiceClient self = null;
    private final BeanFactory factory = new BeanFactory();

    private TradeOrderServiceClient() throws IOException {
    }

    public static final TradeOrderServiceClient getInstance()
            throws IOException {
        if (self == null) {
            self = new TradeOrderServiceClient();
        }
        return self;
    }

    public void SubmitOrder(CustomOrderBean order) throws IOException {
        OrderProcessor processor = new OrderProcessor();
        OrderProcessorService service = getOrderProcessorService(processor);

        SubmitOrder param = new SubmitOrder();
        param.setOrder(factory.toOrder(order));
        service.submitOrder(param);
    }

    private OrderProcessorService getOrderProcessorService(OrderProcessor processor) throws IOException {
        TradeOrderConfig config = TradeConfigServiceClient.getInstance().getTradeOrderConfig();
        OrderProcessorService service = null;
        if (config.isSecure()) {
            if (config.isMessageLevelSecurity()) {
                service = processor.getCustomBindingOrderProcessorService();
            } else if (config.isUsernameTokenOverTLS()) {
                service = processor.getSecureConversationBindingOrderProcessorService();
            }
        } else {
            service = processor.getBasicHttpBindingOrderProcessorService();
        }

        Map<String, Object> requestContext = ((BindingProvider) service).getRequestContext();

        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, config.getEndpointURL());
        requestContext.put(XWSSConstants.USERNAME_PROPERTY, "uid:0");
        requestContext.put(XWSSConstants.PASSWORD_PROPERTY, "xxx");

        return service;
    }

//    public static void main(String[] args) throws Exception {
//        TradeOrderServiceClient asynClient = TradeOrderServiceClient.getInstance();
//        CustomOrderBean order = new CustomOrderBean(2, 2);
//        order.setQuantity(235D);
//        asynClient.SubmitOrder(order);
//    }
}
TOP

Related Classes of org.apache.stonehenge.stocktrader.services.TradeOrderServiceClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.