use of com.laytonsmith.persistence.DataSourceFilter in project CommandHelper by EngineHub.
the class Manager method hiddenKeys.
public static void hiddenKeys() {
String action;
while (true) {
pl(WHITE + "Would you like to \"" + GREEN + "view" + WHITE + "\" or \"" + RED + "delete" + WHITE + "\" the hidden keys (default: view)? [view/delete]");
action = prompt();
if ("".equals(action)) {
action = "view";
}
if ("view".equals(action) || "delete".equals(action)) {
break;
} else {
pl(RED + "Invalid selection.");
}
}
File configuration = MethodScriptFileLocations.getDefault().getPersistenceConfig();
while (true) {
pl("Currently, " + configuration.getAbsolutePath() + " is being used as the persistence config file, but you may" + " specify another (blank to use the default).");
String file = prompt();
if ("".equals(file)) {
break;
} else {
File f = new File(file);
if (f.exists()) {
configuration = f;
break;
} else {
pl(RED + "The file you specified doesn't seem to exist, please enter it again.");
}
}
}
pl(YELLOW + "Using " + configuration.getAbsolutePath() + " as our Persistence Network config.");
File workingDirectory = MethodScriptFileLocations.getDefault().getConfigDirectory();
while (true) {
pl("Currently, " + workingDirectory.getAbsolutePath() + " is being used as the default \"working directory\" for the" + " persistence config file, but you may specify another (blank to use the default).");
String file = prompt();
if ("".equals(file)) {
break;
} else {
File f = new File(file);
if (f.exists()) {
workingDirectory = f;
break;
} else {
pl(RED + "The file you specified doesn't seem to exist, please enter it again.");
}
}
}
pl(YELLOW + "Using " + workingDirectory.getAbsolutePath() + " as our Persistence Network config working directory.");
try {
DataSourceFilter filter = new DataSourceFilter(configuration, new URI("sqlite://persistence.db"));
ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
options.setWorkingDirectory(workingDirectory);
Set<URI> uris = filter.getAllConnections();
boolean noneFound = true;
int runningTotal = 0;
for (URI uri : uris) {
DataSource ds = DataSourceFactory.GetDataSource(uri, options);
Map<String[], String> db = ds.getValues(ArrayUtils.EMPTY_STRING_ARRAY);
Map<String[], String> map = new HashMap<>();
DaemonManager dm = new DaemonManager();
try {
for (String[] key : db.keySet()) {
if (!filter.getConnection(key).equals(uri)) {
map.put(key, db.get(key));
if ("delete".equals(action)) {
ds.clearKey(dm, key);
}
}
}
runningTotal += map.size();
} catch (ReadOnlyException ex) {
pl(RED + "Cannot delete any keys from " + uri + " as it is marked as read only, so it is being skipped.");
}
if ("delete".equals(action)) {
try {
dm.waitForThreads();
} catch (InterruptedException ex) {
// Ignored
}
}
if (!map.isEmpty()) {
noneFound = false;
if ("view".equals(action)) {
pl("Found " + StringUtils.PluralTemplateHelper(map.size(), "one hidden key", "%d hidden keys") + " in data source " + MAGENTA + uri.toString());
for (String[] key : map.keySet()) {
pl("\t" + GREEN + StringUtils.Join(key, ".") + WHITE + ":" + CYAN + map.get(key));
}
if (ds.hasModifier(DataSource.DataSourceModifier.READONLY)) {
pl(YELLOW + "This data source is marked as read only, and the keys cannot be deleted from it by this utility.");
}
pl();
}
}
}
if (noneFound) {
pl(GREEN + "Done searching, no hidden keys were found.");
} else {
if ("delete".equals(action)) {
pl(GREEN + "Done, " + StringUtils.PluralTemplateHelper(runningTotal, "one hidden key was", "%d hidden keys were") + " deleted.");
} else {
pl(GREEN + "Found " + StringUtils.PluralTemplateHelper(runningTotal, "one hidden key", "%d hidden keys") + " in total.");
}
}
} catch (URISyntaxException | IOException | DataSourceException ex) {
pl(RED + ex.getMessage());
ex.printStackTrace(StreamUtils.GetSystemErr());
}
}
Aggregations