use of com.laytonsmith.persistence.PersistenceNetwork 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.PersistenceNetwork in project CommandHelper by EngineHub.
the class Manager method start.
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public static void start() throws IOException, DataSourceException, URISyntaxException, Profiles.InvalidProfileException {
Implementation.useAbstractEnumThread(false);
Implementation.forceServerType(Implementation.Type.BUKKIT);
ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
options.setWorkingDirectory(chDirectory);
persistenceNetwork = new PersistenceNetwork(CommandHelperFileLocations.getDefault().getPersistenceConfig(), CommandHelperFileLocations.getDefault().getDefaultPersistenceDBFile().toURI(), options);
Installer.Install(chDirectory);
CHLog.initialize(chDirectory);
profiler = new Profiler(CommandHelperFileLocations.getDefault().getProfilerConfigFile());
gEnv = new GlobalEnv(new MethodScriptExecutionQueue("Manager", "default"), profiler, persistenceNetwork, chDirectory, new Profiles(MethodScriptFileLocations.getDefault().getProfilesFile()), new TaskManager());
cls();
pl("\n" + Static.Logo() + "\n\n" + Static.DataManagerLogo());
pl("Starting the Data Manager...");
try {
Environment env = Environment.createEnvironment(gEnv, new CommandHelperEnvironment());
MethodScriptCompiler.execute(MethodScriptCompiler.compile(MethodScriptCompiler.lex("player()", null, true)), env, null, null);
} catch (ConfigCompileException | ConfigCompileGroupException ex) {
}
pl(GREEN + "Welcome to the CommandHelper " + CYAN + "Data Manager!");
pl(BLINKON + RED + "Warning!" + BLINKOFF + YELLOW + " Be sure your server is not running before using this tool to make changes to your database!");
pl("------------------------");
boolean finished = false;
do {
pl(YELLOW + "What function would you like to run? Type \"help\" for a full list of options.");
String input = prompt();
pl();
if (input.toLowerCase().startsWith("help")) {
help(input.replaceFirst("help ?", "").toLowerCase().split(" "));
} else if (input.equalsIgnoreCase("refactor")) {
refactor();
} else if (input.toLowerCase().startsWith("print")) {
print(input.replaceFirst("print ?", "").toLowerCase().split(" "));
} else if (input.equalsIgnoreCase("cleardb")) {
cleardb();
} else if (input.equalsIgnoreCase("edit")) {
edit();
} else if (input.equalsIgnoreCase("merge")) {
merge();
} else if (input.equalsIgnoreCase("interpreter")) {
new Interpreter(null, System.getProperty("user.dir"));
} else if (input.equalsIgnoreCase("hidden-keys")) {
hiddenKeys();
} else if (input.equalsIgnoreCase("exit")) {
pl("Thanks for using the " + CYAN + BOLD + "Data Manager!" + reset());
finished = true;
} else {
pl("I'm sorry, that's not a valid command. Here's the help:");
help(new String[] {});
}
} while (finished == false);
StreamUtils.GetSystemOut().println(TermColors.reset());
}
use of com.laytonsmith.persistence.PersistenceNetwork in project CommandHelper by EngineHub.
the class PNViewer method loadFromLocal.
private void loadFromLocal(final String path) {
localPath = path;
new Thread(new Runnable() {
@Override
public void run() {
setStatus("Loading from local file system", true);
setProgress(null);
try {
final PersistenceNetwork pn = getPersistenceNetwork(path);
VirtualPersistenceNetwork vpn = new VirtualPersistenceNetwork() {
@Override
public Map<String[], String> getAllData() throws DataSourceException {
return pn.getNamespace(ArrayUtils.EMPTY_STRING_ARRAY);
}
@Override
public URI getKeySource(String[] key) {
return pn.getKeySource(key);
}
};
displayData(vpn);
} catch (URISyntaxException | IOException | DataSourceException ex) {
Logger.getLogger(PNViewer.class.getName()).log(Level.SEVERE, null, ex);
showError(ex.getMessage());
}
}
}).start();
}
use of com.laytonsmith.persistence.PersistenceNetwork in project CommandHelper by EngineHub.
the class PNViewer method getPersistenceNetwork.
private static PersistenceNetwork getPersistenceNetwork(String configPath) throws URISyntaxException, IOException, DataSourceException {
File config = new File(configPath);
ConnectionMixinFactory.ConnectionMixinOptions options = new ConnectionMixinFactory.ConnectionMixinOptions();
options.setWorkingDirectory(config.getParentFile().getParentFile());
return new PersistenceNetwork(config, new URI("sqlite://" + new File(config.getParentFile().getParentFile(), "persistence.db").toString().replace('\\', '/')), options);
}
use of com.laytonsmith.persistence.PersistenceNetwork in project CommandHelper by EngineHub.
the class PNViewer method startServer.
public static void startServer(int port, final String password) throws IOException {
ServerSocket socket = new ServerSocket(port);
log("Server started on port " + port + ". Type Ctrl+C to kill the server.");
log("Process info: " + ManagementFactory.getRuntimeMXBean().getName());
log("Server version: " + PROTOCOL_VERSION);
connection: while (true) {
log("Persistence Network Viewers may now connect to this server.");
final Socket s = socket.accept();
log("A client has connected from " + s.getInetAddress().toString());
new Thread(new Runnable() {
@Override
public void run() {
Thread waitThread = null;
try {
final AtomicBoolean dataReceieved = new AtomicBoolean(false);
final AtomicBoolean longTimeout = new AtomicBoolean(false);
waitThread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
synchronized (dataReceieved) {
dataReceieved.wait(longTimeout.get() ? 10 * 60 * 1000 : 10 * 1000);
}
if (dataReceieved.get() == false) {
log("No response from client in too long, forcibly closing connection.");
try {
s.close();
} catch (IOException ex) {
//
}
break;
}
dataReceieved.set(false);
}
} catch (InterruptedException ex) {
//
}
}
});
waitThread.start();
ObjectInputStream is = new ObjectInputStream(s.getInputStream());
ObjectOutputStream os = new AutoFlushObjectOutputStream(s.getOutputStream());
int protocolVersion = is.readInt();
if (protocolVersion != PROTOCOL_VERSION) {
log("Client version unsupported: " + protocolVersion);
os.writeUTF("VERSION-MISMATCH");
return;
} else {
log("Client version supported: " + protocolVersion);
os.writeUTF("VERSION-OK");
}
String clientPassword = is.readUTF();
if (!password.equals(clientPassword)) {
log("Client supplied the wrong password, disconnecting.");
os.writeUTF("PASSWORD-BAD");
os.writeUTF("DISCONNECT");
return;
} else {
if (!"".equals(password)) {
log("Client supplied the correct password");
}
os.writeUTF("PASSWORD-OK");
}
// They have now authed correctly, so we can up the idle time.
longTimeout.set(true);
// Now we need to create instance variables for the remainder of
// the connection, that is, the meat of the connection.
String remoteFile = null;
PersistenceNetwork pn = null;
connected: while (s.isConnected()) {
String command = is.readUTF();
log("Command received from client: " + command);
dataReceieved.set(true);
synchronized (dataReceieved) {
dataReceieved.notifyAll();
}
switch(command) {
case "DISCONNECT":
// Write the disconnect out to the client as well, so
// the client will gracefully disconnect.
os.writeUTF("DISCONNECT");
break connected;
case "SET-REMOTE-FILE":
remoteFile = is.readUTF();
log("File set to " + remoteFile);
if (new File(remoteFile).exists()) {
log("File accepted.");
os.writeUTF("FILE-OK");
} else {
log("File not accepted.");
os.writeUTF("FILE-BAD");
os.writeUTF("DISCONNECT");
}
break;
case "LOAD-DATA":
os.writeUTF("LOAD-DATA");
try {
// Load the data from the PN, and send it on
pn = getPersistenceNetwork(remoteFile);
Map<String[], String> data = pn.getNamespace(ArrayUtils.EMPTY_STRING_ARRAY);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(data);
oos.flush();
byte[] output = baos.toByteArray();
os.writeInt(output.length);
os.write(output);
} catch (URISyntaxException | DataSourceException ex) {
os.writeUTF("LOAD-ERROR");
os.writeUTF(ex.getMessage());
log("Load error!");
log(ex);
}
break;
case "KEY-SOURCE":
os.writeUTF("KEY-SOURCE");
String key = is.readUTF();
if (pn == null) {
log("pn is null, can't get key source");
os.writeUTF("DISCONNECT");
} else {
log("Requested source for key: " + key);
URI uri = pn.getKeySource(key.split("\\."));
log("Responding with: " + uri);
os.writeObject(uri);
}
break;
default:
// Bad command, disconnect them.
os.writeUTF("DISCONNECT");
break connected;
}
}
} catch (IOException ex) {
// Disconnected
} finally {
try {
s.close();
} catch (IOException ex) {
//
}
log("Client has disconnected.");
if (waitThread != null) {
waitThread.interrupt();
}
}
}
}).start();
}
}
Aggregations