Skip to content

Commit 1c67222

Browse files
committed
WIP: SQLDEV-2076 Add ConnectionHelperServer
TODO: ConnectionHelperClient (test class/jar) TDO: Documentation & code cleanup
1 parent 79f6b73 commit 1c67222

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

sqldeveloper/extension/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<classpathentry kind="src" output="java/DependencyExample/built/classes" path="java/DependencyExample/src"/>
1010
<classpathentry kind="src" path="java/DumpObjectTypesAction/src"/>
1111
<classpathentry kind="src" path="java/ConnectionHelper/src"/>
12+
<classpathentry kind="src" path="java/ConnectionHelperClient/src"/>
1213
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
1314
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/FXDiagram 0.35.0-SNAPSHOT"/>
1415
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/KIELER KLay Layouters v. 2015.02"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
3+
package oracle.db.example.sqldeveloper.extension.connectionHelper;
4+
5+
import java.io.BufferedReader;
6+
import java.io.InputStreamReader;
7+
import java.net.ServerSocket;
8+
import java.net.Socket;
9+
10+
import oracle.dbtools.raptor.backgroundTask.IRaptorTaskRunMode;
11+
import oracle.dbtools.raptor.backgroundTask.RaptorTask;
12+
import oracle.dbtools.raptor.backgroundTask.RaptorTaskManager;
13+
import oracle.dbtools.raptor.backgroundTask.TaskException;
14+
import oracle.dbtools.raptor.backgroundTask.utils.DatabaseQueryTask;
15+
16+
/**
17+
* ConnectionHelperServer - a simple server to listen for connection requests
18+
*
19+
* @author <a href="mailto:brian.jeffries@oracle.com?subject=oracle.db.example.sqldeveloper.extension.connectionHelper.ConnectionHelperServer">Brian Jeffries</a>
20+
* @since SQL Developer 20.1
21+
*/
22+
public class ConnectionHelperServer {
23+
private static ServerTask serverTask;
24+
private static boolean listening;
25+
26+
private static final String NAME_TEMPLATE = "%s-%d"; //$NON-NLS-1$
27+
28+
public static void start() {
29+
stop();
30+
int port = ConnectionHelperPreferenceModel.getInstance().getExternalConnectionServerPort();
31+
serverTask = new ServerTask(String.format(NAME_TEMPLATE, ConnectionHelperServer.class.getSimpleName(), port), port);
32+
listening = true;
33+
RaptorTaskManager.getInstance().addTask(serverTask);
34+
}
35+
36+
public static void stop() {
37+
if (serverTask != null) {
38+
serverTask.requestCancel();
39+
serverTask = null;
40+
}
41+
}
42+
43+
private static class ServerTask extends RaptorTask<Void> {
44+
private int port;
45+
46+
public ServerTask(String name, int port) {
47+
super(name, true /*isInDeterminate*/, IRaptorTaskRunMode.TASKVIEWER /*mode*/);
48+
this.port = port;
49+
}
50+
51+
@Override
52+
protected Void doWork() throws TaskException {
53+
try (ServerSocket serverSocket = new ServerSocket(port)) {
54+
while (listening) {
55+
checkCanProceed();
56+
ConnectionHelperTask helperTask = new ConnectionHelperTask(serverSocket.accept());
57+
RaptorTaskManager.getInstance().addTask(helperTask);
58+
}
59+
}
60+
catch (Throwable t) {
61+
throw asTaskException(t);
62+
}
63+
return null;
64+
}
65+
}
66+
67+
private static class ConnectionHelperTask extends DatabaseQueryTask<Void> {
68+
private Socket socket;
69+
private static int idx;
70+
71+
public ConnectionHelperTask(Socket socket) {
72+
super(String.format(NAME_TEMPLATE, ConnectionHelperTask.class.getSimpleName(), ++idx),
73+
IRaptorTaskRunMode.TASKVIEWER, false /*isPausable*/, false /*isCancellable*/);
74+
this.socket = socket;
75+
}
76+
77+
@Override
78+
public String getConnectionName() {
79+
// We are cheating here. DatabaseQueryTasks are serialized on the ConnectionName
80+
// so this is a way to make sure ConnectionHelperTasks are done one at a time.
81+
return this.getClass().getName();
82+
}
83+
84+
@Override
85+
public String getQuery() {
86+
// This will be shown on the bottom of the task UI
87+
return this.getDescriptor().getStatus().toString();
88+
}
89+
90+
@Override
91+
protected Void doWork() throws TaskException {
92+
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
93+
String inputLine;
94+
while ((inputLine = in.readLine()) != null) {
95+
ConnectionHelper.processPotentialConnectionRequest(inputLine);
96+
}
97+
}
98+
catch (Throwable t) {
99+
throw asTaskException(t);
100+
}
101+
return null;
102+
}
103+
104+
}
105+
106+
public static TaskException asTaskException(Throwable t) {
107+
if (t instanceof TaskException) {
108+
return (TaskException)t;
109+
} else {
110+
return new TaskException(t);
111+
}
112+
}
113+
114+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
3+
/**
4+
* ConnectionHelperClient - a simple client for the ConnectionHelperServer<p/>
5+
* usage: java -jar ConnectionHelperClient.jar port connectionInfo
6+
*
7+
* @author <a href="mailto:brian.jeffries@oracle.com?subject=.ConnectionHelperClient">Brian Jeffries</a>
8+
* @since SQL Developer 20.1
9+
*/
10+
public class ConnectionHelperClient {
11+
12+
/**
13+
* @param args
14+
*/
15+
public static void main(String[] args) {
16+
17+
}
18+
19+
}

0 commit comments

Comments
 (0)