|  | 
|  | 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.util.regex.Matcher; | 
|  | 6 | +import java.util.regex.Pattern; | 
|  | 7 | + | 
|  | 8 | +import oracle.dbtools.raptor.standalone.connection.ConnectionUtils; | 
|  | 9 | +import oracle.ide.Ide; | 
|  | 10 | +import oracle.ide.cmd.ExitCommand; | 
|  | 11 | +import oracle.ide.cmd.ShutdownHook; | 
|  | 12 | + | 
|  | 13 | +/** | 
|  | 14 | + * ConnectionHelper - Where the 'do the work' stuff lives | 
|  | 15 | + * | 
|  | 16 | + * @author <a href="mailto:brian.jeffries@oracle.com?subject=oracle.db.example.sqldeveloper.extension.connectionHelper.ConnectionHelper">Brian Jeffries</a> | 
|  | 17 | + * @since SQL Developer 20.1 | 
|  | 18 | + */ | 
|  | 19 | +public class ConnectionHelper { | 
|  | 20 | + | 
|  | 21 | +private static boolean processedCommandLineArgs; | 
|  | 22 | + | 
|  | 23 | +public static void processCommandLineArgs() { | 
|  | 24 | +//	if (!processedCommandLineArgs) { | 
|  | 25 | + String[] args = Ide.getIdeArgs().getArgs(); | 
|  | 26 | + for (String arg : args) { | 
|  | 27 | + System.out.println(arg); | 
|  | 28 | + ConnectionHelper.processPotentialConnectionArgument(arg); | 
|  | 29 | + } | 
|  | 30 | + processedCommandLineArgs = true; | 
|  | 31 | +//	} | 
|  | 32 | +} | 
|  | 33 | + | 
|  | 34 | +// -system_DB120101=system/dbtools@llg00hon.uk.oracle.com:1521/DB12201 | 
|  | 35 | + // -sysdba_DB120101=sys/dbtools@llg00hon.uk.oracle.com:1521/DB12201#SYSDBA | 
|  | 36 | + // TODO? Look up valid character requirements for each group | 
|  | 37 | + // format = -conName=user[/[pw]]@host:port(:sid|/svc)[#role] | 
|  | 38 | + // 1 2 4 5 6 8 9 11 | 
|  | 39 | + private static final String conRegex = "-(.*)=([^\\/]*)(\\/([^@]*))?@([^:]*):([^:]*)(:([a-zA-Z0-9_]*)|\\/([a-zA-Z0-9_]*))(#([a-zA-Z0-9_]*))?"; //$NON-NLS-1$ | 
|  | 40 | + private static final Pattern conArg = Pattern.compile(conRegex); | 
|  | 41 | +public static void processPotentialConnectionArgument(String arg) { | 
|  | 42 | + Matcher m = conArg.matcher(arg); | 
|  | 43 | + if (m.matches()) { | 
|  | 44 | + String connName = m.group(1); | 
|  | 45 | + String userName = m.group(2); | 
|  | 46 | + String password = m.group(4); | 
|  | 47 | + String host = m.group(5); | 
|  | 48 | + String port = m.group(6); | 
|  | 49 | + String sid = m.group(8); | 
|  | 50 | + String service = m.group(9); | 
|  | 51 | + String role = m.group(11); | 
|  | 52 | + String folder = ConnectionHelperPreferenceModel.getInstance().isPersistCommandLineConnections() | 
|  | 53 | + ? ConnectionHelperResources.getString(ConnectionHelperResources.PERSISTENT) | 
|  | 54 | + : ConnectionHelperResources.getString(ConnectionHelperResources.TRANSIENT); | 
|  | 55 | +  | 
|  | 56 | +  | 
|  | 57 | + ConnectionUtils.addConnection(connName, userName, password, sid, host, port, false/*osAuth*/, service, role, folder); | 
|  | 58 | + final String fqName = ConnectionUtils.getFqConnectionName(connName); | 
|  | 59 | + ConnectionUtils.connect(fqName); | 
|  | 60 | +  | 
|  | 61 | + if (!ConnectionHelperPreferenceModel.getInstance().isPersistCommandLineConnections()) { | 
|  | 62 | + ExitCommand.addShutdownHook(new ShutdownHook() { | 
|  | 63 | + @Override | 
|  | 64 | + public boolean canShutdown() { | 
|  | 65 | + return true; | 
|  | 66 | + } | 
|  | 67 | + @Override | 
|  | 68 | + public void shutdown() { | 
|  | 69 | + ConnectionUtils.closeAndDeleteConnection(fqName); | 
|  | 70 | + } | 
|  | 71 | + }); | 
|  | 72 | + } | 
|  | 73 | + } | 
|  | 74 | +} | 
|  | 75 | + | 
|  | 76 | +// TODO Refactor to handle request from server | 
|  | 77 | +public static void processPotentialConnectionRequest(String arg) { | 
|  | 78 | + | 
|  | 79 | +} | 
|  | 80 | + | 
|  | 81 | +} | 
0 commit comments