Skip to content

Commit

Permalink
TEIIDDES-1518: Remove references to AdminApi
Browse files Browse the repository at this point in the history
* ExecutionAdmin
 * Confine the admin field
 * Add additional API methods so the teiid server (and other classes) can
   delegate appropriately
  • Loading branch information
Paul Richardson committed Dec 7, 2012
1 parent 0cba990 commit 0e0c906
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
*/
package org.teiid.designer.runtime;

import static org.teiid.designer.runtime.DqpPlugin.PLUGIN_ID;
import static org.teiid.designer.runtime.DqpPlugin.Util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -29,6 +33,8 @@
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.AdminFactory;
import org.teiid.adminapi.PropertyDefinition;
import org.teiid.adminapi.Translator;
import org.teiid.adminapi.VDB;
Expand All @@ -42,6 +48,7 @@
import org.teiid.designer.runtime.connection.ModelConnectionMatcher;
import org.teiid.designer.runtime.preview.Messages;
import org.teiid.designer.vdb.Vdb;
import org.teiid.jdbc.TeiidDriver;


/**
Expand All @@ -64,22 +71,54 @@ public class ExecutionAdmin {

private boolean loaded = false;

public enum PingType {
ADMIN, JDBC;
}

/**
* Constructor used for testing purposes only.
*
* DO NOT MAKE PUBLIC!!
*
* @param admin the associated Teiid Admin API (never <code>null</code>)
* @param teiidServer the server this admin belongs to (never <code>null</code>)
* @param eventManager the event manager used to fire events (never <code>null</code>)
* @throws Exception if there is a problem connecting the server
*/
public ExecutionAdmin( Admin admin,
TeiidServer teiidServer,
EventManager eventManager ) throws Exception {
protected ExecutionAdmin(Admin admin, TeiidServer teiidServer) throws Exception {
CoreArgCheck.isNotNull(admin, "admin"); //$NON-NLS-1$
CoreArgCheck.isNotNull(teiidServer, "server"); //$NON-NLS-1$
CoreArgCheck.isNotNull(eventManager, "eventManager"); //$NON-NLS-1$


this.admin = admin;
this.eventManager = eventManager;
this.teiidServer = teiidServer;
this.eventManager = teiidServer.getEventManager();
this.connectionMatcher = new ModelConnectionMatcher();

init();
}

/**
* Default Constructor
*
* @param teiidServer the server this admin belongs to (never <code>null</code>)
*
* @throws Exception if there is a problem connecting the server
*/
public ExecutionAdmin(TeiidServer teiidServer) throws Exception {
CoreArgCheck.isNotNull(teiidServer, "server"); //$NON-NLS-1$

TeiidAdminInfo teiidAdminInfo = teiidServer.getTeiidAdminInfo();
char[] passwordArray = null;
if (teiidAdminInfo.getPassword() != null) {
passwordArray = teiidAdminInfo.getPassword().toCharArray();
}

this.admin = AdminFactory.getInstance().createAdmin( teiidServer.getHost(),
teiidAdminInfo.getPortNumber(),
teiidAdminInfo.getUsername(),
passwordArray);

this.teiidServer = teiidServer;
this.eventManager = teiidServer.getEventManager();
this.connectionMatcher = new ModelConnectionMatcher();

init();
Expand Down Expand Up @@ -193,11 +232,6 @@ public void disconnect() {
this.dataSourceTypeNames = new HashSet<String>();
this.teiidVdbs = Collections.emptySet();
}

@SuppressWarnings("javadoc")
public Admin getAdminApi() {
return this.admin;
}

/**
* Returns a teiid data source object if it exists in this server
Expand Down Expand Up @@ -527,6 +561,10 @@ public void load() throws Exception {
this.loaded = true;
}
}

public void close() {
admin.close();
}

/**
* Refreshes the cached lists and maps of current Teiid objects
Expand Down Expand Up @@ -626,14 +664,31 @@ public void setPropertyValue( TeiidTranslator translator,
internalSetPropertyValue(translator, propName, value, true);
}

/**
*
* @param vdbName the vdb name
* @throws Exception if undeploying vdb fails
*/
public void undeployVdb( String vdbName) throws Exception {
this.admin.undeploy(appendVdbExtension(vdbName));
VDB vdb = getVdb(vdbName);

refreshVDBs();

if (vdb == null) {

} else {
this.eventManager.notifyListeners(ExecutionConfigurationEvent.createUnDeployVDBEvent(vdb));
}
}

/**
*
* @param vdbName the vdb name
* @param vdbVersion the vdb version
* @throws Exception if undeploying vdb fails
*/
public void undeployVdb( String vdbName,
int vdbVersion ) throws Exception {
public void undeployVdb( String vdbName, int vdbVersion ) throws Exception {
this.admin.undeploy(appendVdbExtension(vdbName));
VDB vdb = getVdb(vdbName);

Expand Down Expand Up @@ -675,6 +730,71 @@ private String appendVdbExtension(String vdbName) {
return vdbName + Vdb.FILE_EXTENSION;
}

/**
* Ping the admin client to determine whether if is still connected
*
* @return
*/
public IStatus ping(PingType pingType) {
String msg = Util.getString("cannotConnectToServer", teiidServer.getTeiidAdminInfo().getUsername()); //$NON-NLS-1$
try {
if (this.admin == null)
throw new Exception(msg);

switch(pingType) {
case JDBC:
return pingJdbc();
case ADMIN:
default:
return pingAdmin();
}
}
catch (Exception ex) {
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
}
}

private IStatus pingAdmin() throws Exception {
admin.getSessions();
return Status.OK_STATUS;
}

private IStatus pingJdbc() {
String host = teiidServer.getHost();
TeiidJdbcInfo teiidJdbcInfo = teiidServer.getTeiidJdbcInfo();

Connection teiidJdbcConnection = null;
String url = "jdbc:teiid:ping@mm://" + host + ':' + teiidJdbcInfo.getPort(); //$NON-NLS-1$

try {
admin.deploy("ping-vdb.xml", new ByteArrayInputStream(TeiidServerUtils.TEST_VDB.getBytes())); //$NON-NLS-1$

try{
String urlAndCredentials = url + ";user=" + teiidJdbcInfo.getUsername() + ";password=" + teiidJdbcInfo.getPassword() + ';'; //$NON-NLS-1$ //$NON-NLS-2$
teiidJdbcConnection = TeiidDriver.getInstance().connect(urlAndCredentials, null);
//pass
} catch(SQLException ex){
String msg = Util.getString("serverDeployUndeployProblemPingingTeiidJdbc", url); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
} finally {
admin.undeploy("ping-vdb.xml"); //$NON-NLS-1$

if( teiidJdbcConnection != null ) {
teiidJdbcConnection.close();
}
admin.close();
}
} catch (AdminException ex) {
String msg = Util.getString("serverDeployUndeployProblemPingingTeiidJdbc", url); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
} catch (Exception ex) {
String msg = Util.getString("serverDeployUndeployProblemPingingTeiidJdbc", url); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
}

return Status.OK_STATUS;
}

/**
* Executes VDB refresh when a VDB is loading - as a background job.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

import static org.teiid.designer.runtime.DqpPlugin.PLUGIN_ID;
import static org.teiid.designer.runtime.DqpPlugin.Util;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.server.core.IServer;
import org.teiid.adminapi.Admin;
import org.teiid.adminapi.AdminFactory;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.jdbc.TeiidDriver;
import org.teiid.designer.runtime.ExecutionAdmin.PingType;


/**
Expand Down Expand Up @@ -145,13 +140,9 @@ public TeiidServer( String host,
*/
public void close() {
if (this.admin != null) {
Admin adminApi = this.admin.getAdminApi();
if (adminApi != null) {
adminApi.close();
}
this.admin.close();
this.admin = null;
}
// System.out.println(" >>>> Server.close() CLOSED Server = " + getUrl());
}

/**
Expand Down Expand Up @@ -207,13 +198,8 @@ public ExecutionAdmin getAdmin() throws Exception {
}

if (this.admin == null) {
char[] pwd = null;
if (getTeiidAdminInfo().getPassword() != null) {
pwd = getTeiidAdminInfo().getPassword().toCharArray();
}
this.admin = new ExecutionAdmin(AdminFactory.getInstance().createAdmin(getHost(), getTeiidAdminInfo().getPortNumber(), getTeiidAdminInfo().getUsername(), pwd),
this,
this.eventManager);

this.admin = new ExecutionAdmin(this);
this.admin.load();
notifyRefresh();
}
Expand Down Expand Up @@ -255,6 +241,10 @@ public TeiidJdbcInfo getTeiidJdbcInfo() {
return teiidJdbcInfo;
}

public EventManager getEventManager() {
return eventManager;
}

/**
* An appropriate name for this teiid server
*
Expand Down Expand Up @@ -365,7 +355,7 @@ public IStatus ping() {
if (! isParentConnected() && this.admin == null)
throw new Exception(msg);

getAdmin().getAdminApi().getSessions();
admin.ping(PingType.ADMIN);
} catch (Exception e) {
return new Status(IStatus.ERROR, PLUGIN_ID, msg, e);
}
Expand Down Expand Up @@ -408,8 +398,9 @@ public void setCustomLabel( String customLabel ) {
*/
public IStatus testPing() {
try {
Admin adminApi = getAdmin().getAdminApi();
adminApi.close();
getAdmin();
ping();
close();
this.admin = null;
} catch (Exception e) {
String msg = Util.getString("cannotConnectToServer", this); //$NON-NLS-1$
Expand All @@ -430,35 +421,16 @@ public IStatus testPing() {
* @return status as to the ping's success
*/
public IStatus testJDBCPing(String host, String port, String username, String password) {
Connection teiidJdbcConnection = null;
String url = "jdbc:teiid:ping@mm://"+host+':'+port; //$NON-NLS-1$

try {
Admin adminApi = getAdmin().getAdminApi();
adminApi.deploy("ping-vdb.xml", new ByteArrayInputStream(TeiidServerUtils.TEST_VDB.getBytes())); //$NON-NLS-1$

try{
String urlAndCredentials = url + ";user="+username+";password="+password+';'; //$NON-NLS-1$ //$NON-NLS-2$
teiidJdbcConnection = TeiidDriver.getInstance().connect(urlAndCredentials, null);
//pass
} catch(SQLException ex){
String msg = Util.getString("serverDeployUndeployProblemPingingTeiidJdbc", url); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
} finally {
adminApi.undeploy("ping-vdb.xml"); //$NON-NLS-1$

if( teiidJdbcConnection != null ) {
teiidJdbcConnection.close();
}
adminApi.close();
this.admin = null;
}
} catch (Exception ex) {
String msg = Util.getString("serverDeployUndeployProblemPingingTeiidJdbc", url); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, ex);
}

return Status.OK_STATUS;
try {
getAdmin().ping(PingType.JDBC);
close();
this.admin = null;
} catch (Exception e) {
String msg = Util.getString("cannotConnectToServer", this); //$NON-NLS-1$
return new Status(IStatus.ERROR, PLUGIN_ID, msg, e);
}

return Status.OK_STATUS;
}

public String getVdbDataSourceConnectionUrl(String vdbName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ public void previewSetup( final Object objectToPreview,

if( deployedProjectVdb != null ) {
String fullProjectVdbName = getFullDeployedVdbName(deployedProjectVdb);
admin.getAdminApi().undeploy(fullProjectVdbName);
admin.undeployVdb(fullProjectVdbName);
}
localProjectVdb.save(null);
localProjectVdb.getFile().refreshLocal(IResource.DEPTH_INFINITE, null);
Expand Down

0 comments on commit 0e0c906

Please sign in to comment.