Java HTTP Port Find writeContactFile(int port, String appName)

Here you can find the source of writeContactFile(int port, String appName)

Description

Write contact details to a file only readable by the owner process and privileged local users.

License

LGPL

Parameter

Parameter Description
port the port number being used by a server process that is listening for connections.

Return

the cookie for authenticating connections. This is null if a failure to write the contact file is encountered.

Declaration

public static String writeContactFile(int port, String appName) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.FileOutputStream;

import java.io.PrintStream;

import java.net.InetAddress;

import java.util.Properties;

public class Main {
    /**//w ww.  j  ava  2 s .  co  m
     * System properties
     */
    static Properties javaProp = System.getProperties();

    /**
     * Write contact details to a file only readable by the owner
     * process and privileged local users.
     * <p>
     * These details are used to authenticate any requests.
     * <p>
     * The file contains the simple line:
     * <pre>
     *    hostname port_number cookie
     * </pre>
     * The cookie is generated by this method and returned as its
     * result.
     *
     * @param port the port number being used by a server process that
     *             is listening for connections.
     * @return the cookie for authenticating connections. This is null
     *         if a failure to write the contact file is encountered.
     */
    public static String writeContactFile(int port, String appName) {
        String cookie = null;

        //  Open the contact file. This needs protection from prying
        //  eyes, so the next is _UNIX_ specific (TODO: something
        //  about this?).  The file is re-created every time, so
        //  connections are only available to the last instance.
        File contactFile = getConfigFile(appName + ".remote");
        if (contactFile != null) {
            try {
                contactFile.createNewFile();
                Runtime.getRuntime().exec("chmod 600 " + contactFile.getPath());
            } catch (Exception e) {
                // Do nothing, chmod can fail validly under Windows.
                //e.printStackTrace();
            }

            //  Add the information we want.
            try {
                PrintStream out = new PrintStream(new FileOutputStream(contactFile));
                InetAddress addr = InetAddress.getLocalHost();
                String hexVal = Integer.toHexString((int) (Math.random() * 12345));
                cookie = hexVal + addr.hashCode();
                out.println(addr.getHostName() + " " + port + " " + cookie);

            } catch (Exception e) {
                //  Do nothing
            }
        }
        //  Return the cookie which should be used to authenticate any
        //  remote connections.
        return cookie;
    }

    /**
     * Construct the proper name of a file stored in the configuration
     * directory. 
     *
     * @param name the name of the file to be stored/located in the
     *             the configuration directory.
     */
    public static File getConfigFile(String name) {
        return new File(getConfigDirectory(), name);
    }

    /**
     * The name of the directory used for storing configuration
     * information. This directory is created if it doesn't exist
     * already.
     */
    public static File getConfigDirectory() {
        File dir = null;
        if (javaProp.getProperty("adam.user") != null) {
            dir = new File(javaProp.getProperty("adam.user"));
        } else {
            dir = new File(javaProp.getProperty("user.home"), ".soap");
        }
        if (!dir.exists()) {
            try {
                dir.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                dir = null;
            }
        } else if (!dir.isDirectory()) {
            System.err.println(
                    "Cannot create a directory: " + dir.getName() + "as a file with " + "this name already exists");
            dir = null;
        }
        return dir;
    }
}

Related

  1. waitForPort(String host, int port)
  2. waitForResponseCode(int code, String name, String host, int port)
  3. waitForRespose(String name, String host, int port)
  4. waitForServerDown(String host, int port, long timeout)
  5. waitForService(int port)