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

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

Introduction

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

Prototype

public static String toString(File file, Charset charset) throws IOException 

Source Link

Usage

From source file:com.swissbit.accesscontrol.AccessControl.java

/** {@inheritDoc}} */
@Override/*  w w  w  .  j  a  va2  s  .c o m*/
public String readAllClientsFile() {
    try {
        return Files.toString(new File(ALL_CLIENTS_FILE_LOCATION), Charsets.UTF_8);
    } catch (final IOException e) {
        LOGGER.error(Throwables.getStackTraceAsString(e));
    }
    return null;
}

From source file:com.indeed.lsmtree.core.Store.java

/**
 * Use {@link StoreBuilder} to create a store.
 *
 * @param root                          root lsm tree index directory
 * @param keySerializer                 key serializer
 * @param valueSerializer               value serializer
 * @param comparator                    key comparator
 * @param maxVolatileGenerationSize     max size of volatile generation in bytes before a compaction should occur
 * @param storageType                   storage type
 * @param codec                         compression codec
 * @param readOnly                      open lsm tree in read only mode
 * @param dedicatedPartition            true if lsm tree is on a dedicated partition
 * @param reservedSpaceThreshold        disk space in bytes that must be available after compactions
 * @param mlockFiles                    mlock files if true
 * @param bloomFilterMemory             memory allocated to bloom filter, in bytes
 * @param mlockBloomFilters             mlock bloom filters if true
 * @throws IOException/*from ww  w .  j av  a2 s.co m*/
 */
Store(File root, Serializer<K> keySerializer, Serializer<V> valueSerializer, Comparator<K> comparator,
        long maxVolatileGenerationSize, StorageType storageType, CompressionCodec codec, boolean readOnly,
        final boolean dedicatedPartition, final long reservedSpaceThreshold, final boolean mlockFiles,
        final long bloomFilterMemory, final boolean mlockBloomFilters) throws IOException {
    this.storageType = storageType;
    this.codec = codec;
    this.dedicatedPartition = dedicatedPartition;
    this.reservedSpaceThreshold = reservedSpaceThreshold;
    this.mlockFiles = mlockFiles;
    if (!root.isDirectory()) {
        if (!root.mkdirs()) {
            final String err = root.getAbsolutePath() + " could not be created";
            log.error(err);
            throw new IOException(err);
        }
    }
    if (!readOnly) {
        final File lockFileLock = new File(root, "write.lock.lock");
        try {
            if (!lockFileLock.createNewFile()) {
                throw new IOException(lockFileLock.getAbsolutePath() + " is already locked");
            }
            final File lockFile = new File(root, "write.lock");
            if (lockFile.exists()) {
                final Integer pid = PosixFileOperations.tryParseInt(Files.toString(lockFile, Charsets.UTF_8));
                if (pid == null || PosixFileOperations.isProcessRunning(pid, true)) {
                    lockFileLock.delete();
                    throw new IOException(lockFile.getAbsolutePath() + " is already locked");
                }
            }
            Files.write(String.valueOf(PosixFileOperations.getPID()), lockFile, Charsets.UTF_8);
            lockFileLock.delete();
            this.lockFile = lockFile;
            this.lockFile.deleteOnExit();
        } catch (IOException e) {
            log.error("problem locking lsmtree in directory " + root.getAbsolutePath(), e);
            throw e;
        }
    } else {
        lockFile = null;
    }
    this.root = root;
    this.keySerializer = keySerializer;
    this.valueSerializer = valueSerializer;
    this.comparator = Ordering.from(comparator);
    this.maxVolatileGenerationSize = maxVolatileGenerationSize;
    generationState = AtomicSharedReference.create();
    dataDir = new File(root, "data");
    final VolatileGeneration<K, V> nextVolatileGeneration;
    final List<Generation<K, V>> stableGenerations = new ArrayList<Generation<K, V>>();
    final List<File> toDelete = new ArrayList<File>();
    lastUsedTimeStamp = new AtomicLong();
    memoryManager = new BloomFilter.MemoryManager(bloomFilterMemory, mlockBloomFilters);
    try {
        if (!dataDir.exists()) {
            dataDir.mkdirs();
            final File newLog = getNextLogFile();
            nextVolatileGeneration = new VolatileGeneration<K, V>(newLog, keySerializer, valueSerializer,
                    comparator);
        } else {
            long maxTimestamp = 0;
            maxTimestamp = findMaxTimestamp(root, maxTimestamp);
            maxTimestamp = findMaxTimestamp(dataDir, maxTimestamp);
            lastUsedTimeStamp.set(maxTimestamp);
            final File latestDir = new File(root, "latest");
            final File state = new File(latestDir, "state");
            final Yaml yaml = new Yaml();
            final Reader reader = new InputStreamReader(new FileInputStream(state));
            final Map<String, Object> map = (Map<String, Object>) yaml.load(reader);
            Closeables2.closeQuietly(reader, log);
            final File volatileGenerationFile = new File(latestDir, (String) map.get("volatileGeneration"));
            final List<String> oldStableGenerations = (List<String>) map.get("stableGenerations");
            if (readOnly) {
                nextVolatileGeneration = new VolatileGeneration<K, V>(volatileGenerationFile, keySerializer,
                        valueSerializer, comparator, true);
                for (String generationName : oldStableGenerations) {
                    final File generationFile = new File(latestDir, generationName);
                    if (generationName.endsWith(".log")) {
                        stableGenerations.add(new VolatileGeneration(getDataFile(generationFile), keySerializer,
                                valueSerializer, comparator, true));
                    } else {
                        stableGenerations.add(StableGeneration.open(memoryManager, getDataFile(generationFile),
                                comparator, keySerializer, valueSerializer, storageType, codec, mlockFiles));
                    }
                }
            } else {
                Collections.addAll(toDelete, dataDir.listFiles(new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        return !oldStableGenerations.contains(name);
                    }
                }));
                Collections.addAll(toDelete, root.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.isDirectory() && pathname.getName().matches("\\d+");
                    }
                }));
                final File newLog = getNextLogFile();
                nextVolatileGeneration = new VolatileGeneration<K, V>(newLog, keySerializer, valueSerializer,
                        comparator);
                nextVolatileGeneration.replayTransactionLog(volatileGenerationFile);
                for (String generationName : oldStableGenerations) {
                    final File generationFile = new File(latestDir, generationName);
                    if (generationName.endsWith(".log")) {
                        final File tempLog = getNextLogFile();
                        final VolatileGeneration temp = new VolatileGeneration(tempLog, keySerializer,
                                valueSerializer, comparator);
                        temp.replayTransactionLog(generationFile);
                        stableGenerations
                                .add(doCompaction(Collections.singletonList((Generation<K, V>) temp), true));
                        temp.delete();
                        toDelete.add(getDataFile(generationFile));
                    } else {
                        stableGenerations.add(StableGeneration.open(memoryManager, getDataFile(generationFile),
                                comparator, keySerializer, valueSerializer, storageType, codec, mlockFiles));
                    }
                }
            }
        }
        final GenerationState<K, V> nextState;
        final List<SharedReference<? extends Generation<K, V>>> stableGenerationReferences = Lists
                .newArrayList();
        for (Generation<K, V> generation : stableGenerations) {
            stableGenerationReferences.add(SharedReference.create(generation));
        }
        if (!readOnly) {
            final File checkpointDir = getNextCheckpointDir();
            checkpointDir.mkdirs();
            nextState = new GenerationState<K, V>(stableGenerationReferences,
                    SharedReference.create(nextVolatileGeneration), checkpointDir);
            checkpointGenerationState(nextState, checkpointDir);
            PosixFileOperations.atomicLink(checkpointDir, new File(root, "latest"));
        } else {
            nextState = new GenerationState<K, V>(stableGenerationReferences,
                    SharedReference.create(nextVolatileGeneration), getDataFile(new File(root, "latest")));
        }
        generationState.set(nextState);
        for (Generation<K, V> generation : nextState.stableGenerations) {
            totalGenerationSpace.addAndGet(generation.sizeInBytes());
        }
        if (!readOnly) {
            compactor = new Compactor();
            for (File f : toDelete) {
                log.info("deleting " + f.getPath());
                if (f.isDirectory()) {
                    PosixFileOperations.rmrf(f);
                } else {
                    f.delete();
                }
            }
        } else {
            if (!toDelete.isEmpty())
                log.error("toDelete should be empty");
            compactor = null;
        }
    } catch (Throwable t) {
        memoryManager.close();
        Throwables.propagateIfInstanceOf(t, IOException.class);
        throw Throwables.propagate(t);
    }
}

From source file:com.gradleware.tooling.toolingutils.distribution.PublishedGradleVersions.java

private static Optional<String> readCacheVersionsFile() {
    try {/*from  ww  w  . jav  a2s . co m*/
        return Optional.of(Files.toString(CACHE_FILE, Charsets.UTF_8));
    } catch (IOException e) {
        LOG.error("Cannot read found Gradle version information cache file.", e);
        // do not throw an exception if cache file cannot be read to be more robust against file system problems
        return Optional.absent();
    }
}

From source file:com.github.nethad.clustermeister.api.impl.KeyPairCredentials.java

/**
 * Get the public key./*  w w w  .j  a v a2 s  .  co m*/
 * 
 * @return  the public key as a String.
 * @throws IOException 
 *      when an error occurs while reading from {@link #publicKeySource}. 
 */
public Optional<String> getPublicKey() throws IOException {
    Optional<String> publicKey;
    if (publicKeySource.isPresent()) {
        publicKey = Optional.of(Files.toString(publicKeySource.get(), charset));
    } else {
        publicKey = Optional.absent();
    }

    return publicKey;
}

From source file:com.uber.hoodie.utilities.HoodieDeltaStreamer.java

private HoodieWriteConfig getHoodieClientConfig(HoodieTableMetadata metadata) throws Exception {
    final String schemaStr = Files.toString(new File(cfg.schemaFile), Charset.forName("UTF-8"));
    return HoodieWriteConfig.newBuilder().withPath(metadata.getBasePath()).withSchema(schemaStr)
            .withParallelism(cfg.groupByParallelism, cfg.groupByParallelism).forTable(metadata.getTableName())
            .withIndexConfig(HoodieIndexConfig.newBuilder().withIndexType(HoodieIndex.IndexType.BLOOM).build())
            .build();// www. j a va  2s . c o m
}

From source file:io.hops.hopsworks.ca.api.certs.CertSigningService.java

private CsrDTO signCSR(String hostId, String commandId, String csr, boolean rotation, CertificateType certType)
        throws HopsSecurityException {
    try {/*ww  w. ja  va 2 s  .c  o  m*/
        // If there is a certificate already for that host, rename it to .TO_BE_REVOKED.COMMAND_ID
        // When AgentResource has received a successful response for the key rotation, revoke and delete it
        if (rotation) {
            File certFile = Paths.get(settings.getIntermediateCaDir(), "certs",
                    hostId + CertificatesMgmService.CERTIFICATE_SUFFIX).toFile();
            if (certFile.exists()) {
                File destination = Paths
                        .get(settings.getIntermediateCaDir(), "certs",
                                hostId + serviceCertificateRotationTimer.getToBeRevokedSuffix(commandId))
                        .toFile();
                try {
                    FileUtils.moveFile(certFile, destination);
                } catch (FileExistsException ex) {
                    FileUtils.deleteQuietly(destination);
                    FileUtils.moveFile(certFile, destination);
                }
            }
        }
        String agentCert = opensslOperations.signCertificateRequest(csr, certType);
        File caCertFile = Paths.get(settings.getIntermediateCaDir(), "certs", "ca-chain.cert.pem").toFile();
        String caCert = Files.toString(caCertFile, Charset.defaultCharset());
        return new CsrDTO(caCert, agentCert, settings.getHadoopVersionedDir());
    } catch (IOException ex) {
        throw new HopsSecurityException(RESTCodes.SecurityErrorCode.CSR_ERROR, Level.SEVERE, "host: " + hostId,
                ex.getMessage(), ex);
    }
}

From source file:com.swissbit.accesscontrol.AccessControl.java

/** {@inheritDoc}} */
@Override/*w  w  w .  j a v a  2  s .co m*/
public String readPermissionFile() {
    try {
        return Files.toString(new File(PERMISSION_FILE_LOCATION), Charsets.UTF_8);
    } catch (final IOException e) {
        LOGGER.error(Throwables.getStackTraceAsString(e));
    }
    return null;
}

From source file:org.apache.kylin.dict.lookup.cache.RocksDBLookupTableCache.java

private void initSnapshotState(String tableName, File snapshotCacheFolder) {
    String snapshotID = snapshotCacheFolder.getName();
    File stateFile = getCacheStateFile(snapshotCacheFolder.getAbsolutePath());
    if (stateFile.exists()) {
        try {//from   ww  w. j  a  v  a2s.  com
            String stateStr = Files.toString(stateFile, Charsets.UTF_8);
            String resourcePath = ExtTableSnapshotInfo.getResourcePath(tableName, snapshotID);
            if (CacheState.AVAILABLE.name().equals(stateStr)) {
                tablesCache.put(resourcePath, new CachedTableInfo(snapshotCacheFolder.getAbsolutePath()));
            }
        } catch (IOException e) {
            logger.error("error to read state file:" + stateFile.getAbsolutePath());
        }
    }
}

From source file:com.googlecode.blaisemath.svg.SVGTool.java

private void loadBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadBActionPerformed
    if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
        FileInputStream fis = null;
        try {/*from   w  ww . j a va2 s . co m*/
            fis = new FileInputStream(chooser.getSelectedFile());
            SVGRoot r = SVGRoot.load(fis);
            gsvg.setElement(r);
            String fs = Files.toString(chooser.getSelectedFile(), Charset.defaultCharset());
            text.setText(fs);
        } catch (IOException x) {
            Logger.getLogger(SVGTool.class.getName()).log(Level.SEVERE, null, x);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(SVGTool.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

From source file:org.ctoolkit.maven.plugins.optimizer.JsOptimizer.java

/**
 * Process js optimization//from  w ww .j  a  v a  2s.c  o  m
 *
 * @param pathToXml    path to java-script configuration xml
 * @param jsOutputPath java-script output path
 * @param settings     maven {@link Settings}
 */
public static void process(String pathToXml, String jsOutputPath, Settings settings) {
    String outputDirectory = FileHelper.getOutputDirectory(jsOutputPath);

    if (pathToXml == null || pathToXml.trim().isEmpty()) {
        log.info("Javascript path to xml is not set. Skipping javascript optimization.");
        return;
    }

    if (!new File(pathToXml).exists()) {
        throw new IllegalArgumentException("Javascript path to xml does not exists: " + pathToXml);
    }

    log.info("Starting to optimize javascript...");

    try {
        for (JSConfig JSConfig : getJsConfigList(pathToXml)) {
            String jsFile = JSConfig.getOutputJavascriptName();
            final String fullJsFilePath = outputDirectory + jsFile;

            new File(outputDirectory).mkdirs();

            List<String> args = new ArrayList<>();

            for (String js : JSConfig.getJsList()) {
                args.add(ARG_JS);
                args.add(js);
            }

            args.add(ARG_MANAGE_CLOSURE_DEPENDENCIES);
            args.add(ARG_COMPILATION_LEVEL);
            args.add("ADVANCED_OPTIMIZATIONS");
            args.add(ARG_JS_OUTPUT_FILE);
            args.add(fullJsFilePath);

            try {
                // make an ugly security manager override to be able prevent js closure exit
                System.setSecurityManager(new MySecurityManager());
                CommandLineRunner.main(args.toArray(new String[args.size()]));
            } catch (NoStopException e) {
                String jsOutputString = Files.toString(new File(fullJsFilePath), Charsets.UTF_8);

                log.info("JS minified output:\n===\n" + jsOutputString + "===\n");
                log.info(
                        ">>> Javascript optimization finished successfully. Optimized js file can be found at: "
                                + fullJsFilePath);
            } finally {
                System.setSecurityManager(null);
            }
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error occurred during processing js: ", e);
    }
}