org.musa.tcpclients.Main.java Source code

Java tutorial

Introduction

Here is the source code for org.musa.tcpclients.Main.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed 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.musa.tcpclients;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.musa.payload.SMLoyalty;
import org.musa.payload.SMRank;
import org.musa.payload.SpaceMarine;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.util.TestingUtilities;
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
import org.springframework.integration.test.util.SocketUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.DestinationResolver;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;

/**
 * Demonstrates the use of a gateway as an entry point into the integration flow.
 * The message generated by the gateway is sent over tcp by the outbound gateway
 * to the inbound gateway. In turn the inbound gateway sends the message to an
 * echo service and the echoed response comes back over tcp and is returned to
 * the test case for verification.
 *
 * The test uses explicit transformers to convert the byte array payloads to
 * Strings.
 *
 * Several other samples are provided as JUnit test-cases:
 *
 * <ul>
 *     <li>TcpClientServerDemoWithConversionServiceTest</li>
 *     <li>TcpServerConnectionDeserializeTest</li>
 *     <li>TcpServerCustomSerializerTest</li>
 * </ul>
 *
 * @author Gunnar Hillert
 *
 */
public final class Main {

    /**
     * Prevent instantiation.
     */
    private Main() {
    }

    /**
     * Load the Spring Integration Application Context
     *
     * @param args - command line arguments
     */
    public static void main(final String... args) {

        final Scanner scanner = new Scanner(System.in);

        //context.getB
        GenericXmlApplicationContext context = Main.setupContext();
        WarpGateway gateway = (WarpGateway) context.getBean("gw");

        System.out.println("running.\n\n");

        System.out.println("Please enter numbers to spawn spacemarines :");
        System.out.println("1: Gabriel Loken");
        System.out.println("2: Nathaniel Garro");
        System.out.println("3: Ezekyl Abaddon");
        System.out.println("4: Sanguinius");
        System.out.println("5: Lucius");

        System.out.println("\t- Entering q will quit the application");
        System.out.print("\n");

        while (true) {

            final String input = scanner.nextLine();

            if ("q".equals(input.trim())) {
                break;
            } else {

                SpaceMarine gabriel = new SpaceMarine("Gabriel Loken", "Luna Wolves", 500, SMRank.CaptainBrother,
                        SMLoyalty.Loyalist, 100);
                SpaceMarine garro = new SpaceMarine("Nathaniel Garro", "Deathguard", 500, SMRank.CaptainBrother,
                        SMLoyalty.Loyalist, 100);
                SpaceMarine ezekyl = new SpaceMarine("Ezekyl Abaddon", "Black Legion", 500, SMRank.CaptainBrother,
                        SMLoyalty.Traitor, 100);
                SpaceMarine sanguinius = new SpaceMarine("Sanguinius", "Blood angels", 999, SMRank.Primarch,
                        SMLoyalty.Loyalist, 600);
                SpaceMarine lucius = new SpaceMarine("Lucius", "Emperor's children", 500, SMRank.SwordMaster,
                        SMLoyalty.Traitor, 700);

                SpaceMarine[] spaceMarines = { gabriel, garro, ezekyl, sanguinius, lucius };
                int max_id = spaceMarines.length;

                int num = 0; //input
                try {
                    num = Integer.parseInt(input);
                } catch (NumberFormatException e) {
                    System.out.println("unable to parse value");
                }
                if (num >= max_id) {
                    System.out.println("no such spacemarine, using Loken");
                    num = 0;
                }

                System.out.println("teleporting " + spaceMarines[num].getName() + "....");

                Message<SpaceMarine> m = MessageBuilder.withPayload(spaceMarines[num]).build();

                //context.
                String reply = (String) gateway.send(m);
                System.out.println(reply);

            }
        }

        System.out.println("Exiting application...bye.");
        System.exit(0);

    }

    public static GenericXmlApplicationContext setupContext() {
        final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

        System.out.print("Detect open server socket...");
        int availableServerSocket = SocketUtils.findAvailableServerSocket(5683);

        final Map<String, Object> sockets = new HashMap<String, Object>();
        sockets.put("availableServerSocket", availableServerSocket);

        final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

        context.getEnvironment().getPropertySources().addLast(propertySource);

        System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

        context.load("classpath:META-INF/spring/integration/clientContext.xml");
        //context.registerShutdownHook();
        context.refresh();

        return context;
    }
}