Skip to content

Commit

Permalink
Add version of TeiidURL to designer
Browse files Browse the repository at this point in the history
* Class taken from teiid codebase

* TeiidURL used in limited locations but necessary but not important enough
  to force dependency of teiid plugin.
  • Loading branch information
Paul Richardson committed Nov 15, 2012
1 parent 7ba9c9e commit 9fd6f7d
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@
import org.eclipse.wst.server.core.ServerEvent;
import org.eclipse.wst.server.core.util.ServerLifecycleAdapter;
import org.jboss.ide.eclipse.as.core.server.internal.v7.JBoss7Server;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.Base64;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.datatools.connectivity.security.ISecureStorageProvider;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.runtime.adapter.TeiidServerAdapterFactory;
import org.teiid.designer.runtime.adapter.TeiidServerAdapterFactory.ServerOptions;
import org.teiid.designer.runtime.connection.IPasswordProvider;
import org.teiid.designer.runtime.preview.PreviewManager;
import org.teiid.net.TeiidURL;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -785,6 +784,7 @@ public IStatus restoreState() {
}

TeiidURL jdbcURL = new TeiidURL(urlNode.getNodeValue());

Node userNode = serverAttributeMap.getNamedItem(USER_ATTR);
Node passwordNode = serverAttributeMap.getNamedItem(PASSWORD_ATTR);
String pswd = ((passwordNode == null) ? null : new String(Base64.decode(passwordNode.getNodeValue()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import java.net.MalformedURLException;
import org.teiid.core.designer.PluginUtil;
import org.teiid.net.TeiidURL;

/**
*
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
/*
* 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 java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

/**
* Class defines the URL in the Teiid Server.
*
* @since 8.0
*/
public class TeiidURL {

/**
* Non-secure Protocol.
*/
private static final String NON_SECURE_PROTOCOL = "mm"; //$NON-NLS-1$

/**
* Secure Protocol.
*/
private static final String SECURE_PROTOCOL = "mms"; //$NON-NLS-1$

private static final String DOUBLE_SLASH_DELIMITER = "//"; //$NON-NLS-1$

private static final String COMMA_DELIMITER = ","; //$NON-NLS-1$

private static final String COLON_DELIMITER = ":"; //$NON-NLS-1$

private static final String DEFAULT_PREFIX= NON_SECURE_PROTOCOL + COLON_DELIMITER + DOUBLE_SLASH_DELIMITER;

private static final String SECURE_PREFIX= SECURE_PROTOCOL + COLON_DELIMITER + DOUBLE_SLASH_DELIMITER;

private static final String INVALID_FORMAT_SERVER = DqpPlugin.Util.getString("TeiidURL.Invalid_format"); //$NON-NLS-1$

private class HostInfo {

private String host;
private int port;

public HostInfo(String host, int port) {
this.host = host;
this.port = port;
}

/**
* @return the host name
*/
public String getHostName() {
return host;
}

/**
* @return the port number
*/
public int getPortNumber() {
return port;
}
}

/*
* appserver URL
*/
private String appServerURL;
/*
* List of <code> HostData </code> in a cluster
*/
private List<HostInfo> hosts = new ArrayList<HostInfo>();

private boolean usingSSL = false;

/**
* Create an MMURL from the server URL. For use by the server-side.
* @param serverURL Expected format: mm[s]://server1:port1[,server2:port2]
* @throws MalformedURLException
* @since 4.2
*/
public TeiidURL(String serverURL) throws MalformedURLException {
if (serverURL == null) {
throw new MalformedURLException(INVALID_FORMAT_SERVER);
}
if (serverURL.toLowerCase().startsWith(SECURE_PREFIX)) {
usingSSL = true;
} else if (!serverURL.toLowerCase().startsWith(DEFAULT_PREFIX)) {
throw new MalformedURLException(INVALID_FORMAT_SERVER);
}

appServerURL = serverURL;
parseServerURL(serverURL.substring(usingSSL?SECURE_PREFIX.length():DEFAULT_PREFIX.length()), INVALID_FORMAT_SERVER);
}

/**
* Create new instance with given parameters
*
* @param host
* @param port
* @param secure
*/
public TeiidURL(String host, int port, boolean secure) {
usingSSL = secure;
if(host.startsWith("[")) { //$NON-NLS-1$
host = host.substring(1, host.indexOf(']'));
}
hosts.add(new HostInfo(host, port));
}

/**
* Validates that a server URL is in the correct format.
*
* @param serverURL Expected format: mm[s]://server1:port1[,server2:port2]
* @return true is URL is valid
*
* @since 4.2
*/
public static boolean isValidServerURL(String serverURL) {
boolean valid = true;
try {
new TeiidURL(serverURL);
} catch (Exception e) {
valid = false;
}

return valid;
}

/**
* Get a list of hosts
*
* @return string of host separated by commas
* @since 4.2
*/
public String getHosts() {
StringBuffer hostList = new StringBuffer(""); //$NON-NLS-1$
if( hosts != null) {
Iterator<HostInfo> iterator = hosts.iterator();
while (iterator.hasNext()) {
HostInfo element = iterator.next();
hostList.append(element.getHostName());
if( iterator.hasNext()) {
hostList.append(COMMA_DELIMITER);
}
}
}
return hostList.toString();
}

/**
* Get a list of ports
*
* @return string of ports seperated by commas
* @since 4.2
*/
public String getPorts() {
StringBuffer portList = new StringBuffer(""); //$NON-NLS-1$
if( hosts != null) {
Iterator<HostInfo> iterator = hosts.iterator();
while (iterator.hasNext()) {
HostInfo element = iterator.next();
portList.append(element.getPortNumber());
if( iterator.hasNext()) {
portList.append(COMMA_DELIMITER);
}
}
}
return portList.toString();
}

/**
* @param url
* @throws MalformedURLException
* @since 4.2
*/
private void parseServerURL(String serverURL, String exceptionMessage) throws MalformedURLException {
StringTokenizer st = new StringTokenizer(serverURL, COMMA_DELIMITER);
if (!st.hasMoreTokens()) {
throw new MalformedURLException(exceptionMessage);
}
while (st.hasMoreTokens()) {
String nextToken = st.nextToken();
nextToken = nextToken.trim();
String host = ""; //$NON-NLS-1$
String port = ""; //$NON-NLS-1$
if (nextToken.startsWith("[")) { //$NON-NLS-1$
int hostEnd = nextToken.indexOf("]:"); //$NON-NLS-1$
if (hostEnd == -1) {
throw new MalformedURLException(DqpPlugin.Util.getString("TeiidURL.invalid_ipv6_hostport", nextToken, exceptionMessage)); //$NON-NLS-1$
}
host = nextToken.substring(1, hostEnd);
port = nextToken.substring(hostEnd+2);
}
else {
int hostEnd = nextToken.indexOf(":"); //$NON-NLS-1$
if (hostEnd == -1) {
throw new MalformedURLException(DqpPlugin.Util.getString("TeiidURL.invalid_hostport", nextToken, exceptionMessage)); //$NON-NLS-1$
}
host = nextToken.substring(0, hostEnd);
port = nextToken.substring(hostEnd+1);
}
host = host.trim();
port = port.trim();
if (host.equals("") || port.equals("")) { //$NON-NLS-1$ //$NON-NLS-2$
throw new MalformedURLException(DqpPlugin.Util.getString("TeiidURL.invalid_hostport", nextToken, exceptionMessage)); //$NON-NLS-1$
}
int portNumber = validatePort(port);
HostInfo hostInfo = new HostInfo(host, portNumber);
hosts.add(hostInfo);
}
}

private int validatePort(String port) throws MalformedURLException {
int portNumber;
try {
portNumber = Integer.parseInt(port);
} catch (NumberFormatException nfe) {
throw new MalformedURLException(DqpPlugin.Util.getString("TeiidURL.non_numeric_port", port)); //$NON-NLS-1$
}

if (portNumber < 0 || portNumber > 0xFFFF) {
String msg = DqpPlugin.Util.getString("TeiidURL.port_out_of_range", portNumber); //$NON-NLS-1$
throw new MalformedURLException(msg);
}
return portNumber;
}

/**
* Get the Application Server URL
*
* @return String for connection to the Server
* @since 4.2
*/
public String getAppServerURL() {
if (appServerURL == null) {
StringBuffer sb = new StringBuffer();
if (usingSSL) {
sb.append(SECURE_PREFIX);
} else {
sb.append(DEFAULT_PREFIX);
}
Iterator<HostInfo> iter = hosts.iterator();
while (iter.hasNext()) {
HostInfo host = iter.next();

boolean ipv6HostName = host.getHostName().indexOf(':') != -1;
if (ipv6HostName) {
sb.append('[');
}
sb.append(host.getHostName());
if (ipv6HostName) {
sb.append(']');
}
sb.append(COLON_DELIMITER);
sb.append(host.getPortNumber());
if (iter.hasNext()) {
sb.append(COMMA_DELIMITER);
}
}
appServerURL = sb.toString();
}
return appServerURL;
}

/**
* @see java.lang.Object#toString()
* @since 4.2
*/
@Override
public String toString() {
return getAppServerURL();
}

/**
* @see java.lang.Object#equals(java.lang.Object)
* @since 4.2
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TeiidURL)) {
return false;
}
TeiidURL url = (TeiidURL)obj;
return (appServerURL.equals(url.getAppServerURL()));
}

/**
* @see java.lang.Object#hashCode()
* @since 4.2
*/
@Override
public int hashCode() {
return appServerURL.hashCode();
}

/**
* @return whether url is using SSL
*/
public boolean isUsingSSL() {
return usingSSL;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,9 @@ jbossServerConnectionFailureMessage = Failed to connect to the jboss server at {

# OrphanedTeiidServerException
OrphanedTeiidServerException.message = The Teiid Server {0} does not have a parent jboss server

TeiidURL.Invalid_format=The required socket url format is mm[s]://server1:port1[,server2:port2]
TeiidURL.invalid_ipv6_hostport=The IPv6 host:port ''{0}'' is not valid. {1}
TeiidURL.invalid_hostport=The host:port ''{0}'' is not valid. {1}
TeiidURL.non_numeric_port=The port ''{0}'' is a non-numeric value.
TeiidURL.port_out_of_range=The port ''{0}'' is out of range.

0 comments on commit 9fd6f7d

Please sign in to comment.