use of com.laytonsmith.persistence.DataSourceException in project CommandHelper by EngineHub.
the class FederationMasterSocket method ensureMasterSocketOpen.
/**
* Ensures the master socket is open for the given port. If it is already up and running, nothing happens.
*
* @param pn The persistence network, for finding the registration of a particular server.
* @param port The port the master socket should be listening on.
* @throws java.io.IOException
*/
public void ensureMasterSocketOpen(final PersistenceNetwork pn, int port) throws IOException {
if (!servers.containsKey(port) || servers.get(port).isClosed()) {
if (Federation.available(port)) {
// We're the first server to register on this port, so
// we need to actually start it up.
final ServerSocket masterSocket = new ServerSocket(port);
servers.put(port, masterSocket);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
final Socket s = masterSocket.accept();
Thread connectionWatcher = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
// too long. Forcibly terminate it.
try {
if (s.isConnected()) {
s.close();
}
} catch (IOException ex) {
Logger.getLogger(FederationServer.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (InterruptedException ex) {
// Ok, it's good.
}
}
}, "FederationMasterSocketConnectionWatcher-" + s.hashCode());
connectionWatcher.start();
FederationCommunication communicator = new FederationCommunication(new BufferedInputStream(s.getInputStream()), new BufferedOutputStream(s.getOutputStream()));
String hello = communicator.readUnencryptedLine();
if (!"HELLO".equals(hello)) {
// Bad message. Close the socket immediately, and return.
s.close();
connectionWatcher.interrupt();
return;
}
// Ohhai
// Now get the version...
FederationVersion version;
String sVersion = communicator.readUnencryptedLine();
try {
version = FederationVersion.fromVersion(sVersion);
communicator.writeUnencryptedLine("VERSION OK");
} catch (IllegalArgumentException ex) {
// The version is unsupported. The client is newer than this server knows how
// to deal with. So, write out the version error data, then close the socket and
// continue.
communicator.writeUnencryptedLine("VERSION BAD");
byte[] errorMsg = ("The server does not support the version of this client (" + sVersion + ")!").getBytes("UTF-8");
communicator.writeUnencryptedLine(Integer.toString(errorMsg.length));
communicator.writeUnencrypted(errorMsg);
s.close();
connectionWatcher.interrupt();
return;
}
// The rest of the code may vary based on the version.
if (version == FederationVersion.V1_0_0) {
String command = communicator.readUnencryptedLine();
if ("GET PORT".equals(command)) {
String serverName = communicator.readUnencryptedLine();
String value = pn.get(new String[] { "federation", serverName });
if (value != null) {
FederationRegistration reg = FederationRegistration.fromJSON(value);
if (reg.updatedSince(Federation.DEAD_SERVER_TIMEOUT)) {
int port = reg.getPort();
communicator.writeLine("OK");
communicator.writeLine(Integer.toString(port));
s.close();
connectionWatcher.interrupt();
return;
}
}
byte[] errorMsg = ("The server \"" + serverName + "\" could not be found on this host.").getBytes("UTF-8");
communicator.writeUnencryptedLine("ERROR");
communicator.writeUnencryptedLine(Integer.toString(errorMsg.length));
communicator.writeUnencrypted(errorMsg);
s.close();
connectionWatcher.interrupt();
} else {
// Programming error.
s.close();
connectionWatcher.interrupt();
}
}
} catch (IOException | DataSourceException ex) {
Logger.getLogger(FederationMasterSocket.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}, "FederationMasterSocket-Port " + port).start();
}
}
}
use of com.laytonsmith.persistence.DataSourceException in project CommandHelper by EngineHub.
the class SiteDeploy method deploy.
@SuppressWarnings("StringEquality")
private void deploy() throws InterruptedException, IOException {
apiJson = JSONValue.toJSONString(new APIBuilder().build());
apiJsonVersion = getLocalMD5(StreamUtils.GetInputStream(apiJson));
deployResources();
deployFrontPages();
deployLearningTrail();
deployAPI();
deployEventAPI();
deployFunctions();
deployEvents();
deployObjects();
deployAPIJSON();
Runnable generateFinalizer = new Runnable() {
@Override
public void run() {
// Just us left, shut us down
if (generateQueue.getQueue().isEmpty()) {
generateQueue.shutdown();
} else {
// Oops, we're a bit premature. Schedule us to run again.
generateQueue.submit(this);
}
}
};
generateQueue.submit(generateFinalizer);
generateQueue.awaitTermination(1, TimeUnit.DAYS);
Runnable uploadFinalizer = new Runnable() {
@Override
public void run() {
if (uploadQueue.getQueue().isEmpty()) {
uploadQueue.shutdown();
} else {
uploadQueue.submit(this);
}
}
};
uploadQueue.submit(uploadFinalizer);
uploadQueue.awaitTermination(1, TimeUnit.DAYS);
dm.waitForThreads();
deploymentMethod.finish();
// Next, we need to validate the pages
System.out.println();
if (doValidation) {
System.out.println("Upload complete, running html5 validation");
int filesValidated = 0;
int specifiedErrors = 0;
try {
for (Map.Entry<String, String> e : uploadedPages.entrySet()) {
Map<String, List<String>> headers = new HashMap<>();
RequestSettings settings = new RequestSettings();
// settings.setLogger(Logger.getLogger(SiteDeploy.class.getName()));
settings.setFollowRedirects(true);
headers.put("Content-Type", Arrays.asList(new String[] { "text/html; charset=utf-8" }));
headers.put("Content-Encoding", Arrays.asList(new String[] { "gzip" }));
headers.put("Accept-Encoding", Arrays.asList(new String[] { "gzip" }));
settings.setHeaders(headers);
byte[] outStream = e.getValue().getBytes("UTF-8");
ByteArrayOutputStream out = new ByteArrayOutputStream(outStream.length);
try (GZIPOutputStream gz = new GZIPOutputStream(out)) {
gz.write(outStream);
}
byte[] param = out.toByteArray();
settings.setRawParameter(param);
settings.setTimeout(10000);
settings.setMethod(HTTPMethod.POST);
HTTPResponse response = WebUtility.GetPage(new URL(validatorUrl + "?out=gnu"), settings);
if (response.getResponseCode() != 200) {
System.out.println(Static.MCToANSIColors("Response for " + MCChatColor.AQUA + e.getKey() + MCChatColor.PLAIN_WHITE + ":"));
System.out.println(response.getContent());
throw new IOException("Response was non-200, refusing to continue with validation");
}
String[] errors = response.getContent().split("\n");
int errorsDisplayed = 0;
for (String error : errors) {
GNUErrorMessageFormat gnuError = new GNUErrorMessageFormat(error);
String supressWarning = "info warning: Section lacks heading. Consider using “h2”-“h6”" + " elements to add identifying headings to all sections.";
if (supressWarning.equals(gnuError.message())) {
continue;
}
// == on String, yes this is what I want
if (error == errors[0]) {
System.out.println(Static.MCToANSIColors("Response for " + MCChatColor.AQUA + e.getKey() + MCChatColor.PLAIN_WHITE + ":"));
}
StringBuilder output = new StringBuilder();
switch(gnuError.messageType()) {
case ERROR:
output.append(MCChatColor.RED);
break;
case WARNING:
output.append(MCChatColor.GOLD);
break;
}
output.append("line ").append(gnuError.fromLine()).append(" ").append(gnuError.message()).append(MCChatColor.PLAIN_WHITE);
String[] page = e.getValue().split("\n");
for (int i = gnuError.fromLine(); i < gnuError.toLine() + 1; i++) {
output.append("\n").append(page[i - 1]);
}
output.append("\n");
for (int i = 0; i < gnuError.fromColumn() - 1; i++) {
output.append(" ");
}
output.append(MCChatColor.RED).append("^").append(MCChatColor.PLAIN_WHITE);
System.out.println(Static.MCToANSIColors(output.toString()));
specifiedErrors++;
errorsDisplayed++;
}
filesValidated++;
}
} catch (IOException ex) {
System.err.println("Validation could not occur due to the following exception: " + ex.getMessage());
ex.printStackTrace(System.err);
}
System.out.println("Files validated: " + filesValidated);
System.out.println("Errors found: " + specifiedErrors);
}
if (finalizerScript != null) {
System.out.println("Running post-script");
if (finalizerScript.getPath().endsWith(".ms")) {
try {
Interpreter.startWithTTY(finalizerScript, filesChanged, false);
} catch (DataSourceException | URISyntaxException | Profiles.InvalidProfileException ex) {
ex.printStackTrace(System.err);
}
} else {
List<String> args = new ArrayList<>();
args.add(finalizerScript.getCanonicalPath());
args.addAll(filesChanged);
CommandExecutor exec = new CommandExecutor(args.toArray(new String[args.size()]));
exec.setSystemInputsAndOutputs();
exec.start();
exec.waitFor();
}
}
System.out.println("Done!");
System.out.println("Summary of changed files (" + filesChanged.size() + ")");
System.out.println(StringUtils.Join(filesChanged, "\n"));
}
use of com.laytonsmith.persistence.DataSourceException in project CommandHelper by EngineHub.
the class Manager method refactor.
public static void refactor() {
pl("This tool allows you to granularly move individual keys from one datasource to another." + " Unlike the merge tool, this works with individual keys, not necessarily keys that are" + " within a particular data source. There are three required inputs, the transfer key pattern," + " the input configuration file, and the output configuration file. Data is transferred from" + " one configuration to the other, that is, it is added in the new place, and removed in the old place." + " This tool is more complicated" + " than the merge tool, so consider using the other tool for simple tasks.\n\n");
pl("Would you like to continue? [" + GREEN + "Y" + WHITE + "/" + RED + "N" + WHITE + "]");
String choice = prompt();
if ("Y".equals(choice)) {
String filter;
File input;
File output;
while (true) {
while (true) {
pl("What keys are you interested in transferring? The filter should be in the same format as the persistence.ini file, i.e." + " \"storage.test\" or \"storage.test.**\". If a wildcard is used, multiple keys may be moved, otherwise, only one will" + " be.");
filter = prompt();
break;
}
File def = MethodScriptFileLocations.getDefault().getPersistenceConfig();
while (true) {
pl("What is the input configuration (where keys will be read in from, then deleted)? Leave blank for the default, which is " + def.toString() + ". The path should be relative to " + jarLocation.toString());
String sinput = prompt();
if ("".equals(sinput.trim())) {
input = def;
} else {
File temp = new File(sinput);
if (!temp.isAbsolute()) {
temp = new File(jarLocation, sinput);
}
input = temp;
}
if (!input.exists() || !input.isFile()) {
pl(RED + input.toString() + " isn't a file. Please enter an existing file.");
} else {
break;
}
}
while (true) {
pl("What is the output configuration (where keys will be written to)? The path should be relative to " + jarLocation.toString());
String soutput = prompt();
if ("".equals(soutput.trim())) {
pl(RED + "The output cannot be empty");
continue;
} else {
File temp = new File(soutput);
if (!temp.isAbsolute()) {
temp = new File(jarLocation, soutput);
}
output = temp;
}
if (!output.exists() || !output.isFile()) {
pl(RED + output.toString() + " isn't a file. Please enter an existing file.");
} else {
break;
}
}
pl("The filter is \"" + MAGENTA + filter + WHITE + "\".");
pl("The input configuration is \"" + MAGENTA + input.toString() + WHITE + "\".");
pl("The output configuration is \"" + MAGENTA + output.toString() + WHITE + "\".");
pl("Is this correct? [" + GREEN + "Y" + WHITE + "/" + RED + "N" + WHITE + "]");
if ("Y".equals(prompt())) {
break;
}
}
pl(YELLOW + "Now beginning transfer...");
URI defaultURI;
try {
defaultURI = new URI("file://persistence.db");
} catch (URISyntaxException ex) {
throw new Error(ex);
}
ConnectionMixinFactory.ConnectionMixinOptions mixinOptions = new ConnectionMixinFactory.ConnectionMixinOptions();
try {
DaemonManager dm = new DaemonManager();
mixinOptions.setWorkingDirectory(chDirectory);
PersistenceNetwork pninput = new PersistenceNetwork(input, defaultURI, mixinOptions);
PersistenceNetwork pnoutput = new PersistenceNetwork(output, defaultURI, mixinOptions);
Pattern p = Pattern.compile(DataSourceFilter.toRegex(filter));
Map<String[], String> inputData = pninput.getNamespace(new String[] {});
boolean errors = false;
int transferred = 0;
int skipped = 0;
for (String[] k : inputData.keySet()) {
String key = StringUtils.Join(k, ".");
if (p.matcher(key).matches()) {
pl(GREEN + "transferring " + YELLOW + key);
// from the input network
if (pnoutput.getKeySource(k).equals(pninput.getKeySource(k))) {
// Don't transfer it if it's the same source, otherwise we would
continue;
// end up just deleting it.
}
try {
pnoutput.set(dm, k, inputData.get(k));
transferred++;
try {
pninput.clearKey(dm, k);
} catch (ReadOnlyException ex) {
pl(RED + "Could not clear out original key for the value for \"" + MAGENTA + StringUtils.Join(k, ".") + RED + "\", as the input" + " file is set to read only.");
errors = true;
}
} catch (ReadOnlyException ex) {
pl(RED + "Could not write out the value for \"" + MAGENTA + StringUtils.Join(k, ".") + RED + "\", as the output" + " file is set to read only.");
errors = true;
}
} else {
skipped++;
}
}
pl(YELLOW + StringUtils.PluralTemplateHelper(transferred, "%d key was", "%d keys were") + " transferred.");
pl(YELLOW + StringUtils.PluralTemplateHelper(skipped, "%d key was", "%d keys were") + " skipped.");
if (errors) {
pl(YELLOW + "Other than the errors listed above, all keys were transferred successfully.");
} else {
pl(GREEN + "Done!");
}
pl(GREEN + "If this is being done as part of an entire transfer process, don't forget to set " + output.toString() + " as your main Persistence Network configuration file.");
} catch (IOException | DataSourceException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
use of com.laytonsmith.persistence.DataSourceException in project CommandHelper by EngineHub.
the class Manager method cleardb.
public static void cleardb() {
try {
pl(RED + "Are you absolutely sure you want to clear out your database? " + BLINKON + "No backup is going to be made." + BLINKOFF);
pl(WHITE + "This will completely wipe your persistence information out. (No other data will be changed)");
pl("[YES/No]");
String choice = prompt();
if (choice.equals("YES")) {
pl("Positive? [YES/No]");
if (prompt().equals("YES")) {
p("Ok, here we go... ");
Set<String[]> keySet = persistenceNetwork.getNamespace(new String[] {}).keySet();
DaemonManager dm = new DaemonManager();
for (String[] key : keySet) {
try {
persistenceNetwork.clearKey(dm, key);
} catch (ReadOnlyException ex) {
pl(RED + "Read only data source found: " + ex.getMessage());
}
}
try {
dm.waitForThreads();
} catch (InterruptedException e) {
//
}
pl("Done!");
}
} else if (choice.equalsIgnoreCase("yes")) {
pl("No, you have to type YES exactly.");
}
} catch (DataSourceException | IOException ex) {
pl(RED + ex.getMessage());
}
}
use of com.laytonsmith.persistence.DataSourceException 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