Skip to content

Commit

Permalink
TEIIDDES-1751: Provide api for getting client-side Teiid Driver
Browse files Browse the repository at this point in the history
 * Rather than fetching the Teiid Driver from the Teiid Server, requiring
   connection, the driver is available in each client runtime plugin. For
   cases where only the driver is required, eg. Database Developer connections,
   provide the driver.

 * To allow for changes in the Teiid Driver, each runtime will provide its
   own version through the ExecutionAdminFactory so that the correct driver
   for the server is provided.

 * ModelerCore
  * Stop providing access to the default teiid server since it could well
    be null and invite null pointer exceptions.
  * Provides an API for the relevent properties of the default server
    requested by clients and handle the possibilty of the default server
    being null.
  • Loading branch information
Paul Richardson committed Jun 6, 2013
1 parent 8873bf8 commit 46b923e
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import org.eclipse.datatools.connectivity.drivers.jdbc.IJDBCDriverDefinitionConstants;
import org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.runtime.spi.ITeiidServer;
import org.teiid.designer.runtime.version.spi.ITeiidServerVersion;
import org.teiid.designer.runtime.version.spi.TeiidServerVersion;

/**
* @since 8.0
Expand Down Expand Up @@ -74,13 +75,19 @@ protected Object createConnection( ClassLoader cl ) throws Throwable {
addPairs = addPairs + pairs[i];
}
}

ITeiidServer defaultServer = ModelerCore.getDefaultServer();

Driver jdbcDriver = defaultServer.getTeiidDriver(driverClass);

if (jdbcDriver != null) {
return jdbcDriver.connect(connectURL, connectionProps);

/*
* Using the database version id, attempt to acquire the driver
* from the teiid client runtimes. This no longer requires using
* the default teiid server (which has to be connected).
*/
String teiidVersion = props.getProperty(IJDBCDriverDefinitionConstants.DATABASE_VERSION_PROP_ID);
if (teiidVersion != null) {
ITeiidServerVersion teiidServerVersion = new TeiidServerVersion(teiidVersion);
Driver jdbcDriver = ModelerCore.getTeiidDriver(teiidServerVersion, driverClass);
if (jdbcDriver != null) {
return jdbcDriver.connect(connectURL, connectionProps);
}
}

throw new Exception("Cannot find Teiid Driver"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.teiid.designer.core;

import java.io.File;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -42,6 +43,7 @@
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMLMapImpl;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
Expand All @@ -60,6 +62,7 @@
import org.teiid.core.designer.id.ObjectID;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.core.designer.util.PluginUtilImpl;
import org.teiid.core.designer.util.Stopwatch;
import org.teiid.designer.WorkspaceUUIDService;
Expand Down Expand Up @@ -100,6 +103,7 @@
import org.teiid.designer.core.workspace.ModelWorkspaceManagerSaveParticipant;
import org.teiid.designer.query.IQueryService;
import org.teiid.designer.runtime.registry.TeiidRuntimeRegistry;
import org.teiid.designer.runtime.spi.EventManager;
import org.teiid.designer.runtime.spi.ITeiidServer;
import org.teiid.designer.runtime.spi.ITeiidServerVersionListener;
import org.teiid.designer.runtime.version.spi.ITeiidServerVersion;
Expand Down Expand Up @@ -289,6 +293,8 @@ public static enum RegistryOption {
*/
private static final String I18N_NAME = PACKAGE_ID + ".i18n"; //$NON-NLS-1$

private static final String I18N_PREFIX = I18nUtil.getPropertyPrefix(ModelerCore.class);

public static final PluginUtil Util = new PluginUtilImpl(PLUGIN_ID, I18N_NAME, ResourceBundle.getBundle(I18N_NAME));

private static Plugin MODELER_CORE_PLUGIN = null;
Expand Down Expand Up @@ -2063,21 +2069,60 @@ public static interface ILicense {
String PRODUCER_NAME = getProducerName();
String VERSION = getVersion();
}

/**
* Get the targeted teiid server
*
* @return teiid server
* Has a default server been set
*
* @return
*/
public static ITeiidServer getDefaultServer() {
return defaultTeiidServer;
public static boolean hasDefaultTeiidServer() {
return defaultTeiidServer != null;
}

/**
* Get the targeted teiid server version
*
* @return teiid server version
*/
public static String getDefaultServerName() {
if (defaultTeiidServer == null) {
return Util.getString(I18N_PREFIX + "noDefaultServer"); //$NON-NLS-1$
}
return defaultTeiidServer.getDisplayName();
}

/**
* Get the {@link EventManager} associated with the default
* server or null if there is no default server set.
*
* @return event manager
*/
public static EventManager getDefaultServerEventManager() {
if (defaultTeiidServer == null) {
return null;
}

return defaultTeiidServer.getEventManager();
}

/**
* Create an SWT {@link Event} that encapsulates the
* default teiid server as its data object
*
* @return an event
*/
public static Event createDefaultTeiidServerEvent() {
Event event = new Event();
event.data = defaultTeiidServer;

return event;
}

/**
* Get the targeted teiid server version
*
* @return teiid server version
*/
public static ITeiidServerVersion getTeiidServerVersion() {
if (defaultTeiidServer == null) {
// Preference is stored in the ui plugin which we do not have a dependency on so have to
Expand All @@ -2090,6 +2135,26 @@ public static ITeiidServerVersion getTeiidServerVersion() {

return defaultTeiidServer.getServerVersion();
}

/**
* Find a Teiid {@link Driver} for the given server version.
*
* The driver class should be provided as a check to ensure the class name
* is as expected.
*
* @param teiidServerVersion
* @param driverClass
*
* @return the Teiid {@link Driver}
* @throws Exception
*/
public static Driver getTeiidDriver(ITeiidServerVersion teiidServerVersion, String driverClass) throws Exception {
Driver driver = TeiidRuntimeRegistry.getInstance().getTeiidDriver(teiidServerVersion);
if (driver != null && driver.getClass().getSimpleName().equals(driverClass))
return driver;

throw new IllegalStateException(Util.getString(I18N_PREFIX + "noTeiidDriver", driverClass, teiidServerVersion));
}

/**
* Add a listener to be notified in the event the default server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,4 +965,6 @@ VdbHelper.errorGettingJarPath=Error getting the udfJarPath property for: {0}
VdbHelper.errorWithFileLookupInFolder=Error with file lookup in folder: {0}
VdbHelper.errorWithJarLookupInFolder=Error with jar file lookup in folder: {0}
VdbHelper.errorCountingFolderFiles=Error counting files in folder: {0}


ModelerCore.noDefaultServer=No default server defined
ModelerCore.noTeiidDriver=No Teiid Driver ({0}) available for version {1}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.teiid.designer.runtime.registry;

import java.sql.Driver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -118,6 +119,22 @@ public IDataTypeManagerService getDataTypeManagerService(ITeiidServerVersion tei
return factory.getDataTypeManagerService();
}

/**
* Get the Teiid Driver for the given server version
*
* @param teiidServerVersion
*
* @return the Teiid Driver
* @throws Exception
*/
public Driver getTeiidDriver(ITeiidServerVersion teiidServerVersion) throws Exception {
IExecutionAdminFactory factory = search(teiidServerVersion);
if (factory == null)
throw new Exception(NLS.bind(Messages.NoExecutionAdminFactory, teiidServerVersion));

return factory.getTeiidDriver();
}

/**
* Get the teiid sql syntax service
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.teiid.designer.runtime.spi;

import java.sql.Driver;
import org.teiid.designer.query.IQueryService;
import org.teiid.designer.type.IDataTypeManagerService;

Expand All @@ -33,6 +34,13 @@ public interface IExecutionAdminFactory {
*/
IDataTypeManagerService getDataTypeManagerService();

/**
* Get the {@link Driver} for the Teiid Server
*
* @return the driver
*/
Driver getTeiidDriver();

/**
* Get the query service
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.workspace.DotProjectUtils;
import org.teiid.designer.runtime.spi.EventManager;
import org.teiid.designer.runtime.spi.ExecutionConfigurationEvent;
import org.teiid.designer.runtime.spi.IExecutionConfigurationListener;
import org.teiid.designer.runtime.spi.ITeiidServer;
Expand Down Expand Up @@ -262,8 +263,8 @@ public void serverChanged(ITeiidServer server) {
if (defaultServerLink == null)
return;

setDefaultServerText(server);
addExecutionConfigurationListener(server);
setDefaultServerText(server.getDisplayName());
addExecutionConfigurationListener(server.getEventManager());
}

@Override
Expand All @@ -280,7 +281,7 @@ public void versionChanged(ITeiidServerVersion version) {

@Override
public void configurationChanged(ExecutionConfigurationEvent event) {
setDefaultServerText(ModelerCore.getDefaultServer());
setDefaultServerText(ModelerCore.getDefaultServerName());
setDefaultServerVersionText(ModelerCore.getTeiidServerVersion());
}
};
Expand Down Expand Up @@ -487,15 +488,14 @@ private void createServerStatusPanel(Composite parent) {
GridLayoutFactory.fillDefaults().numColumns(3).applyTo(defaultServerSectionBody);
defaultServerSectionBody.setBackground(bkgdColor);

ITeiidServer defaultServer = ModelerCore.getDefaultServer();
setDefaultServerText(defaultServer);
setDefaultServerText(ModelerCore.getDefaultServerName());
setDefaultServerVersionText(ModelerCore.getTeiidServerVersion());

defaultServerSection.setClient(defaultServerSectionBody);

/* Listen for changes to the default server */
ModelerCore.addTeiidServerVersionListener(teiidServerVersionListener);
addExecutionConfigurationListener(defaultServer);
addExecutionConfigurationListener(ModelerCore.getDefaultServerEventManager());

IEclipsePreferences prefs = UiPlugin.getDefault().getPreferences();
prefs.addPreferenceChangeListener(preferenceChangeListener);
Expand All @@ -507,26 +507,23 @@ private void createServerStatusPanel(Composite parent) {
*
* @param teiidServer
*/
private void addExecutionConfigurationListener(ITeiidServer teiidServer) {
if (teiidServer == null)
private void addExecutionConfigurationListener(EventManager eventManager) {
if (eventManager == null)
return;

teiidServer.getEventManager().addListener(execConfigurationListener);
eventManager.addListener(execConfigurationListener);
}

/**
* Called by a number of different methods to update the default server name text field.
*
* @param defaultServer can be null if there are no servers and thus no default server has been set
* @param serverName name of the server or a noDefaultServer message
*/
private void setDefaultServerText(ITeiidServer defaultServer) {
String defaultName = defaultServer != null ? defaultServer.getDisplayName() : getString("noDefaultServer"); //$NON-NLS-1$
String linkText = defaultName;

private void setDefaultServerText(String serverName) {
if (defaultServerLink == null) {
toolkit.createLabel(defaultServerSectionBody, getString("defaultServerPrefix")); //$NON-NLS-1$

defaultServerLink = toolkit.createHyperlink(defaultServerSectionBody, linkText, SWT.NONE);
defaultServerLink = toolkit.createHyperlink(defaultServerSectionBody, serverName, SWT.NONE);
GridDataFactory.fillDefaults().grab(true, false).applyTo(defaultServerLink);

defaultServerLink.addHyperlinkListener(new HyperlinkAdapter() {
Expand All @@ -540,8 +537,7 @@ public void linkActivated(HyperlinkEvent e) {

// Need to fetch the default server again since the parameter in the
// parent method does not remain assigned becoming null
ITeiidServer defServer = ModelerCore.getDefaultServer();
if (defServer == null) {
if (! ModelerCore.hasDefaultTeiidServer()) {
// No default server so most likely no servers at all so open the new server wizard
IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
handlerService.executeCommand("org.teiid.designer.dqp.ui.newServerAction", null); //$NON-NLS-1$
Expand All @@ -551,8 +547,7 @@ public void linkActivated(HyperlinkEvent e) {

// Load the default server into an event's data in order to
// make it available to the command's handler.
Event event = new Event();
event.data = defServer;
Event event = ModelerCore.createDefaultTeiidServerEvent();
handlerService.executeCommand("org.teiid.designer.dqp.ui.editServerAction", event); //$NON-NLS-1$
}

Expand Down Expand Up @@ -583,7 +578,7 @@ public void linkActivated(HyperlinkEvent e) {
if( defaultServerSectionBody.isDisposed() || defaultServerLink.isDisposed() ) return;

Display display = defaultServerSectionBody.getDisplay();
final String hyperlinkText = linkText;
final String hyperlinkText = serverName;
display.asyncExec(new Runnable() {

@Override
Expand Down
2 changes: 1 addition & 1 deletion plugins/teiid/org.teiid.7.7.x
2 changes: 1 addition & 1 deletion plugins/teiid/org.teiid.8.3.x
2 changes: 1 addition & 1 deletion plugins/teiid/org.teiid.8.4.x

0 comments on commit 46b923e

Please sign in to comment.