Skip to content

Commit

Permalink
Merge pull request #29 from mdrillin/TEIIDDES-1490
Browse files Browse the repository at this point in the history
TEIIDDES-1490
  • Loading branch information
blafond committed Sep 25, 2012
2 parents 94eda21 + 13c3e4e commit e94f5a9
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@
package org.teiid.designer.runtime;

import static org.teiid.designer.runtime.DqpPlugin.Util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.xml.stream.XMLStreamException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.osgi.util.NLS;
import org.teiid.adminapi.Admin;
import org.teiid.adminapi.AdminException;
import org.teiid.adminapi.PropertyDefinition;
import org.teiid.adminapi.Translator;
import org.teiid.adminapi.VDB;
Expand All @@ -44,6 +45,7 @@
import org.teiid.designer.runtime.adapter.TeiidServerAdapterUtil;
import org.teiid.designer.runtime.connection.IPasswordProvider;
import org.teiid.designer.runtime.connection.ModelConnectionMatcher;
import org.teiid.designer.runtime.preview.Messages;
import org.teiid.designer.vdb.Vdb;


Expand Down Expand Up @@ -309,29 +311,38 @@ public TeiidDataSource getOrCreateDataSource( String displayName,

// For JDBC types, find the matching installed driver. This is done currently by matching
// the profile driver classname to the installed driver classname
if("connector-jdbc".equals(typeName)) {
// Get the driver class defined on the connection profile
String connProfileDriverClass = properties.getProperty("driver-class");
String connProfileDriverClass = properties.getProperty("driver-class"); //$NON-NLS-1$
if("connector-jdbc".equals(typeName)) { //$NON-NLS-1$
// List of driver jars on the connection profile
String jarList = properties.getProperty("jarList"); //$NON-NLS-1$

// Get the installed JDBC Driver mappings
Map<String,String> dsNameToDriverMap = TeiidServerAdapterUtil.getInstalledJDBCDriverMap(teiidServer.getParent());
// Get first driver name with the driver class that matches the connection profile
String dsNameMatch = getDSMatchForDriverClass(teiidServer,connProfileDriverClass);

// Use the first driver name with driver class that matches connection profile
Set<String> keySet = dsNameToDriverMap.keySet();
String dsNameMatch = null;
for(String dsName: keySet) {
String dsDriverClass = dsNameToDriverMap.get(dsName);
if(connProfileDriverClass.equalsIgnoreCase(dsDriverClass)) {
dsNameMatch=dsName;
break;
// If a matching datasource was found, set typename
if(dsNameMatch!=null) {
typeName=dsNameMatch;
// No matching datasource, attempt to deploy the driver if jarList is populated.
} else if(jarList!=null && jarList.trim().length()>0) {
// Try to deploy the jars
deployJars(this.admin,jarList);

refresh();

// Retry the name match after deployment.
dsNameMatch = getDSMatchForDriverClass(teiidServer,connProfileDriverClass);
if(dsNameMatch!=null) {
typeName=dsNameMatch;
}
}
// Change the typeName to the matched driver name
if(dsNameMatch!=null) typeName=dsNameMatch;
}
// Verify the "typeName" exists.
if (!this.dataSourceTypeNames.contains(typeName)) {
throw new Exception(Util.getString("dataSourceTypeDoesNotExist", typeName, getServer())); //$NON-NLS-1$
if("connector-jdbc".equals(typeName)) { //$NON-NLS-1$
throw new Exception(Util.getString("jdcbSourceForClassNameNotFound", connProfileDriverClass, getServer())); //$NON-NLS-1$
} else {
throw new Exception(Util.getString("dataSourceTypeDoesNotExist", typeName, getServer())); //$NON-NLS-1$
}
}

this.admin.createDataSource(jndiName, typeName, properties);
Expand All @@ -353,6 +364,73 @@ public TeiidDataSource getOrCreateDataSource( String displayName,
throw new Exception(Util.getString("errorCreatingDataSource", jndiName, typeName, getServer())); //$NON-NLS-1$
}

/*
* Look for an installed driver that has the driverClass which matches the supplied driverClass name.
* @param teiidServer the TeiidServer instance
* @param driverClass the driver class to match
* @return the name of the matching driver, null if not found
*/
private String getDSMatchForDriverClass(TeiidServer teiidServer, String driverClass) throws Exception {
String dsNameMatch = null;

if(teiidServer!=null && driverClass!=null) {
// Get the installed JDBC Driver mappings
Map<String,String> dsNameToDriverMap = TeiidServerAdapterUtil.getInstalledJDBCDriverMap(teiidServer.getParent());

// Use the first driver name with driver class that matches connection profile
Set<String> keySet = dsNameToDriverMap.keySet();
for(String dsName: keySet) {
String dsDriverClass = dsNameToDriverMap.get(dsName);
if(driverClass.equalsIgnoreCase(dsDriverClass)) {
dsNameMatch=dsName;
break;
}
}
}

return dsNameMatch;
}

/*
* Deploy all jars in the supplied jarList
* @param admin the Admin instance
* @param jarList the colon-separated list of jar path locations
*/
private void deployJars(Admin admin, String jarList) {
// Path Entries are colon separated
String[] jarPathStrs = jarList.split("[:]"); //$NON-NLS-1$

// Attempt to deploy each jar
for(String jarPathStr: jarPathStrs) {
File theFile = new File(jarPathStr);
if(theFile.exists()) {
if(theFile.canRead()) {
String fileName = theFile.getName();
InputStream iStream = null;
try {
iStream = new FileInputStream(theFile);
} catch (FileNotFoundException ex) {
Util.log(IStatus.ERROR, NLS.bind(Messages.JarDeploymentJarNotFound, theFile.getPath()));
continue;
}
try {
admin.deploy(fileName, iStream);
} catch (AdminException ex) {
// Jar deployment failed
Util.log(IStatus.ERROR, ex, NLS.bind(Messages.JarDeploymentFailed, theFile.getPath()));
}
} else {
// Could not read the file
Util.log(IStatus.ERROR, NLS.bind(Messages.JarDeploymentJarNotReadable, theFile.getPath()));
}
} else {
// The file was not found
Util.log(IStatus.ERROR, NLS.bind(Messages.JarDeploymentJarNotFound, theFile.getPath()));
}

}
}

/**
* @return the server who owns this admin object (never <code>null</code>)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ missingPropertyDefinition = No property definition found for property "{0}."
unknownPropertyType = Property "{0}" has an unknown type of "{1}."

dataSourceTypeDoesNotExist= Data Source Type {0} does not exist on server {1}
jdcbSourceForClassNameNotFound = JDBC Source for Driver class {0} was not found on server {1}

errorCreatingDataSource= Data Source {0} could not be created for type {1} on server {2}

errorFindingModelResourceForModelFile=Error finding Model Resource for model file {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class Messages extends NLS {
public static String UpdatePreviewVdbJob;
public static String UpdatePreviewVdbJobError;
public static String UpdatePreviewVdbJobSuccessfullyCompleted;
public static String JarDeploymentJarNotFound;
public static String JarDeploymentFailed;
public static String JarDeploymentJarNotReadable;

static {
NLS.initializeMessages("org.teiid.designer.runtime.preview.messages", Messages.class); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ UnexpectedErrorRunningJob = An unexpected error occurred running the {0} job.
UpdatePreviewVdbJob = Update Workspace Preview VDB for "{0}"
UpdatePreviewVdbJobError = An error synchronizing and saving the Preview VDB for model "{0}" occurred.
UpdatePreviewVdbJobSuccessfullyCompleted = The "{0}" Preview VDB was successfully synchronized and saved.
JarDeploymentJarNotFound = The driver jar was not found: "{0}"
JarDeploymentFailed = The driver jar deployment failed: "{0}"
JarDeploymentJarNotReadable = The driver jar is not readable: "{0}"
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class JDBCConnectionInfoProvider extends ConnectionInfoHelper implements
String URL = "connection-url"; //$NON-NLS-1$
String USERNAME = "user-name"; //$NON-NLS-1$
String PASSWORD = "password"; //$NON-NLS-1$
String JARLIST = "jarList"; //$NON-NLS-1$
String UNKNOWN = "unknown"; //$NON-NLS-1$

/**
Expand Down Expand Up @@ -166,6 +167,10 @@ public Properties getTeiidRelatedProperties( IConnectionProfile connectionProfil
connectionProps.put(PASSWORD, baseProps.get(PASSWORD_KEY));
}

if (baseProps.get(JARLIST) != null) {
connectionProps.put(JARLIST, baseProps.get(JARLIST));
}

return connectionProps;
}

Expand Down

0 comments on commit e94f5a9

Please sign in to comment.