Search in sources :

Example 1 with XmlEofInputStream

use of org.opensolaris.opengrok.util.XmlEofInputStream in project OpenGrok by OpenGrok.

the class RuntimeEnvironment method startConfigurationListenerThread.

/**
     * Start a thread to listen on a socket to receive new messages.
     * The messages can contain various commands for the webapp, including
     * upload of new configuration.
     *
     * @param endpoint The socket address to listen on
     * @return true if the endpoint was available (and the thread was started)
     */
public boolean startConfigurationListenerThread(SocketAddress endpoint) {
    boolean ret = false;
    try {
        configServerSocket = new ServerSocket();
        configServerSocket.bind(endpoint);
        ret = true;
        final ServerSocket sock = configServerSocket;
        configurationListenerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                ByteArrayOutputStream bos = new ByteArrayOutputStream(1 << 15);
                while (!sock.isClosed()) {
                    try (Socket s = sock.accept();
                        BufferedInputStream in = new BufferedInputStream(new XmlEofInputStream(s.getInputStream()));
                        OutputStream output = s.getOutputStream()) {
                        bos.reset();
                        LOGGER.log(Level.FINE, "OpenGrok: Got request from {0}", s.getInetAddress().getHostAddress());
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) != -1) {
                            bos.write(buf, 0, len);
                        }
                        buf = bos.toByteArray();
                        if (LOGGER.isLoggable(Level.FINE)) {
                            LOGGER.log(Level.FINE, "new config:{0}", new String(buf));
                        }
                        Object obj;
                        try (XMLDecoder d = new XMLDecoder(new ByteArrayInputStream(buf))) {
                            obj = d.readObject();
                        }
                        if (obj instanceof Message) {
                            Message m = ((Message) obj);
                            handleMessage(m, output);
                        }
                    } catch (IOException e) {
                        LOGGER.log(Level.SEVERE, "Error reading config file: ", e);
                    } catch (RuntimeException e) {
                        LOGGER.log(Level.SEVERE, "Error parsing config file: ", e);
                    }
                }
            }
        }, "configurationListener");
        configurationListenerThread.start();
    } catch (UnknownHostException ex) {
        LOGGER.log(Level.WARNING, "Problem resolving sender: ", ex);
    } catch (IOException ex) {
        LOGGER.log(Level.WARNING, "I/O error when waiting for config: ", ex);
    }
    if (!ret && configServerSocket != null) {
        IOUtils.close(configServerSocket);
    }
    return ret;
}
Also used : Message(org.opensolaris.opengrok.configuration.messages.Message) UnknownHostException(java.net.UnknownHostException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) XmlEofInputStream(org.opensolaris.opengrok.util.XmlEofInputStream) JSONObject(org.json.simple.JSONObject) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Aggregations

XMLDecoder (java.beans.XMLDecoder)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1 JSONObject (org.json.simple.JSONObject)1 Message (org.opensolaris.opengrok.configuration.messages.Message)1 XmlEofInputStream (org.opensolaris.opengrok.util.XmlEofInputStream)1