|
| 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 | +} |
0 commit comments