use of java.nio.file.Files in project jetty.project by eclipse.
the class BaseBuilder method build.
/**
* Build out the Base directory (if needed)
*
* @return true if base directory was changed, false if left unchanged.
* @throws IOException if unable to build
*/
public boolean build() throws IOException {
Modules modules = startArgs.getAllModules();
// Select all the added modules to determine which ones are newly enabled
Set<String> newly_added = new HashSet<>();
if (!startArgs.getStartModules().isEmpty()) {
for (String name : startArgs.getStartModules()) {
newly_added.addAll(modules.enable(name, "--add-to-start"));
if (!newly_added.contains(name)) {
Set<String> sources = modules.get(name).getEnableSources();
sources.remove("--add-to-start");
StartLog.info("%s already enabled by %s", name, sources);
}
}
}
if (StartLog.isDebugEnabled())
StartLog.debug("added=%s", newly_added);
// Check the licenses
if (startArgs.isLicenseCheckRequired()) {
Licensing licensing = new Licensing();
for (String name : newly_added) licensing.addModule(modules.get(name));
if (licensing.hasLicenses()) {
if (startArgs.isApproveAllLicenses()) {
StartLog.info("All Licenses Approved via Command Line Option");
} else if (!licensing.acknowledgeLicenses()) {
StartLog.warn(EXITING_LICENSE_NOT_ACKNOWLEDGED);
System.exit(1);
}
}
}
// generate the files
List<FileArg> files = new ArrayList<FileArg>();
AtomicReference<BaseBuilder.Config> builder = new AtomicReference<>();
AtomicBoolean modified = new AtomicBoolean();
Path startd = getBaseHome().getBasePath("start.d");
Path startini = getBaseHome().getBasePath("start.ini");
if (startArgs.isCreateStartd() && !Files.exists(startd)) {
if (FS.ensureDirectoryExists(startd)) {
StartLog.log("MKDIR", baseHome.toShortForm(startd));
modified.set(true);
}
if (Files.exists(startini)) {
int ini = 0;
Path startd_startini = startd.resolve("start.ini");
while (Files.exists(startd_startini)) {
ini++;
startd_startini = startd.resolve("start" + ini + ".ini");
}
Files.move(startini, startd_startini);
modified.set(true);
}
}
if (!newly_added.isEmpty()) {
if (Files.exists(startini) && Files.exists(startd))
StartLog.warn("Use both %s and %s is deprecated", getBaseHome().toShortForm(startd), getBaseHome().toShortForm(startini));
boolean useStartD = Files.exists(startd);
builder.set(useStartD ? new StartDirBuilder(this) : new StartIniBuilder(this));
newly_added.stream().map(n -> modules.get(n)).forEach(module -> {
String ini = null;
try {
if (module.isSkipFilesValidation()) {
StartLog.debug("Skipping [files] validation on %s", module.getName());
} else {
if (startArgs.getStartModules().contains(module.getName())) {
ini = builder.get().addModule(module, startArgs.getProperties());
if (ini != null)
modified.set(true);
}
for (String file : module.getFiles()) files.add(new FileArg(module, startArgs.getProperties().expand(file)));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (module.isDynamic()) {
for (String s : module.getEnableSources()) StartLog.info("%-15s %s", module.getName(), s);
} else if (module.isTransitive()) {
if (module.hasIniTemplate())
StartLog.info("%-15s transitively enabled, ini template available with --add-to-start=%s", module.getName(), module.getName());
else
StartLog.info("%-15s transitively enabled", module.getName());
} else
StartLog.info("%-15s initialized in %s", module.getName(), ini);
});
}
files.addAll(startArgs.getFiles());
if (!files.isEmpty() && processFileResources(files))
modified.set(Boolean.TRUE);
return modified.get();
}
use of java.nio.file.Files in project elasticsearch by elastic.
the class SharedClusterSnapshotRestoreIT method testGetSnapshotsRequest.
public void testGetSnapshotsRequest() throws Exception {
final String repositoryName = "test-repo";
final String indexName = "test-idx";
final Client client = client();
final Path repo = randomRepoPath();
logger.info("--> creating repository at {}", repo.toAbsolutePath());
assertAcked(client.admin().cluster().preparePutRepository(repositoryName).setType("mock").setSettings(Settings.builder().put("location", repo).put("compress", false).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES).put("wait_after_unblock", 200)));
logger.info("--> get snapshots on an empty repository");
expectThrows(SnapshotMissingException.class, () -> client.admin().cluster().prepareGetSnapshots(repositoryName).addSnapshots("non-existent-snapshot").get());
// with ignore unavailable set to true, should not throw an exception
GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots(repositoryName).setIgnoreUnavailable(true).addSnapshots("non-existent-snapshot").get();
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
logger.info("--> creating an index and indexing documents");
// Create index on 2 nodes and make sure each node has a primary by setting no replicas
assertAcked(prepareCreate(indexName, 1, Settings.builder().put("number_of_replicas", 0)));
ensureGreen();
for (int i = 0; i < 10; i++) {
index(indexName, "doc", Integer.toString(i), "foo", "bar" + i);
}
refresh();
// make sure we return only the in-progress snapshot when taking the first snapshot on a clean repository
// take initial snapshot with a block, making sure we only get 1 in-progress snapshot returned
// block a node so the create snapshot operation can remain in progress
final String initialBlockedNode = blockNodeWithIndex(repositoryName, indexName);
ListenableActionFuture<CreateSnapshotResponse> responseListener = client.admin().cluster().prepareCreateSnapshot(repositoryName, "snap-on-empty-repo").setWaitForCompletion(false).setIndices(indexName).execute();
// wait for block to kick in
waitForBlock(initialBlockedNode, repositoryName, TimeValue.timeValueSeconds(60));
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots(randomFrom("_all", "_current", "snap-on-*", "*-on-empty-repo", "snap-on-empty-repo")).get();
assertEquals(1, getSnapshotsResponse.getSnapshots().size());
assertEquals("snap-on-empty-repo", getSnapshotsResponse.getSnapshots().get(0).snapshotId().getName());
// unblock node
unblockNode(repositoryName, initialBlockedNode);
// timeout after 10 seconds
responseListener.actionGet(TimeValue.timeValueMillis(10000L));
client.admin().cluster().prepareDeleteSnapshot(repositoryName, "snap-on-empty-repo").get();
final int numSnapshots = randomIntBetween(1, 3) + 1;
logger.info("--> take {} snapshot(s)", numSnapshots - 1);
final String[] snapshotNames = new String[numSnapshots];
for (int i = 0; i < numSnapshots - 1; i++) {
final String snapshotName = randomAsciiOfLength(8).toLowerCase(Locale.ROOT);
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repositoryName, snapshotName).setWaitForCompletion(true).setIndices(indexName).get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
snapshotNames[i] = snapshotName;
}
logger.info("--> take another snapshot to be in-progress");
// add documents so there are data files to block on
for (int i = 10; i < 20; i++) {
index(indexName, "doc", Integer.toString(i), "foo", "bar" + i);
}
refresh();
final String inProgressSnapshot = randomAsciiOfLength(8).toLowerCase(Locale.ROOT);
snapshotNames[numSnapshots - 1] = inProgressSnapshot;
// block a node so the create snapshot operation can remain in progress
final String blockedNode = blockNodeWithIndex(repositoryName, indexName);
client.admin().cluster().prepareCreateSnapshot(repositoryName, inProgressSnapshot).setWaitForCompletion(false).setIndices(indexName).get();
// wait for block to kick in
waitForBlock(blockedNode, repositoryName, TimeValue.timeValueSeconds(60));
logger.info("--> get all snapshots with a current in-progress");
// with ignore unavailable set to true, should not throw an exception
final List<String> snapshotsToGet = new ArrayList<>();
if (randomBoolean()) {
// use _current plus the individual names of the finished snapshots
snapshotsToGet.add("_current");
for (int i = 0; i < numSnapshots - 1; i++) {
snapshotsToGet.add(snapshotNames[i]);
}
} else {
snapshotsToGet.add("_all");
}
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots(repositoryName).setSnapshots(snapshotsToGet.toArray(Strings.EMPTY_ARRAY)).get();
List<String> sortedNames = Arrays.asList(snapshotNames);
Collections.sort(sortedNames);
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(numSnapshots));
assertThat(getSnapshotsResponse.getSnapshots().stream().map(s -> s.snapshotId().getName()).sorted().collect(Collectors.toList()), equalTo(sortedNames));
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots(repositoryName).addSnapshots(snapshotNames).get();
sortedNames = Arrays.asList(snapshotNames);
Collections.sort(sortedNames);
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(numSnapshots));
assertThat(getSnapshotsResponse.getSnapshots().stream().map(s -> s.snapshotId().getName()).sorted().collect(Collectors.toList()), equalTo(sortedNames));
logger.info("--> make sure duplicates are not returned in the response");
String regexName = snapshotNames[randomIntBetween(0, numSnapshots - 1)];
final int splitPos = regexName.length() / 2;
final String firstRegex = regexName.substring(0, splitPos) + "*";
final String secondRegex = "*" + regexName.substring(splitPos);
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots(repositoryName).addSnapshots(snapshotNames).addSnapshots(firstRegex, secondRegex).get();
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(numSnapshots));
assertThat(getSnapshotsResponse.getSnapshots().stream().map(s -> s.snapshotId().getName()).sorted().collect(Collectors.toList()), equalTo(sortedNames));
// unblock node
unblockNode(repositoryName, blockedNode);
waitForCompletion(repositoryName, inProgressSnapshot, TimeValue.timeValueSeconds(60));
}
use of java.nio.file.Files in project buck by facebook.
the class ClassesImpl method createJar.
@Override
public void createJar(Path jarPath) throws IOException {
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarPath))) {
List<Path> files = Files.walk(root.getRoot().toPath()).filter(path -> path.toFile().isFile()).collect(Collectors.toList());
for (Path file : files) {
ZipEntry entry = new ZipEntry(MorePaths.pathWithUnixSeparators(root.getRoot().toPath().relativize(file)));
jar.putNextEntry(entry);
ByteStreams.copy(Files.newInputStream(file), jar);
jar.closeEntry();
}
}
}
use of java.nio.file.Files in project opennms by OpenNMS.
the class NewtsConverter method processStringsProperties.
private void processStringsProperties(final Path path) {
try {
// Find an process all 'strings.properties' files
Files.walk(path).filter(p -> p.endsWith("strings.properties")).forEach(p -> {
final Properties properties = new Properties();
try (final BufferedReader r = Files.newBufferedReader(p)) {
properties.load(r);
} catch (final IOException e) {
throw Throwables.propagate(e);
}
final ResourcePath resourcePath = buildResourcePath(p.getParent());
if (resourcePath == null) {
return;
}
this.injectStringPropertiesToNewts(resourcePath, Maps.fromProperties(properties));
});
} catch (Exception e) {
LOG.error("Error while reading string.properties", e);
return;
}
}
use of java.nio.file.Files in project jdk8u_jdk by JetBrains.
the class Compiler method compileOne.
private File compileOne(Type type) {
if (this.flags.contains(Flags.USECACHE)) {
File dir = cache.get(type.getName());
if (dir != null) {
return dir;
}
}
List<JavaFileObject> files = new ArrayList<>();
SourceProcessor accum = (name, src) -> files.add(new SourceFile(name, src));
for (Type dep : type.typeDependencies()) {
dep.generateAsDependency(accum, type.methodDependencies());
}
type.generate(accum);
JavacTask ct = (JavacTask) this.systemJavaCompiler.getTask(null, this.fm, null, null, null, files);
File destDir = null;
do {
int value = counter.incrementAndGet();
destDir = new File(root, Integer.toString(value));
} while (destDir.exists());
if (this.flags.contains(Flags.VERBOSE)) {
System.out.println("Compilation unit for " + type.getName() + " : compiled into " + destDir);
for (JavaFileObject jfo : files) {
System.out.println(jfo.toString());
}
}
try {
destDir.mkdirs();
this.fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
} catch (IOException e) {
throw new RuntimeException("IOException encountered during compilation");
}
Boolean result = ct.call();
if (result == Boolean.FALSE) {
throw new RuntimeException("Compilation failure in " + type.getName() + " unit");
}
if (this.flags.contains(Flags.USECACHE)) {
File existing = cache.putIfAbsent(type.getName(), destDir);
if (existing != null) {
deleteDir(destDir);
return existing;
}
} else {
this.tempDirs.add(destDir);
}
return destDir;
}
Aggregations