diff options
| author | Rajith Muditha Attapattu <rajith@apache.org> | 2007-08-17 23:32:53 +0000 |
|---|---|---|
| committer | Rajith Muditha Attapattu <rajith@apache.org> | 2007-08-17 23:32:53 +0000 |
| commit | 857c70fa8b6ca7e9a3b0544e1fd746fdf2752e9d (patch) | |
| tree | 6c06f3b46cd83c255dedb80473e5ba287a2a34a0 /java/common/src | |
| parent | d86b55a492b11a617e7032820bc5045f272eba7f (diff) | |
| download | qpid-python-857c70fa8b6ca7e9a3b0544e1fd746fdf2752e9d.tar.gz | |
wrote a parser for the new URL format
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@567174 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src')
3 files changed, 431 insertions, 15 deletions
diff --git a/java/common/src/main/java/org/apache/qpidity/BrokerDetails.java b/java/common/src/main/java/org/apache/qpidity/BrokerDetails.java index e1974f1859..3f39dfff41 100644 --- a/java/common/src/main/java/org/apache/qpidity/BrokerDetails.java +++ b/java/common/src/main/java/org/apache/qpidity/BrokerDetails.java @@ -17,6 +17,8 @@ */ package org.apache.qpidity; +import java.util.Map; + /** * This interface represents a broker and provides the basic information * required for opening a connection with a broker. @@ -28,6 +30,11 @@ public interface BrokerDetails */ public static final String PROTOCOL_TCP = "tcp"; public static final String PROTOCOL_TLS = "tls"; + + public static final String VIRTUAL_HOST = "virtualhost"; + public static final String CLIENT_ID = "client_id"; + public static final String USERNAME = "username"; + public static final String PASSWORD = "password"; /** * Get the broker host name. @@ -97,7 +104,7 @@ public interface BrokerDetails * * @param userName The user name */ - public void getUserName(String userName); + public void setUserName(String userName); /** * Set the user password @@ -112,4 +119,20 @@ public interface BrokerDetails * @param protocol the protocol used to connect to the broker. */ public void setProtocol(String protocol); + + /** + * Ex: keystore path + * + * @return the Properties associated with this connection. + */ + public Map<String,String> getProperties(); + + /** + * Sets the properties associated with this connection + * + * @param props + */ + public void setProperties(Map<String,String> props); + + public void setProperty(String key,String value); } diff --git a/java/common/src/main/java/org/apache/qpidity/BrokerDetailsImpl.java b/java/common/src/main/java/org/apache/qpidity/BrokerDetailsImpl.java index e5b5fb215b..2141886c4f 100644 --- a/java/common/src/main/java/org/apache/qpidity/BrokerDetailsImpl.java +++ b/java/common/src/main/java/org/apache/qpidity/BrokerDetailsImpl.java @@ -17,6 +17,9 @@ */ package org.apache.qpidity; +import java.util.HashMap; +import java.util.Map; + /** * Implements the interface BrokerDetails */ @@ -31,11 +34,17 @@ public class BrokerDetailsImpl implements BrokerDetails private String _host; private int _port; private String _virtualHost; - private String _userName; - private String _password; + private String _userName = DEFAULT_USERNAME; + private String _password = DEFAULT_PASSWORD; private String _protocol; + private Map<String,String> _props = new HashMap<String,String>();; + //--- Constructors + public BrokerDetailsImpl() + { + } + /** * Create a new broker details given all the reuqired information * @@ -47,7 +56,7 @@ public class BrokerDetailsImpl implements BrokerDetails * @param password The user password. */ public BrokerDetailsImpl(String protocol, String host, int port, String virtualHost, String userName, - String password) + String password,Map<String,String> props) { _protocol = protocol; _host = host; @@ -55,6 +64,7 @@ public class BrokerDetailsImpl implements BrokerDetails _virtualHost = virtualHost; _userName = userName; _password = password; + _props = props; } /** @@ -177,7 +187,7 @@ public class BrokerDetailsImpl implements BrokerDetails * * @param userName The user name */ - public void getUserName(String userName) + public void setUserName(String userName) { _userName = userName; } @@ -201,4 +211,50 @@ public class BrokerDetailsImpl implements BrokerDetails { _protocol = protocol; } + + /** + * Ex: keystore path + * + * @return the Properties associated with this connection. + */ + public Map<String,String> getProperties() + { + return _props; + } + + /** + * Sets the properties associated with this connection + * + * @param props + */ + public void setProperties(Map<String,String> props) + { + _props = props; + } + + public void setProperty(String key,String value) + { + _props.put(key, value); + } + + public String toString() + { + StringBuilder b = new StringBuilder(); + b.append("[username=" + _userName); + b.append(",password=" + _password); + b.append(",transport="+ _protocol); + b.append(",host=" + _host); + b.append(",port=" + _port + "]"); + b.append(" - Properties["); + if (_props != null) + { + for (String k:_props.keySet()) + { + b.append(k + "=" + _props.get(k) + ","); + } + } + b.append("]"); + + return b.toString(); + } } diff --git a/java/common/src/main/java/org/apache/qpidity/url/QpidURLImpl.java b/java/common/src/main/java/org/apache/qpidity/url/QpidURLImpl.java index c0ce6617d0..a9f3fd0712 100644 --- a/java/common/src/main/java/org/apache/qpidity/url/QpidURLImpl.java +++ b/java/common/src/main/java/org/apache/qpidity/url/QpidURLImpl.java @@ -18,7 +18,10 @@ package org.apache.qpidity.url; import org.apache.qpidity.BrokerDetails; +import org.apache.qpidity.BrokerDetailsImpl; +import java.net.MalformedURLException; +import java.util.ArrayList; import java.util.List; /** @@ -39,27 +42,361 @@ import java.util.List; * <p> future_prot_id = <placeholder, must end in ":". Example "sctp:"> * <p> future_prot_addr = <placeholder, protocl-specific address> * <p> future_parameters = <placeholder, not used in failover addresses> + * + * Ex: qpid:virtualhost=test@client_id1:tcp:myhost.com:5672,virtualhost=prod,keystore=/opt/keystore@client_id2:tls:mysecurehost.com:5672 */ public class QpidURLImpl implements QpidURL { - //-- Constructors - - public QpidURLImpl(String url) + private static final char[] URL_START_SEQ = new char[]{'q','p','i','d',':'}; + private static final char PROPERTY_EQUALS_CHAR = '='; + private static final char PROPERTY_SEPARATOR_CHAR = ','; + private static final char ADDRESS_SEPERATOR_CHAR = ','; + private static final char CLIENT_ID_TRANSPORT_SEPARATOR_CHAR = ':'; + private static final char TRANSPORT_HOST_SEPARATOR_CHAR = ':'; + private static final char HOST_PORT_SEPARATOR_CHAR = ':'; + private static final char AT_CHAR = '@'; + + enum URLParserState { - // todo pars this URL + QPID_URL_START, + ADDRESS_START, + PROPERTY_NAME, + PROPERTY_EQUALS, + PROPERTY_VALUE, + PROPERTY_SEPARATOR, + AT_CHAR, + CLIENT_ID, + CLIENT_ID_TRANSPORT_SEPARATOR, + TRANSPORT, + TRANSPORT_HOST_SEPARATOR, + HOST, + HOST_PORT_SEPARATOR, + PORT, + ADDRESS_END, + ADDRESS_SEPERATOR, + QPID_URL_END, + ERROR; } - + + //-- Constructors + + private char[] _url; + private List<BrokerDetails> _brokerDetailList = new ArrayList<BrokerDetails>(); + private String _error; + private int _index = 0; + private BrokerDetails _currentBroker; + private String _currentPropName; + private boolean _endOfURL = false; + private URLParserState _currentParserState; + + public QpidURLImpl(String url) throws MalformedURLException + { + _url = url.toCharArray(); + _endOfURL = false; + _currentParserState = URLParserState.QPID_URL_START; + URLParserState prevState = _currentParserState; // for error handling + BrokerDetails _brokerDetails = new BrokerDetailsImpl(); + try + { + while ( _currentParserState != URLParserState.ERROR && _currentParserState != URLParserState.QPID_URL_END) + { + prevState = _currentParserState; + _currentParserState = next(); + } + + if(_currentParserState == URLParserState.ERROR) + { + _error = "Invalid URL format [current_state = " + prevState + ", broker details parsed so far " + _currentBroker + " ] error at (" + _index +") due to " + _error; + MalformedURLException ex = new MalformedURLException(_error); + throw ex; + } + } + catch(ArrayIndexOutOfBoundsException e) + { + _error = "Invalid URL format [current_state = " + prevState + ", broker details parsed so far " + _currentBroker + " ] error at (" + _index +")"; + MalformedURLException ex = new MalformedURLException(_error); + throw ex; + } + } + //-- interface QpidURL - public List<BrokerDetails> getAllBrokerDetails() { - // TODO - return null; + return _brokerDetailList; } public String getURL() { - //TODO - return ""; + return new String(_url); + } + + private URLParserState next() + { + switch(_currentParserState) + { + case QPID_URL_START: + return checkSequence(URL_START_SEQ,URLParserState.ADDRESS_START); + case ADDRESS_START: + return startAddress(); + case PROPERTY_NAME: + return extractPropertyName(); + case PROPERTY_EQUALS: + _index++; // skip the equal sign + return URLParserState.PROPERTY_VALUE; + case PROPERTY_VALUE: + return extractPropertyValue(); + case PROPERTY_SEPARATOR: + _index++; // skip "," + return URLParserState.PROPERTY_NAME; + case AT_CHAR: + _index++; // skip the @ sign + case CLIENT_ID: + return extractClientId(); + case CLIENT_ID_TRANSPORT_SEPARATOR: + _index++; // skip ":" + return URLParserState.TRANSPORT; + case TRANSPORT: + return extractTransport(); + case TRANSPORT_HOST_SEPARATOR: + _index++; // skip ":" + return URLParserState.HOST; + case HOST: + return extractHost(); + case HOST_PORT_SEPARATOR: + _index++; // skip ":" + return URLParserState.PORT; + case PORT: + extractPort(); + case ADDRESS_END: + return endAddress(); + case ADDRESS_SEPERATOR: + _index++; // skip "," + return URLParserState.ADDRESS_START; + default: + return URLParserState.ERROR; + } + } + + private URLParserState checkSequence(char[] expected,URLParserState nextPart) + { + for(int i=0;i<expected.length;i++) + { + if(expected[i] != _url[_index]) + { + _error = "Excepted (" + expected[i] + ") at position " + _index + ", got (" + _url[_index] + ")"; + return URLParserState.ERROR; + } + _index++; + } + return nextPart; + } + + private URLParserState startAddress() + { + _currentBroker = new BrokerDetailsImpl(); + return URLParserState.PROPERTY_NAME; + } + + private URLParserState endAddress() + { + _brokerDetailList.add(_currentBroker); + if(_endOfURL) + { + return URLParserState.QPID_URL_END; + } + else + { + return URLParserState.ADDRESS_SEPERATOR; + } + } + + private URLParserState extractPropertyName() + { + StringBuilder b = new StringBuilder(); + char next = _url[_index]; + while(next != PROPERTY_EQUALS_CHAR && next != AT_CHAR) + { + b.append(next); + next = _url[++_index]; + } + _currentPropName = b.toString(); + if(_currentPropName.trim().equals("")) + { + _error = "Property name cannot be empty"; + return URLParserState.ERROR; + } + else if (next == PROPERTY_EQUALS_CHAR) + { + return URLParserState.PROPERTY_EQUALS; + } + else + { + return URLParserState.AT_CHAR; + } + } + + private URLParserState extractPropertyValue() + { + StringBuilder b = new StringBuilder(); + char next = _url[_index]; + while(next != PROPERTY_SEPARATOR_CHAR && next != AT_CHAR) + { + b.append(next); + next = _url[++_index]; + } + String propValue = b.toString(); + if(propValue.trim().equals("")) + { + _error = "Property values cannot be empty"; + return URLParserState.ERROR; + } + else + { + _currentBroker.setProperty(_currentPropName, propValue); + if (next == PROPERTY_SEPARATOR_CHAR) + { + return URLParserState.PROPERTY_SEPARATOR; + } + else + { + return URLParserState.AT_CHAR; + } + } + } + + private URLParserState extractClientId() + { + //Check if atleast virtualhost is there + if (_currentBroker.getProperties().get(BrokerDetails.VIRTUAL_HOST) == null) + { + _error = "Virtual host is a mandatory property"; + return URLParserState.ERROR; + } + else + { + _currentBroker.setVirtualHost(_currentBroker.getProperties().get(BrokerDetails.VIRTUAL_HOST)); + _currentBroker.getProperties().remove(BrokerDetails.VIRTUAL_HOST); + } + + if (_currentBroker.getProperties().get(BrokerDetails.USERNAME) != null) + { + String username = _currentBroker.getProperties().get(BrokerDetails.USERNAME); + _currentBroker.setUserName(username); + } + if (_currentBroker.getProperties().get(BrokerDetails.PASSWORD) != null) + { + String password = _currentBroker.getProperties().get(BrokerDetails.PASSWORD); + _currentBroker.setPassword(password); + } + + String clientId = buildUntil(CLIENT_ID_TRANSPORT_SEPARATOR_CHAR); + if (clientId.trim().equals("")) + { + _error = "Client Id cannot be empty"; + return URLParserState.ERROR; + } + else + { + _currentBroker.setProperty(BrokerDetails.CLIENT_ID, clientId); + return URLParserState.CLIENT_ID_TRANSPORT_SEPARATOR; + } + } + + private URLParserState extractTransport() + { + String transport = buildUntil(TRANSPORT_HOST_SEPARATOR_CHAR); + if (transport.trim().equals("")) + { + _error = "Transport cannot be empty"; + return URLParserState.ERROR; + } + else + { + _currentBroker.setProtocol(transport); + return URLParserState.TRANSPORT_HOST_SEPARATOR; + } + } + + private URLParserState extractHost() + { + String host = buildUntil(HOST_PORT_SEPARATOR_CHAR); + if (host.trim().equals("")) + { + _error = "Host cannot be empty"; + return URLParserState.ERROR; + } + else + { + _currentBroker.setHost(host); + return URLParserState.HOST_PORT_SEPARATOR; + } + } + + private URLParserState extractPort() + { + + StringBuilder b = new StringBuilder(); + try + { + char next = _url[_index]; + while(next != ADDRESS_SEPERATOR_CHAR) + { + b.append(next); + next = _url[++_index]; + } + } + catch(ArrayIndexOutOfBoundsException e) + { + _endOfURL = true; + } + String portStr = b.toString(); + if (portStr.trim().equals("")) + { + _error = "Host cannot be empty"; + return URLParserState.ERROR; + } + else + { + try + { + int port = Integer.parseInt(portStr); + _currentBroker.setPort(port); + return URLParserState.ADDRESS_END; + } + catch(NumberFormatException e) + { + _error = "Illegal number for port"; + return URLParserState.ERROR; + } + } + } + + private String buildUntil(char c) + { + StringBuilder b = new StringBuilder(); + char next = _url[_index]; + while(next != c) + { + b.append(next); + next = _url[++_index]; + } + return b.toString(); + } + + public static void main(String[] args) + { + String testurl = "qpid:virtualhost=test@client_id1:tcp:myhost.com:5672,virtualhost=prod,keystore=/opt/keystore@client_id2:tls:mysecurehost.com:5672"; + try + { + QpidURLImpl impl = new QpidURLImpl(testurl); + for (BrokerDetails d : impl.getAllBrokerDetails()) + { + System.out.println(d); + } + } + catch(Exception e) + { + e.printStackTrace(); + } } } |
