Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

In this page you can find the example usage for java.util Scanner close.

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:uk.ac.soton.itinnovation.sad.service.services.PluginsService.java

/**
 * Returns all plugins metadata directly from the file system.
 *
 * @return/* w w  w. j ava 2  s.  c o  m*/
 */
public final JSONObject getLivePluginsInfo() {

    logger.debug("Returning plugins using property name: " + PLUGIN_PROPERTY_NAME);

    //        JSONObject pluginsRoot = propertiesService.getLiveProperties().getJSONObject(PLUGIN_PROPERTY_NAME);
    String pluginsPath = configurationService.getConfiguration().getPluginsPath();

    logger.debug("Plugins folder filepath from configuration: " + pluginsPath);

    File pluginsFolder = new File(pluginsPath);
    JSONObject result = new JSONObject();

    if (!pluginsFolder.isDirectory()) {
        throw new RuntimeException(
                "Plugins path does not exist or not a folder: " + pluginsFolder.getAbsolutePath());

    } else {
        File filenamesInPluginsFolder[] = pluginsFolder.listFiles();

        File tempFile, testConfig;
        Scanner scanner;
        StringBuilder fileContents;
        String pluginConfigAsString;
        for (int i = 0; i < filenamesInPluginsFolder.length; i++) {

            tempFile = filenamesInPluginsFolder[i];

            if (tempFile.isDirectory()) {
                logger.debug("Looking in: " + tempFile.getAbsolutePath());

                testConfig = new File(
                        tempFile.getAbsolutePath() + fileSeparator + PLUGIN_CONFIGURATION_FILE_NAME);

                if (!testConfig.exists()) {
                    logger.debug("Missing configuration file in: " + tempFile.getAbsolutePath());

                } else {
                    logger.debug("Looking for plugin configuration in: " + tempFile.getAbsolutePath());

                    pluginConfigAsString = "";
                    fileContents = new StringBuilder((int) testConfig.length());

                    try {

                        scanner = new Scanner(testConfig);

                        while (scanner.hasNextLine()) {
                            fileContents.append(scanner.nextLine());
                            fileContents.append(lineSeparator);
                        }

                        scanner.close();

                        pluginConfigAsString = fileContents.toString();

                    } catch (FileNotFoundException ex) {
                        throw new RuntimeException("Failed to read plugin configuration file", ex);
                    }

                    if (pluginConfigAsString.length() < 1) {
                        logger.error("No configuration found in: " + tempFile.getAbsolutePath());

                    } else {
                        logger.debug("Found plugin configuration: " + pluginConfigAsString);
                        JSONObject foundConfig = JSONObject.fromObject(pluginConfigAsString);

                        boolean addPluginToResult = true;
                        if (foundConfig.containsKey("enabled")) {
                            if (foundConfig.getString("enabled").equals("n")) {
                                addPluginToResult = false;
                            }
                        }

                        if (addPluginToResult) {
                            logger.debug("Adding pluginFolder to configuration: " + tempFile.getName());
                            foundConfig.put("pluginFolder", tempFile.getName());

                            String pluginName;
                            if (foundConfig.containsKey("name")) {
                                pluginName = foundConfig.getString("name");

                                logger.debug("Saving plugin configuration with name: " + pluginName);

                                result.put(pluginName, foundConfig);

                            } else {
                                pluginName = tempFile.getName();

                                logger.error("Plugin name not found in configuration! Saving with file name: "
                                        + pluginName);

                                result.put(pluginName, foundConfig);
                            }
                        } else {
                            logger.debug("Plugin disabled in configuration, skipping: "
                                    + foundConfig.getString("name"));
                        }
                    }
                }

            } else {
                logger.debug("Ignoring: " + tempFile.getAbsolutePath());

            }
        }

    }

    return result;
}

From source file:commondb.mock.MockResultSet.java

public void loadCSV(Readable in) throws SQLException {
    final Scanner sc = new Scanner(in);

    if (!sc.hasNextLine()) {
        throw new SQLException("empty data source");
    }/*from  www.  jav  a  2 s  .c o m*/

    // load column headers
    String line = sc.nextLine();
    int index = 1;
    for (String column : splitter.split(line)) {
        columnMap.put(column, index);
        index++;
    }

    // load data
    while (sc.hasNextLine()) {
        line = sc.nextLine();

        String[] row = splitter.split(line);
        rowset.add(row);
    }

    sc.close();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function acquire a newly issued ticket from SPFE.</p>
 *
 * @return Ticket issued by the SPFE/* w w  w  .  ja  v a 2  s. c om*/
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String getTicket() throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();

    Scanner scanner = new Scanner(makeRequest("GET", "get", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();

    return sb.toString();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function check the authentication of the ticket.</p>
 *
 * @param ticket The ticket to validate.
 * @return Returns current ticket or newly issued ticket. The new ticket should be used in future 
 * operations with the SPFE.//from   w  w w  .j a  v  a  2s. c om
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String putTicket(String ticket) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("ticket", ticket);

    Scanner scanner = new Scanner(makeRequest("GET", "put", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();
    return sb.toString();
}

From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java

private static Map<String, Integer> mapDeviceNamesToSerial() {
    Map<String, Integer> mapping = new HashMap<String, Integer>();
    CommandLine command = new CommandLine(AndroidSdk.adb());
    command.addArgument("devices");
    Scanner scanner;
    try {//www . j a  v a 2 s.c  o m
        scanner = new Scanner(ShellCommand.exec(command));
    } catch (ShellCommandException e) {
        return mapping;
    }
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        Pattern pattern = Pattern.compile("emulator-\\d\\d\\d\\d");
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String serial = matcher.group(0);

            Integer port = Integer.valueOf(serial.replaceAll("emulator-", ""));
            TelnetClient client = null;
            try {
                client = new TelnetClient(port);
                String avdName = client.sendCommand("avd name");
                mapping.put(avdName, port);
            } catch (AndroidDeviceException e) {
                // ignore
            } finally {
                if (client != null) {
                    client.close();
                }
            }
            Socket socket = null;
            PrintWriter out = null;
            BufferedReader in = null;
            try {
                socket = new Socket("127.0.0.1", port);
                out = new PrintWriter(socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                if (in.readLine() == null) {
                    throw new AndroidDeviceException("error");
                }

                out.write("avd name\r\n");
                out.flush();
                in.readLine();// OK
                String avdName = in.readLine();
                mapping.put(avdName, port);
            } catch (Exception e) {
                // ignore
            } finally {
                try {
                    out.close();
                    in.close();
                    socket.close();
                } catch (Exception e) {
                    // do nothing
                }
            }
        }
    }
    scanner.close();

    return mapping;
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Gets an id of the user from the Service Provider Front End. This ID is unique for one
 * Service Provider, and different for different Service Providers.</p>
 *
 * @param ticket Ticket issued by the SPFE
 *
 * @return PUID issued by the SPFE/*  w w  w  .  j a  va  2  s  .  co  m*/
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String getPUID(String ticket) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("ticket", ticket);

    Scanner scanner = new Scanner(makeRequest("GET", "puid", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();
    return sb.toString();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function acquire a newly issued ticket from SPFE.</p>
 *
 * @param auth_type Defines which credentials will be asked of the user to authorize this ticket. 
 * Currently only two values supported: 'p': to ask for PassKey and password; empty string to ask 
 * for PassKey only (default)./* w w w.  j  a v  a 2  s  . c  om*/
 * @return Ticket issued by the SPFE
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String getTicket(String auth_type) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("auth_type", auth_type);

    Scanner scanner = new Scanner(makeRequest("GET", "get", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();

    return sb.toString();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function acquire a newly issued ticket from SPFE.</p>
 *
 * @param ttl The period in seconds for the ticket to remain valid since issuance. 
 * The default value is 120 seconds.//from  w  w w.j  a v  a2 s .  co  m
 * @return Ticket issued by the SPFE
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String getTicket(int ttl) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("ttl", Integer.toString(ttl));

    Scanner scanner = new Scanner(makeRequest("GET", "get", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();

    return sb.toString();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function check the authentication of the ticket.</p>
 *
 * @param ticket The ticket to validate.
 * @param auth_type Defines which credentials will be asked of the user to authorize this ticket. 
 * Currently only two values supported: 'p': to ask for PassKey and password; empty string to ask 
 * for PassKey only (default).//from ww  w  . jav a  2s.com
 * @return Returns current ticket or newly issued ticket. The new ticket should be used in future 
 * operations with the SPFE.
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String putTicket(String ticket, String auth_type) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("ticket", ticket);
    parameters.put("auth_type", auth_type);

    Scanner scanner = new Scanner(makeRequest("GET", "put", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();
    return sb.toString();
}

From source file:com.wwpass.connection.WWPassConnection.java

/**
 * <p>Calls to this function check the authentication of the ticket.</p>
 *
 * @param ticket The ticket to validate.
 * @param ttl The period in seconds for the ticket to remain valid since issuance. The default 
 * value is 120 seconds.// w w  w  . ja  v  a 2 s.  c  o m
 * @return Returns current ticket or newly issued ticket. The new ticket should be used in future 
 * operations with the SPFE.
 * @throws IOException
 * @throws WWPassProtocolException
 */
public String putTicket(String ticket, int ttl) throws IOException, WWPassProtocolException {
    HashMap<String, String> parameters = new HashMap<String, String>();
    parameters.put("ticket", ticket);
    parameters.put("ttl", Integer.toString(ttl));

    Scanner scanner = new Scanner(makeRequest("GET", "put", parameters));
    StringBuilder sb = new StringBuilder();

    while (scanner.hasNextLine()) {
        sb.append(scanner.nextLine());
    }
    scanner.close();
    return sb.toString();
}