Repository: incubator-rocketmq Updated Branches: refs/heads/spec 591dea838 -> 57e775a83
Develop a vendor-neutral open standard for distributed messaging 1, add MessagingEndPoint Project: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/commit/57e775a8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/tree/57e775a8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/diff/57e775a8 Branch: refs/heads/spec Commit: 57e775a833068e54d279c3cb5da38b43a5cf0515 Parents: 591dea8 Author: vintagewang <[email protected]> Authored: Sun Dec 25 17:12:57 2016 +0800 Committer: vintagewang <[email protected]> Committed: Sun Dec 25 17:12:57 2016 +0800 ---------------------------------------------------------------------- .../apache/openmessaging/MessagingEndPoint.java | 25 ++++++++ .../openmessaging/MessagingEndPointManager.java | 43 +++++++++++++ .../internal/MessagingEndPointFactory.java | 38 +++++++++++ .../internal/ServiceConstants.java | 35 +++++++++++ .../internal/ServiceEndPointAdapter.java | 66 ++++++++++++++++++++ .../openmessaging/internal/URISpecParser.java | 63 +++++++++++++++++++ 6 files changed, 270 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPoint.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPoint.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPoint.java new file mode 100644 index 0000000..5d90ae2 --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPoint.java @@ -0,0 +1,25 @@ +/** + * 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. + */ + +package org.apache.openmessaging; + +/** + * @author [email protected] + * @since 2016-12-25 + */ +public interface MessagingEndPoint { +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPointManager.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPointManager.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPointManager.java new file mode 100644 index 0000000..3dfffb0 --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/MessagingEndPointManager.java @@ -0,0 +1,43 @@ +/** + * 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. + */ + +package org.apache.openmessaging; + +import org.apache.openmessaging.internal.MessagingEndPointFactory; +import org.apache.openmessaging.internal.URISpecParser; + +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * @author [email protected] + * @since 2016-12-25 + */ +public class MessagingEndPointManager { + public static MessagingEndPoint getMessagingEndPoint(String url) throws Exception { + return getMessagingEndPoint(url, new Properties()); + } + + public static MessagingEndPoint getMessagingEndPoint(String url, Properties properties) throws Exception { + Map<String, List<String>> driverUrl = URISpecParser.parseURI(url); + if (null == driverUrl || driverUrl.size() == 0) { + throw new IllegalArgumentException("driver url parsed result.size ==0"); + } + return MessagingEndPointFactory.createServiceEndPoint(driverUrl, properties); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/MessagingEndPointFactory.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/MessagingEndPointFactory.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/MessagingEndPointFactory.java new file mode 100644 index 0000000..5c4990a --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/MessagingEndPointFactory.java @@ -0,0 +1,38 @@ +/* + * 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. + */ + +package org.apache.openmessaging.internal; + +import org.apache.openmessaging.MessagingEndPoint; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Map; +import java.util.Properties; + + +public class MessagingEndPointFactory { + public static MessagingEndPoint createServiceEndPoint(Map<String, List<String>> url, Properties properties) + throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, + InstantiationException, IllegalAccessException { + List<String> driver = url.get(ServiceConstants.SPI_NAME); + List<String> urls = url.get(ServiceConstants.URL_NAME); + if (urls != null && urls.size() > 0) + properties.put(ServiceConstants.URL, urls.get(0)); + return ServiceEndPointAdapter.instantiateServiceEndPoint(driver.get(0), properties); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceConstants.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceConstants.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceConstants.java new file mode 100644 index 0000000..42e867a --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceConstants.java @@ -0,0 +1,35 @@ +/* + * 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. + */ + +package org.apache.openmessaging.internal; + +public class ServiceConstants { + public static final String PROTOCOL_NAME = "protocol"; + public static final String SPI_NAME = "spi"; + public static final String URL_NAME = "urls"; + public static final String URL = "url"; + public static final String DISCOVERY_URL = "service.discovery.url"; + public static final String DEFAULT_SERVICE_END_POINT = "aliRelay"; + public static final String DEFAULT_SERVICE_IMPL = "com.alibaba.jukola.java.sdk.ServiceEndPointStandardImpl"; + public static final String DEFAULT_SERVICE_DISCOVERY_URL = "http://jukola.alibaba.com:8443"; + public static final String NAMESPACE_ID = "method.namespace.id"; + public static final String URL_SEPARATOR = ":"; + public static final String LIST_SEPARATOR = ","; + public static final String PARAM_SEPARATOR = "&"; + public static final String KV_SEPARATOR = "="; + public static final String SERVICE_DISCOVER_PROTOCOL = "http"; +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceEndPointAdapter.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceEndPointAdapter.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceEndPointAdapter.java new file mode 100644 index 0000000..19b5733 --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/ServiceEndPointAdapter.java @@ -0,0 +1,66 @@ +/* + * 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. + */ + +package org.apache.openmessaging.internal; + + +import org.apache.openmessaging.MessagingEndPoint; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + + +public class ServiceEndPointAdapter { + private static Map<String, String> serviceEndPointClassMap = new HashMap<String, String>(); + + static { + serviceEndPointClassMap.put(ServiceConstants.DEFAULT_SERVICE_END_POINT, + ServiceConstants.DEFAULT_SERVICE_IMPL); + } + + static MessagingEndPoint instantiateServiceEndPoint(String url, Properties properties) + throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, + InvocationTargetException, InstantiationException { + String serviceImpl = ServiceConstants.DEFAULT_SERVICE_IMPL; + if (serviceEndPointClassMap.containsKey(url)) + serviceImpl = serviceEndPointClassMap.get(url); + Class<?> serviceEndPointClass = Class.forName(serviceImpl); + if (serviceEndPointClass == null) + return null; + + String serviceUrl = ServiceConstants.DEFAULT_SERVICE_DISCOVERY_URL; + if (properties.get(ServiceConstants.URL) != null) { + String[] propertySplits = ((String) properties.get(ServiceConstants.URL)).split(ServiceConstants.PARAM_SEPARATOR); + if (propertySplits.length > 0) { + serviceUrl = propertySplits[0]; + for (int index = 1; index < propertySplits.length; index++) { + String[] kv = propertySplits[index].split(ServiceConstants.KV_SEPARATOR); + properties.put(kv[0], kv[1]); + } + } + } + properties.remove(ServiceConstants.URL); + properties.put(ServiceConstants.DISCOVERY_URL, serviceUrl); + Class[] paramTypes = {Properties.class}; + Constructor constructor = serviceEndPointClass.getConstructor(paramTypes); + assert constructor != null; + return (MessagingEndPoint) constructor.newInstance(properties); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq/blob/57e775a8/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/URISpecParser.java ---------------------------------------------------------------------- diff --git a/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/URISpecParser.java b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/URISpecParser.java new file mode 100644 index 0000000..cbe07ba --- /dev/null +++ b/spec/code/messaging-user-level-api/java/src/main/java/org/apache/openmessaging/internal/URISpecParser.java @@ -0,0 +1,63 @@ +/* + * 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. + */ + +package org.apache.openmessaging.internal; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author xuanyin + */ +public class URISpecParser { + public static Map<String, List<String>> parseURI(String uri) { + if (uri == null || uri.length() == 0) { + return new HashMap<String, List<String>>(); + } + + int spiIndex = 0; + int index = uri.indexOf(ServiceConstants.URL_SEPARATOR); + Map<String, List<String>> results = new HashMap<String, List<String>>(); + String protocol = uri.substring(0, index); + List<String> protocolSet = new ArrayList<String>(); + protocolSet.add(protocol); + results.put(ServiceConstants.PROTOCOL_NAME, protocolSet); + if (index > 0) { + String spi; + spiIndex = uri.indexOf(ServiceConstants.URL_SEPARATOR, index + 1); + if (spiIndex > 0) { + spi = uri.substring(index + 1, spiIndex); + } else { + spi = uri.substring(index + 1); + } + List<String> spiSet = new ArrayList<String>(); + spiSet.add(spi); + results.put(ServiceConstants.SPI_NAME, spiSet); + } + if (spiIndex > 0) { + String urlList = uri.substring(spiIndex + 1); + String[] list = urlList.split(ServiceConstants.LIST_SEPARATOR); + if (list.length > 0) { + results.put(ServiceConstants.URL_NAME, Arrays.asList(list)); + } + } + return results; + } +}
