Author: gsim Date: Fri Oct 10 12:55:41 2014 New Revision: 1630842 URL: http://svn.apache.org/r1630842 Log: PROTON-693: Implement C-API wrappers in Java for jython, clean up Url implementation.
- Java implementation of fake C API for Jython SWIG wrappers based on messenger.impl.Address. - Minor extensions to Address.java - Minor change to Address.java to be consistent with C parser on one corner case: - is url("/foo") a host called "/foo" or an empty host and a path "foo" - fixed Java parser to agree with C - path "foo" - Rename getters from url_* to url_get_*. - Use pn_string_t formatting functions to build URL string. Added: qpid/proton/branches/examples/proton-j/src/main/resources/curl.py Modified: qpid/proton/branches/examples/proton-c/bindings/python/proton.py qpid/proton/branches/examples/proton-c/include/proton/url.h qpid/proton/branches/examples/proton-c/src/url.c qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/messenger/impl/Address.java qpid/proton/branches/examples/proton-j/src/main/resources/cproton.py qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/messenger/impl/AddressTest.java Modified: qpid/proton/branches/examples/proton-c/bindings/python/proton.py URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-c/bindings/python/proton.py?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-c/bindings/python/proton.py (original) +++ qpid/proton/branches/examples/proton-c/bindings/python/proton.py Fri Oct 10 12:55:41 2014 @@ -31,6 +31,7 @@ The proton APIs consist of the following """ from cproton import * + import weakref, re, socket try: import uuid @@ -3718,7 +3719,7 @@ class Url(object): class PartDescriptor(object): def __init__(self, part): - self.getter = globals()["pn_url_%s" % part] + self.getter = globals()["pn_url_get_%s" % part] self.setter = globals()["pn_url_set_%s" % part] def __get__(self, obj, type=None): return self.getter(obj._url) def __set__(self, obj, value): return self.setter(obj._url, str(value)) @@ -3729,20 +3730,24 @@ class Url(object): host = PartDescriptor('host') path = PartDescriptor('path') - @property - def port(self): - portstr = pn_url_port(self._url) + def _get_port(self): + portstr = pn_url_get_port(self._url) return portstr and Url.Port(portstr) - @port.setter - def port(self, value): + def _set_port(self, value): if value is None: pn_url_set_port(self._url, None) else: pn_url_set_port(self._url, str(Url.Port(value))) + port = property(_get_port, _set_port) + def __str__(self): return pn_url_str(self._url) def __repr__(self): return "Url(%r)" % str(self) + def __del__(self): + pn_url_free(self._url); + self._url = None + def defaults(self): """ Fill in missing values (scheme, host or port) with defaults Modified: qpid/proton/branches/examples/proton-c/include/proton/url.h URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-c/include/proton/url.h?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-c/include/proton/url.h (original) +++ qpid/proton/branches/examples/proton-c/include/proton/url.h Fri Oct 10 12:55:41 2014 @@ -46,7 +46,12 @@ PN_EXTERN void pn_url_free(pn_url_t *url /** Clear the contents of the URL. */ PN_EXTERN void pn_url_clear(pn_url_t *url); -/** Return the string form of a URL. Owned by the pn_url_t.*/ +/** + * Return the string form of a URL. + * + * The returned string is owned by the pn_url_t and will become invalid if it + * is modified. + */ PN_EXTERN const char *pn_url_str(pn_url_t *url); /** @@ -56,12 +61,12 @@ PN_EXTERN const char *pn_url_str(pn_url_ * *@{ */ -PN_EXTERN const char *pn_url_scheme(pn_url_t *url); -PN_EXTERN const char *pn_url_username(pn_url_t *url); -PN_EXTERN const char *pn_url_password(pn_url_t *url); -PN_EXTERN const char *pn_url_host(pn_url_t *url); -PN_EXTERN const char *pn_url_port(pn_url_t *url); -PN_EXTERN const char *pn_url_path(pn_url_t *url); +PN_EXTERN const char *pn_url_get_scheme(pn_url_t *url); +PN_EXTERN const char *pn_url_get_username(pn_url_t *url); +PN_EXTERN const char *pn_url_get_password(pn_url_t *url); +PN_EXTERN const char *pn_url_get_host(pn_url_t *url); +PN_EXTERN const char *pn_url_get_port(pn_url_t *url); +PN_EXTERN const char *pn_url_get_path(pn_url_t *url); ///@} /** Modified: qpid/proton/branches/examples/proton-c/src/url.c URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-c/src/url.c?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-c/src/url.c (original) +++ qpid/proton/branches/examples/proton-c/src/url.c Fri Oct 10 12:55:41 2014 @@ -20,6 +20,7 @@ */ #include "proton/url.h" +#include "proton/object.h" #include "proton/util.h" #include "platform.h" @@ -29,7 +30,7 @@ static char* copy(const char* str) { if (str == NULL) return NULL; - char *str2 = (char*)malloc(strlen(str)); + char *str2 = (char*)malloc(strlen(str)+1); if (str2) strcpy(str2, str); return str2; } @@ -41,12 +42,13 @@ struct pn_url_t { char *host; char *port; char *path; - char *str; + pn_string_t *str; }; PN_EXTERN pn_url_t *pn_url() { pn_url_t *url = (pn_url_t*)malloc(sizeof(pn_url_t)); - memset(url, 0, sizeof(*url)); + if (url) memset(url, 0, sizeof(*url)); + url->str = pn_string(""); return url; } @@ -59,7 +61,7 @@ PN_EXTERN pn_url_t *pn_url_parse(const c return NULL; pn_url_t *url = pn_url(); - char *str2 = copy(str); /* FIXME aconway 2014-09-19: clean up */ + char *str2 = copy(str); pni_parse_url(str2, &url->scheme, &url->username, &url->password, &url->host, &url->port, &url->path); url->scheme = copy(url->scheme); url->username = copy(url->username); @@ -67,56 +69,53 @@ PN_EXTERN pn_url_t *pn_url_parse(const c url->host = (url->host && !*url->host) ? NULL : copy(url->host); url->port = copy(url->port); url->path = copy(url->path); + + free(str2); return url; } /** Free a URL */ PN_EXTERN void pn_url_free(pn_url_t *url) { pn_url_clear(url); + pn_free(url->str); free(url); } /** Clear the contents of the URL. */ PN_EXTERN void pn_url_clear(pn_url_t *url) { + pn_url_set_scheme(url, NULL); pn_url_set_username(url, NULL); pn_url_set_password(url, NULL); pn_url_set_host(url, NULL); pn_url_set_port(url, NULL); pn_url_set_path(url, NULL); - free(url->str); url->str = NULL; + pn_string_clear(url->str); } static inline int len(const char *str) { return str ? strlen(str) : 0; } /** Return the string form of a URL. */ PN_EXTERN const char *pn_url_str(pn_url_t *url) { - int size = len(url->scheme) + len(url->username) + len(url->password) - + len(url->host) + len(url->port) + len(url->path) - + len("s://u:p@[h]:p/p"); - free(url->str); - url->str = (char*)malloc(size); - if (!url->str) return NULL; - - int i = 0; - if (url->scheme) i += snprintf(url->str+i, size-i, "%s://", url->scheme); - if (url->username) i += snprintf(url->str+i, size-i, "%s", url->username); - if (url->password) i += snprintf(url->str+i, size-i, ":%s", url->password); - if (url->username || url->password) i += snprintf(url->str+i, size-i, "@"); + pn_string_set(url->str, ""); + if (url->scheme) pn_string_addf(url->str, "%s://", url->scheme); + if (url->username) pn_string_addf(url->str, "%s", url->username); + if (url->password) pn_string_addf(url->str, ":%s", url->password); + if (url->username || url->password) pn_string_addf(url->str, "@"); if (url->host) { - if (strchr(url->host, ':')) i += snprintf(url->str+i, size-i, "[%s]", url->host); - else i += snprintf(url->str+i, size-i, "%s", url->host); + if (strchr(url->host, ':')) pn_string_addf(url->str, "[%s]", url->host); + else pn_string_addf(url->str, "%s", url->host); } - if (url->port) i += snprintf(url->str+i, size-i, ":%s", url->port); - if (url->path) i += snprintf(url->str+i, size-i, "/%s", url->path); - return url->str; + if (url->port) pn_string_addf(url->str, ":%s", url->port); + if (url->path) pn_string_addf(url->str, "/%s", url->path); + return pn_string_get(url->str); } -PN_EXTERN const char *pn_url_scheme(pn_url_t *url) { return url->scheme; } -PN_EXTERN const char *pn_url_username(pn_url_t *url) { return url->username; } -PN_EXTERN const char *pn_url_password(pn_url_t *url) { return url->password; } -PN_EXTERN const char *pn_url_host(pn_url_t *url) { return url->host; } -PN_EXTERN const char *pn_url_port(pn_url_t *url) { return url->port; } -PN_EXTERN const char *pn_url_path(pn_url_t *url) { return url->path; } +PN_EXTERN const char *pn_url_get_scheme(pn_url_t *url) { return url->scheme; } +PN_EXTERN const char *pn_url_get_username(pn_url_t *url) { return url->username; } +PN_EXTERN const char *pn_url_get_password(pn_url_t *url) { return url->password; } +PN_EXTERN const char *pn_url_get_host(pn_url_t *url) { return url->host; } +PN_EXTERN const char *pn_url_get_port(pn_url_t *url) { return url->port; } +PN_EXTERN const char *pn_url_get_path(pn_url_t *url) { return url->path; } #define SET(part) free(url->part); url->part = copy(part) PN_EXTERN void pn_url_set_scheme(pn_url_t *url, const char *scheme) { SET(scheme); } Modified: qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/messenger/impl/Address.java URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/messenger/impl/Address.java?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/messenger/impl/Address.java (original) +++ qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/messenger/impl/Address.java Fri Oct 10 12:55:41 2014 @@ -26,7 +26,7 @@ package org.apache.qpid.proton.messenger * */ -class Address +public class Address { private String _address; @@ -38,13 +38,7 @@ class Address private String _port; private String _name; - public Address(String address) - { - _address = address; - parse(); - } - - private void parse() + public void clear() { _passive = false; _scheme = null; @@ -53,21 +47,30 @@ class Address _host = null; _port = null; _name = null; + } + + public Address() + { + clear(); + } + public Address(String address) + { + clear(); int start = 0; - int schemeEnd = _address.indexOf("://", start); + int schemeEnd = address.indexOf("://", start); if (schemeEnd >= 0) { - _scheme = _address.substring(start, schemeEnd); + _scheme = address.substring(start, schemeEnd); start = schemeEnd + 3; } String uphp; - int slash = _address.indexOf("/", start); - if (slash > 0) { - uphp = _address.substring(start, slash); - _name = _address.substring(slash + 1); + int slash = address.indexOf("/", start); + if (slash >= 0) { + uphp = address.substring(start, slash); + _name = address.substring(slash + 1); } else { - uphp = _address.substring(start); + uphp = address.substring(start); } String hp; @@ -115,7 +118,18 @@ class Address public String toString() { - return _address; + String str = new String(); + if (_scheme != null) str += _scheme + "://"; + if (_user != null) str += _user; + if (_pass != null) str += ":" + _pass; + if (_user != null || _pass != null) str += "@"; + if (_host != null) { + if (_host.contains(":")) str += "[" + _host + "]"; + else str += _host; + } + if (_port != null) str += ":" + _port; + if (_name != null) str += "/" + _name; + return str; } public boolean isPassive() @@ -168,4 +182,33 @@ class Address return _name; } + public void setScheme(String scheme) + { + _scheme= scheme; + } + + public void setUser(String user) + { + _user= user; + } + + public void setPass(String pass) + { + _pass= pass; + } + + public void setHost(String host) + { + _host= host; + } + + public void setPort(String port) + { + _port= port; + } + + public void setName(String name) + { + _name= name; + } } Modified: qpid/proton/branches/examples/proton-j/src/main/resources/cproton.py URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/main/resources/cproton.py?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-j/src/main/resources/cproton.py (original) +++ qpid/proton/branches/examples/proton-j/src/main/resources/cproton.py Fri Oct 10 12:55:41 2014 @@ -36,3 +36,4 @@ from cssl import * from cdriver import * from cmessenger import * from cmessage import * +from curl import * Added: qpid/proton/branches/examples/proton-j/src/main/resources/curl.py URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/main/resources/curl.py?rev=1630842&view=auto ============================================================================== --- qpid/proton/branches/examples/proton-j/src/main/resources/curl.py (added) +++ qpid/proton/branches/examples/proton-j/src/main/resources/curl.py Fri Oct 10 12:55:41 2014 @@ -0,0 +1,47 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License +# + +from org.apache.qpid.proton.messenger.impl import Address + +def pn_url(): + return Address() + +def pn_url_parse(urlstr): + return Address(urlstr) + +def pn_url_free(url): pass + +def pn_url_clear(url): + url.clear(); + +def pn_url_str(url): return url.toString() + +def pn_url_get_scheme(url): return url.getScheme() +def pn_url_get_username(url): return url.getUser() +def pn_url_get_password(url): return url.getPass() +def pn_url_get_host(url): return url.getHost() or None +def pn_url_get_port(url): return url.getPort() +def pn_url_get_path(url): return url.getName() + +def pn_url_set_scheme(url, value): url.setScheme(value) +def pn_url_set_username(url, value): url.setUser(value) +def pn_url_set_password(url, value): url.setPass(value) +def pn_url_set_host(url, value): url.setHost(value) +def pn_url_set_port(url, value): url.setPort(value) +def pn_url_set_path(url, value): url.setName(value) Modified: qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/messenger/impl/AddressTest.java URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/messenger/impl/AddressTest.java?rev=1630842&r1=1630841&r2=1630842&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/messenger/impl/AddressTest.java (original) +++ qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/messenger/impl/AddressTest.java Fri Oct 10 12:55:41 2014 @@ -6,24 +6,25 @@ import org.junit.Test; public class AddressTest { - private void testParse(String url, String scheme, String user, String pass, String host, String port, String name) - { - Address address = new Address(url); - assertEquals(scheme, address.getScheme()); - assertEquals(user, address.getUser()); - assertEquals(pass, address.getPass()); - assertEquals(host, address.getHost()); - assertEquals(port, address.getPort()); - } + private void testParse(String url, String scheme, String user, String pass, String host, String port, String name) + { + Address address = new Address(url); + assertEquals(scheme, address.getScheme()); + assertEquals(user, address.getUser()); + assertEquals(pass, address.getPass()); + assertEquals(host, address.getHost()); + assertEquals(port, address.getPort()); + assertEquals(url, address.toString()); + } - @Test - public void addressTests() - { - testParse("host", null, null, null, "host", null, null); - testParse("host:423", null, null, null, "host", "423", null); - testParse("user@host", null, "user", null, "host", null, null); - testParse("user:1243^&^:pw@host:423", null, "user", "1243^&^:pw", "host", "423", null); - testParse("user:1243^&^:pw@host:423/Foo.bar:90087", null, "user", "1243^&^:pw", "host", "423", "Foo.bar:90087"); + @Test + public void addressTests() + { + testParse("host", null, null, null, "host", null, null); + testParse("host:423", null, null, null, "host", "423", null); + testParse("user@host", null, "user", null, "host", null, null); + testParse("user:1243^&^:pw@host:423", null, "user", "1243^&^:pw", "host", "423", null); + testParse("user:1243^&^:pw@host:423/Foo.bar:90087", null, "user", "1243^&^:pw", "host", "423", "Foo.bar:90087"); testParse("user:1243^&^:pw@host:423/Foo.bar:90087@somewhere", null, "user", "1243^&^:pw", "host", "423", "Foo.bar:90087@somewhere"); testParse("[::1]", null, null, null, "::1", null, null); testParse("[::1]:amqp", null, null, null, "::1", "amqp", null); @@ -38,14 +39,13 @@ public class AddressTest { testParse("amqp://user@[1234:52:0:1260:f2de:f1ff:fe59:8f87]:amqp", "amqp", "user", null, "1234:52:0:1260:f2de:f1ff:fe59:8f87", "amqp", null); testParse("amqp://user:1243^&^:pw@[::1]:amqp", "amqp", "user", "1243^&^:pw", "::1", "amqp", null); testParse("amqp://user:1243^&^:pw@[::1]:amqp/Foo.bar:90087", "amqp", "user", "1243^&^:pw", "::1", "amqp", "Foo.bar:90087"); - testParse("amqp://host", "amqp", null, null, "host", null, null); - testParse("amqp://user@host", "amqp", "user", null, "host", null, null); - testParse("amqp://user@host/path:%", "amqp", "user", null, "host", null, "path:%"); - testParse("amqp://user@host:5674/path:%", "amqp", "user", null, "host", "5674", "path:%"); - testParse("amqp://user@host/path:%", "amqp", "user", null, "host", null, "path:%"); - testParse("amqp://bigbird@host/queue@host", "amqp", "bigbird", null, "host", null, "queue@host"); - testParse("amqp://host/queue@host", "amqp", null, null, "host", null, "queue@host"); - testParse("amqp://host:9765/queue@host", "amqp", null, null, "host", "9765", "queue@host"); - } - + testParse("amqp://host", "amqp", null, null, "host", null, null); + testParse("amqp://user@host", "amqp", "user", null, "host", null, null); + testParse("amqp://user@host/path:%", "amqp", "user", null, "host", null, "path:%"); + testParse("amqp://user@host:5674/path:%", "amqp", "user", null, "host", "5674", "path:%"); + testParse("amqp://user@host/path:%", "amqp", "user", null, "host", null, "path:%"); + testParse("amqp://bigbird@host/queue@host", "amqp", "bigbird", null, "host", null, "queue@host"); + testParse("amqp://host/queue@host", "amqp", null, null, "host", null, "queue@host"); + testParse("amqp://host:9765/queue@host", "amqp", null, null, "host", "9765", "queue@host"); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org