Code example for Logger

Methods: fine, log

0
            .add("-e") 
            .add("server_port") 
            .add(deployment.getInstrumentationConfiguration().getPort())
            .add(deployment.getServerBasePackage() + "/io.selendroid.ServerInstrumentation");
 
        logger.fine(startApplicationInstrumentationCommand.toString());
 
        try { 
            Monkey monkey = new Monkey(DroidiumNativeFileUtils.createRandomEmptyFile(DroidiumNativeFileUtils.getTmpDir()),
                deployment.getInstrumentedDeployment().getApplicationBasePackage(), true);
            device.executeShellCommand(startApplicationInstrumentationCommand.getAsString(), monkey);
            Monkey.wait(device, monkey, TOP_CMD);
            waitUntilSelendroidServerCommunication(port);
        } catch (Exception ex) {
            removePortForwarding(port, port);
            throw new AndroidExecutionException(ex.getMessage());
        } 
    } 
 
    /** 
     * Disables Selendroid server on running on Android device. 
     * 
     * @param deployment 
     * @throws IllegalArgumentException if {@code deployment} is a null object 
     */ 
    public void disable(SelendroidDeployment deployment) {
        Validate.notNull(deployment, "Selendroid deployment to disable can not be a null object!");
        device.executeShellCommand(new Command().addAsString("pm disable " + deployment.getServerBasePackage()).getAsString());
    } 
 
    /** 
     * Uninstalls Selendroid server from Android device. 
     * 
     * @param deployment 
     * @throws IllegalArgumentException if {@code deployment} is a null object 
     */ 
    public void uninstall(SelendroidDeployment deployment) {
        Validate.notNull(deployment, "Selendroid deployment to uninstall can not be a null object!");
        try { 
            Command command = new Command();
            command.addAsString("pm uninstall " + deployment.getServerBasePackage());
            device.executeShellCommand(command.getAsString());
        } catch (AndroidExecutionException ex) {
            throw new AndroidExecutionException("Unable to uninstall Selendroid server.", ex);
        } finally { 
            int port = Integer.parseInt(deployment.getInstrumentationConfiguration().getPort());
            removePortForwarding(port, port);
        } 
    } 
 
    /** 
     * Waits for the start of Selendroid server. 
     * 
     * After installation and execution of instrumentation command, we repeatedly send HTTP request to status page to get 
     * response code of 200 - server is up and running and we can proceed safely to testing process. 
     * 
     * @param port port to wait on the communication from installed Selendroid server 
     * @throws InvalidSelendroidPortException if {@code port} is invalid 
     */ 
    private void waitUntilSelendroidServerCommunication(int port) {
        validatePort(port);
        HttpClient httpClient = new DefaultHttpClient();
 
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIME_OUT_SECONDS * 1000);
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIME_OUT_SECONDS * 1000);
 
        HttpGet httpGet = new HttpGet(getSelendroidStatusURI(port));
 
        boolean connectionSuccess = false;
 
        for (int i = NUM_CONNECTION_RETIRES; i > 0; i--) {
            try { 
                HttpResponse response = httpClient.execute(httpGet);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    connectionSuccess = true;
                    break; 
                } 
                logger.log(Level.INFO, i + ": Response was not 200 from port " + port + ", response was: " + statusCode);
            } catch (ClientProtocolException e) {
                logger.log(Level.WARNING, e.getMessage());
            } catch (IOException e) {
                logger.log(Level.WARNING, e.getMessage());
            } 
        } 
 
        httpClient.getConnectionManager().shutdown();
 
        if (!connectionSuccess) {
            throw new AndroidExecutionException("Unable to get successful connection from Selendroid http server."); 
        } 
    } 
 
    private URI getSelendroidStatusURI(int port) {
        try { 
            return new URI("http://localhost:" + port + "/wd/hub/status");
        } catch (URISyntaxException e) {
            return null; 
        } 
    } 
 
    private void createPortForwarding(int from, int to) {
        validatePort(from);
        validatePort(to);
        logger.log(Level.FINE, "Creating port forwarding from {0} to {1}", new Object[] { from, to });
        device.createPortForwarding(from, to);
    } 
 
    private void removePortForwarding(int from, int to) {
        validatePort(from);
        validatePort(to);
        logger.log(Level.FINE, "Removing port forwarding from {0} to {1}", new Object[] { from, to });
        device.removePortForwarding(from, to);
    } 
 
    private void validatePort(int port) {
        if (port < 1024 || port > 65535) {