java.nio.file.FileSystems Java Examples

The following examples show how to use java.nio.file.FileSystems. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Bucket.java    From jpms-module-names with MIT License 6 votes vote down vote up
private Map<Integer, FileSystem> createZips() throws IOException {
  var loader = getClass().getClassLoader();
  var map = new HashMap<Integer, FileSystem>();
  for (int year = 2018; year < 2030; year++) {
    var zipfile = cache.resolve(bucketName + "-" + year + ".zip");
    if (Files.notExists(zipfile)) {
      continue;
    }
    LOG.log(INFO, "Creating cache file system from {0}...", zipfile);
    var zipFileSystem = FileSystems.newFileSystem(zipfile, loader);
    if (LOG.isLoggable(INFO)) {
      var count = Files.list(zipFileSystem.getPath("/")).count();
      LOG.log(INFO, "{0} entries in {1}", count, zipFileSystem);
    }
    map.put(year, zipFileSystem);
  }
  return map;
}
 
Example #2
Source File: UploadConfigurationHandler.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void uploadMissingConfigFiles(SolrZkClient zkClient, ZkConfigManager zkConfigManager, String configName) throws IOException {
  logger.info("Check any of the configs files are missing for config ({})", configName);
  for (String configFile : configFiles) {
    if ("enumsConfig.xml".equals(configFile) && !hasEnumConfig) {
      logger.info("Config file ({}) is not needed for {}", configFile, configName);
      continue;
    }
    String zkPath = String.format("%s/%s", configName, configFile);
    if (zkConfigManager.configExists(zkPath)) {
      logger.info("Config file ({}) has already uploaded properly.", configFile);
    } else {
      logger.info("Config file ({}) is missing. Reupload...", configFile);
      FileSystems.getDefault().getSeparator();
      uploadFileToZk(zkClient,
        String.format("%s%s%s", getConfigSetFolder(), FileSystems.getDefault().getSeparator(), configFile),
        String.format("%s%s", "/configs/", zkPath));
    }
  }
}
 
Example #3
Source File: SettingsFileWatcher.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Override
public void init() {
    if (!enabled) {
        LOG.info("File watcher loaded but not enabled: {}", this.toString());
        return;
    }

    Path configFilePath = this.fileConfiguration.getPath();

    LOG.info("Adding watch on '{}'.", configFilePath);

    try {
        this.watchService = FileSystems.getDefault().newWatchService();
        Path parent = configFilePath.getParent();
        if (parent == null) {
            throw new ConfigurationError("Can not get parent directory of config file");
        }
        parent.register(this.watchService, StandardWatchEventKinds.ENTRY_MODIFY);
    } catch (IOException e) {
        throw new ConfigurationError("Error creating and registering watch service", e);
    }

    executor.submit(new Watcher(this.watchService, this.settingsService, this.fileConfiguration));
}
 
Example #4
Source File: FhirMetadataRetrievalTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void includeResourcesInBundle() throws Exception {
    Path bundle = FileSystems.getDefault().getPath("target/classes/META-INF/syndesis/schemas/dstu3/bundle.json");

    String inspection;
    try (InputStream fileIn = Files.newInputStream(bundle)) {
        inspection = IOUtils.toString(fileIn, StandardCharsets.UTF_8);
    }

    String inspectionWithResources = FhirMetadataRetrieval.includeResources(inspection, "patient", "account");

    ObjectMapper mapper = io.atlasmap.v2.Json.mapper();
    XmlDocument xmlDocument = mapper.readValue(inspectionWithResources, XmlDocument.class);

    XmlComplexType resource = (XmlComplexType) xmlDocument.getFields().getField().get(0);
    Assertions.assertThat(resource.getName()).isEqualTo("tns:Bundle");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:resource", "tns:Patient");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:resource", "tns:Patient", "tns:contained", "tns:Account");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:response", "tns:outcome", "tns:Patient");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:response", "tns:outcome", "tns:Patient", "tns:contained", "tns:Account");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:resource", "tns:Account");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:resource", "tns:Account", "tns:contained", "tns:Patient");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:response", "tns:outcome", "tns:Account");
    assertCorrectPath(resource, "tns:Bundle", "tns:entry", "tns:response", "tns:outcome", "tns:Account", "tns:contained", "tns:Patient");
}
 
Example #5
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void validate(Module module) throws IOException {
    ModuleDescriptor md = module.getDescriptor();

    // read m1/module-info.class
    FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
                                              Collections.emptyMap());
    Path path = fs.getPath("/", "modules", module.getName(), "module-info.class");
    ModuleDescriptor md1 = ModuleDescriptor.read(Files.newInputStream(path));


    // check the module descriptor of a system module and read from jimage
    checkPackages(md.packages(), "p1", "p2");
    checkPackages(md1.packages(), "p1", "p2");

    try (InputStream in = Files.newInputStream(path)) {
        checkModuleTargetAttribute(in, "p1");
    }
}
 
Example #6
Source File: TestFileHandler.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    File logsBase = new File(System.getProperty("tomcat.test.temp", "output/tmp"));
    if (!logsBase.mkdirs() && !logsBase.isDirectory()) {
        Assert.fail("Unable to create logs directory.");
    }
    Path logsBasePath = FileSystems.getDefault().getPath(logsBase.getAbsolutePath());
    logsDir = Files.createTempDirectory(logsBasePath, "test").toFile();

    generateLogFiles(logsDir, PREFIX_1, SUFIX_2, 3);
    generateLogFiles(logsDir, PREFIX_2, SUFIX_1, 3);
    generateLogFiles(logsDir, PREFIX_3, SUFIX_1, 3);
    generateLogFiles(logsDir, PREFIX_4, SUFIX_1, 3);

    Calendar date = Calendar.getInstance();
    date.add(Calendar.DAY_OF_MONTH, -3);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss.SSS");
    File file = new File(logsDir, PREFIX_1 + formatter.format(date.getTime()) + SUFIX_1);
    file.createNewFile();

}
 
Example #7
Source File: TestStressLayers.java    From openjdk-systemtest with Apache License 2.0 6 votes vote down vote up
@Override
public String call() throws Exception {
	ModuleLayer parentLayer = ModuleLayer.boot();
	
	// Obtain the parent Configuration 
	Path modulePath = FileSystems.getDefault().getPath(System.getProperty("module.path"));
	ModuleFinder moduleFinder = ModuleFinder.of(modulePath);
	ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

	Configuration cf1 = parentLayer.configuration()
			.resolve(moduleFinder, ModuleFinder.of(), Set.of("java.logging"));
	
	ModuleLayer layer1 = parentLayer.defineModulesWithManyLoaders(cf1, systemClassLoader);
	
	ClassLoader cl = layer1.findLoader("java.logging");
	
	assertNull("ClassLoader returned not-null for a bootstrap "
			+ "class being loaded in a non-boot layer", cl);

	System.out.println("Thread : " + Thread.currentThread().getName() + " done!");
	return null;
}
 
Example #8
Source File: PubSubEmulator.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private void startEmulator() throws IOException, InterruptedException {
	boolean configPresent = Files.exists(EMULATOR_CONFIG_PATH);
	WatchService watchService = null;

	if (configPresent) {
		watchService = FileSystems.getDefault().newWatchService();
		EMULATOR_CONFIG_DIR.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
	}

	try {
		this.emulatorProcess = new ProcessBuilder("gcloud", "beta", "emulators", "pubsub", "start")
				.start();
	}
	catch (IOException ex) {
		fail("Gcloud not found; leaving host/port uninitialized.");
	}

	if (configPresent) {
		updateConfig(watchService);
		watchService.close();
	}
	else {
		createConfig();
	}

}
 
Example #9
Source File: TestUtils.java    From commerce-gradle-plugin with Apache License 2.0 6 votes vote down vote up
public static void generateDummyPlatform(Path destination, String version) throws IOException, URISyntaxException {
    Path targetZip = destination.resolve(String.format("hybris-commerce-suite-%s.zip", version));
    Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    try (FileSystem zipfs = FileSystems.newFileSystem(URI.create("jar:" + targetZip.toUri().toString()), env, null)) {
        Path sourceDir = Paths.get(TestUtils.class.getResource("/dummy-platform").toURI());
        Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path relative = sourceDir.relativize(file);
                if (relative.getParent() != null) {
                    Path path = zipfs.getPath(relative.getParent().toString());
                    Files.createDirectories(path);
                }
                Path target = zipfs.getPath(relative.toString());
                Files.copy(file, target);
                return super.visitFile(file, attrs);
            }
        });
        String build = String.format("version=%s\n", version);
        Path buildNumber = zipfs.getPath("hybris", "bin", "platform", "build.number");
        Files.write(buildNumber, build.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    }
}
 
Example #10
Source File: ElasticMappingUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
private static FileSystem getFileSystem(URI jarUri) {
    FileSystem fs = fileSystems.get(jarUri);
    if (fs == null) {
        synchronized (fileSystems) {
            fs = fileSystems.get(jarUri);
            if (fs == null) {
                try {
                    fs = FileSystems.getFileSystem(jarUri);
                } catch (FileSystemNotFoundException e1) {
                    try {
                        fs = FileSystems.newFileSystem(jarUri, Collections.emptyMap());
                    } catch (IOException e2) {
                        throw new IllegalStateException("Could not create FileSystem for " + jarUri, e2);
                    }
                }
                fileSystems.put(jarUri, fs);
            }
        }
    }
    return fs;
}
 
Example #11
Source File: LotsOfCancels.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        // create a bunch of directories. Create two tasks for each directory,
        // one to bash on cancel, the other to poll the events
        ExecutorService pool = Executors.newCachedThreadPool();
        try {
            Path top = Files.createTempDirectory("work");
            top.toFile().deleteOnExit();
            for (int i=1; i<=16; i++) {
                Path dir = Files.createDirectory(top.resolve("dir-" + i));
                WatchService watcher = FileSystems.getDefault().newWatchService();
                pool.submit(() -> handle(dir, watcher));
                pool.submit(() -> poll(watcher));
            }
        } finally {
            pool.shutdown();
        }

        // give thread pool lots of time to terminate
        if (!pool.awaitTermination(5L, TimeUnit.MINUTES))
            throw new RuntimeException("Thread pool did not terminate");

        if (failed)
            throw new RuntimeException("Test failed, see log for details");
    }
 
Example #12
Source File: ZipUtils.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * This call is not thread safe, a single of FileSystem can be created for the
 * profided uri until it is closed.
 * 
 * @param uri The uri to the zip file.
 * @param env Env map.
 * @return A new FileSystem.
 * @throws IOException in case of a failure
 */
public static FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
    // If Multi threading required, logic should be added to wrap this fs
    // onto a fs that handles a reference counter and close the fs only when all thread are done
    // with it.
    try {
        return FileSystems.newFileSystem(uri, env);
    } catch (IOException | ZipError ioe) {
        // TODO: (at a later date) Get rid of the ZipError catching (and instead only catch IOException)
        //  since it's a JDK bug which threw the undeclared ZipError instead of an IOException.
        //  Java 9 fixes it https://bugs.openjdk.java.net/browse/JDK-8062754

        // include the URI for which the filesystem creation failed
        throw new IOException("Failed to create a new filesystem for " + uri, ioe);
    }
}
 
Example #13
Source File: Main.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static void main(String[] args) {

        Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");
        String customFilePrefix = "log_";
        String customFileSuffix = ".txt";

        try {
            Path tmpFile = Files.createTempFile(customBaseDir, customFilePrefix, customFileSuffix);
            System.out.println("Created as: " + tmpFile);
            System.out.println("Sleep for 10 seconds until deletion ...");

            File asFile = tmpFile.toFile();
            asFile.deleteOnExit();

            // simulate some operations with temp file until delete it                        
            Thread.sleep(10000);            

        } catch (IOException | InterruptedException e) {
            // handle exception, don't print it as below
            System.err.println(e);
        }
        
        System.out.println("Delete file completed ...");
    }
 
Example #14
Source File: SaltActionChainGeneratorService.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Make sure the {@literal actionchains} subdir exists.
 * @return the {@link Path} of the {@literal} actionchains directory
 */
private Path createActionChainsDir() {
    Path targetDir = getTargetDir();
    if (!Files.exists(targetDir)) {
        try {
            Files.createDirectories(targetDir);
            if (!skipSetOwner) {
                FileSystem fileSystem = FileSystems.getDefault();
                UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
                UserPrincipal tomcatUser = service.lookupPrincipalByName("tomcat");
                Files.setOwner(targetDir, tomcatUser);
            }
        }
        catch (IOException e) {
            LOG.error("Could not create action chain directory " + targetDir, e);
            throw new RuntimeException(e);
        }
    }
    return targetDir;
}
 
Example #15
Source File: ZipCat.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The main method for the ZipCat program. Run the program with an empty
 * argument list to see possible arguments.
 *
 * @param args the argument list for ZipCat
 */
public static void main(String[] args) {
    if (args.length != 2) {
        System.out.println("Usage: ZipCat zipfile fileToPrint");
    }
    /*
     * Creates AutoCloseable FileSystem and BufferedReader.
     * They will be closed automatically after the try block.
     * If reader initialization fails, then zipFileSystem will be closed
     * automatically.
     */
    try (FileSystem zipFileSystem
            = FileSystems.newFileSystem(Paths.get(args[0]),null);
            InputStream input
            = Files.newInputStream(zipFileSystem.getPath(args[1]))) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = input.read(buffer)) != -1) {
                    System.out.write(buffer, 0, len);
                }

    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example #16
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the URI of every file in the jrt file system
 */
@Test
public void testToAndFromUri() throws Exception {
    FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
    Path top = fs.getPath("/");
    try (Stream<Path> stream = Files.walk(top)) {
        stream.forEach(path -> {
            URI u = path.toUri();
            assertTrue(u.getScheme().equalsIgnoreCase("jrt"));
            assertFalse(u.isOpaque());
            assertTrue(u.getAuthority() == null);
            assertEquals(u.getPath(), path.toAbsolutePath().toString());
            Path p = Paths.get(u);
            assertEquals(p, path);
        });
    }
}
 
Example #17
Source File: StorageManager.java    From waltz with Apache License 2.0 6 votes vote down vote up
/**
 * This method initializes private data members of this class and also creates the Control File.
 *
 * @param directory The root directory of Storage where the transaction data is stored.
 * @param segmentSizeThreshold The maximum size of each Segment.
 * @param numPartitions The total number of partitions in the cluster.
 * @param key The cluster key.
 * @throws StorageException
 * @throws IOException
 */
public StorageManager(String directory, long segmentSizeThreshold, int numPartitions, UUID key, int segmentCacheCapacity) throws IOException, StorageException {
    logger.debug("StorageManager constructor is called");
    this.directory = FileSystems.getDefault().getPath(directory);
    this.partitions = new HashMap<>();
    this.segmentSizeThreshold = segmentSizeThreshold;
    this.segmentCacheCapacity = segmentCacheCapacity;

    this.controlFile = new ControlFile(key, this.directory.resolve(ControlFile.FILE_NAME), numPartitions, true);
    logger.debug("storage opened: directory={}", directory);

    for (int id : controlFile.getPartitionIds()) {
        if (controlFile.getPartitionInfo(id).getSnapshot().isAssigned) {
            createPartition(id);
        }
    }
}
 
Example #18
Source File: StorageManagerTest.java    From waltz with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemovePartitionWithoutFilePreserve() throws ConcurrentUpdateException, IOException, StorageException {
    int partitionId = new Random().nextInt(NUM_PARTITION);
    String fileNameFormat = "%019d.%s";
    File dir = Files.createTempDirectory("StorageManagerTest-").toFile();
    Path partitionDir = FileSystems.getDefault().getPath(dir.getPath()).resolve(Integer.toString(partitionId));
    Path segPath = partitionDir.resolve(String.format(fileNameFormat, 0L, "seg"));
    Path idxPath = partitionDir.resolve(String.format(fileNameFormat, 0L, "idx"));

    StorageManager manager = new StorageManager(dir.getPath(), WaltzStorageConfig.DEFAULT_SEGMENT_SIZE_THRESHOLD, NUM_PARTITION, clusterKey, WaltzStorageConfig.DEFAULT_STORAGE_SEGMENT_CACHE_CAPACITY);

    try {
        manager.open(clusterKey, NUM_PARTITION);
        manager.setPartitionAssignment(partitionId, true, false);

        manager.setPartitionAssignment(partitionId, false, true);

        assertFalse(segPath.toFile().exists());
        assertFalse(idxPath.toFile().exists());
    } finally {
        manager.close();
    }
}
 
Example #19
Source File: WinGammaPlatformVC7.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void handleIncludes(Vector<String> includes, Vector<BuildConfig> allConfigs) {
   for (String path : includes)  {
      FileTreeCreatorVC7 ftc = new FileTreeCreatorVC7(FileSystems.getDefault().getPath(path) , allConfigs, this);
      try {
         ftc.writeFileTree();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
 
Example #20
Source File: WinGammaPlatformVC10.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void handleIncludes(Vector<String> includes, Vector<BuildConfig> allConfigs) {
   for (String path : includes)  {
      FileTreeCreatorVC10 ftc = new FileTreeCreatorVC10(FileSystems.getDefault().getPath(path) , allConfigs, this);
      try {
         ftc.writeFileTree();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}
 
Example #21
Source File: Plugin.java    From gate-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean hasResources() {
  try (FileSystem zipFs = FileSystems
      .newFileSystem(getMetadataArtifactURL().toURI(), new HashMap<>());) {
    Path pathInZip = zipFs.getPath("/resources");
    return Files.isDirectory(pathInZip);
  } catch(Exception e) {
    return false;
  }
}
 
Example #22
Source File: CompileTheWorld.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private String[] readJimageEntries() {
    try {
        // Use reflection so this can be compiled on JDK8
        Path path = FileSystems.getDefault().getPath(name);
        Method open = Class.forName("jdk.internal.jimage.BasicImageReader").getDeclaredMethod("open", Path.class);
        Object reader = open.invoke(null, path);
        Method getEntryNames = reader.getClass().getDeclaredMethod("getEntryNames");
        getEntryNames.setAccessible(true);
        String[] entries = (String[]) getEntryNames.invoke(reader);
        return entries;
    } catch (Exception e) {
        TTY.println("Error reading entries from " + name + ": " + e);
        return new String[0];
    }
}
 
Example #23
Source File: ResourcesHandler.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
private void walkJar(Path jarfile, ArrayList<Path> classfiles) {
	try {
		FileSystem jarfs = FileSystems.newFileSystem(jarfile,null);
		Iterable<Path> rootDirectories = jarfs.getRootDirectories();
		ClassCollectorFileVisitor x = new ClassCollectorFileVisitor();
		for (Path path: rootDirectories) {
			Files.walkFileTree(path, x);
		}
		classfiles.addAll(x.getClassFiles());
	} catch (IOException e) {
		throw new IllegalStateException("Problem opening "+jarfile,e);
	}
}
 
Example #24
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testParentInDirList(String dir) throws Exception {
    FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
    Path base = fs.getPath(dir);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(base)) {
        for (Path entry: stream) {
            assertTrue( entry.getParent().equals(base),
                base.toString() + "-> " + entry.toString() );
        }
    }
}
 
Example #25
Source File: ZipFileStore.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public ZipFileStoreAttributes(ZipFileStore fileStore)
    throws IOException
{
    Path path = FileSystems.getDefault().getPath(fileStore.name());
    this.size = Files.size(path);
    this.fstore = Files.getFileStore(path);
}
 
Example #26
Source File: CodeStoreAndPathTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Path getCodeCachePath(final boolean optimistic) {
    final String codeCache = System.getProperty("nashorn.persistent.code.cache");
    final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
    final String[] files = codeCachePath.toFile().list();
    for (final String file : files) {
        if (file.endsWith("_opt") == optimistic) {
            return codeCachePath.resolve(file);
        }
    }
    throw new AssertionError("Code cache path not found");
}
 
Example #27
Source File: TestStressLayers.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
@Override
public String call() throws Exception {
	for (int i = 0 ; i < REPEATATION_PER_THREAD ; i++ ) {
		//Obtain the parent Layer we currently have, which should be the boot layer 
		ModuleLayer parentLayer = ModuleLayer.boot();
		
		//Obtain the parent Configuration 
		Path modulePath = FileSystems.getDefault().getPath(System.getProperty("module.path"));
		ModuleFinder moduleFinder = ModuleFinder.of(modulePath);
	
		ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
	
		Configuration cf1 = parentLayer.configuration()
				.resolve(moduleFinder, ModuleFinder.of(), Set.of("com.hello"));
				
		Configuration cf2 = parentLayer.configuration()
				.resolve(moduleFinder, ModuleFinder.of(), Set.of("com.helper"));
		
		ModuleLayer layer1 = parentLayer.defineModulesWithManyLoaders(cf1, systemClassLoader);
		ModuleLayer layer2 = parentLayer.defineModulesWithManyLoaders(cf2, systemClassLoader);
		
		Class<?> c1 = layer1.findLoader("com.hello").loadClass("adoptopenjdk.test.modularity.hello.Hello");
		String returnValue = (String) c1.getMethod("name").invoke(null);
		assertEquals("Test failed - Expected value: [Hello], but value received: [" + returnValue + "]", returnValue, "Hello");
		
		Class<?> c2 = layer2.findLoader("com.helper").loadClass("adoptopenjdk.test.modularity.helper.pkgOne.HelperClass");
		returnValue = (String) c2.getMethod("name").invoke(null);
		assertEquals("Test failed - Expected value: [Helper], but value received: [" + returnValue + "]", returnValue, "helper");
	}
	
	System.out.println("Thread : " + Thread.currentThread().getName() + " done!");
	return null;
}
 
Example #28
Source File: DataLoad.java    From arvo2parquet with MIT License 5 votes vote down vote up
private static void doTestParquet(final Schema schema, final int maxRecords, final Path parquetFilePath)
        throws IOException
{
  final RandomString session = new RandomString(64);
  final GenericData.Record record = new GenericData.Record(schema);
  final int[] count = { 1 };
  final int[] iterations = { maxRecords };

  writeToParquet(schema, parquetFilePath, writer -> {
    record.put("c1", count[0]++);
    record.put("c2", session.nextString());
    writer.write(record);
    return (--iterations[0]) > 0;
  });

  readFromParquet(parquetFilePath);

  extractMetaDataFooter(parquetFilePath);

  Path parentDirPath = parquetFilePath.getParent();
  if (parentDirPath == null) {
    parentDirPath = FileSystems.getDefault().getPath(".");
  }
  final Path emptyParquetFilePath = Paths.get(parentDirPath.toString(), "empty.parquet");
  //noinspection EmptyTryBlock
  try (final ParquetWriter<GenericData.Record> ignored = createParquetWriterInstance(schema, emptyParquetFilePath)) {}
}
 
Example #29
Source File: LocalFileCheckpointHandler.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
LocalFileCheckpointHandler(String directory, FileHelper fileHelper) {
  String checkpointDir =
      checkNotNull(directory, "checkpoint directory can not be null").trim();
  this.basePath = FileSystems.getDefault().getPath(checkpointDir);
  this.fileHelper = checkNotNull(fileHelper);
}
 
Example #30
Source File: ExternalEditor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a WatchService and registers the given directory
 */
private void setupWatch(final String initialText) throws IOException {
    this.watcher = FileSystems.getDefault().newWatchService();
    this.dir = Files.createTempDirectory("REPL");
    this.tmpfile = Files.createTempFile(dir, null, ".js");
    Files.write(tmpfile, initialText.getBytes(Charset.forName("UTF-8")));
    dir.register(watcher,
            ENTRY_CREATE,
            ENTRY_DELETE,
            ENTRY_MODIFY);
    watchedThread = new Thread(() -> {
        for (;;) {
            WatchKey key;
            try {
                key = watcher.take();
            } catch (final ClosedWatchServiceException ex) {
                break;
            } catch (final InterruptedException ex) {
                continue; // tolerate an intrupt
            }

            if (!key.pollEvents().isEmpty()) {
                if (!input.terminalEditorRunning()) {
                    saveFile();
                }
            }

            boolean valid = key.reset();
            if (!valid) {
                errorHandler.accept("Invalid key");
                break;
            }
        }
    });
    watchedThread.start();
}