001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.util;
021    
022    import java.io.Closeable;
023    import java.io.IOException;
024    import java.io.InputStream;
025    import java.io.OutputStream;
026    import java.net.InetSocketAddress;
027    import java.net.ServerSocket;
028    import java.net.Socket;
029    
030    public abstract class AbstractSocketServer implements Closeable {
031    
032      /** . */
033      private final int bindingPort;
034    
035      /** . */
036      private ServerSocket socketServer;
037    
038      /** . */
039      private Socket socket;
040    
041      /** . */
042      private InputStream in;
043    
044      /** . */
045      private OutputStream out;
046    
047      /** . */
048      private int port;
049    
050      public AbstractSocketServer(int bindingPort) {
051        this.bindingPort = bindingPort;
052      }
053    
054      public final int getBindingPort() {
055        return socketServer.getLocalPort();
056      }
057    
058      public final int getPort() {
059        return port;
060      }
061    
062      public final int bind() throws IOException {
063        ServerSocket socketServer = new ServerSocket();
064        socketServer.bind(new InetSocketAddress(bindingPort));
065        int port = socketServer.getLocalPort();
066    
067        //
068        this.socketServer = socketServer;
069        this.port = port;
070    
071        //
072        return port;
073      }
074    
075      public final void accept() throws IOException {
076        if (socketServer == null) {
077          throw new IllegalStateException();
078        }
079    
080        //
081        this.socket = socketServer.accept();
082        this.in = socket.getInputStream();
083        this.out = socket.getOutputStream();
084    
085        //
086        handle(in, out);
087      }
088    
089      protected abstract void handle(InputStream in, OutputStream out) throws IOException;
090    
091      public final void close() {
092        try {
093          Safe.close(socket);
094          Safe.close(in);
095          Safe.close(out);
096        }
097        finally {
098          this.socket = null;
099          this.in = null;
100          this.out = null;
101        }
102      }
103    }