|
17 | 17 |
|
18 | 18 | package io.openmessaging.api; |
19 | 19 |
|
20 | | -import io.openmessaging.api.exception.OMSRuntimeException; |
21 | 20 | import io.openmessaging.api.internal.MessagingAccessPointAdapter; |
22 | 21 | import java.io.IOException; |
23 | 22 | import java.io.InputStream; |
| 23 | +import java.util.Map; |
24 | 24 | import java.util.Properties; |
| 25 | +import java.util.Set; |
25 | 26 |
|
26 | 27 | /** |
| 28 | + * <p> |
27 | 29 | * The oms class provides some static methods to create a {@code MessagingAccessPoint} from the specified OMS driver url |
28 | 30 | * and some useful util methods. |
29 | | - * <p> |
30 | | - * The complete OMS driver URL syntax is: |
31 | | - * <p> |
32 | | - * {@literal oms:<driver_type>://[account_id@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]/<region>} |
33 | | - * <p> |
34 | | - * The first part of the URL specifies which OMS implementation is to be used, rocketmq is an optional driver type. |
| 31 | + * </p> |
| 32 | + * |
35 | 33 | * <p> |
36 | 34 | * The brackets indicate that the extra access points are optional, and a correct OMS driver url needs at least one |
37 | 35 | * access point, which consists of hostname and port, like localhost:8081. |
| 36 | + * </p> |
38 | 37 | * |
39 | | - * @version OMS 1.1.0 |
40 | | - * @since OMS 1.1.0 |
| 38 | + * @version OMS 1.2.0 |
| 39 | + * @since OMS 1.1.0 |
41 | 40 | */ |
42 | 41 | public final class OMS { |
| 42 | + |
| 43 | + private final Properties properties = new Properties(); |
| 44 | + |
| 45 | + public static OMS builder() { |
| 46 | + return new OMS(); |
| 47 | + } |
| 48 | + |
43 | 49 | /** |
44 | | - * Returns a {@code MessagingAccessPoint} instance from the specified OMS driver url. |
| 50 | + * Set the endpoint provided by messaging vendor. |
45 | 51 | * |
46 | | - * @param url the specified OMS driver url |
47 | | - * @return a {@code MessagingAccessPoint} instance |
48 | | - * @throws OMSRuntimeException if the factory fails to create a {@code MessagingAccessPoint} due to some driver url |
49 | | - * some syntax error or internal error. |
| 52 | + * @param endpoint |
| 53 | + * @return |
50 | 54 | */ |
51 | | - public static MessagingAccessPoint getMessagingAccessPoint(String url) { |
52 | | - return getMessagingAccessPoint(url, new Properties()); |
| 55 | + public OMS endpoint(String endpoint) { |
| 56 | + this.properties.put(OMSBuiltinKeys.ENDPOINT, endpoint); |
| 57 | + return this; |
53 | 58 | } |
54 | 59 |
|
55 | 60 | /** |
56 | | - * Returns a {@code MessagingAccessPoint} instance from the specified OMS driver url with some preset attributes, |
57 | | - * which will be passed to MessagingAccessPoint's implementation class as a unique constructor parameter. |
| 61 | + * Set the region provided by messaging vendor. |
58 | 62 | * |
59 | | - * There are some standard attributes defined by OMS for this method, the same as {@link |
60 | | - * MessagingAccessPoint#attributes()} ()} |
| 63 | + * @param region |
| 64 | + * @return |
| 65 | + */ |
| 66 | + public OMS region(String region) { |
| 67 | + this.properties.put(OMSBuiltinKeys.REGION, region); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * <p> |
| 73 | + * Set the the driver type of the specified MessagingAccessPoint's * implementation, the default value is {@literal |
| 74 | + * io.openmessaging.<driver_type>.MessagingAccessPointImpl}. |
| 75 | + * </p> |
| 76 | + * |
| 77 | + * <p> |
| 78 | + * But if the {@link OMS#driverImpl(String)} attribute was set, this attribute will be ignored. |
| 79 | + * </p> |
61 | 80 | * |
62 | | - * @param url the specified OMS driver url |
63 | | - * @return a {@code MessagingAccessPoint} instance |
64 | | - * @throws OMSRuntimeException if the factory fails to create a {@code MessagingAccessPoint} due to some driver url |
65 | | - * some syntax error or internal error. |
| 81 | + * @param driver |
| 82 | + * @return |
66 | 83 | */ |
67 | | - public static MessagingAccessPoint getMessagingAccessPoint(String url, Properties attributes) { |
68 | | - return MessagingAccessPointAdapter.getMessagingAccessPoint(url, attributes); |
| 84 | + public OMS driver(String driver) { |
| 85 | + this.properties.put(OMSBuiltinKeys.DRIVER, driver); |
| 86 | + return this; |
69 | 87 | } |
70 | 88 |
|
| 89 | + /** |
| 90 | + * <p> |
| 91 | + * Set the the fully qualified class name of the specified MessagingAccessPoint's * implementation, the default |
| 92 | + * value is {@literal io.openmessaging.<driver_type>.MessagingAccessPointImpl}. |
| 93 | + * </p> |
| 94 | + * |
| 95 | + * <p> |
| 96 | + * If this attribute was set, {@link OMS#driver(String)} will be ignored. |
| 97 | + * </p> |
| 98 | + * |
| 99 | + * @param driverImpl |
| 100 | + * @return |
| 101 | + */ |
| 102 | + public OMS driverImpl(String driverImpl) { |
| 103 | + this.properties.put(OMSBuiltinKeys.DRIVER_IMPL, driverImpl); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * <p> |
| 109 | + * Set credentials used by the client. |
| 110 | + * </p> |
| 111 | + * |
| 112 | + * @param credentials provided by vendors. |
| 113 | + * @return |
| 114 | + */ |
| 115 | + public OMS withCredentials(Properties credentials) { |
| 116 | + if (credentials.getProperty(OMSBuiltinKeys.ACCESS_KEY) != null) { |
| 117 | + this.properties.put(OMSBuiltinKeys.ACCESS_KEY, credentials.getProperty(OMSBuiltinKeys.ACCESS_KEY)); |
| 118 | + } |
| 119 | + if (credentials.getProperty(OMSBuiltinKeys.SECRET_KEY) != null) { |
| 120 | + this.properties.put(OMSBuiltinKeys.SECRET_KEY, credentials.getProperty(OMSBuiltinKeys.SECRET_KEY)); |
| 121 | + } |
| 122 | + if (credentials.getProperty(OMSBuiltinKeys.SECURITY_TOKEN) != null) { |
| 123 | + this.properties.put(OMSBuiltinKeys.SECURITY_TOKEN, credentials.getProperty(OMSBuiltinKeys.SECURITY_TOKEN)); |
| 124 | + } |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public MessagingAccessPoint build() { |
| 129 | + return MessagingAccessPointAdapter.getMessagingAccessPoint(this.properties); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Set extra custom configs. |
| 134 | + * |
| 135 | + * @param config extra configs |
| 136 | + * @return |
| 137 | + */ |
| 138 | + public MessagingAccessPoint build(Properties config) { |
| 139 | + Set<Map.Entry<Object, Object>> entrySet = config.entrySet(); |
| 140 | + for (Map.Entry<Object, Object> entry : entrySet) { |
| 141 | + if (!this.properties.containsKey(entry.getKey())) { |
| 142 | + this.properties.put(entry.getKey(), entry.getValue()); |
| 143 | + } |
| 144 | + } |
| 145 | + return this.build(properties); |
| 146 | + } |
71 | 147 |
|
72 | 148 | /** |
73 | 149 | * The version format is X.Y.Z (Major.Minor.Patch), a pre-release version may be denoted by appending a hyphen and a |
|
0 commit comments