提交 adc476c7 编写于 作者: S serge-rider

RMI server

上级 5ddfc091
......@@ -32,10 +32,14 @@ import org.eclipse.ui.PlatformUI;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.runtime.rmi.IInstanceController;
import org.jkiss.dbeaver.runtime.rmi.InstanceClient;
import org.jkiss.dbeaver.runtime.rmi.InstanceServer;
import org.jkiss.utils.ArrayUtils;
import java.io.File;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
......@@ -57,15 +61,12 @@ public class DBeaverApplication implements IApplication
Location instanceLoc = Platform.getInstanceLocation();
String defaultHomePath = getDefaultWorkspaceLocation().getAbsolutePath();
try {
URL defaultHomeURL = new URL(
"file", //$NON-NLS-1$
null,
defaultHomePath);
URL defaultHomeURL = new File(defaultHomePath).toURI().toURL();
boolean keepTrying = true;
Shell shell = null;
while (keepTrying) {
if (!instanceLoc.set(defaultHomeURL, true)) {
if (handleCommandLine(instanceLoc)) {
if (handleCommandLine(defaultHomePath)) {
return IApplication.EXIT_OK;
}
// Can't lock specified path
......@@ -131,11 +132,38 @@ public class DBeaverApplication implements IApplication
}
}
private boolean handleCommandLine(Location instanceLoc) {
private boolean handleCommandLine(String instanceLoc) {
CommandLine commandLine = getCommandLine();
if (commandLine == null) {
return false;
}
if (commandLine.hasOption(DBeaverCommandLine.PARAM_HELP)) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.setWidth(120);
helpFormatter.setOptionComparator(new Comparator<Option>() {
@Override
public int compare(Option o1, Option o2) {
return 0;
}
});
helpFormatter.printHelp("dbeaver", DBeaverCore.getProductTitle(), DBeaverCommandLine.ALL_OPTIONS, "(C) 2016 JKISS", true);
return true;
}
try {
IInstanceController controller = InstanceClient.createClient(instanceLoc);
if (controller == null) {
return false;
}
return callRemoteServer(commandLine, controller);
} catch (Throwable e) {
log.error("Error calling remote server", e);
return false;
}
}
private boolean callRemoteServer(CommandLine commandLine, IInstanceController controller) throws RemoteException {
String[] files = commandLine.getOptionValues(DBeaverCommandLine.PARAM_FILE);
String[] fileArgs = commandLine.getArgs();
if (!ArrayUtils.isEmpty(files) || !ArrayUtils.isEmpty(fileArgs)) {
......@@ -146,24 +174,16 @@ public class DBeaverApplication implements IApplication
if (!ArrayUtils.isEmpty(fileArgs)) {
Collections.addAll(fileNames, fileArgs);
}
controller.openExternalFiles(fileNames.toArray(new String[fileNames.size()]));
return true;
}
if (commandLine.hasOption(DBeaverCommandLine.PARAM_STOP)) {
controller.quit();
return true;
}
if (commandLine.hasOption(DBeaverCommandLine.PARAM_THREAD_DUMP)) {
return true;
}
if (commandLine.hasOption(DBeaverCommandLine.PARAM_HELP)) {
HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.setWidth(120);
helpFormatter.setOptionComparator(new Comparator<Option>() {
@Override
public int compare(Option o1, Option o2) {
return 0;
}
});
helpFormatter.printHelp("dbeaver", "Universal Database Manager", DBeaverCommandLine.ALL_OPTIONS, "(C) 2016 Serge Rider", true);
String threadDump = controller.getThreadDump();
System.out.println(threadDump);
return true;
}
return false;
......
......@@ -23,6 +23,7 @@ Export-Package: org.jkiss.dbeaver,
org.jkiss.dbeaver.runtime.net,
org.jkiss.dbeaver.runtime.properties,
org.jkiss.dbeaver.runtime.qm,
org.jkiss.dbeaver.runtime.rmi,
org.jkiss.dbeaver.runtime.sql,
org.jkiss.dbeaver.runtime.ui,
org.jkiss.dbeaver.tools,
......
......@@ -27,6 +27,7 @@ import java.rmi.RemoteException;
public interface IInstanceController extends Remote {
public static final String CONTROLLER_ID = "DBeaver.InstanceController";
public static final String RMI_PROP_FILE = ".dbeaver-server.properties";
String getVersion() throws RemoteException;
......
......@@ -18,12 +18,32 @@
package org.jkiss.dbeaver.runtime.rmi;
import org.jkiss.dbeaver.Log;
import java.io.*;
import java.net.URL;
import java.rmi.registry.LocateRegistry;
import java.util.Properties;
/**
* DBeaver instance controller.
* InstanceClient
*/
public class InstanceClient {
public static IInstanceController createClient() {
private static final Log log = Log.getLog(InstanceClient.class);
public static IInstanceController createClient(String location) {
try {
File rmiFile = new File(location, ".metadata/" + IInstanceController.RMI_PROP_FILE);
Properties props = new Properties();
try (InputStream is = new FileInputStream(rmiFile)) {
props.load(is);
}
String rmiPort = props.getProperty("port");
return (IInstanceController) LocateRegistry.getRegistry(Integer.parseInt(rmiPort)).lookup(IInstanceController.CONTROLLER_ID);
} catch (Exception e) {
log.error("Error reading RMI config", e);
}
return null;
}
......
......@@ -37,7 +37,7 @@ import java.util.Properties;
public class InstanceServer implements IInstanceController {
private static final Log log = Log.getLog(InstanceServer.class);
private static final String RMI_PROP_FILE = ".dbeaver-server.properties";
private static int portNumber;
private static Registry registry;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册