Search in sources :

Example 1 with NativeException

use of net.rubygrapefruit.platform.NativeException in project gradle by gradle.

the class NativeServices method initialize.

/**
 * Initializes the native services to use the given user home directory to store native libs and other resources. Does nothing if already initialized.
 */
public static synchronized void initialize(File userHomeDir, boolean initializeJansi) {
    if (!initialized) {
        useNativeIntegrations = "true".equalsIgnoreCase(System.getProperty("org.gradle.native", "true"));
        if (useNativeIntegrations) {
            File nativeBaseDir = getNativeServicesDir(userHomeDir);
            try {
                net.rubygrapefruit.platform.Native.init(nativeBaseDir);
            } catch (NativeIntegrationUnavailableException ex) {
                LOGGER.debug("Native-platform is not available.");
                useNativeIntegrations = false;
            } catch (NativeException ex) {
                if (ex.getCause() instanceof UnsatisfiedLinkError && ex.getCause().getMessage().toLowerCase().contains("already loaded in another classloader")) {
                    LOGGER.debug("Unable to initialize native-platform. Failure: {}", format(ex));
                    useNativeIntegrations = false;
                } else if (ex.getMessage().equals("Could not extract native JNI library.") && ex.getCause().getMessage().contains("native-platform.dll (The process cannot access the file because it is being used by another process)")) {
                    // triggered through tooling API of Gradle <2.3 - native-platform.dll is shared by tooling client (<2.3) and daemon (current) and it is locked by the client (<2.3 issue)
                    LOGGER.debug("Unable to initialize native-platform. Failure: {}", format(ex));
                    useNativeIntegrations = false;
                } else {
                    throw ex;
                }
            }
            if (initializeJansi) {
                JANSI_BOOT_PATH_CONFIGURER.configure(nativeBaseDir);
            }
            LOGGER.info("Initialized native services in: " + nativeBaseDir);
        }
        initialized = true;
    }
}
Also used : NativeIntegrationUnavailableException(net.rubygrapefruit.platform.NativeIntegrationUnavailableException) File(java.io.File) NativeException(net.rubygrapefruit.platform.NativeException)

Example 2 with NativeException

use of net.rubygrapefruit.platform.NativeException in project gradle by gradle.

the class NativePlatformBackedFileMetadataAccessor method stat.

@Override
public FileMetadata stat(File f) {
    FileInfo stat;
    try {
        stat = files.stat(f, false);
    } catch (NativeException e) {
        throw new UncheckedIOException("Could not stat file " + f.getAbsolutePath(), e);
    }
    AccessType accessType = AccessType.viaSymlink(stat.getType() == FileInfo.Type.Symlink);
    if (accessType == AccessType.VIA_SYMLINK) {
        try {
            stat = files.stat(f, true);
        } catch (NativeException e) {
            // For a symlink cycle, file.exists() returns false when unable to stat the file.
            if (!f.exists()) {
                return DefaultFileMetadata.missing(accessType);
            }
            throw new UncheckedIOException("Could not stat file " + f.getAbsolutePath(), e);
        }
    }
    switch(stat.getType()) {
        case File:
            return DefaultFileMetadata.file(stat.getLastModifiedTime(), stat.getSize(), accessType);
        case Directory:
            return DefaultFileMetadata.directory(accessType);
        case Missing:
            return DefaultFileMetadata.missing(accessType);
        case Other:
            throw new UncheckedIOException("Unsupported file type for " + f.getAbsolutePath());
        default:
            throw new IllegalArgumentException("Unrecognised file type: " + stat.getType());
    }
}
Also used : FileInfo(net.rubygrapefruit.platform.file.FileInfo) UncheckedIOException(org.gradle.api.UncheckedIOException) NativeException(net.rubygrapefruit.platform.NativeException) AccessType(org.gradle.internal.file.FileMetadata.AccessType)

Example 3 with NativeException

use of net.rubygrapefruit.platform.NativeException in project gradle by gradle.

the class NativeOsMemoryInfo method getOsSnapshot.

@Override
public OsMemoryStatus getOsSnapshot() {
    try {
        Memory memory = NativeServices.getInstance().get(Memory.class);
        MemoryInfo memoryInfo = memory.getMemoryInfo();
        return new OsMemoryStatusSnapshot(memoryInfo.getTotalPhysicalMemory(), memoryInfo.getAvailablePhysicalMemory());
    } catch (NativeException ex) {
        throw new UnsupportedOperationException("Unable to get system memory", ex);
    } catch (NativeIntegrationException ex) {
        throw new UnsupportedOperationException("Unable to get system memory", ex);
    }
}
Also used : MemoryInfo(net.rubygrapefruit.platform.memory.MemoryInfo) Memory(net.rubygrapefruit.platform.memory.Memory) NativeException(net.rubygrapefruit.platform.NativeException) NativeIntegrationException(org.gradle.internal.nativeintegration.NativeIntegrationException)

Example 4 with NativeException

use of net.rubygrapefruit.platform.NativeException in project gradle by gradle.

the class NonHierarchicalFileWatcherUpdater method updateWatchedDirectories.

private void updateWatchedDirectories(Map<String, Integer> changedWatchDirectories) {
    Set<File> directoriesToStopWatching = new HashSet<>();
    Set<File> directoriesToStartWatching = new HashSet<>();
    changedWatchDirectories.forEach((absolutePath, value) -> {
        int count = value;
        if (count < 0) {
            int toRemove = -count;
            int contained = watchedDirectories.remove(absolutePath, toRemove);
            if (contained <= toRemove) {
                directoriesToStopWatching.add(new File(absolutePath));
            }
        } else if (count > 0) {
            int contained = watchedDirectories.add(absolutePath, count);
            if (contained == 0) {
                directoriesToStartWatching.add(new File(absolutePath));
            }
        }
    });
    LOGGER.info("Watching {} directories to track changes", watchedDirectories.entrySet().size());
    try {
        if (!directoriesToStopWatching.isEmpty()) {
            if (!fileWatcher.stopWatching(directoriesToStopWatching)) {
                LOGGER.debug("Couldn't stop watching directories: {}", directoriesToStopWatching);
            }
        }
        if (!directoriesToStartWatching.isEmpty()) {
            fileWatcher.startWatching(directoriesToStartWatching);
        }
    } catch (NativeException e) {
        if (e.getMessage().contains("Already watching path: ")) {
            throw new WatchingNotSupportedException("Unable to watch same file twice via different paths: " + e.getMessage(), e);
        }
        throw e;
    }
}
Also used : WatchingNotSupportedException(org.gradle.internal.watch.WatchingNotSupportedException) File(java.io.File) NativeException(net.rubygrapefruit.platform.NativeException) HashSet(java.util.HashSet)

Example 5 with NativeException

use of net.rubygrapefruit.platform.NativeException in project gradle by gradle.

the class WatchingVirtualFileSystem method afterBuildStarted.

@Override
public boolean afterBuildStarted(WatchMode watchMode, VfsLogging vfsLogging, WatchLogging watchLogging, BuildOperationRunner buildOperationRunner) {
    warningLogger = watchMode.loggerForWarnings(LOGGER);
    stateInvalidatedAtStartOfBuild = false;
    reasonForNotWatchingFiles = null;
    rootReference.update(currentRoot -> buildOperationRunner.call(new CallableBuildOperation<SnapshotHierarchy>() {

        @Override
        public SnapshotHierarchy call(BuildOperationContext context) {
            if (watchMode.isEnabled()) {
                SnapshotHierarchy newRoot;
                boolean couldDetectUnsupportedFileSystems;
                try {
                    unsupportedFileSystems.clear();
                    if (watchMode == WatchMode.DEFAULT) {
                        watchableFileSystemDetector.detectUnsupportedFileSystems().forEach(unsupportedFileSystems::add);
                    }
                    couldDetectUnsupportedFileSystems = true;
                } catch (NativeException e) {
                    couldDetectUnsupportedFileSystems = false;
                    LOGGER.info("Unable to list file systems to check whether they can be watched. Disabling watching. Reason: {}", e.getMessage());
                }
                FileSystemWatchingStatistics statisticsSinceLastBuild;
                if (watchRegistry == null) {
                    if (couldDetectUnsupportedFileSystems) {
                        context.setStatus("Starting file system watching");
                        newRoot = startWatching(currentRoot, watchMode, unsupportedFileSystems);
                    } else {
                        newRoot = currentRoot.empty();
                    }
                    statisticsSinceLastBuild = null;
                } else {
                    FileWatcherRegistry.FileWatchingStatistics statistics = watchRegistry.getAndResetStatistics();
                    if (hasDroppedStateBecauseOfErrorsReceivedWhileWatching(statistics) || !couldDetectUnsupportedFileSystems) {
                        newRoot = stopWatchingAndInvalidateHierarchyAfterError(currentRoot);
                    } else {
                        newRoot = watchRegistry.updateVfsOnBuildStarted(currentRoot, watchMode, unsupportedFileSystems);
                    }
                    stateInvalidatedAtStartOfBuild = newRoot != currentRoot;
                    statisticsSinceLastBuild = new DefaultFileSystemWatchingStatistics(statistics, newRoot);
                    if (vfsLogging == VfsLogging.VERBOSE) {
                        LOGGER.warn("Received {} file system events since last build while watching {} locations", statisticsSinceLastBuild.getNumberOfReceivedEvents(), statisticsSinceLastBuild.getNumberOfWatchedHierarchies());
                        LOGGER.warn("Virtual file system retained information about {} files, {} directories and {} missing files since last build", statisticsSinceLastBuild.getRetainedRegularFiles(), statisticsSinceLastBuild.getRetainedDirectories(), statisticsSinceLastBuild.getRetainedMissingFiles());
                        if (stateInvalidatedAtStartOfBuild) {
                            LOGGER.warn("Parts of the virtual file system have been invalidated since they didn't support watching");
                        }
                    }
                }
                if (watchRegistry != null) {
                    watchRegistry.setDebugLoggingEnabled(watchLogging == WatchLogging.DEBUG);
                }
                context.setResult(new BuildStartedFileSystemWatchingBuildOperationType.Result() {

                    @Override
                    public boolean isWatchingEnabled() {
                        return true;
                    }

                    @Override
                    public boolean isStartedWatching() {
                        return statisticsSinceLastBuild == null;
                    }

                    @Override
                    public FileSystemWatchingStatistics getStatistics() {
                        return statisticsSinceLastBuild;
                    }
                });
                return newRoot;
            } else {
                context.setResult(BuildStartedFileSystemWatchingBuildOperationType.Result.WATCHING_DISABLED);
                return stopWatchingAndInvalidateHierarchy(currentRoot);
            }
        }

        @Override
        public BuildOperationDescriptor.Builder description() {
            return BuildOperationDescriptor.displayName(BuildStartedFileSystemWatchingBuildOperationType.DISPLAY_NAME).details(BuildStartedFileSystemWatchingBuildOperationType.Details.INSTANCE);
        }
    }));
    return watchRegistry != null;
}
Also used : BuildOperationContext(org.gradle.internal.operations.BuildOperationContext) FileSystemWatchingStatistics(org.gradle.internal.watch.vfs.FileSystemWatchingStatistics) BuildOperationDescriptor(org.gradle.internal.operations.BuildOperationDescriptor) BuildStartedFileSystemWatchingBuildOperationType(org.gradle.internal.watch.vfs.BuildStartedFileSystemWatchingBuildOperationType) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) CallableBuildOperation(org.gradle.internal.operations.CallableBuildOperation) FileWatcherRegistry(org.gradle.internal.watch.registry.FileWatcherRegistry) NativeException(net.rubygrapefruit.platform.NativeException)

Aggregations

NativeException (net.rubygrapefruit.platform.NativeException)5 File (java.io.File)2 HashSet (java.util.HashSet)1 NativeIntegrationUnavailableException (net.rubygrapefruit.platform.NativeIntegrationUnavailableException)1 FileInfo (net.rubygrapefruit.platform.file.FileInfo)1 Memory (net.rubygrapefruit.platform.memory.Memory)1 MemoryInfo (net.rubygrapefruit.platform.memory.MemoryInfo)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 AccessType (org.gradle.internal.file.FileMetadata.AccessType)1 NativeIntegrationException (org.gradle.internal.nativeintegration.NativeIntegrationException)1 BuildOperationContext (org.gradle.internal.operations.BuildOperationContext)1 BuildOperationDescriptor (org.gradle.internal.operations.BuildOperationDescriptor)1 CallableBuildOperation (org.gradle.internal.operations.CallableBuildOperation)1 SnapshotHierarchy (org.gradle.internal.snapshot.SnapshotHierarchy)1 WatchingNotSupportedException (org.gradle.internal.watch.WatchingNotSupportedException)1 FileWatcherRegistry (org.gradle.internal.watch.registry.FileWatcherRegistry)1 BuildStartedFileSystemWatchingBuildOperationType (org.gradle.internal.watch.vfs.BuildStartedFileSystemWatchingBuildOperationType)1 FileSystemWatchingStatistics (org.gradle.internal.watch.vfs.FileSystemWatchingStatistics)1