Skip to content

Commit

Permalink
TEIIDDES-1459: parent of teiid server should be provided
Browse files Browse the repository at this point in the history
* TeiidServer
 * In order for the unit tests to work correctly, the parent of the teiid
   server should be provided to the constructor rather than the latter
   trying to initialise it itself.
 * If the parent server is null then an illegal argument exception is thrown
   like the other parameters
 * The hashcode() and equals() methods have been modified to be the eclipse
   versions
 * Setters have been removed for the fields involved in the equals() and
   hashcode() methods since these fields should be immutable
  • Loading branch information
Paul Richardson committed Sep 5, 2012
1 parent 03621b3 commit d202626
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

import static org.teiid.designer.runtime.ui.DqpUiConstants.PLUGIN_ID;
import static org.teiid.designer.runtime.ui.DqpUiConstants.UTIL;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
Expand All @@ -39,10 +37,10 @@
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.runtime.EventManager;
import org.teiid.designer.runtime.HostProvider;
import org.teiid.designer.runtime.TeiidServer;
import org.teiid.designer.runtime.TeiidServerManager;
import org.teiid.designer.runtime.TeiidAdminInfo;
import org.teiid.designer.runtime.TeiidJdbcInfo;
import org.teiid.designer.runtime.TeiidServer;
import org.teiid.designer.runtime.TeiidServerManager;
import org.teiid.designer.runtime.ui.DqpUiConstants;
import org.teiid.designer.runtime.ui.DqpUiPlugin;
import org.teiid.designer.ui.common.util.WidgetFactory;
Expand Down Expand Up @@ -202,7 +200,7 @@ public ServerPage() {
this.jdbcURLIsSecure);
this.localJdbcInfo.setHostProvider(this);

this.teiidServer = new TeiidServer(null, this.localAdminInfo, this.localJdbcInfo, EventManager.EVENT_MANAGER_ADAPTER);
this.teiidServer = new TeiidServer(null, this.localAdminInfo, this.localJdbcInfo, EventManager.EVENT_MANAGER_ADAPTER, null);
}

/**
Expand Down Expand Up @@ -721,7 +719,7 @@ public String getHost() {
*/
public TeiidServer getServer() {
if (this.status.getSeverity() != IStatus.ERROR) {
TeiidServer newServer = new TeiidServer(this.host, this.localAdminInfo, this.localJdbcInfo, getServerManager());
TeiidServer newServer = new TeiidServer(this.host, this.localAdminInfo, this.localJdbcInfo, getServerManager(), null);
newServer.setCustomLabel(this.displayName);
return newServer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.runtime;

import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.ServerCore;

/**
* Exception thrown if a {@link TeiidServer} is constructed but a related
* {@link IServer} cannot be found in the {@link ServerCore#getServers()}
* collection.
*
* @since 8.0
*/
public class OrphanedTeiidServerException extends Exception {

/**
*
*/
private static final long serialVersionUID = 1L;

private String teiidServerUrl;

/**
* Create a new instance
*
* @param teiidAdminInfo
*/
public OrphanedTeiidServerException(TeiidAdminInfo teiidAdminInfo) {
this.teiidServerUrl = teiidAdminInfo.getUrl();
}

/* (non-Javadoc)
* @see java.lang.Throwable#getMessage()
*/
@Override
public String getMessage() {
return DqpPlugin.Util.getString(getClass().getSimpleName() + ".message", teiidServerUrl); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.IServerListener;
import org.eclipse.wst.server.core.ServerCore;
import org.eclipse.wst.server.core.ServerEvent;
import org.jboss.ide.eclipse.as.core.server.internal.v7.JBoss7Server;
import org.teiid.adminapi.Admin;
import org.teiid.adminapi.AdminException;
import org.teiid.adminapi.AdminFactory;
import org.teiid.core.util.CoreArgCheck;
import org.teiid.core.util.HashCodeUtil;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.runtime.adapter.TeiidServerAdapterUtil;
import org.teiid.jdbc.TeiidDriver;
Expand Down Expand Up @@ -140,15 +137,18 @@ public void serverChanged(ServerEvent event) {
* @param adminInfo the server admin connection properties (never <code>null</code>)
* @param jdbcInfo the server JDBC connection properties (never <code>null</code>)
* @param eventManager the event manager (never <code>null</code>)
* @param parentServer the parent {@link IServer} (never <code>null</code>)
* @throws IllegalArgumentException if any of the parameters are <code>null</code>
*/
public TeiidServer( String host,
TeiidAdminInfo adminInfo,
TeiidJdbcInfo jdbcInfo,
EventManager eventManager ) {
EventManager eventManager,
IServer parentServer) {
CoreArgCheck.isNotNull(adminInfo, "adminInfo"); //$NON-NLS-1$
CoreArgCheck.isNotNull(jdbcInfo, "jdbcInfo"); //$NON-NLS-1$
CoreArgCheck.isNotNull(eventManager, "eventManager"); //$NON-NLS-1$
CoreArgCheck.isNotNull(parentServer, "parentServer"); //$NON-NLS-1$

this.host = host;

Expand All @@ -159,40 +159,17 @@ public TeiidServer( String host,
this.teiidJdbcInfo.setHostProvider(this);

this.eventManager = eventManager;
this.parentServer = parentServer;

initParent();
if (parentServer.getServerState() != IServer.STATE_STARTED)
disconnect();

parentServer.addServerListener(serverListener);
}

// ===========================================================================================================================
// Methods
// ===========================================================================================================================

private void initParent() {
IServer[] servers = ServerCore.getServers();
for (IServer server : servers) {
if (! getHost().equals(server.getHost()))
continue;

JBoss7Server jb7 = (JBoss7Server) server.loadAdapter(JBoss7Server.class, null);
if (jb7 == null)
continue;

if (getTeiidAdminInfo().getPortNumber() != jb7.getManagementPort())
continue;

// The host and admin port match so must be the same server
parentServer = server;

if (parentServer.getServerState() != IServer.STATE_STARTED)
disconnect();

parentServer.addServerListener(serverListener);

return;
}

DqpPlugin.Util.log(IStatus.WARNING, DqpPlugin.Util.getString("parentServerFailureMessage", this)); //$NON-NLS-1$
}

/**
* Perform cleanup
Expand Down Expand Up @@ -221,34 +198,31 @@ public void disconnect() {
}
}

/**
* {@inheritDoc}
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals( Object obj ) {
if (this == obj) {
return true;
}

if ((obj == null) || (getClass() != obj.getClass())) {
return false;
}

TeiidServer otherServer = (TeiidServer) obj;

if (!getTeiidAdminInfo().equals(otherServer.getTeiidAdminInfo())) {
return false;
}

if (!getTeiidJdbcInfo().equals(otherServer.getTeiidJdbcInfo())) {
return false;
}

return equivalent(getHost(), otherServer.getHost()) && equivalent(getCustomLabel(), otherServer.getCustomLabel())
&& getTeiidAdminInfo().equals(otherServer.getTeiidAdminInfo())
&& getTeiidJdbcInfo().equals(otherServer.getTeiidJdbcInfo());
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
TeiidServer other = (TeiidServer)obj;
if (this.admin == null) {
if (other.admin != null) return false;
} else if (!this.admin.equals(other.admin)) return false;
if (this.eventManager == null) {
if (other.eventManager != null) return false;
} else if (!this.eventManager.equals(other.eventManager)) return false;
if (this.host == null) {
if (other.host != null) return false;
} else if (!this.host.equals(other.host)) return false;
if (this.parentServer == null) {
if (other.parentServer != null) return false;
} else if (!this.parentServer.equals(other.parentServer)) return false;
if (this.teiidAdminInfo == null) {
if (other.teiidAdminInfo != null) return false;
} else if (!this.teiidAdminInfo.equals(other.teiidAdminInfo)) return false;
if (this.teiidJdbcInfo == null) {
if (other.teiidJdbcInfo != null) return false;
} else if (!this.teiidJdbcInfo.equals(other.teiidJdbcInfo)) return false;
return true;
}

public ExecutionAdmin getAdmin() throws Exception {
Expand Down Expand Up @@ -327,14 +301,17 @@ public IServer getParent() {
}


/**
* {@inheritDoc}
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return HashCodeUtil.hashCode(0, getHost(), getCustomLabel(), getTeiidAdminInfo(), getTeiidJdbcInfo());
final int prime = 31;
int result = 1;
result = prime * result + ((this.admin == null) ? 0 : this.admin.hashCode());
result = prime * result + ((this.eventManager == null) ? 0 : this.eventManager.hashCode());
result = prime * result + ((this.host == null) ? 0 : this.host.hashCode());
result = prime * result + ((this.parentServer == null) ? 0 : this.parentServer.hashCode());
result = prime * result + ((this.teiidAdminInfo == null) ? 0 : this.teiidAdminInfo.hashCode());
result = prime * result + ((this.teiidJdbcInfo == null) ? 0 : this.teiidJdbcInfo.hashCode());
return result;
}

/**
Expand Down Expand Up @@ -399,21 +376,6 @@ public void setCustomLabel( String customLabel ) {
this.customLabel = StringUtilities.isEmpty(customLabel) ? null : customLabel;
}

/**
* @param host the new host value (<code>null</code> if default host should be used)
*/
public void setHost( String host ) {
this.host = host;
}

public void setTeiidAdminInfo( TeiidAdminInfo adminInfo ) {
this.teiidAdminInfo = adminInfo;
}

public void setTeiidJdbcInfo( TeiidJdbcInfo jdbcInfo ) {
this.teiidJdbcInfo = jdbcInfo;
}

/**
* Attempts to establish communication with the specified server for testing purposes only.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.server.core.IServer;
import org.eclipse.wst.server.core.ServerCore;
import org.jboss.ide.eclipse.as.core.server.internal.v7.JBoss7Server;
import org.teiid.core.util.Base64;
import org.teiid.core.util.CoreArgCheck;
import org.teiid.designer.core.ModelerCore;
Expand Down Expand Up @@ -633,7 +636,8 @@ public IStatus restoreState() {
}

// add server to registry
TeiidServer teiidServer = new TeiidServer(host, teiidAdminInfo, teiidJdbcInfo, this);
IServer parentServer = findParentServer(host, teiidAdminInfo);
TeiidServer teiidServer = new TeiidServer(host, teiidAdminInfo, teiidJdbcInfo, this, parentServer);
teiidServer.setCustomLabel(customLabel);

addServer(teiidServer);
Expand All @@ -653,6 +657,26 @@ public IStatus restoreState() {
// do nothing of there is no save location or state file does not exist
return Status.OK_STATUS;
}

private IServer findParentServer(String host, TeiidAdminInfo teiidAdminInfo) throws OrphanedTeiidServerException {
IServer[] servers = ServerCore.getServers();
for (IServer server : servers) {
if (! host.equals(server.getHost()))
continue;

JBoss7Server jb7 = (JBoss7Server) server.loadAdapter(JBoss7Server.class, null);
if (jb7 == null)
continue;

if (teiidAdminInfo.getPortNumber() != jb7.getManagementPort())
continue;

// The host and admin port match so must be the same server
return server;
}

throw new OrphanedTeiidServerException(teiidAdminInfo);
}

/**
* @param teiidServer Sets defaultServer to the specified value. May be null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public String getHost() {
true,
false);

TeiidServer teiidServer = new TeiidServer(jb7.getHost(), teiidAdminInfo, teiidJdbcInfo, serverManager);
TeiidServer teiidServer = new TeiidServer(jb7.getHost(), teiidAdminInfo, teiidJdbcInfo, serverManager, jb7.getServer());

// Initialise the ExecutionAdmin component of the teiid server
teiidServer.getAdmin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ invalidServerUrl = Invalid {0} server URL: {1}
adminInfoType = Admin
jdbcInfoType = JDBC

# TeiidServerManager
parentServerFailureMessage = Failed to find a registered server parent for the Teiid Server {0}

# TeiidServerAdapterUtil
jbossServerNotStartedMessage = No Teiid Server found (server not started)
jbossServerConnectionFailureMessage = Failed to connect to the jboss server at {0}
jbossServerConnectionFailureMessage = Failed to connect to the jboss server at {0}

# OrphanedTeiidServerException
OrphanedTeiidServerException.message = The Teiid Server {0} does not have a parent jboss server
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.File;
import org.eclipse.wst.server.core.IServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -233,11 +234,12 @@ public void shouldRestoreServerRegistry() throws Exception {
String jdbcPassword = null;
boolean jdbcPersistPassword = false;
EventManager eventMgr = mock(EventManager.class);
IServer parentServer = mock(IServer.class);

// construct a server just to get its URL
TeiidAdminInfo adminInfo = new TeiidAdminInfo(adminPort, adminUser, adminPassword, adminPersistPassword, adminSecure);
TeiidJdbcInfo jdbcInfo = new TeiidJdbcInfo(jdbcPort, jdbcUser, jdbcPassword, jdbcPersistPassword, jdbcSecure);
TeiidServer testServer = new TeiidServer(null, adminInfo, jdbcInfo, eventMgr);
TeiidServer testServer = new TeiidServer(null, adminInfo, jdbcInfo, eventMgr, parentServer);
adminInfo.setHostProvider(testServer);
jdbcInfo.setHostProvider(testServer);

Expand Down Expand Up @@ -273,7 +275,7 @@ public void shouldRestoreServerRegistry() throws Exception {
// construct a server just to get its URL
adminInfo = new TeiidAdminInfo(adminPort, adminUser, adminPassword, adminPersistPassword, adminSecure);
jdbcInfo = new TeiidJdbcInfo(jdbcPort, jdbcUser, jdbcPassword, jdbcPersistPassword, jdbcSecure);
testServer = new TeiidServer(host, adminInfo, jdbcInfo, eventMgr);
testServer = new TeiidServer(host, adminInfo, jdbcInfo, eventMgr, parentServer);
adminInfo.setHostProvider(testServer);
jdbcInfo.setHostProvider(testServer);

Expand Down

0 comments on commit d202626

Please sign in to comment.