Skip to content

Commit

Permalink
Added option to clear the cache and use it by default (#2340)
Browse files Browse the repository at this point in the history
This is an attempt to fix problems where users try to start UGS after upgrading and the application dies silently due to the cache being corrupted. This often happens on Windows machines. It will now clear the cache on startup
  • Loading branch information
breiler authored Oct 15, 2023
1 parent 8cb7416 commit c41a526
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/UGS_Platform.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void beginStreamingShouldSendEvents() {
}

@Test
public void streamCompleteShouldBeExecutedWhenStreamIsFinished() throws IOException {
public void streamCompleteShouldBeExecutedWhenStreamIsFinished() throws IOException, InterruptedException {
ControllerListener listener = mock(ControllerListener.class);
InOrder inOrder = inOrder(listener);
target.addListener(listener);
Expand All @@ -138,6 +138,8 @@ public void streamCompleteShouldBeExecutedWhenStreamIsFinished() throws IOExcept
nextCommand.appendResponse("ok");
target.rawResponseListener("ok");

Thread.sleep(100);

inOrder.verify(listener, times(1)).statusStringListener(any());
inOrder.verify(listener, times(1)).streamStarted();
inOrder.verify(listener, times(1)).commandSent(any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_cachedir="${DEFAULT_CACHEDIR_ROOT}/var/cache"

# options used by the launcher by default, can be overridden by explicit
# command line switches
default_options="--branding ${branding.token} -J-Xms64m -J-Xverify:none -J-Dsun.java2d.noddraw=true -J-Dsun.awt.noerasebackground=true -J-Dpolyglot.engine.WarnInterpreterOnly=false -J-Dnetbeans.indexing.noFileRefresh=true -J-Dorg.openide.text.big.file.size=20 -J--add-opens=java.base/java.net=ALL-UNNAMED -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED -J--add-opens=java.base/java.lang=ALL-UNNAMED -J--add-opens=java.base/java.security=ALL-UNNAMED -J--add-opens=java.base/java.util=ALL-UNNAMED -J--add-opens=java.base/java.nio=ALL-UNNAMED -J--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.text=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED -J--add-opens=java.desktop/java.awt=ALL-UNNAMED -J--add-opens=java.desktop/java.awt.event=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.X11=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.synth=ALL-UNNAMED -J--add-opens=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.shell=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=java.base/sun.reflect.annotation=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED -J--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.beans.editors=ALL-UNNAMED -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED -J-XX:+IgnoreUnrecognizedVMOptions"
default_options="--branding ${branding.token} --clearcache -J-Xms64m -J-Xverify:none -J-Dsun.java2d.noddraw=true -J-Dsun.awt.noerasebackground=true -J-Dpolyglot.engine.WarnInterpreterOnly=false -J-Dnetbeans.indexing.noFileRefresh=true -J-Dorg.openide.text.big.file.size=20 -J--add-opens=java.base/java.net=ALL-UNNAMED -J--add-opens=java.base/java.lang.ref=ALL-UNNAMED -J--add-opens=java.base/java.lang=ALL-UNNAMED -J--add-opens=java.base/java.security=ALL-UNNAMED -J--add-opens=java.base/java.util=ALL-UNNAMED -J--add-opens=java.base/java.nio=ALL-UNNAMED -J--add-opens=java.prefs/java.util.prefs=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.basic=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.text=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing=ALL-UNNAMED -J--add-opens=java.desktop/java.awt=ALL-UNNAMED -J--add-opens=java.desktop/java.awt.event=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.X11=ALL-UNNAMED -J--add-opens=java.desktop/javax.swing.plaf.synth=ALL-UNNAMED -J--add-opens=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.shell=ALL-UNNAMED -J--add-opens=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=java.base/sun.reflect.annotation=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt=ALL-UNNAMED -J--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.beans.editors=ALL-UNNAMED -J--add-exports=java.desktop/sun.swing=ALL-UNNAMED -J--add-exports=java.desktop/sun.awt.im=ALL-UNNAMED -J--add-exports=java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED -J-XX:+IgnoreUnrecognizedVMOptions"
# for development purposes you may wish to append: -J-Dnetbeans.logger.console=true -J-ea

# default location of JDK/JRE, can be overridden by using --jdkhome <dir> switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,18 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.Utils;
import com.willwinder.universalgcodesender.utils.Settings;
import com.willwinder.universalgcodesender.utils.Version;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.Option;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.cookies.OpenCookie;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.modules.OnStart;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
import org.openide.windows.WindowManager;

import java.io.File;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author wwinder
*/
@ServiceProvider(service = OptionProcessor.class)
@OnStart
public class startup extends OptionProcessor implements Runnable {
private static final Logger logger = Logger.getLogger(startup.class.getName());

private final Option openOption = Option.additionalArguments('o', "open");
private final Option defaultOpenOption = Option.defaultArguments();
public class ServiceStarter implements Runnable {
private static final Logger logger = Logger.getLogger(ServiceStarter.class.getName());

@Override
public void run() {
Expand Down Expand Up @@ -90,52 +71,6 @@ public void run() {

private void setupVersionInformation(Settings settings) {
// Only change the window title when all the UI components are fully loaded.
WindowManager.getDefault().invokeWhenUIReady(() -> {
Utils.checkNightlyBuild(settings);
});
}

/**
* Register interest in the "open" option.
*/
@Override
public Set<Option> getOptions() {
HashSet<Option> set = new HashSet<>();
set.add(openOption);
set.add(defaultOpenOption);
return set;
}

/**
* CLI Handler.
*/
@Override
protected void process(Env env, Map<Option, String[]> optionsMap) throws CommandException {
Optional<String> fileToOpen = getFileToOpen(optionsMap, openOption);
if (!fileToOpen.isPresent()) {
fileToOpen = getFileToOpen(optionsMap, defaultOpenOption);
}

if (fileToOpen.isPresent()) {
String inputFile = fileToOpen.get();
logger.info("File to open: " + inputFile);
try {
DataObject.find(FileUtil.toFileObject(new File(inputFile)))
.getLookup()
.lookup(OpenCookie.class)
.open();
} catch (DataObjectNotFoundException e) {
logger.log(Level.SEVERE, "Could not open file " + inputFile, e);
}
}
}

private Optional<String> getFileToOpen(Map<Option, String[]> optionsMap, Option option) {
String[] files = optionsMap.getOrDefault(option, new String[0]);

if (files.length > 0) {
return Optional.of(files[0]);
}
return Optional.empty();
WindowManager.getDefault().invokeWhenUIReady(() -> Utils.checkNightlyBuild(settings));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This file is part of Universal Gcode Sender (UGS).
* @author wwinder
*/
@OnStop
public class shutdown implements Runnable {
public class ServiceStopper implements Runnable {
@Override
public void run() {
// Save settings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.willwinder.ugs.nbp.core.lifecycle;

import org.apache.commons.io.FileUtils;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.Option;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.cookies.OpenCookie;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.modules.Places;
import org.openide.util.lookup.ServiceProvider;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@ServiceProvider(service = OptionProcessor.class)
public class StartupOptionProcessor extends OptionProcessor {
private static final Logger logger = Logger.getLogger(StartupOptionProcessor.class.getName());

private final Option openOption = Option.additionalArguments('o', "open");
private final Option defaultOpenOption = Option.defaultArguments();
private final Option clearCacheOption = Option.withoutArgument('c', "clearcache");

/**
* Register interest in the "open" option.
*/
@Override
public Set<Option> getOptions() {
HashSet<Option> set = new HashSet<>();
set.add(openOption);
set.add(defaultOpenOption);
set.add(clearCacheOption);
return set;
}

/**
* CLI Handler.
*/
@Override
protected void process(Env env, Map<Option, String[]> optionsMap) throws CommandException {
if (optionsMap.containsKey(clearCacheOption)) {
clearCache();
}

Optional<String> fileToOpen = getFileToOpen(optionsMap, openOption);
if (!fileToOpen.isPresent()) {
fileToOpen = getFileToOpen(optionsMap, defaultOpenOption);
}

if (fileToOpen.isPresent()) {
String inputFile = fileToOpen.get();
logger.info("File to open: " + inputFile);
try {
DataObject.find(FileUtil.toFileObject(new File(inputFile)))
.getLookup()
.lookup(OpenCookie.class)
.open();
} catch (DataObjectNotFoundException e) {
logger.log(Level.SEVERE, "Could not open file " + inputFile, e);
}
}
}

private void clearCache() {
try {
Arrays.stream(Objects.requireNonNull(Places.getCacheDirectory().listFiles(f -> f.getName().endsWith(".dat"))))
.forEach(FileUtils::deleteQuietly);

FileUtils.deleteQuietly(new File(Places.getCacheDirectory().getAbsolutePath() + File.separator + ".lastUsedVersion"));
FileUtils.deleteQuietly(new File(Places.getCacheDirectory().getAbsolutePath() + File.separator + "localeVariants"));
FileUtils.deleteDirectory(new File(Places.getCacheDirectory().getAbsolutePath() + File.separator + "lastModified"));
} catch (IOException e) {
logger.log(Level.WARNING, "An error occurred while clearing cache files", e);
}
}

private Optional<String> getFileToOpen(Map<Option, String[]> optionsMap, Option option) {
String[] files = optionsMap.getOrDefault(option, new String[0]);

if (files.length > 0) {
return Optional.of(files[0]);
}
return Optional.empty();
}
}

0 comments on commit c41a526

Please sign in to comment.