Example usage for org.springframework.integration.test.util SocketUtils findAvailableServerSocket

List of usage examples for org.springframework.integration.test.util SocketUtils findAvailableServerSocket

Introduction

In this page you can find the example usage for org.springframework.integration.test.util SocketUtils findAvailableServerSocket.

Prototype

public static int findAvailableServerSocket(int seed) 

Source Link

Document

Determines a free available server socket (port) using the 'seed' value as the starting port.

Usage

From source file:com.github.exper0.efilecopier.ftp.TestSuite.java

@BeforeClass
public static void setupFtpServer() throws FtpException, SocketException, IOException {

    final int availableServerSocket;

    if (System.getProperty(SERVER_PORT_SYSTEM_PROPERTY) == null) {
        availableServerSocket = SocketUtils.findAvailableServerSocket(4444);
        System.setProperty(SERVER_PORT_SYSTEM_PROPERTY, Integer.valueOf(availableServerSocket).toString());
    } else {//w  w w  . ja v  a  2 s .  c o m
        availableServerSocket = Integer.valueOf(System.getProperty(SERVER_PORT_SYSTEM_PROPERTY));
    }

    LOGGER.info("Using open server port..." + availableServerSocket);

    File ftpRoot = new File(FTP_ROOT_DIR);
    ftpRoot.mkdirs();

    TestUserManager userManager = new TestUserManager(ftpRoot.getAbsolutePath());

    FtpServerFactory serverFactory = new FtpServerFactory();
    serverFactory.setUserManager(userManager);
    ListenerFactory factory = new ListenerFactory();

    factory.setPort(availableServerSocket);

    serverFactory.addListener("default", factory.createListener());

    server = serverFactory.createServer();

    server.start();

}

From source file:org.musa.tcpserver.Main.java

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

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

    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/serverContext.xml");

    context.registerShutdownHook();//  w w  w.  ja  v  a2 s .  c om
    context.refresh();

    return context;
}

From source file:com.richard.memorystore.udp.UDPServer.java

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

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

    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:udpServer.xml");
    context.registerShutdownHook();/*w w  w  .j av  a2s  .c  o  m*/
    context.refresh();

    return context;
}

From source file:com.richard.memorystore.tcp.TcpServer.java

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

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

    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:tcpClientServerDemo-context.xml");
    context.registerShutdownHook();/*from w w  w . j a  va  2s . c om*/
    context.refresh();

    return context;
}

From source file:com.haythem.integration.Main.java

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

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

    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/tcpClientServerDemo-context.xml");
    context.registerShutdownHook();/*from   ww  w  .ja v  a  2 s  .  c o  m*/
    context.refresh();

    return context;
}

From source file:org.musa.tcpclients.Main.java

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();/*from ww w. j  a va  2  s  . c  o  m*/

    return context;
}

From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java

@Before
public void startWiser() {

    this.wiserPort = SocketUtils.findAvailableServerSocket(2500);

    wiser = new Wiser();
    wiser.setPort(this.wiserPort);
    wiser.start();/*  w  w w . ja  v a2  s  . com*/
    LOGGER.info("Wiser was started.");

}

From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java

@Test
public void testTcp() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
            SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    int port = SocketUtils.findAvailableServerSocket(1514);
    factory.setPort(port);/*from  w  ww .j a  va  2  s  .  com*/
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setApplicationEventPublisher(publisher);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE\n".getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}

From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java

@Test
public void testTcpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(
            SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    int port = SocketUtils.findAvailableServerSocket(1514);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {//  w ww  .ja  v a2  s.  c  o  m
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setBeanFactory(mock(BeanFactory.class));
    AbstractServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(port);
    connectionFactory.setDeserializer(new RFC6587SyslogDeserializer());
    connectionFactory.setApplicationEventPublisher(publisher);
    factory.setConnectionFactory(connectionFactory);
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = ("253 <14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - "
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]"
            + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance")
                    .getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    @SuppressWarnings("unchecked")
    Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}

From source file:org.springframework.xd.integration.test.FtpHdfsTest.java

@BeforeClass
public static void setupFtpServer() throws FtpException, SocketException, IOException {

    availableServerSocket = SocketUtils.findAvailableServerSocket(4444);
    System.setProperty(SERVER_PORT_SYSTEM_PROPERTY, Integer.valueOf(availableServerSocket).toString());

    LOGGER.info("Using open server port..." + availableServerSocket);

    File ftpRoot = new File(FTP_ROOT_DIR);
    ftpRoot.mkdirs();//from  ww w .j a v  a  2s.  c o m

    FtpServerFactory serverFactory = new FtpServerFactory();
    serverFactory.setUserManager(new TestUserManager());
    ListenerFactory factory = new ListenerFactory();

    factory.setPort(availableServerSocket);

    serverFactory.addListener("default", factory.createListener());

    server = serverFactory.createServer();

    server.start();

}