Search in sources :

Example 61 with SocketAddress

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();
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ServletContext(javax.servlet.ServletContext) ParseException(org.json.simple.parser.ParseException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File)

Example 62 with SocketAddress

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();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 63 with SocketAddress

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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) BufferedWriter(java.io.BufferedWriter)

Example 64 with SocketAddress

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);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) DatagramPacket(java.net.DatagramPacket) BindException(java.net.BindException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress)

Example 65 with SocketAddress

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);
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) InetSocketAddress(java.net.InetSocketAddress) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket)

Aggregations

SocketAddress (java.net.SocketAddress)355 InetSocketAddress (java.net.InetSocketAddress)279 IOException (java.io.IOException)79 Test (org.junit.Test)73 Socket (java.net.Socket)48 InetAddress (java.net.InetAddress)35 Channel (org.jboss.netty.channel.Channel)33 SocketException (java.net.SocketException)27 UnknownHostException (java.net.UnknownHostException)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)23 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)23 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)20 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)19 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Proxy (java.net.Proxy)18 HashMap (java.util.HashMap)17 DatagramPacket (java.net.DatagramPacket)16 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.apache.log4j.Logger)16 Test (org.testng.annotations.Test)16