-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* multiple variable completion Signed-off-by: Arun Venmany <[email protected]> * multiple variable diagnostic Signed-off-by: Arun Venmany <[email protected]> * file watcher for config files alteration for variable re processing Signed-off-by: Arun Venmany <[email protected]> * changes for review comments Signed-off-by: Arun Venmany <[email protected]> * changes for review comments Signed-off-by: Arun Venmany <[email protected]> * correcting file watch path Signed-off-by: Arun Venmany <[email protected]> * correcting ci commons snapshot version Signed-off-by: Arun Venmany <[email protected]> * correcting ci commons snapshot version Signed-off-by: Arun Venmany <[email protected]> * adding file filters Signed-off-by: Arun Venmany <[email protected]> * adding more file filters and changing completion logic for multiple vars Signed-off-by: Arun Venmany <[email protected]> * adding more file filters and changing completion logic for multiple vars Signed-off-by: Arun Venmany <[email protected]> * updating one test based on review comments Signed-off-by: Arun Venmany <[email protected]> --------- Signed-off-by: Arun Venmany <[email protected]>
- Loading branch information
1 parent
2dc07a5
commit 07eca75
Showing
11 changed files
with
442 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...erty/src/main/java/io/openliberty/tools/langserver/lemminx/services/FileWatchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 IBM Corporation and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package io.openliberty.tools.langserver.lemminx.services; | ||
|
||
import io.openliberty.tools.langserver.lemminx.util.LibertyConstants; | ||
import io.openliberty.tools.langserver.lemminx.util.LibertyUtils; | ||
import org.apache.commons.io.IOCase; | ||
import org.apache.commons.io.filefilter.FileFilterUtils; | ||
import org.apache.commons.io.filefilter.IOFileFilter; | ||
import org.apache.commons.io.filefilter.NameFileFilter; | ||
import org.apache.commons.io.filefilter.SuffixFileFilter; | ||
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; | ||
import org.apache.commons.io.monitor.FileAlterationMonitor; | ||
import org.apache.commons.io.monitor.FileAlterationObserver; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
public class FileWatchService { | ||
|
||
private final Set<FileAlterationObserver> fileObservers = new HashSet<>(); | ||
private final Set<FileAlterationMonitor> monitors = new HashSet<>(); | ||
|
||
private static final FileWatchService instance = new FileWatchService(); | ||
|
||
public static FileWatchService getInstance() { | ||
return instance; | ||
} | ||
|
||
private static final Logger LOGGER = Logger.getLogger(FileWatchService.class.getName()); | ||
|
||
private FileWatchService() { | ||
} | ||
|
||
/** | ||
* add observer for file changes | ||
* | ||
* @param workspace workspace | ||
* @param watchLocations parent locations | ||
*/ | ||
public void addFileAlterationObserver(LibertyWorkspace workspace, List<String> watchLocations) | ||
throws Exception { | ||
for (String location:watchLocations) { | ||
FileAlterationObserver observer = getFileAlterationObserver(location, workspace); | ||
observer.initialize(); | ||
fileObservers.add(observer); | ||
FileAlterationMonitor monitor = new FileAlterationMonitor(); | ||
monitor.addObserver(observer); | ||
monitor.start(); | ||
monitors.add(monitor); | ||
} | ||
} | ||
|
||
private FileAlterationObserver getFileAlterationObserver(final String parentPath, LibertyWorkspace workspace) { | ||
//ignore files/directories with below suffixes and names | ||
IOFileFilter notFileFilter = FileFilterUtils.notFileFilter( | ||
new SuffixFileFilter(Arrays.asList(".class", ".lst", ".txt", ".log", ".manager", ".libertyls", | ||
".sLock", ".jar", ".war", ".ear",".mf"), | ||
IOCase.INSENSITIVE) | ||
.or(new NameFileFilter(Arrays.asList("plugin-cfg.xml", "libs", "tmp", "classes", | ||
"generated-sources", "generated-test-sources", "invoker-reports", | ||
"it", "maven-status", "surefire-reports", "test-classes"), IOCase.INSENSITIVE))); | ||
FileAlterationObserver observer = new FileAlterationObserver(parentPath, notFileFilter); | ||
addFileAlterationListener(observer, workspace); | ||
return observer; | ||
} | ||
|
||
private void addFileAlterationListener(FileAlterationObserver observer, LibertyWorkspace workspace) { | ||
observer.addListener(new FileAlterationListenerAdaptor() { | ||
@Override | ||
public void onDirectoryCreate(File file) { | ||
// not required | ||
} | ||
|
||
@Override | ||
public void onDirectoryDelete(File file) { | ||
// not required | ||
} | ||
|
||
@Override | ||
public void onDirectoryChange(File file) { | ||
// not required | ||
} | ||
|
||
@Override | ||
public void onFileCreate(File file) { | ||
onAlteration(file, workspace); | ||
} | ||
|
||
@Override | ||
public void onFileDelete(File file) { | ||
onAlteration(file, workspace); | ||
} | ||
|
||
@Override | ||
public void onFileChange(File file) { | ||
onAlteration(file, workspace); | ||
} | ||
|
||
/** | ||
* update variables on file alteration, if modified file is a config | ||
* | ||
* @param file changed file | ||
* @param workspace current workspace | ||
*/ | ||
private void onAlteration(File file, LibertyWorkspace workspace) { | ||
boolean watchedFileChanged = LibertyConstants.filesToWatch.stream().anyMatch(fileName -> file.getName().contains(fileName)); | ||
boolean isConfigXmlFile = false; | ||
try { | ||
isConfigXmlFile = LibertyUtils.isConfigXMLFile(file.getCanonicalPath()); | ||
} catch (IOException e) { | ||
LOGGER.warning("Liberty XML variables cannot be updated for file path %s with error %s" | ||
.formatted(file.getPath(), e.getMessage())); | ||
} | ||
if (watchedFileChanged || isConfigXmlFile) { | ||
SettingsService.getInstance().populateVariablesForWorkspace(workspace); | ||
LOGGER.info("Liberty XML variables updated for workspace URI " + workspace.getWorkspaceString()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* clean all monitors for all workspaces | ||
*/ | ||
public void cleanFileMonitors() { | ||
fileObservers.clear(); | ||
try { | ||
for (FileAlterationMonitor monitor : monitors) { | ||
monitor.stop(); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.warning("Issue while removing file monitors with error %s" | ||
.formatted(e.getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.