use of java.nio.file.Files in project archiva by apache.
the class RepositoryScannerInstance method visitFile.
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (excludeMatcher.stream().noneMatch(m -> m.matches(file)) && includeMatcher.stream().allMatch(m -> m.matches(file))) {
log.debug("Walk Step: {}, {}", file);
stats.increaseFileCount();
// consume files regardless - the predicate will check the timestamp
Path repoPath = PathUtil.getPathFromUri(repository.getLocation());
BaseFile basefile = new BaseFile(repoPath.toString(), file.toFile());
// Timestamp finished points to the last successful scan, not this current one.
if (Files.getLastModifiedTime(file).toMillis() >= changesSince) {
stats.increaseNewFileCount();
}
consumerProcessFile.setBasefile(basefile);
consumerWantsFile.setBasefile(basefile);
Closure<RepositoryContentConsumer> processIfWanted = IfClosure.ifClosure(consumerWantsFile, consumerProcessFile);
IterableUtils.forEach(this.knownConsumers, processIfWanted);
if (consumerWantsFile.getWantedFileCount() <= 0) {
// Nothing known processed this file. It is invalid!
IterableUtils.forEach(this.invalidConsumers, consumerProcessFile);
}
}
return FileVisitResult.CONTINUE;
}
use of java.nio.file.Files in project Bytecoder by mirkosertic.
the class ModulePath method deriveModuleDescriptor.
/**
* Treat the given JAR file as a module as follows:
*
* 1. The value of the Automatic-Module-Name attribute is the module name
* 2. The version, and the module name when the Automatic-Module-Name
* attribute is not present, is derived from the file ame of the JAR file
* 3. All packages are derived from the .class files in the JAR file
* 4. The contents of any META-INF/services configuration files are mapped
* to "provides" declarations
* 5. The Main-Class attribute in the main attributes of the JAR manifest
* is mapped to the module descriptor mainClass if possible
*/
private ModuleDescriptor deriveModuleDescriptor(JarFile jf) throws IOException {
// Read Automatic-Module-Name attribute if present
Manifest man = jf.getManifest();
Attributes attrs = null;
String moduleName = null;
if (man != null) {
attrs = man.getMainAttributes();
if (attrs != null) {
moduleName = attrs.getValue(AUTOMATIC_MODULE_NAME);
}
}
// Derive the version, and the module name if needed, from JAR file name
String fn = jf.getName();
int i = fn.lastIndexOf(File.separator);
if (i != -1)
fn = fn.substring(i + 1);
// drop ".jar"
String name = fn.substring(0, fn.length() - 4);
String vs = null;
// find first occurrence of -${NUMBER}. or -${NUMBER}$
Matcher matcher = Patterns.DASH_VERSION.matcher(name);
if (matcher.find()) {
int start = matcher.start();
// attempt to parse the tail as a version string
try {
String tail = name.substring(start + 1);
ModuleDescriptor.Version.parse(tail);
vs = tail;
} catch (IllegalArgumentException ignore) {
}
name = name.substring(0, start);
}
// Create builder, using the name derived from file name when
// Automatic-Module-Name not present
Builder builder;
if (moduleName != null) {
try {
builder = ModuleDescriptor.newAutomaticModule(moduleName);
} catch (IllegalArgumentException e) {
throw new FindException(AUTOMATIC_MODULE_NAME + ": " + e.getMessage());
}
} else {
builder = ModuleDescriptor.newAutomaticModule(cleanModuleName(name));
}
// module version if present
if (vs != null)
builder.version(vs);
// scan the names of the entries in the JAR file
Map<Boolean, Set<String>> map = VersionedStream.stream(jf).filter(e -> !e.isDirectory()).map(JarEntry::getName).filter(e -> (e.endsWith(".class") ^ e.startsWith(SERVICES_PREFIX))).collect(Collectors.partitioningBy(e -> e.startsWith(SERVICES_PREFIX), Collectors.toSet()));
Set<String> classFiles = map.get(Boolean.FALSE);
Set<String> configFiles = map.get(Boolean.TRUE);
// the packages containing class files
Set<String> packages = classFiles.stream().map(this::toPackageName).flatMap(Optional::stream).distinct().collect(Collectors.toSet());
// all packages are exported and open
builder.packages(packages);
// map names of service configuration files to service names
Set<String> serviceNames = configFiles.stream().map(this::toServiceName).flatMap(Optional::stream).collect(Collectors.toSet());
// parse each service configuration file
for (String sn : serviceNames) {
JarEntry entry = jf.getJarEntry(SERVICES_PREFIX + sn);
List<String> providerClasses = new ArrayList<>();
try (InputStream in = jf.getInputStream(entry)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String cn;
while ((cn = nextLine(reader)) != null) {
if (cn.length() > 0) {
String pn = packageName(cn);
if (!packages.contains(pn)) {
String msg = "Provider class " + cn + " not in module";
throw new InvalidModuleDescriptorException(msg);
}
providerClasses.add(cn);
}
}
}
if (!providerClasses.isEmpty())
builder.provides(sn, providerClasses);
}
// Main-Class attribute if it exists
if (attrs != null) {
String mainClass = attrs.getValue(Attributes.Name.MAIN_CLASS);
if (mainClass != null) {
mainClass = mainClass.replace("/", ".");
if (Checks.isClassName(mainClass)) {
String pn = packageName(mainClass);
if (packages.contains(pn)) {
builder.mainClass(mainClass);
}
}
}
}
return builder.build();
}
use of java.nio.file.Files in project n4js by eclipse.
the class AbstractFileChecker method containsFileWithName.
protected boolean containsFileWithName(Path path, String fileName) {
final File file = path.toFile();
final File[] files = file.listFiles();
return files != null && Stream.of(files).anyMatch(f -> fileName.equals(f.getName()));
}
use of java.nio.file.Files in project n4js by eclipse.
the class AbstractFileChecker method readListOfThirdPartyFiles.
// ################################################################################################################
/**
* <pre>
* #
* # List of files and folders with third-party copyright.
* #
* #
* # This file is processed automatically by FileChecker.java to ensure the below information is kept up-to-date.
* #
* # Format:
* # every non-empty line in this file either starts with '#' and is then a comment (to be ignored) or must
* # contain the relative path to a file with third-party copyright. If a path ends in "/**" it must point to
* # a folder and its contents are declared to be third-party files. All paths must be relative to the folder
* # containing this file.
* #
* </pre>
*/
private static Set<Path> readListOfThirdPartyFiles(Path rootPath) throws IOException {
System.out.println("Reading list of third-party files from \"" + FILE_NAME__THIRD_PARTY + "\" ...");
final Path thirdPartyList = rootPath.resolve(FILE_NAME__THIRD_PARTY);
if (!thirdPartyList.toFile().exists()) {
// note: providing a third-party.txt file is optional, so no error here:
System.out.println(" no such file found, assuming 0 third-party files.");
return Collections.emptySet();
}
final List<String> lines = Files.readAllLines(thirdPartyList, StandardCharsets.UTF_8);
// trim all lines
lines.replaceAll((l) -> l.trim());
// remove empty lines and comments
lines.removeIf((l) -> l.length() == 0 || l.startsWith("#"));
// make sure all paths are relative
if (lines.stream().anyMatch((l) -> l.startsWith("/") || l.startsWith("\\")))
throw new IOException("paths in " + FILE_NAME__THIRD_PARTY + " must be relative, i.e. not start with '/'");
// make sure all files/folders exist & are of correct type
final List<Path> paths = lines.stream().map((l) -> rootPath.resolve(l)).collect(Collectors.toList());
int files = 0;
int folders = 0;
// replace folders by their contained files
for (int i = 0; i < paths.size(); i++) {
final Path p = paths.get(i);
if (p.endsWith("**")) {
final Path parent = p.getParent();
final List<Path> allFiles = getAllContainedFiles(parent);
if (allFiles.isEmpty()) {
// throw new IOException("folder is empty: " + parent);
}
paths.remove(i);
paths.addAll(i, allFiles);
i += allFiles.size() - 1;
}
}
// check for duplicates
final Set<Path> duplicates = collectDuplicates(paths);
if (!duplicates.isEmpty()) {
throw new IOException("the following files are declared more than once in " + FILE_NAME__THIRD_PARTY + " (maybe because they are contained in a folder declared with \"/**\"):\n " + Joiner.on("\n ").join(duplicates));
}
// report to user
System.out.println(" " + files + " files and " + folders + " folders (for a total of " + paths.size() + " files) declared as third-party artifacts.");
// order does not matter, so don't need LinkedHashSet
return new HashSet<>(paths);
}
use of java.nio.file.Files in project fakereplace by fakereplace.
the class WildflyAutoUpdate method runUpdate.
public static synchronized Result runUpdate(ModuleClassLoader classLoader) {
String moduleName = classLoader.getModule().getIdentifier().getName();
if (moduleName.startsWith(DEPLOYMENT)) {
moduleName = moduleName.substring(DEPLOYMENT.length());
}
String sourcePaths = System.getProperty(FAKEREPLACE_SOURCE_PATHS + moduleName);
if (sourcePaths == null) {
return Result.NO_CHANGE;
}
List<Path> paths = Arrays.stream(sourcePaths.split(",")).map((s) -> Paths.get(s)).collect(Collectors.toList());
try {
for (Path base : paths) {
final Map<String, Long> timestamps = new HashMap<>();
scan(base, base, timestamps);
List<String> toUpdate = new ArrayList<>();
List<String> added = new ArrayList<>();
List<String> replace = new ArrayList<>();
for (Map.Entry<String, Long> entry : timestamps.entrySet()) {
String name = entry.getKey();
if (name.endsWith(".java")) {
String baseName = name.substring(0, name.length() - 5);
Long last = replacedTimestamps.get(baseName);
if (last != null) {
if (last < entry.getValue()) {
toUpdate.add(baseName);
replacedTimestamps.put(baseName, entry.getValue());
replace.add(baseName);
}
} else {
URL res = classLoader.getResource(baseName + ".class");
if (res != null) {
URLConnection con = res.openConnection();
long lm = con.getLastModified();
if (lm < entry.getValue()) {
toUpdate.add(baseName);
replacedTimestamps.put(baseName, entry.getValue());
replace.add(baseName);
}
} else {
toUpdate.add(baseName);
replacedTimestamps.put(baseName, entry.getValue());
added.add(baseName);
}
}
}
}
if (!toUpdate.isEmpty()) {
System.out.println("Fakereplace detected the following source files have been changed: " + toUpdate);
ClassLoaderCompiler compiler = new ClassLoaderCompiler(classLoader, base, toUpdate);
compiler.compile();
Map<String, byte[]> byteMap = REPLACED_CLASSES.computeIfAbsent(classLoader, k -> new HashMap<>());
AddedClass[] addedClass = new AddedClass[added.size()];
for (int i = 0; i < added.size(); ++i) {
String className = added.get(i);
addedClass[i] = new AddedClass(className, compiler.getOutput().get(className).toByteArray(), classLoader);
byteMap.put(className, compiler.getOutput().get(className).toByteArray());
}
ClassDefinition[] classDefinition = new ClassDefinition[replace.size()];
for (int i = 0; i < replace.size(); ++i) {
String className = replace.get(i);
classDefinition[i] = new ClassDefinition(classLoader.loadClass(className.replace("/", ".")), compiler.getOutput().get(className).toByteArray());
byteMap.put(className, compiler.getOutput().get(className).toByteArray());
}
try {
Fakereplace.redefine(classDefinition, addedClass);
} catch (Exception e) {
System.err.println("Hot replace failed, redeploy required" + e.getMessage());
return Result.REDEPLOY_REQUIRED;
}
return Result.RELOAD;
}
}
return Result.NO_CHANGE;
} catch (Exception e) {
System.err.println("Check for updated classes failed");
e.printStackTrace();
} finally {
// something in the compiler clears the TCCL, fix it up
Thread.currentThread().setContextClassLoader(classLoader);
}
return Result.NO_CHANGE;
}
Aggregations