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;
}
Aggregations