Example usage for com.google.common.io Files write

List of usage examples for com.google.common.io Files write

Introduction

In this page you can find the example usage for com.google.common.io Files write.

Prototype

public static void write(CharSequence from, File to, Charset charset) throws IOException 

Source Link

Usage

From source file:com.cloudera.oryx.als.computation.iterate.row.RowStep.java

@Override
protected MRPipeline createPipeline() throws IOException {

    IterationState iterationState = getIterationState();
    String iterationKey = iterationState.getIterationKey();
    boolean x = iterationState.isComputingX();
    int lastIteration = iterationState.getIteration() - 1;
    Store store = Store.get();//from w w w.j a  va2 s . c  o  m

    JobStepConfig config = getConfig();
    String instanceDir = config.getInstanceDir();
    int generationID = config.getGenerationID();

    if (store.exists(Namespaces.getInstanceGenerationPrefix(instanceDir, generationID) + "X/", false)) {
        // Actually, looks like whole computation of X/Y finished -- just proceed
        return null;
    }

    // Take the opportunity to clean out iteration before last, if computing X
    if (x) {
        String lastLastIterationKey = Namespaces.getIterationsPrefix(instanceDir, generationID)
                + (lastIteration - 1) + '/';
        if (store.exists(lastLastIterationKey, false)) {
            log.info("Deleting old iteration data from {}", lastLastIterationKey);
            store.recursiveDelete(lastLastIterationKey);
        }
    }

    String yKey;
    if (x) {
        yKey = Namespaces.getIterationsPrefix(instanceDir, generationID) + lastIteration + "/Y/";
    } else {
        yKey = iterationKey + "X/";
    }

    String xKey = iterationKey + (x ? "X/" : "Y/");
    String tempKey = Namespaces.getTempPrefix(instanceDir, generationID);
    String rKey = tempKey + (x ? "userVectors/" : "itemVectors/");

    if (!validOutputPath(xKey)) {
        return null;
    }

    MRPipeline p = createBasicPipeline(RowReduceFn.class);
    Configuration conf = p.getConfiguration();
    conf.set(Y_KEY_KEY, yKey);

    String popularKey = tempKey + (x ? "popularItemsByUserPartition/" : "popularUsersByItemPartition/");
    conf.set(POPULAR_KEY, popularKey);

    String testPrefix = Namespaces.getInstanceGenerationPrefix(instanceDir, generationID) + "test/";
    conf.set(MAP_KEY, testPrefix);

    YState yState = new YState(ALSTypes.DENSE_ROW_MATRIX); // Shared Y-Matrix state

    GroupingOptions opts = groupingOptions();
    PCollection<MatrixRow> matrix = PTables.asPTable(p.read(input(rKey, ALSTypes.SPARSE_ROW_MATRIX)))
            .groupByKey(opts).parallelDo("rowReduce", new RowReduceFn(yState), ALSTypes.DENSE_ROW_MATRIX)
            .write(output(xKey));

    if (!x) {
        matrix.parallelDo("asPair", MatrixRow.AS_PAIR, Avros.tableOf(Avros.longs(), ALSTypes.FLOAT_ARRAY))
                .parallelDo("convergenceSample", new ConvergenceSampleFn(yState), Avros.strings())
                .write(compressedTextOutput(p.getConfiguration(), iterationKey + "Yconvergence"));
    }

    if (x && ConfigUtils.getDefaultConfig().getDouble("model.test-set-fraction") > 0.0
            && store.exists(testPrefix, false)) {
        PCollection<Double> aps = matrix
                .parallelDo("asPair", MatrixRow.AS_PAIR, Avros.tableOf(Avros.longs(), ALSTypes.FLOAT_ARRAY))
                .parallelDo("computeAP", new ComputeUserAPFn(yState), Avros.doubles());
        Mean meanAveragePrecision = new Mean();
        for (double ap : aps.materialize()) {
            meanAveragePrecision.increment(ap);
        }
        log.info("Mean average precision: {}", meanAveragePrecision.getResult());

        File tempMAPFile = File.createTempFile("MAP", ".txt");
        tempMAPFile.deleteOnExit();
        Files.write(Double.toString(meanAveragePrecision.getResult()), tempMAPFile, StandardCharsets.UTF_8);
        store.upload(iterationKey + "MAP", tempMAPFile, false);
        IOUtils.delete(tempMAPFile);
    }

    return p;
}

From source file:com.google.api.server.spi.tools.GetOpenApiDocAction.java

/**
 * Generates an OpenAPI document for an array of service classes.
 *
 * @param classPath Class path to load service classes and their dependencies
 * @param outputFilePath File to store the OpenAPI document in
 * @param hostname The hostname to use for the OpenAPI document
 * @param basePath The base path to use for the OpenAPI document, e.g. /_ah/api
 * @param serviceClassNames Array of service class names of the API
 * @param outputToDisk Iff {@code true}, outputs a openapi.json to disk.
 * @return a single OpenAPI document representing all service classes.
 *///from  www.ja va  2  s.  c  om
public String genOpenApiDoc(URL[] classPath, String outputFilePath, String hostname, String basePath,
        List<String> serviceClassNames, boolean outputToDisk)
        throws ClassNotFoundException, IOException, ApiConfigException {
    File outputDir = new File(outputFilePath).getParentFile();
    if (!outputDir.isDirectory()) {
        throw new IllegalArgumentException(outputFilePath + " is not a file");
    }

    ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
    ApiConfig.Factory configFactory = new ApiConfig.Factory();
    Class<?>[] serviceClasses = loadClasses(classLoader, serviceClassNames);
    List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClasses.length);
    TypeLoader typeLoader = new TypeLoader(classLoader);
    ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader,
            new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
    ServiceContext serviceContext = ServiceContext.create();
    for (Class<?> serviceClass : serviceClasses) {
        apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
    }
    SwaggerGenerator generator = new SwaggerGenerator();
    SwaggerContext swaggerContext = new SwaggerContext().setHostname(hostname).setBasePath(basePath);
    Swagger swagger = generator.writeSwagger(apiConfigs, true, swaggerContext);
    String swaggerStr = Json.mapper().writer(new EndpointsPrettyPrinter()).writeValueAsString(swagger);
    if (outputToDisk) {
        Files.write(swaggerStr, new File(outputFilePath), UTF_8);
        System.out.println("OpenAPI document written to " + outputFilePath);
    }

    return swaggerStr;
}

From source file:brooklyn.entity.rebind.persister.BrooklynMementoPersisterToFile.java

private void writeMemento() {
    assert Thread.holdsLock(mutex);
    try {/*  w w  w  .  java2  s  .  c o  m*/
        Files.write(serializer.toString(memento), file, Charsets.UTF_8);
    } catch (IOException e) {
        LOG.error("Failed to persist memento", e);
    }
}

From source file:org.apache.sentry.provider.file.PolicyFile.java

public void write(File file) throws IOException {
    if (file.exists() && !file.delete()) {
        throw new IllegalStateException("Unable to delete " + file);
    }/*  w  w  w  .ja  v  a 2 s.  co m*/
    String contents = Joiner.on(NL).join(getSection(DATABASES, databasesToPolicyFiles),
            getSection(PolicyFileConstants.USERS, usersToGroups), getSection(GROUPS, groupsToRoles),
            getSection(ROLES, rolesToPermissions), "");
    LOGGER.info("Writing policy file to " + file + ":\n" + contents);
    Files.write(contents, file, Charsets.UTF_8);
}

From source file:net.minecraftforge.gradle.patcher.TaskGenSubprojects.java

private static void generateProjectBuild(URI workspace, File output, DevProject project) throws IOException {
    StringBuilder builder = new StringBuilder();

    File src = project.getExternalSrcDir();
    File res = project.getExternalResDir();
    File testSrc = project.getExternalTestSrcDir();
    File testRes = project.getExternalTestResDir();

    // @formatter:off

    // why use relatvie paths? so the eclipse hack below can work correctly.
    // add extra sourceDirs
    append(builder, "sourceSets {", NEWLINE);
    append(builder, INDENT, "main.java.srcDir 'src/main/start'", NEWLINE); // add start dir to gradle sources
    if (src != null)
        append(builder, INDENT, "main.java.srcDir '", relative(workspace, src), "'", NEWLINE);
    if (res != null)
        append(builder, INDENT, "main.resources.srcDir '", relative(workspace, res), "'", NEWLINE);
    if (testSrc != null)
        append(builder, INDENT, "test.java.srcDir '", relative(workspace, testSrc), "'", NEWLINE);
    if (testRes != null)
        append(builder, INDENT, "test.resources.srcDir '", relative(workspace, testRes), "'", NEWLINE);
    append(builder, "}");

    // @formatter:on

    // write/*from  w  w  w.  j ava  2s  .  c o  m*/
    Files.write(builder.toString(), output, Constants.CHARSET);
}

From source file:org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter.java

private void resetLastConfig(String content) throws IOException {
    content = content.replaceFirst(SEPARATOR_SL, SEPARATOR_S);
    Files.write(content, storage, ENCODING);
}

From source file:org.spiffyui.maven.plugins.ClosureMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (!sourceDirectory.exists()) {
        getLog().debug("Source directory doesn't exist, skipping");
        return;//ww w  .  ja  v  a  2s .co  m
    }

    /*
     * The first step is to copy the uncompressed files into the
     * target directory so users can see the real source for debugging
     */
    File jsDir = new File(outputFile.getParentFile(), "js");
    jsDir.mkdirs();

    try {
        FileUtils.copyDirectory(sourceDirectory, jsDir);
    } catch (IOException ioe) {
        throw new MojoFailureException("Unable to copy JavaScripot source" + ioe.getMessage());
    }

    String[] exts = { "js" };
    List<File> files = new ArrayList<File>(FileUtils.listFiles(sourceDirectory, exts, true));
    if (files.size() == 0) {
        getLog().debug("No source files detected, skipping");
        return;
    }

    CompilationLevel compLevel = null;
    try {
        compLevel = CompilationLevel.valueOf(compilationLevel);
    } catch (IllegalArgumentException e) {
        throw new MojoFailureException("Compilation level invalid" + e.getMessage());
    }

    CompilerOptions compilerOptions = new CompilerOptions();
    compLevel.setOptionsForCompilationLevel(compilerOptions);

    List<JSSourceFile> sources = new ArrayList<JSSourceFile>();

    long lastMod = -1;

    /* spiffyui.min.js _must_ be first */
    sources.add(JSSourceFile.fromFile(new File(gwtModulePath, "spiffyui.min.js")));
    for (File file : files) {
        lastMod = Math.max(lastMod, file.lastModified());
        sources.add(JSSourceFile.fromFile(file));
    }

    if (outputFile.exists() && lastMod < outputFile.lastModified()) {
        /*
         If the newest source file is older then the output file then
         everything is up to date and we don't need to compile anything.
         */
        getLog().info("Skipping JavaScript compilation. The compiled JavaScript file is up to date.");
        return;
    }

    Compiler compiler = new Compiler();
    compiler.setLoggingLevel(Level.WARNING);
    Result result = compiler.compile(new ArrayList<JSSourceFile>(), sources, compilerOptions);

    for (JSError warning : result.warnings) {
        getLog().warn(warning.toString());
    }

    for (JSError error : result.errors) {
        getLog().error(error.toString());
    }

    if (!result.success) {
        throw new MojoFailureException("Compilation failure");
    }

    try {
        Files.createParentDirs(outputFile);
        Files.touch(outputFile);
        Files.write(compiler.toSource(), outputFile, Charsets.UTF_8);
    } catch (IOException e) {
        throw new MojoFailureException(outputFile != null ? outputFile.toString() : e.getMessage());
    }
}

From source file:me.emmy.db.MySQLLayer.java

private void enforceVersion() {
    File versionFile = new File(plugin.getDataFolder(), "mysql.dl.version");
    if (versionFile.exists()) {
        checkState(versionFile.isFile(), "%s is not a file!", "mysql.dl.version");
        versionFile.delete();/* w w w .j a va  2s.co m*/
    }
    try {
        Files.touch(versionFile);
        Files.write(DataLayerVersion.latest().toString(), versionFile, Charsets.UTF_8);
    } catch (IOException e) {
        Throwables.propagate(e);
    }

}

From source file:com.google.testing.pogen.GenerateCommand.java

@Override
public void execute() throws IOException {
    File rootInputDir = createDirectory(rootDirectoryPath, false, true);
    File testOutDir = createDirectory(testOutDirPath, false, true);

    // Check whether the root directory exists
    if (!rootInputDir.exists()) {
        throw new FileProcessException("Not found root intpu directory", rootInputDir);
    }//from www.ja v  a 2s .  c  om

    // Generate the AbstractPage class
    File newAbstractPageFile = new File(testOutDir.getPath(), ABSTRACT_PAGE_NAME);
    if (!newAbstractPageFile.exists()) {
        URL abstractPageUrl = Resources.getResource(ABSTRACT_PAGE_NAME);
        String abstractPage = Resources.toString(abstractPageUrl, Charset.defaultCharset());
        abstractPage = abstractPage.replaceAll(ABSTRACT_PAGE_PACKAGE, packageName);
        Files.write(abstractPage, newAbstractPageFile, Charset.defaultCharset());
    } else if (verbose) {
        System.err.println("Already exists: " + newAbstractPageFile.getAbsolutePath() + ".");
    }

    // Collect the template files from the arguments indicating paths of template files
    ArrayList<File> templateFiles = new ArrayList<File>();
    for (String templatePath : templatePaths) {
        File file = createFileFromFilePath(templatePath);
        templateFiles.add(file);
    }

    // Collect the template files from the specified pattern with the root directory
    if (!Strings.isNullOrEmpty(templateFilePattern)) {
        templateFiles.addAll(FileUtils.listFiles(rootInputDir, new RegexFileFilter(templateFilePattern),
                isRecusive ? FileFilterUtils.trueFileFilter() : null));
    }

    TemplateUpdater updater = TemplateUpdaters.getPreferredUpdater(attributeName);
    TestCodeGenerator generator = TestCodeGenerators.getPreferredGenerator(attributeName);
    for (File file : templateFiles) {
        checkExistenceAndPermission(file, true, true);
        try {
            TemplateParser parser = TemplateParsers.getPreferredParser(file.getPath(), attributeName);
            if (attributeName.equals("id") && parser instanceof JsfParser) {
                System.out.println("WARNING: Using id attribute is not recommmended for JSF templat engine.");
            }
            parseAndGenerate(file, rootInputDir, testOutDir, parser, updater, generator);
        } catch (TemplateParseException e) {
            throw new FileProcessException("Errors occur in parsing the specified files", file, e);
        } catch (PageObjectUpdateException e) {
            throw new FileProcessException("Errors occur in updating the specified files", file, e);
        }
    }
}

From source file:org.apache.metron.common.cli.ConfigurationManager.java

public void pull(CuratorFramework client, String outFileStr, final boolean force) throws Exception {
    final File outputDir = new File(outFileStr);
    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new IllegalStateException("Unable to make directories: " + outputDir.getAbsolutePath());
        }//  ww w.  j  a  v  a2  s  .  c o  m
    }

    ConfigurationsUtils.visitConfigs(client, new ConfigurationsUtils.ConfigurationVisitor() {
        @Override
        public void visit(ConfigurationType configurationType, String name, String data) {
            File out = getFile(outputDir, configurationType, name);
            if (!out.exists() || force) {
                if (!out.exists()) {
                    out.getParentFile().mkdirs();
                }
                try {
                    Files.write(data, out, Charset.defaultCharset());
                } catch (IOException e) {
                    throw new RuntimeException("Sorry, something bad happened writing the config to "
                            + out.getAbsolutePath() + ": " + e.getMessage(), e);
                }
            } else if (out.exists() && !force) {
                throw new IllegalStateException("Unable to overwrite existing file (" + out.getAbsolutePath()
                        + ") without the force flag (-f or --force) being set.");
            }
        }
    });
}