How can I get all the properties of a SqlDeveloper Connection?
The short answer is this:
String qualifiedName = "IdeConnections%23MyConnection"; java.util.Properties properties = oracle.dbtools.raptor.utils.Connections.getInstance().getConnectionInfo(qualifiedName);
In the qualifiedName, "IdeConnections" is a connection store and "MyConnection" is the name of a user-defined connection in SqlDeveloper. (see this post for more information about connection stores and qualified names.)
qualifiedName
"IdeConnections"
"MyConnection"
The properties returned include:
OracleConnectionType oraDriverType RaptorConnectionType subtype ConnName hostname customUrl port user role NoPasswordConnection SavePassword password serviceName OS_AUTHENTICATION driver
So that's the easy part. If you have a qualifiedName, then you're done and you can skip the rest of this post. But for me, the more difficult part was getting a current qualifiedName.
The following looked very promising: oracle.dbtools.raptor.utils.Connections.getActiveConnectionName(), but getActiveConnectionName() was returning null even though there's a valid active connection.
oracle.dbtools.raptor.utils.Connections.getActiveConnectionName()
getActiveConnectionName()
java.sql.Connection
java.sql.Connection conn = oracle.dbtools.raptor.utils.Connections.getInstance().getConnection(qualifiedName);
I wanted the ability to select any node on any part of the connection tree and know my 'context'. That is, what is my current connection and what are its various properties. To do this, I wanted to tie into the selection event when any node on the tree was selected so I could update the UI and the context of my extension. There may be other ways of doing this, but lacking any documentation, here's how I accomplished this goal:
First I needed to implement 2 custom classes:
public class MyIdeListener implements oracle.ide.IdeListener public class MyViewSelectionListener implements oracle.ide.view.ViewSelectionListener
In the initialilze() method of an oracle.ide.Addin, I added my own implementation of an oracle.ide.IdeListener. Here's some psuedo-code:
initialilze()
oracle.ide.Addin
oracle.ide.IdeListener
public class MyAddin implements oracle.ide.Addin { public void initialize() { oracle.ide.Ide.addIdeListener(new MyIdeListener()); } }
This is only done to register my own implementation of an oracle.ide.view.ViewSelectionListener to the Connections tree. You wont have access to the Connections tree until the mainWindowOpened() event fires in the oracle.ide.IdeListener:
oracle.ide.view.ViewSelectionListener
mainWindowOpened()
public class MyIdeListener implements oracle.ide.IdeListener { @Override public void mainWindowOpened(IdeEvent ideEvent) { oracle.ide.explorer.TreeExplorer treeExplorer = oracle.dbtools.raptor.navigator.DatabaseNavigatorManager.getInstance().getNavigatorWindow().getTreeExplorer(); treeExplorer.addViewSelectionListener(new MyViewSelectionListener()); } }
It gets interesting in the oracle.ide.view.ViewSelectionListener, especially the overidden viewSelectionChanged() method. Here you'll get access to the Connections tree (an oracle.ide.explorer.TreeExplorer), its underlying javax.swing.JTree, the current selection, selection path, and selection data. Essentially, all the magic will happen here in the viewSelectionChanged() method. Although far from overwhelming, there's just too much code to post here, so you'll need to download the source (Eclipse Project). Here's a snippet:
viewSelectionChanged()
oracle.ide.explorer.TreeExplorer
javax.swing.JTree
public class MyViewSelectionListener implements oracle.ide.view.ViewSelectionListener { @Override public void viewSelectionChanged(ViewSelectionEvent viewSelectionEvent) { System.out.println("VIEW SELECTION CHANGED"); // ... too much code to post... // ... but you'll get an oracle.dbtools.raptor.navigator.DatabaseConnection } }
Once you have a valid, current, oracle.dbtools.raptor.navigator.DatabaseConnection you will then have access to the qualifiedName:
oracle.dbtools.raptor.navigator.DatabaseConnection
String qualifiedName = databaseConnection.getQualifiedConnectionName();
With the qualifiedName you can then, in a round-about way, get more information using the following method indicated at the beginning of this post:
java.util.Properties properties = oracle.dbtools.raptor.utils.Connections.getInstance().getConnectionInfo(qualifiedName);
Just access any of the properties listed at the beginning of this post. For example:
String user = this.properties.getProperty("user"); String password = this.properties.getProperty("password");
There are a few more interesting items of note:
None of this is of any use if a user simply EXPANDS the connection node rather than selecting it. For that you might consider implementing an oracle.dbtools.raptor.connections.ConnectionListener (which is what I did). Add the following to the initialize() method of your oracle.ide.Addin:
oracle.dbtools.raptor.connections.ConnectionListener
initialize()
oracle.dbtools.raptor.utils.Connections.getInstance().addConnectionListener(new MyConnectionListener());
Or you could add a javax.swing.event.TreeWillExpandListener which gets more complicated and out of scope.
javax.swing.event.TreeWillExpandListener
It's interesting that you can't get the properties directly from the databaseConnection instance; you have to use the static Connections.getInstance().getConnectionInfo() helper method. I would have expected something along the lines of databaseConnection.getProperties() but there isn't any such method.
properties
databaseConnection
Connections.getInstance().getConnectionInfo()
databaseConnection.getProperties()
Additionally, you wont get a password if a user chose NOT to save the password when the connection was defined.
I created a 'safe' helper class that makes it stupid-easy to get all the properties from a databaseConnection in a type-safe manner. This class ensures:
boolean
int
string
ConnectionProperties connectionProperties = new ConnectionProperties(properties);