use of java.net.SocketAddress in project OpenGrok by OpenGrok.
the class WebappListener method contextInitialized.
/**
* {@inheritDoc}
*/
@Override
public void contextInitialized(final ServletContextEvent servletContextEvent) {
ServletContext context = servletContextEvent.getServletContext();
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String config = context.getInitParameter("CONFIGURATION");
if (config == null) {
LOGGER.severe("CONFIGURATION section missing in web.xml");
} else {
try {
env.readConfiguration(new File(config));
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "OpenGrok Configuration error. Failed to read config file: ", ex);
}
}
String address = context.getInitParameter("ConfigAddress");
if (address != null && address.length() > 0) {
LOGGER.log(Level.CONFIG, "Will listen for configuration on [{0}]", address);
String[] cfg = address.split(":");
if (cfg.length == 2) {
try {
SocketAddress addr = new InetSocketAddress(InetAddress.getByName(cfg[0]), Integer.parseInt(cfg[1]));
if (!RuntimeEnvironment.getInstance().startConfigurationListenerThread(addr)) {
LOGGER.log(Level.SEVERE, "OpenGrok: Failed to start configuration listener thread");
}
} catch (NumberFormatException | UnknownHostException ex) {
LOGGER.log(Level.SEVERE, "OpenGrok: Failed to start configuration listener thread:", ex);
}
} else {
LOGGER.log(Level.SEVERE, "Incorrect format for the configuration address: ");
for (int i = 0; i < cfg.length; ++i) {
LOGGER.log(Level.SEVERE, "[{0}]", cfg[i]);
}
}
}
try {
RuntimeEnvironment.getInstance().loadStatistics();
} catch (IOException ex) {
LOGGER.log(Level.INFO, "Could not load statistics from a file.", ex);
} catch (ParseException ex) {
LOGGER.log(Level.SEVERE, "Could not parse statistics from a file.", ex);
}
String pluginDirectory = context.getInitParameter(AUTHORIZATION_PLUGIN_DIRECTORY);
if (pluginDirectory != null) {
env.getConfiguration().setPluginDirectory(pluginDirectory);
// start + load
AuthorizationFramework.getInstance();
} else {
if (env.getDataRootPath() == null) {
env.getConfiguration().setPluginDirectory(PLUGIN_DIRECTORY_DEFAULT);
} else {
env.getConfiguration().setPluginDirectory(env.getDataRootPath() + "/../" + PLUGIN_DIRECTORY_DEFAULT);
}
LOGGER.log(Level.INFO, AUTHORIZATION_PLUGIN_DIRECTORY + " is not set in web.xml. Default location will be used.");
}
String watchDog = context.getInitParameter(ENABLE_AUTHORIZATION_WATCH_DOG);
if (pluginDirectory != null && watchDog != null && Boolean.parseBoolean(watchDog)) {
RuntimeEnvironment.getInstance().startWatchDogService(new File(pluginDirectory));
}
RuntimeEnvironment.getInstance().startIndexReopenThread();
RuntimeEnvironment.getInstance().startExpirationTimer();
}
use of java.net.SocketAddress in project OpenGrok by OpenGrok.
the class RuntimeEnvironmentTest method testConfigListenerThread.
@Test
public void testConfigListenerThread() throws IOException {
RuntimeEnvironment instance = RuntimeEnvironment.getInstance();
SocketAddress addr = new InetSocketAddress(0);
assertTrue(instance.startConfigurationListenerThread(addr));
try {
Thread.sleep(1000);
} catch (InterruptedException exp) {
// do nothing
}
instance.writeConfiguration();
instance.stopConfigurationListenerThread();
}
use of java.net.SocketAddress in project jmxtrans by jmxtrans.
the class SocketAllocator method allocate.
@Override
public SocketPoolable allocate(Slot slot) throws Exception {
// create new InetSocketAddress to ensure name resolution is done again
SocketAddress serverAddress = new InetSocketAddress(server.getHostName(), server.getPort());
Socket socket = new Socket();
socket.setKeepAlive(false);
socket.connect(serverAddress, socketTimeoutMillis);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), charset));
return new SocketPoolable(slot, socket, writer, flushStrategy);
}
use of java.net.SocketAddress in project hudson-2.x by hudson.
the class UDPBroadcastThread method run.
@Override
public void run() {
try {
mcs.joinGroup(MULTICAST);
ready.signal();
while (true) {
byte[] buf = new byte[2048];
DatagramPacket p = new DatagramPacket(buf, buf.length);
mcs.receive(p);
SocketAddress sender = p.getSocketAddress();
// prepare a response
TcpSlaveAgentListener tal = hudson.getTcpSlaveAgentListener();
StringBuilder rsp = new StringBuilder("<hudson>");
tag(rsp, "version", Hudson.VERSION);
tag(rsp, "url", hudson.getRootUrl());
tag(rsp, "slave-port", tal == null ? null : tal.getPort());
for (UDPBroadcastFragment f : UDPBroadcastFragment.all()) f.buildFragment(rsp, sender);
rsp.append("</hudson>");
byte[] response = rsp.toString().getBytes("UTF-8");
mcs.send(new DatagramPacket(response, response.length, sender));
}
} catch (ClosedByInterruptException e) {
// shut down
} catch (BindException e) {
// if we failed to listen to UDP, just silently abandon it, as a stack trace
// makes people unnecessarily concerned, for a feature that currently does no good.
LOGGER.log(Level.WARNING, "Failed to listen to UDP port " + PORT, e);
} catch (IOException e) {
// forcibly closed
if (shutdown)
return;
LOGGER.log(Level.WARNING, "UDP handling problem", e);
}
}
use of java.net.SocketAddress in project Smack by igniterealtime.
the class Socks5Client method getSocket.
/**
* Returns the initialized socket that can be used to transfer data between peers via the SOCKS5
* proxy.
*
* @param timeout timeout to connect to SOCKS5 proxy in milliseconds
* @return socket the initialized socket
* @throws IOException if initializing the socket failed due to a network error
* @throws XMPPErrorException if establishing connection to SOCKS5 proxy failed
* @throws TimeoutException if connecting to SOCKS5 proxy timed out
* @throws InterruptedException if the current thread was interrupted while waiting
* @throws SmackException if the connection to the SOC
* @throws XMPPException
*/
public Socket getSocket(int timeout) throws IOException, XMPPErrorException, InterruptedException, TimeoutException, SmackException, XMPPException {
// wrap connecting in future for timeout
FutureTask<Socket> futureTask = new FutureTask<Socket>(new Callable<Socket>() {
@Override
public Socket call() throws IOException, SmackException {
// initialize socket
Socket socket = new Socket();
SocketAddress socketAddress = new InetSocketAddress(streamHost.getAddress(), streamHost.getPort());
socket.connect(socketAddress);
// initialize connection to SOCKS5 proxy
try {
establish(socket);
} catch (SmackException e) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e2) {
LOGGER.log(Level.WARNING, "Could not close SOCKS5 socket", e2);
}
}
throw e;
}
return socket;
}
});
Thread executor = new Thread(futureTask);
executor.start();
// get connection to initiator with timeout
try {
return futureTask.get(timeout, TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause != null) {
// case exceptions to comply with method signature
if (cause instanceof IOException) {
throw (IOException) cause;
}
if (cause instanceof SmackException) {
throw (SmackException) cause;
}
}
// throw generic Smack exception if unexpected exception was thrown
throw new SmackException("Error while connecting to SOCKS5 proxy", e);
}
}
Aggregations