Search in sources :

Example 11 with FileDetails

use of org.springframework.roo.file.monitor.event.FileDetails in project spring-roo by spring-projects.

the class PollingFileMonitorService method recursiveAntMatch.

/**
 * Locates all files under the specified current directory which patch the
 * given Ant Path.
 *
 * @param antPath to match (required)
 * @param currentDirectory an existing directory to search from (required)
 * @param result to append located files into (required)
 */
private void recursiveAntMatch(final String antPath, final File currentDirectory, final SortedSet<FileDetails> result) {
    Validate.notNull(currentDirectory, "Current directory required");
    Validate.isTrue(currentDirectory.exists() && currentDirectory.isDirectory(), "Path '%s' does not exist or is not a directory", currentDirectory);
    Validate.notBlank(antPath, "Ant path required");
    Validate.notNull(result, "Result required");
    final File[] listFiles = currentDirectory.listFiles();
    if (listFiles == null || listFiles.length == 0) {
        return;
    }
    for (final File f : listFiles) {
        try {
            if (FileUtils.matchesAntPath(antPath, f.getCanonicalPath())) {
                result.add(new FileDetails(f, f.lastModified()));
            }
        } catch (final IOException ignored) {
        }
        if (f.isDirectory()) {
            recursiveAntMatch(antPath, f, result);
        }
    }
}
Also used : FileDetails(org.springframework.roo.file.monitor.event.FileDetails) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 12 with FileDetails

use of org.springframework.roo.file.monitor.event.FileDetails in project spring-roo by spring-projects.

the class PollingFileMonitorService method scanAll.

public int scanAll() {
    synchronized (lock) {
        if (requests.isEmpty()) {
            return 0;
        }
        int changes = 0;
        for (final MonitoringRequest request : requests) {
            boolean includeSubtree = false;
            if (request instanceof DirectoryMonitoringRequest) {
                includeSubtree = ((DirectoryMonitoringRequest) request).isWatchSubtree();
            }
            if (!request.getFile().exists()) {
                continue;
            }
            // Build contents of the monitored location
            final Map<File, Long> currentExecution = new HashMap<File, Long>();
            computeEntries(currentExecution, request.getFile(), includeSubtree);
            final List<FileEvent> eventsToPublish = new ArrayList<FileEvent>();
            if (priorExecution.containsKey(request)) {
                // Need to perform a comparison, as we have data from a
                // previous execution
                final Map<File, Long> priorFiles = priorExecution.get(request);
                // Locate created and modified files
                for (final Entry<File, Long> entry : currentExecution.entrySet()) {
                    final File thisFile = entry.getKey();
                    final Long currentTimestamp = entry.getValue();
                    if (!priorFiles.containsKey(thisFile)) {
                        // This file did not exist last execution, so it
                        // must be new
                        eventsToPublish.add(new FileEvent(new FileDetails(thisFile, currentTimestamp), FileOperation.CREATED, null));
                        try {
                            // If this file was already going to be
                            // notified, there is no need to do it twice
                            notifyCreated.remove(thisFile.getCanonicalPath());
                        } catch (final IOException ignored) {
                        }
                        continue;
                    }
                    final Long previousTimestamp = priorFiles.get(thisFile);
                    if (!currentTimestamp.equals(previousTimestamp)) {
                        // Modified
                        eventsToPublish.add(new FileEvent(new FileDetails(thisFile, currentTimestamp), FileOperation.UPDATED, null));
                        try {
                            // If this file was already going to be
                            // notified, there is no need to do it twice
                            notifyChanged.remove(thisFile.getCanonicalPath());
                        } catch (final IOException ignored) {
                        }
                    }
                }
                // Now locate deleted files
                priorFiles.keySet().removeAll(currentExecution.keySet());
                for (final Entry<File, Long> entry : priorFiles.entrySet()) {
                    final File deletedFile = entry.getKey();
                    eventsToPublish.add(new FileEvent(new FileDetails(deletedFile, entry.getValue()), FileOperation.DELETED, null));
                    try {
                        // If this file was already going to be notified,
                        // there is no need to do it twice
                        notifyDeleted.remove(deletedFile.getCanonicalPath());
                    } catch (final IOException ignored) {
                    }
                }
            } else {
                // newly-monitored location
                for (final Entry<File, Long> entry : currentExecution.entrySet()) {
                    eventsToPublish.add(new FileEvent(new FileDetails(entry.getKey(), entry.getValue()), FileOperation.MONITORING_START, null));
                }
            }
            // Record the monitored location's contents, ready for next
            // execution
            priorExecution.put(request, currentExecution);
            // We can discard the created and deleted notifications, as they
            // would have been correctly discovered in the above loop
            notifyCreated.clear();
            notifyDeleted.clear();
            // this indicates an identical millisecond update occurred
            for (final String canonicalPath : notifyChanged) {
                final File file = new File(canonicalPath);
                eventsToPublish.add(new FileEvent(new FileDetails(file, file.lastModified()), FileOperation.UPDATED, null));
            }
            notifyChanged.clear();
            // ROO-3622: Validate if version change
            if (!isDifferentVersion()) {
                // Publishing pending events if needed
                if (!eventsPendingToPublish.isEmpty()) {
                    publish(eventsPendingToPublish);
                    // Clear events pending to publish
                    eventsPendingToPublish.clear();
                }
                publish(eventsToPublish);
            } else {
                for (FileEvent event : eventsToPublish) {
                    if (eventsPendingToPublish.indexOf(event) == -1) {
                        eventsPendingToPublish.add(event);
                    }
                }
            }
            changes += eventsToPublish.size();
        }
        return changes;
    }
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) FileDetails(org.springframework.roo.file.monitor.event.FileDetails) IOException(java.io.IOException) FileEvent(org.springframework.roo.file.monitor.event.FileEvent) DirectoryMonitoringRequest(org.springframework.roo.file.monitor.DirectoryMonitoringRequest) MonitoringRequest(org.springframework.roo.file.monitor.MonitoringRequest) DirectoryMonitoringRequest(org.springframework.roo.file.monitor.DirectoryMonitoringRequest) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

FileDetails (org.springframework.roo.file.monitor.event.FileDetails)12 File (java.io.File)9 JarFile (java.util.jar.JarFile)7 ArrayList (java.util.ArrayList)6 FileEvent (org.springframework.roo.file.monitor.event.FileEvent)5 IOException (java.io.IOException)3 CreateFile (org.springframework.roo.file.undo.CreateFile)2 DeleteFile (org.springframework.roo.file.undo.DeleteFile)2 UpdateFile (org.springframework.roo.file.undo.UpdateFile)2 MutableFile (org.springframework.roo.process.manager.MutableFile)2 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 WeakHashMap (java.util.WeakHashMap)1 DirectoryMonitoringRequest (org.springframework.roo.file.monitor.DirectoryMonitoringRequest)1 MonitoringRequest (org.springframework.roo.file.monitor.MonitoringRequest)1 CreateDirectory (org.springframework.roo.file.undo.CreateDirectory)1 Pom (org.springframework.roo.project.maven.Pom)1