Example usage for com.google.common.io Resources readLines

List of usage examples for com.google.common.io Resources readLines

Introduction

In this page you can find the example usage for com.google.common.io Resources readLines.

Prototype

public static List<String> readLines(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all of the lines from a URL.

Usage

From source file:com.cloudbees.sdk.Tool.java

public static void main(String[] args) throws Exception {

    Map<String, Account> accountsById = new HashMap<String, Account>();

    {//  ww  w . j  av  a  2s  .c  o  m
        List<String> lines = Resources.readLines(
                Thread.currentThread().getContextClassLoader().getResource("account-databases.txt"),
                Charsets.ISO_8859_1);

        for (String line : lines) {
            String[] splitted = line.split(" ");
            String accountId = splitted[0];

            Account account = accountsById.get(accountId);
            if (account == null) {
                account = new Account();
                account.accountId = accountId;
                accountsById.put(account.accountId, account);
            }

            for (int i = 1; i < splitted.length; i++) {
                account.databases.add(splitted[i]);
            }
        }
    }
    {
        List<String> lines = Resources.readLines(
                Thread.currentThread().getContextClassLoader().getResource("account-emails.txt"),
                Charsets.ISO_8859_1);

        for (String line : lines) {
            String[] splitted = line.split(" ");
            String accountId = splitted[0];

            Account account = accountsById.get(accountId);
            if (account == null) {
                account = new Account();
                account.accountId = accountId;
                accountsById.put(account.accountId, account);
            }

            for (int i = 1; i < splitted.length; i++) {
                account.emails.add(splitted[i]);
            }
        }
    }

    for (Account account : accountsById.values()) {

        for (String email : account.emails) {
            System.out
                    .println(email + "\t" + account.accountId + "\t" + Joiner.on(", ").join(account.databases));

        }
    }
}

From source file:fr.xebia.demo.amazon.aws.AmazonAwsSesEmailVerifier.java

public static void main(String[] args) throws Exception {
    InputStream credentialsAsStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("AwsCredentials.properties");
    Preconditions.checkNotNull(credentialsAsStream,
            "File 'AwsCredentials.properties' NOT found in the classpath");
    AWSCredentials awsCredentials = new PropertiesCredentials(credentialsAsStream);

    AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(awsCredentials);

    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader().getResource("emails-to-verify.txt");
    List<String> emailsToVerify = Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1);

    for (String emailToVerify : emailsToVerify) {
        System.out.println(emailToVerify);
        Thread.sleep(10 * 1000);//www  . j  av  a2 s .com
        ses.verifyEmailAddress(new VerifyEmailAddressRequest().withEmailAddress(emailToVerify));
    }
}

From source file:com.cloudbees.api.Main.java

public static void main(String[] args) throws Exception {

    File beesCredentialsFile = new File(System.getProperty("user.home"), ".bees/bees.config");
    Preconditions.checkArgument(beesCredentialsFile.exists(), "File %s not found", beesCredentialsFile);
    Properties beesCredentials = new Properties();
    beesCredentials.load(new FileInputStream(beesCredentialsFile));
    String apiUrl = "https://api.cloudbees.com/api";
    String apiKey = beesCredentials.getProperty("bees.api.key");
    String secret = beesCredentials.getProperty("bees.api.secret");
    BeesClient client = new BeesClient(apiUrl, apiKey, secret, "xml", "1.0");
    client.setVerbose(false);/*from w  w w.  j  a  va 2  s .  com*/

    URL databasesUrl = Thread.currentThread().getContextClassLoader().getResource("databases.txt");
    Preconditions.checkNotNull(databasesUrl, "File 'databases.txt' NOT found in the classpath");

    Collection<String> databaseNames;
    try {
        databaseNames = Sets.newTreeSet(Resources.readLines(databasesUrl, Charsets.ISO_8859_1));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }

    databaseNames = Collections2.transform(databaseNames, new Function<String, String>() {
        @Nullable
        @Override
        public String apply(@Nullable String input) {
            // {host_db_create,<<"tco_q5rm">>,<<"TCO_q5rm">>,

            if (input == null)
                return null;

            if (input.startsWith("#"))
                return null;

            if (input.indexOf('"') == -1) {
                logger.warn("Skip invalid line {}", input);
                return null;
            }
            input = input.substring(input.indexOf('"') + 1);
            if (input.indexOf('"') == -1) {
                logger.warn("Skip invalid line {}", input);
                return null;
            }
            return input.substring(0, input.indexOf('"'));

        }
    });
    databaseNames = Collections2.filter(databaseNames, new Predicate<String>() {
        @Override
        public boolean apply(@Nullable String s) {
            return !Strings.isNullOrEmpty(s);
        }
    });

    Multimap<String, String> databasesByAccount = ArrayListMultimap.create();

    Class.forName("com.mysql.jdbc.Driver");

    for (String databaseName : databaseNames) {
        try {
            DatabaseInfo databaseInfo = client.databaseInfo(databaseName, true);
            databasesByAccount.put(databaseInfo.getOwner(), databaseInfo.getName());
            logger.debug("Evaluate " + databaseInfo.getName());

            if (true == false) {
                // Hibernate
                logger.info("Hibernate {}", databaseName);
                Map<String, String> params = new HashMap<String, String>();
                params.put("database_id", databaseName);
                String url = client.getRequestURL("database.hibernate", params);
                String response = client.executeRequest(url);
                DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response);
                logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(),
                        apiResponse.getDatabaseInfo().getStatus());

            }
            if (true == false) {
                // Hibernate
                logger.info("Activate {}", databaseName);
                Map<String, String> params = new HashMap<String, String>();
                params.put("database_id", databaseName);
                String url = client.getRequestURL("database.activate", params);
                String response = client.executeRequest(url);
                DatabaseInfoResponse apiResponse = (DatabaseInfoResponse) client.readResponse(response);
                logger.info("DB {} status: {}", apiResponse.getDatabaseInfo().getName(),
                        apiResponse.getDatabaseInfo().getStatus());
            }

            String dbUrl = "jdbc:mysql://" + databaseInfo.getMaster() + "/" + databaseInfo.getName();
            logger.info("Connect to {} user={}", dbUrl, databaseInfo.getUsername());
            Connection cnn = DriverManager.getConnection(dbUrl, databaseInfo.getUsername(),
                    databaseInfo.getPassword());
            cnn.setAutoCommit(false);
            cnn.close();

        } catch (Exception e) {
            logger.warn("Exception for {}", databaseName, e);
        }
    }

    System.out.println("OWNERS");
    for (String account : databasesByAccount.keySet()) {
        System.out.println(account + ": " + Joiner.on(", ").join(databasesByAccount.get(account)));
    }

}

From source file:com.technobium.MultinomialLogisticRegression.java

public static void main(String[] args) throws Exception {
    // this test trains a 3-way classifier on the famous Iris dataset.
    // a similar exercise can be accomplished in R using this code:
    //    library(nnet)
    //    correct = rep(0,100)
    //    for (j in 1:100) {
    //      i = order(runif(150))
    //      train = iris[i[1:100],]
    //      test = iris[i[101:150],]
    //      m = multinom(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, train)
    //      correct[j] = mean(predict(m, newdata=test) == test$Species)
    //    }/*from   w ww.  j ava2s .c  om*/
    //    hist(correct)
    //
    // Note that depending on the training/test split, performance can be better or worse.
    // There is about a 5% chance of getting accuracy < 90% and about 20% chance of getting accuracy
    // of 100%
    //
    // This test uses a deterministic split that is neither outstandingly good nor bad

    RandomUtils.useTestSeed();
    Splitter onComma = Splitter.on(",");

    // read the data
    List<String> raw = Resources.readLines(Resources.getResource("iris.csv"), Charsets.UTF_8);

    // holds features
    List<Vector> data = Lists.newArrayList();

    // holds target variable
    List<Integer> target = Lists.newArrayList();

    // for decoding target values
    Dictionary dict = new Dictionary();

    // for permuting data later
    List<Integer> order = Lists.newArrayList();

    for (String line : raw.subList(1, raw.size())) {
        // order gets a list of indexes
        order.add(order.size());

        // parse the predictor variables
        Vector v = new DenseVector(5);
        v.set(0, 1);
        int i = 1;
        Iterable<String> values = onComma.split(line);
        for (String value : Iterables.limit(values, 4)) {
            v.set(i++, Double.parseDouble(value));
        }
        data.add(v);

        // and the target
        target.add(dict.intern(Iterables.get(values, 4)));
    }

    // randomize the order ... original data has each species all together
    // note that this randomization is deterministic
    Random random = RandomUtils.getRandom();
    Collections.shuffle(order, random);

    // select training and test data
    List<Integer> train = order.subList(0, 100);
    List<Integer> test = order.subList(100, 150);
    logger.warn("Training set = {}", train);
    logger.warn("Test set = {}", test);

    // now train many times and collect information on accuracy each time
    int[] correct = new int[test.size() + 1];
    for (int run = 0; run < 200; run++) {
        OnlineLogisticRegression lr = new OnlineLogisticRegression(3, 5, new L2(1));
        // 30 training passes should converge to > 95% accuracy nearly always but never to 100%
        for (int pass = 0; pass < 30; pass++) {
            Collections.shuffle(train, random);
            for (int k : train) {
                lr.train(target.get(k), data.get(k));
            }
        }

        // check the accuracy on held out data
        int x = 0;
        int[] count = new int[3];
        for (Integer k : test) {
            Vector vt = lr.classifyFull(data.get(k));
            int r = vt.maxValueIndex();
            count[r]++;
            x += r == target.get(k) ? 1 : 0;
        }
        correct[x]++;

        if (run == 199) {

            Vector v = new DenseVector(5);
            v.set(0, 1);
            int i = 1;
            Iterable<String> values = onComma.split("6.0,2.7,5.1,1.6,versicolor");
            for (String value : Iterables.limit(values, 4)) {
                v.set(i++, Double.parseDouble(value));
            }

            Vector vt = lr.classifyFull(v);
            for (String value : dict.values()) {
                System.out.println("target:" + value);
            }
            int t = dict.intern(Iterables.get(values, 4));

            int r = vt.maxValueIndex();
            boolean flag = r == t;
            lr.close();

            Closer closer = Closer.create();

            try {
                FileOutputStream byteArrayOutputStream = closer
                        .register(new FileOutputStream(new File("model.txt")));
                DataOutputStream dataOutputStream = closer
                        .register(new DataOutputStream(byteArrayOutputStream));
                PolymorphicWritable.write(dataOutputStream, lr);
            } finally {
                closer.close();
            }
        }
    }

    // verify we never saw worse than 95% correct,
    for (int i = 0; i < Math.floor(0.95 * test.size()); i++) {
        System.out.println(String.format("%d trials had unacceptable accuracy of only %.0f%%: ", correct[i],
                100.0 * i / test.size()));
    }
    // nor perfect
    System.out.println(String.format("%d trials had unrealistic accuracy of 100%%", correct[test.size() - 1]));
}

From source file:org.testcontainers.utility.LicenseAcceptance.java

public static void assertLicenseAccepted(final String imageName) {
    try {/*from   w w  w.  j a  v a2  s . c o  m*/
        final URL url = Resources.getResource(ACCEPTANCE_FILE_NAME);
        final List<String> acceptedLicences = Resources.readLines(url, Charsets.UTF_8);

        if (acceptedLicences.stream().map(String::trim).anyMatch(imageName::equals)) {
            return;
        }
    } catch (Exception ignored) {
        // suppressed
    }

    throw new IllegalStateException("The image " + imageName + " requires you to accept a license agreement. "
            + "Please place a file at the root of the classpath named " + ACCEPTANCE_FILE_NAME + ", e.g. at "
            + "src/test/resources/" + ACCEPTANCE_FILE_NAME + ". This file should contain the line:\n  "
            + imageName);

}

From source file:com.madvay.tools.android.perf.apat.Main.java

private static void printUsage() {
    try {/*from  w  w  w  .  ja va 2  s .co  m*/
        List<String> lines = Resources.readLines(Resources.getResource("README.txt"), Charsets.UTF_8);
        boolean foundUsage = false, foundBeginHash = false;

        /*
         * We want to output the lines of usage in the README.md file:
                
        <other stuff>
        ## Usage
        ```
        <stuff to display>
        ```
        <other stuff>
                
         */
        lineLoop: for (String l : lines) {
            if (foundUsage) {
                if (foundBeginHash) {
                    if (l.equals("```")) {
                        // end!
                        break lineLoop;
                    }
                    outln(l);
                }
                if (l.equals("```")) {
                    foundBeginHash = true;
                }
            }
            if (l.equals("## Usage")) {
                foundUsage = true;
            }
        }
    } catch (IOException err) {
        err(err);
    }
}

From source file:ec.util.jdbc.SqlKeywords.java

private static Set<String> loadWords(String resourceName) {
    try {/*www  .  ja  va 2s . c om*/
        Set<String> result = new HashSet<>();
        for (String o : Resources.readLines(Resources.getResource(resourceName), StandardCharsets.UTF_8)) {
            result.add(o);
        }
        return Collections.unmodifiableSet(result);
    } catch (IOException ex) {
        throw new RuntimeException("Missing resource '" + resourceName + "'", ex);
    }
}

From source file:org.apache.provisionr.amazon.core.ImageTable.java

/**
 * Load the list of AMIs from a resource file (csv format)
 * <p/>/*from  ww  w . j  a v a  2s  . c  o m*/
 * Note: the parser is doing only split by comma. There is no
 * support for escaping line components
 *
 * @param resource path to resource
 * @return an instance of {@see ImageTable}
 * @throws IOException
 */
public static ImageTable fromCsvResource(String resource) throws IOException {
    checkNotNull(resource, "resource is null");

    List<String> lines = Resources.readLines(Resources.getResource(ImageTable.class, resource), Charsets.UTF_8);
    checkArgument(!lines.isEmpty(), "the resource is an empty file");

    final ImmutableTable.Builder<String, String, String> table = ImmutableTable.builder();
    final Iterable<String> headers = extractHeaders(lines);

    int index = 0;
    for (String line : Iterables.skip(lines, 1)) {
        final Iterable<Table.Cell<String, String, String>> cells = combineHeadersWithLinePartsAsTableCells(
                index, headers, COMMA.split(line));
        for (Table.Cell<String, String, String> cell : cells) {
            table.put(cell);
        }
        index++;
    }

    return new ImageTable(table.build());
}

From source file:org.springframework.data.cassandra.support.CqlDataSet.java

@SneakyThrows
private List<String> getLines() {
    return Resources.readLines(location, Charset.defaultCharset());
}

From source file:edu.si.sidora.excel2tabular.integration.IntegrationTestUtilities.java

protected static List<String> readLines(final URL result) throws IOException {
    return Resources.readLines(result, UTF_8);
}