View Javadoc

1   /*
2    * $Id: SimpleHttpServer.java 21916 2011-05-15 05:58:02Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.rss;
12  
13  import java.io.IOException;
14  import java.net.InetSocketAddress;
15  import java.net.SocketAddress;
16  
17  import org.simpleframework.http.core.Container;
18  import org.simpleframework.transport.connect.Connection;
19  import org.simpleframework.transport.connect.SocketConnection;
20  
21  /**
22   * HTTP server for testing purposes using the <a
23   * href="http://www.simpleframework.org/">Simple Framework</a> as server
24   * implementation.
25   */
26  public class SimpleHttpServer
27  {
28      private Container container;
29      private SocketAddress address;
30      private Connection connection;
31  
32      public SimpleHttpServer(int port, Container container)
33      {
34          super();
35          this.address = new InetSocketAddress(port);
36          this.container = container;
37      }
38  
39      public void start() throws IOException
40      {
41          connection = new SocketConnection(container);
42          connection.connect(address);
43      }
44  
45      public void stop()
46      {
47          try
48          {
49              if (connection != null)
50              {
51                  connection.close();
52              }
53          }
54          catch (IOException e)
55          {
56              throw new IllegalStateException(e);
57          }
58      }
59  }