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:fr.xebia.demo.amazon.aws.AmazonAwsIamAccountCreatorV2.java

/**
 * Builds difference between list of emails provided in
 * "accounts-to-create.txt" and the already created users (obtained via
 * {@link AmazonIdentityManagement#listUsers()}).
 *///from  w  w w . j  a  va  2  s . c  om
public Set<String> buildUserNamesToCreate() {
    List<String> existingUserNames = Lists.transform(iam.listUsers().getUsers(), new Function<User, String>() {
        @Override
        public String apply(User user) {
            return user.getUserName();
        }
    });

    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader()
            .getResource("accounts-to-create.txt");
    Preconditions.checkNotNull(emailsToVerifyURL, "File 'accounts-to-create.txt' NOT found in the classpath");
    List<String> userNamesToCreate;
    try {
        userNamesToCreate = Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    return Sets.difference(Sets.newHashSet(userNamesToCreate), Sets.newHashSet(existingUserNames));

}

From source file:org.apache.ctakes.temporal.ae.ConstituencyBasedTimeAnnotator.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    CombinedExtractor1<BaseToken> charExtractors = new CombinedExtractor1<>(
            CharacterCategoryPatternFunction.<BaseToken>createExtractor(PatternType.REPEATS_MERGED),
            CharacterCategoryPatternFunction.<BaseToken>createExtractor(PatternType.ONE_PER_CHAR));

    this.wordTypes = Maps.newHashMap();
    URL url = TimeWordsExtractor.class.getResource(LOOKUP_PATH);
    try {//from w  ww .j a  v a2 s.  c o m
        for (String line : Resources.readLines(url, Charsets.US_ASCII)) {
            String[] typeAndWord = line.split("\\s+");
            if (typeAndWord.length != 2) {
                throw new IllegalArgumentException("Expected '<type> <word>', found: " + line);
            }
            this.wordTypes.put(typeAndWord[1], typeAndWord[0]);
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }

    CombinedExtractor1<BaseToken> allExtractors = new CombinedExtractor1<>(
            new CoveredTextExtractor<BaseToken>(),
            //        new TimeWordTypeExtractor(),
            charExtractors, new TypePathExtractor<>(BaseToken.class, "partOfSpeech"));

    featureExtractors = new ArrayList<FeatureExtractor1>();
    //    featureExtractors.add(new CleartkExtractor(BaseToken.class, new CoveredTextExtractor(), new Bag(new Covered())));
    featureExtractors.add(new CleartkExtractor(BaseToken.class, allExtractors, new Bag(new Covered())));
    //    featureExtractors.add(charExtractors);
    wordTypeExtractor = new CleartkExtractor(BaseToken.class, new TimeWordTypeExtractor<BaseToken>(),
            new Bag(new Covered()));
    //    featureExtractors.add(new CleartkExtractor(BaseToken.class, new CoveredTextExtractor(), new Bag(new Preceding(1))));
    //   featureExtractors.add(new CleartkExtractor(BaseToken.class, new CoveredTextExtractor(), new Bag(new Following(1))));
    // bag of constituent descendent labels
    //    featureExtractors.add(new CleartkExtractor(TreebankNode.class, new TypePathExtractor(TreebankNode.class, "nodeType"), new Bag(new Covered())));

}

From source file:fr.xebia.cloud.amazon.aws.tools.AmazonAwsToolsSender.java

public void sendEmails() {
    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader().getResource("users-to-notify.txt");
    Preconditions.checkNotNull(emailsToVerifyURL, "File 'users-to-notify.txt' NOT found in the classpath");
    Collection<String> userNames;
    try {/* w w  w.ja  v a  2 s  .c  o  m*/
        userNames = Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    for (String userName : userNames) {
        try {
            sendEmail(userName);
        } catch (Exception e) {
            logger.error("Failure to send email to user '{}'", userName, e);
        }

        // sleep 10 seconds to prevent "Throttling exception"
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }
}

From source file:com.twitter.common.runtime.NativeLoader.java

private Iterable<NativeResource> findNativeResources() throws IOException {
    Enumeration<URL> resourcesEnumeration = getClass().getClassLoader().getResources("META-INF/native.mf");

    Set<NativeResource> resources = Sets.newLinkedHashSet();
    while (resourcesEnumeration.hasMoreElements()) {
        URL manifestUrl = resourcesEnumeration.nextElement();
        for (String line : Resources.readLines(manifestUrl, Charsets.UTF_8)) {
            String normalizedLine = line.trim();
            if (!normalizedLine.startsWith("#")) {
                NativeResource nativeResource = NativeResource.parse(libPath, deleteExtractedOnExit,
                        normalizedLine);
                if (!resources.add(nativeResource)) {
                    throw new IllegalStateException(
                            "Already detected a native resource for " + normalizedLine + " in " + manifestUrl);
                }//from   w w w  .  jav a2  s . com
            }
        }
    }
    LOG.info("Found native resources: " + resources);
    return resources;
}

From source file:com.google.api.tools.framework.snippet.SnippetSet.java

/**
 * Returns an input supplier which works on the given resource root path. All input names are
 * resolved as resource paths relative to this root, and read from the class path.
 *//*from   w  w w. j  av a2  s .com*/
public static InputSupplier resourceInputSupplier(final String root) {
    return new InputSupplier() {
        @Override
        public Iterable<String> readInput(String snippetSetName) throws IOException {
            try {
                return Resources.readLines(Resources.getResource(root + "/" + snippetSetName), UTF_8);
            } catch (Exception e) {
                throw new IOException(e);
            }
        }
    };
}

From source file:org.languagetool.rules.spelling.hunspell.HunspellRule.java

private void addIgnoreWords() throws IOException {
    hunspellDict.addWord(SpellingCheckRule.LANGUAGETOOL);
    hunspellDict.addWord(SpellingCheckRule.LANGUAGETOOLER);
    URL ignoreUrl = JLanguageTool.getDataBroker().getFromResourceDirAsUrl(getIgnoreFileName());
    List<String> ignoreLines = Resources.readLines(ignoreUrl, Charsets.UTF_8);
    for (String ignoreLine : ignoreLines) {
        if (!ignoreLine.startsWith("#")) {
            hunspellDict.addWord(ignoreLine);
        }// w  w  w .  j a  va  2 s .com
    }
}

From source file:fr.xebia.cloud.amazon.aws.iam.AmazonAwsIamAccountCreator.java

public void createUsers(String groupName, String keyPairName) {

    GetGroupResult groupDescriptor = iam.getGroup(new GetGroupRequest(groupName));

    URL emailsToVerifyURL = Thread.currentThread().getContextClassLoader()
            .getResource("accounts-to-create.txt");
    Preconditions.checkNotNull(emailsToVerifyURL, "File 'accounts-to-create.txt' NOT found in the classpath");
    Collection<String> userNames;
    try {//from   w w w.  j  ava  2 s .c o m
        userNames = Sets.newTreeSet(Resources.readLines(emailsToVerifyURL, Charsets.ISO_8859_1));
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    userNames = Collections2.filter(userNames, new Predicate<String>() {
        @Override
        public boolean apply(@Nullable String s) {
            return !Strings.isNullOrEmpty(s);
        }
    });
    for (String userName : userNames) {
        try {
            createUser(userName, groupDescriptor, keyPairName);
        } catch (Exception e) {
            logger.error("Failure to create user '{}'", userName, e);
        }

        // sleep 10 seconds to prevent "Throttling exception"
        try {
            Thread.sleep(10 * 1000);
        } catch (InterruptedException e) {
            throw Throwables.propagate(e);
        }
    }
}

From source file:co.rsk.bitcoinj.core.Utils.java

/**
 * Reads and joins together with LF char (\n) all the lines from given file. It's assumed that file is in UTF-8.
 *///from  ww w. j ava2 s. c  o  m
public static String getResourceAsString(URL url) throws IOException {
    List<String> lines = Resources.readLines(url, Charsets.UTF_8);
    return Joiner.on('\n').join(lines);
}