Search in sources :

Example 71 with Files

use of java.nio.file.Files in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class FileArchiver method run.

private void run(String rootPath, String sip) throws IOException {
    File sipFile = new File(sip).getCanonicalFile();
    if (!sipFile.getParentFile().mkdirs() && !sipFile.getParentFile().isDirectory()) {
        throw new IllegalStateException("Could not create all required directories in path " + sipFile.getParentFile().getAbsolutePath());
    }
    System.out.printf("%nSample 2: Archiving files from %s into %s%n", rootPath, sipFile.getPath());
    // Tell InfoArchive where and how to archive the data
    URI entityUri = URI.create("urn:com.opentext.ia.sdk.sample.file:1.0");
    String entityName = "file";
    PackagingInformation prototype = PackagingInformation.builder().dss().application("fileApplication").holding("fileHolding").producer("SIP SDK").entity(entityName).schema(entityUri.toString()).end().build();
    // Define a mapping from our domain object to the PDI XML
    PdiAssembler<File> pdiAssembler = new XmlPdiAssembler<File>(entityUri, entityName) {

        @Override
        protected void doAdd(File file, Map<String, ContentInfo> contentInfo) {
            try {
                String path = relativePath(file, rootPath);
                getBuilder().element("path", path).element("size", Long.toString(file.length())).element("permissions", permissionsOf(file)).element("contentType", Files.probeContentType(file.toPath())).elements("hashes", "hash", contentInfo.get(path).getContentHashes(), (hash, builder) -> {
                    builder.attribute("algorithm", hash.getHashFunction()).attribute("encoding", hash.getEncoding()).attribute("value", hash.getValue());
                });
            } catch (IOException e) {
                throw new RuntimeIoException(e);
            }
        }
    };
    DigitalObjectsExtraction<File> contentAssembler = file -> Collections.singleton(DigitalObject.fromFile(relativePath(file, rootPath), file)).iterator();
    HashAssembler contentHashAssembler = new SingleHashAssembler(HashFunction.SHA256, Encoding.BASE64);
    // Assemble the SIP
    SipAssembler<File> assembler = SipAssembler.forPdiAndContentWithContentHashing(prototype, pdiAssembler, contentAssembler, contentHashAssembler);
    assembler.start(new FileBuffer(sipFile));
    try {
        addFilesIn(new File(rootPath), rootPath, relativePath(sipFile, rootPath), assembler);
    } finally {
        assembler.end();
    }
    // Show metrics about the assembly process
    SipMetrics metrics = assembler.getMetrics();
    System.out.printf("  Added %d files to SIP of %d bytes in %d ms%n", metrics.numDigitalObjects(), metrics.sipFileSize(), metrics.assemblyTime());
}
Also used : XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) DigitalObject(com.opentext.ia.sdk.sip.DigitalObject) Files(java.nio.file.Files) PdiAssembler(com.opentext.ia.sdk.sip.PdiAssembler) IOException(java.io.IOException) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation) SipMetrics(com.opentext.ia.sdk.sip.SipMetrics) File(java.io.File) ContentInfo(com.opentext.ia.sdk.sip.ContentInfo) HashFunction(com.opentext.ia.sdk.support.io.HashFunction) SipAssembler(com.opentext.ia.sdk.sip.SipAssembler) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) Map(java.util.Map) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) DigitalObjectsExtraction(com.opentext.ia.sdk.sip.DigitalObjectsExtraction) FileBuffer(com.opentext.ia.sdk.support.io.FileBuffer) Encoding(com.opentext.ia.sdk.support.io.Encoding) URI(java.net.URI) Collections(java.util.Collections) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) SingleHashAssembler(com.opentext.ia.sdk.support.io.SingleHashAssembler) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) FileBuffer(com.opentext.ia.sdk.support.io.FileBuffer) IOException(java.io.IOException) URI(java.net.URI) RuntimeIoException(com.opentext.ia.sdk.support.io.RuntimeIoException) XmlPdiAssembler(com.opentext.ia.sdk.sip.XmlPdiAssembler) File(java.io.File) Map(java.util.Map) PackagingInformation(com.opentext.ia.sdk.sip.PackagingInformation) SipMetrics(com.opentext.ia.sdk.sip.SipMetrics)

Example 72 with Files

use of java.nio.file.Files in project jena by apache.

the class FilenameUtils method scanForDirByPattern.

/**
 * Find the files in this directory that have namebase as a prefix and
 *  are then numbered.
 *  <p>
 *  Returns a sorted list from, low to high index.
 */
public static List<Path> scanForDirByPattern(Path directory, String namebase, String nameSep) {
    Pattern pattern = Pattern.compile(Pattern.quote(namebase) + Pattern.quote(nameSep) + "[\\d]+");
    List<Path> paths = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, namebase + nameSep + "*")) {
        for (Path entry : stream) {
            if (!pattern.matcher(entry.getFileName().toString()).matches()) {
                throw new DBOpEnvException("Invalid filename for matching: " + entry.getFileName());
            // Alternative: Skip bad trailing parts but more likely there is a naming problem.
            // LOG.warn("Invalid filename for matching: {} skipped", entry.getFileName());
            // continue;
            }
            // Follows symbolic links.
            if (!Files.isDirectory(entry))
                throw new DBOpEnvException("Not a directory: " + entry);
            paths.add(entry);
        }
    } catch (IOException ex) {
        FmtLog.warn(LOG, "Can't inspect directory: (%s, %s)", directory, namebase);
        throw new DBOpEnvException(ex);
    }
    Comparator<Path> comp = (f1, f2) -> {
        int num1 = extractIndex(f1.getFileName().toString(), namebase, nameSep);
        int num2 = extractIndex(f2.getFileName().toString(), namebase, nameSep);
        return Integer.compare(num1, num2);
    };
    paths.sort(comp);
    // indexes.sort(Long::compareTo);
    return paths;
}
Also used : Path(java.nio.file.Path) DirectoryStream(java.nio.file.DirectoryStream) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) List(java.util.List) Logger(org.slf4j.Logger) Files(java.nio.file.Files) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) Path(java.nio.file.Path) FmtLog(org.apache.jena.atlas.logging.FmtLog) ArrayList(java.util.ArrayList) Pattern(java.util.regex.Pattern) DBOpEnvException(org.apache.jena.dboe.DBOpEnvException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 73 with Files

use of java.nio.file.Files in project crate by crate.

the class BlobPathITest method testDataIsStoredInGlobalBlobPath.

@Test
public void testDataIsStoredInGlobalBlobPath() throws Exception {
    launchNodeAndInitClient(configureGlobalBlobPath());
    Settings indexSettings = oneShardAndZeroReplicas();
    blobAdminClient.createBlobTable("test", indexSettings).get();
    client.put("test", "abcdefg");
    String digest = "2fb5e13419fc89246865e7a324f476ec624e8740";
    try (Stream<Path> files = Files.walk(globalBlobPath)) {
        assertThat(files.anyMatch(i -> digest.equals(i.getFileName().toString())), is(true));
    }
}
Also used : Path(java.nio.file.Path) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) SETTING_NUMBER_OF_SHARDS(org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) BlobIndicesService(io.crate.blob.v2.BlobIndicesService) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) BlobAdminClient(io.crate.blob.v2.BlobAdminClient) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) Stream(java.util.stream.Stream) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Paths(java.nio.file.Paths) SETTING_NUMBER_OF_REPLICAS(org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Is.is(org.hamcrest.core.Is.is) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Path(java.nio.file.Path) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 74 with Files

use of java.nio.file.Files in project zeppelin by apache.

the class SparkInterpreterLauncher method buildEnvFromProperties.

@Override
public Map<String, String> buildEnvFromProperties(InterpreterLaunchContext context) throws IOException {
    Map<String, String> env = super.buildEnvFromProperties(context);
    Properties sparkProperties = new Properties();
    String spMaster = getSparkMaster();
    if (spMaster != null) {
        sparkProperties.put(SPARK_MASTER_KEY, spMaster);
    }
    for (String key : properties.stringPropertyNames()) {
        String propValue = properties.getProperty(key);
        if (RemoteInterpreterUtils.isEnvString(key) && !StringUtils.isBlank(propValue)) {
            env.put(key, propValue);
        }
        if (isSparkConf(key, propValue)) {
            // instead of into sparkProperties.
            if (Objects.equals("spark.driver.extraJavaOptions", key)) {
                env.put("SPARK_DRIVER_EXTRAJAVAOPTIONS_CONF", (String) properties.remove(key));
                continue;
            }
            sparkProperties.setProperty(key, propValue);
        }
    }
    // set spark.app.name if it is not set or empty
    if (!sparkProperties.containsKey("spark.app.name") || StringUtils.isBlank(sparkProperties.getProperty("spark.app.name"))) {
        sparkProperties.setProperty("spark.app.name", context.getInterpreterGroupId());
    }
    setupPropertiesForPySpark(sparkProperties);
    setupPropertiesForSparkR(sparkProperties);
    String condaEnvName = context.getProperties().getProperty("zeppelin.interpreter.conda.env.name");
    if (StringUtils.isNotBlank(condaEnvName)) {
        if (!isYarnCluster()) {
            throw new IOException("zeppelin.interpreter.conda.env.name only works for yarn-cluster mode");
        }
        sparkProperties.setProperty("spark.pyspark.python", condaEnvName + "/bin/python");
    }
    if (isYarnCluster()) {
        env.put("ZEPPELIN_SPARK_YARN_CLUSTER", "true");
        sparkProperties.setProperty("spark.yarn.submit.waitAppCompletion", "false");
        // Need to set `zeppelin.interpreter.forceShutdown` in interpreter properties directly
        // instead of updating sparkProperties.
        // Because `zeppelin.interpreter.forceShutdown` is initialized in RemoteInterpreterServer
        // before SparkInterpreter is created.
        context.getProperties().put("zeppelin.interpreter.forceShutdown", "false");
    } else if (zConf.isOnlyYarnCluster()) {
        throw new IOException("Only yarn-cluster mode is allowed, please set " + ZeppelinConfiguration.ConfVars.ZEPPELIN_SPARK_ONLY_YARN_CLUSTER.getVarName() + " to false if you want to use other modes.");
    }
    if (isYarnMode() && getDeployMode().equals("cluster")) {
        if (sparkProperties.containsKey("spark.files")) {
            sparkProperties.put("spark.files", sparkProperties.getProperty("spark.files") + "," + zConf.getConfDir() + "/log4j_yarn_cluster.properties");
        } else {
            sparkProperties.put("spark.files", zConf.getConfDir() + "/log4j_yarn_cluster.properties");
        }
        sparkProperties.put("spark.yarn.maxAppAttempts", "1");
    }
    String scalaVersion = null;
    try {
        scalaVersion = detectSparkScalaVersion(getEnv("SPARK_HOME"), env);
        context.getProperties().put("zeppelin.spark.scala.version", scalaVersion);
    } catch (Exception e) {
        throw new IOException("Fail to detect scala version, the reason is:" + e.getMessage());
    }
    if (isYarnMode() && getDeployMode().equals("cluster")) {
        try {
            List<String> additionalJars = new ArrayList<>();
            Path localRepoPath = Paths.get(zConf.getInterpreterLocalRepoPath(), context.getInterpreterSettingId());
            if (Files.exists(localRepoPath) && Files.isDirectory(localRepoPath)) {
                try (DirectoryStream<Path> localRepoStream = Files.newDirectoryStream(localRepoPath, Files::isRegularFile)) {
                    List<String> localRepoJars = StreamSupport.stream(localRepoStream.spliterator(), false).map(jar -> jar.toAbsolutePath().toString()).collect(Collectors.toList());
                    additionalJars.addAll(localRepoJars);
                }
            }
            Path scalaFolder = Paths.get(zConf.getZeppelinHome(), "/interpreter/spark/scala-" + scalaVersion);
            if (!scalaFolder.toFile().exists()) {
                throw new IOException("spark scala folder " + scalaFolder.toFile() + " doesn't exist");
            }
            try (DirectoryStream<Path> scalaStream = Files.newDirectoryStream(scalaFolder, Files::isRegularFile)) {
                List<String> scalaJars = StreamSupport.stream(scalaStream.spliterator(), false).map(jar -> jar.toAbsolutePath().toString()).collect(Collectors.toList());
                additionalJars.addAll(scalaJars);
            }
            // add zeppelin-interpreter-shaded
            Path interpreterFolder = Paths.get(zConf.getZeppelinHome(), "/interpreter");
            try (DirectoryStream<Path> interpreterStream = Files.newDirectoryStream(interpreterFolder, Files::isRegularFile)) {
                List<String> interpreterJars = StreamSupport.stream(interpreterStream.spliterator(), false).filter(jar -> jar.toFile().getName().startsWith("zeppelin-interpreter-shaded") && jar.toFile().getName().endsWith(".jar")).map(jar -> jar.toAbsolutePath().toString()).collect(Collectors.toList());
                if (interpreterJars.isEmpty()) {
                    throw new IOException("zeppelin-interpreter-shaded jar is not found");
                } else if (interpreterJars.size() > 1) {
                    throw new IOException("more than 1 zeppelin-interpreter-shaded jars are found: " + StringUtils.join(interpreterJars, ","));
                }
                additionalJars.addAll(interpreterJars);
            }
            if (sparkProperties.containsKey("spark.jars")) {
                sparkProperties.put("spark.jars", sparkProperties.getProperty("spark.jars") + "," + StringUtils.join(additionalJars, ","));
            } else {
                sparkProperties.put("spark.jars", StringUtils.join(additionalJars, ","));
            }
        } catch (Exception e) {
            throw new IOException("Fail to set additional jars for spark interpreter", e);
        }
    }
    StringJoiner sparkConfSJ = new StringJoiner("|");
    if (context.getOption().isUserImpersonate() && zConf.getZeppelinImpersonateSparkProxyUser()) {
        sparkConfSJ.add("--proxy-user");
        sparkConfSJ.add(context.getUserName());
        sparkProperties.remove("spark.yarn.keytab");
        sparkProperties.remove("spark.yarn.principal");
    }
    for (String name : sparkProperties.stringPropertyNames()) {
        sparkConfSJ.add("--conf");
        sparkConfSJ.add(name + "=" + sparkProperties.getProperty(name) + "");
    }
    env.put("ZEPPELIN_SPARK_CONF", sparkConfSJ.toString());
    // we also fallback to zeppelin-env.sh if it is not specified in interpreter setting.
    for (String envName : new String[] { "SPARK_HOME", "SPARK_CONF_DIR", "HADOOP_CONF_DIR" }) {
        String envValue = getEnv(envName);
        if (!StringUtils.isBlank(envValue)) {
            env.put(envName, envValue);
        }
    }
    String keytab = properties.getProperty("spark.yarn.keytab", zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_KERBEROS_KEYTAB));
    String principal = properties.getProperty("spark.yarn.principal", zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_SERVER_KERBEROS_PRINCIPAL));
    if (!StringUtils.isBlank(keytab) && !StringUtils.isBlank(principal)) {
        env.put("ZEPPELIN_SERVER_KERBEROS_KEYTAB", keytab);
        env.put("ZEPPELIN_SERVER_KERBEROS_PRINCIPAL", principal);
        LOGGER.info("Run Spark under secure mode with keytab: {}, principal: {}", keytab, principal);
    } else {
        LOGGER.info("Run Spark under non-secure mode as no keytab and principal is specified");
    }
    env.put("PYSPARK_PIN_THREAD", "true");
    // ZEPPELIN_INTP_CLASSPATH
    String sparkConfDir = getEnv("SPARK_CONF_DIR");
    if (StringUtils.isBlank(sparkConfDir)) {
        String sparkHome = getEnv("SPARK_HOME");
        sparkConfDir = sparkHome + "/conf";
    }
    Properties sparkDefaultProperties = new Properties();
    File sparkDefaultFile = new File(sparkConfDir, "spark-defaults.conf");
    if (sparkDefaultFile.exists()) {
        sparkDefaultProperties.load(new FileInputStream(sparkDefaultFile));
        String driverExtraClassPath = sparkDefaultProperties.getProperty("spark.driver.extraClassPath");
        if (!StringUtils.isBlank(driverExtraClassPath)) {
            env.put("ZEPPELIN_INTP_CLASSPATH", driverExtraClassPath);
        }
    } else {
        LOGGER.warn("spark-defaults.conf doesn't exist: {}", sparkDefaultFile.getAbsolutePath());
    }
    if (isYarnMode()) {
        boolean runAsLoginUser = Boolean.parseBoolean(context.getProperties().getProperty("zeppelin.spark.run.asLoginUser", "true"));
        String userName = context.getUserName();
        if (runAsLoginUser && !"anonymous".equals(userName)) {
            env.put("HADOOP_USER_NAME", userName);
        }
    }
    LOGGER.info("buildEnvFromProperties: {}", env);
    return env;
}
Also used : Path(java.nio.file.Path) FilenameFilter(java.io.FilenameFilter) ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) java.util(java.util) RemoteInterpreterUtils(org.apache.zeppelin.interpreter.remote.RemoteInterpreterUtils) Logger(org.slf4j.Logger) Files(java.nio.file.Files) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) RecoveryStorage(org.apache.zeppelin.interpreter.recovery.RecoveryStorage) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) StandardCharsets(java.nio.charset.StandardCharsets) File(java.io.File) DirectoryStream(java.nio.file.DirectoryStream) IOUtils(org.apache.commons.io.IOUtils) URLClassLoader(java.net.URLClassLoader) Matcher(java.util.regex.Matcher) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) StreamSupport(java.util.stream.StreamSupport) Pattern(java.util.regex.Pattern) Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Files(java.nio.file.Files) File(java.io.File)

Example 75 with Files

use of java.nio.file.Files in project gocd by gocd.

the class JarUtilTest method shouldExtractJars.

@Test
public void shouldExtractJars() throws Exception {
    File sourceFile = new File(PATH_WITH_HASHES + "test-agent.jar");
    Set<File> files = new HashSet<>(JarUtil.extractFilesInLibDirAndReturnFiles(sourceFile, jarEntry -> jarEntry.getName().endsWith(".class"), temporaryFolder));
    Set<File> actualFiles = Files.list(temporaryFolder.toPath()).map(Path::toFile).collect(Collectors.toSet());
    assertEquals(files, actualFiles);
    assertEquals(files.size(), 2);
    Set<String> fileNames = files.stream().map(File::getName).collect(Collectors.toSet());
    assertEquals(fileNames, new HashSet<>(Arrays.asList("ArgPrintingMain.class", "HelloWorldStreamWriter.class")));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) TEST_AGENT(com.thoughtworks.go.agent.testhelper.FakeGoServer.TestResource.TEST_AGENT) Files(java.nio.file.Files) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) AfterEach(org.junit.jupiter.api.AfterEach) TempDir(org.junit.jupiter.api.io.TempDir) Matchers.is(org.hamcrest.Matchers.is) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Path(java.nio.file.Path) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Files (java.nio.file.Files)243 IOException (java.io.IOException)210 Path (java.nio.file.Path)196 List (java.util.List)176 Collectors (java.util.stream.Collectors)154 Paths (java.nio.file.Paths)133 File (java.io.File)127 ArrayList (java.util.ArrayList)117 Map (java.util.Map)109 Set (java.util.Set)96 Collections (java.util.Collections)89 Arrays (java.util.Arrays)81 Stream (java.util.stream.Stream)77 HashMap (java.util.HashMap)74 HashSet (java.util.HashSet)58 InputStream (java.io.InputStream)55 Collection (java.util.Collection)55 Logger (org.slf4j.Logger)54 Pattern (java.util.regex.Pattern)53 Optional (java.util.Optional)51