|
6 | 6 | import okhttp3.OkHttpClient;
|
7 | 7 | import org.influxdb.impl.Preconditions;
|
8 | 8 |
|
| 9 | +import java.net.*; |
9 | 10 | import java.util.Objects;
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | /**
|
13 | 14 | * A Factory to create a instance of a InfluxDB Database adapter.
|
14 | 15 | *
|
15 | 16 | * @author stefan.majer [at] gmail.com
|
16 |
| - * |
17 | 17 | */
|
18 | 18 | public enum InfluxDBFactory {
|
19 |
| - INSTANCE; |
| 19 | + INSTANCE; |
| 20 | + |
| 21 | + /** |
| 22 | + * Create a connection to a InfluxDB. |
| 23 | + * |
| 24 | + * @param url the url to connect to. |
| 25 | + * @return a InfluxDB adapter suitable to access a InfluxDB. |
| 26 | + */ |
| 27 | + public static InfluxDB connect(final String url) { |
| 28 | + Preconditions.checkNonEmptyString(url, "url"); |
| 29 | + checkUrl(url); |
| 30 | + return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a connection to a InfluxDB. |
| 35 | + * |
| 36 | + * @param url the url to connect to. |
| 37 | + * @param username the username which is used to authorize against the influxDB instance. |
| 38 | + * @param password the password for the username which is used to authorize against the influxDB |
| 39 | + * instance. |
| 40 | + * @return a InfluxDB adapter suitable to access a InfluxDB. |
| 41 | + */ |
| 42 | + public static InfluxDB connect(final String url, final String username, final String password) { |
| 43 | + Preconditions.checkNonEmptyString(url, "url"); |
| 44 | + Preconditions.checkNonEmptyString(username, "username"); |
| 45 | + checkUrl(url); |
| 46 | + return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a connection to a InfluxDB. |
| 51 | + * |
| 52 | + * @param url the url to connect to. |
| 53 | + * @param client the HTTP client to use |
| 54 | + * @return a InfluxDB adapter suitable to access a InfluxDB. |
| 55 | + */ |
| 56 | + public static InfluxDB connect(final String url, final OkHttpClient.Builder client) { |
| 57 | + Preconditions.checkNonEmptyString(url, "url"); |
| 58 | + Objects.requireNonNull(client, "client"); |
| 59 | + checkUrl(url); |
| 60 | + return new InfluxDBImpl(url, null, null, client); |
| 61 | + } |
20 | 62 |
|
21 |
| - /** |
22 |
| - * Create a connection to a InfluxDB. |
23 |
| - * |
24 |
| - * @param url |
25 |
| - * the url to connect to. |
26 |
| - * @return a InfluxDB adapter suitable to access a InfluxDB. |
27 |
| - */ |
28 |
| - public static InfluxDB connect(final String url) { |
29 |
| - Preconditions.checkNonEmptyString(url, "url"); |
30 |
| - return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder()); |
31 |
| - } |
| 63 | + /** |
| 64 | + * Create a connection to a InfluxDB. |
| 65 | + * |
| 66 | + * @param url the url to connect to. |
| 67 | + * @param username the username which is used to authorize against the influxDB instance. |
| 68 | + * @param password the password for the username which is used to authorize against the influxDB |
| 69 | + * instance. |
| 70 | + * @param client the HTTP client to use |
| 71 | + * @return a InfluxDB adapter suitable to access a InfluxDB. |
| 72 | + */ |
| 73 | + public static InfluxDB connect(final String url, final String username, final String password, |
| 74 | + final OkHttpClient.Builder client) { |
| 75 | + checkUrl(url); |
| 76 | + return connect(url, username, password, client, ResponseFormat.JSON); |
| 77 | + } |
32 | 78 |
|
33 |
| - /** |
34 |
| - * Create a connection to a InfluxDB. |
35 |
| - * |
36 |
| - * @param url |
37 |
| - * the url to connect to. |
38 |
| - * @param username |
39 |
| - * the username which is used to authorize against the influxDB instance. |
40 |
| - * @param password |
41 |
| - * the password for the username which is used to authorize against the influxDB |
42 |
| - * instance. |
43 |
| - * @return a InfluxDB adapter suitable to access a InfluxDB. |
44 |
| - */ |
45 |
| - public static InfluxDB connect(final String url, final String username, final String password) { |
46 |
| - Preconditions.checkNonEmptyString(url, "url"); |
47 |
| - Preconditions.checkNonEmptyString(username, "username"); |
48 |
| - return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder()); |
49 |
| - } |
| 79 | + /** |
| 80 | + * Create a connection to a InfluxDB. |
| 81 | + * |
| 82 | + * @param url the url to connect to. |
| 83 | + * @param username the username which is used to authorize against the influxDB instance. |
| 84 | + * @param password the password for the username which is used to authorize against the influxDB |
| 85 | + * instance. |
| 86 | + * @param client the HTTP client to use |
| 87 | + * @param responseFormat The {@code ResponseFormat} to use for response from InfluxDB server |
| 88 | + * @return a InfluxDB adapter suitable to access a InfluxDB. |
| 89 | + */ |
| 90 | + public static InfluxDB connect(final String url, final String username, final String password, |
| 91 | + final OkHttpClient.Builder client, final ResponseFormat responseFormat) { |
| 92 | + Preconditions.checkNonEmptyString(url, "url"); |
| 93 | + Preconditions.checkNonEmptyString(username, "username"); |
| 94 | + Objects.requireNonNull(client, "client"); |
| 95 | + checkUrl(url); |
| 96 | + return new InfluxDBImpl(url, username, password, client, responseFormat); |
| 97 | + } |
50 | 98 |
|
51 |
| - /** |
52 |
| - * Create a connection to a InfluxDB. |
53 |
| - * |
54 |
| - * @param url |
55 |
| - * the url to connect to. |
56 |
| - * @param client |
57 |
| - * the HTTP client to use |
58 |
| - * @return a InfluxDB adapter suitable to access a InfluxDB. |
59 |
| - */ |
60 |
| - public static InfluxDB connect(final String url, final OkHttpClient.Builder client) { |
61 |
| - Preconditions.checkNonEmptyString(url, "url"); |
62 |
| - Objects.requireNonNull(client, "client"); |
63 |
| - return new InfluxDBImpl(url, null, null, client); |
64 |
| - } |
| 99 | + /** |
| 100 | + * Check url is legal |
| 101 | + */ |
| 102 | + private static void checkUrl(final String url) { |
| 103 | + String colon = ":"; |
| 104 | + if (!url.contains(colon) || url.endsWith(colon)) { |
| 105 | + throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url)); |
| 106 | + } |
65 | 107 |
|
66 |
| - /** |
67 |
| - * Create a connection to a InfluxDB. |
68 |
| - * |
69 |
| - * @param url |
70 |
| - * the url to connect to. |
71 |
| - * @param username |
72 |
| - * the username which is used to authorize against the influxDB instance. |
73 |
| - * @param password |
74 |
| - * the password for the username which is used to authorize against the influxDB |
75 |
| - * instance. |
76 |
| - * @param client |
77 |
| - * the HTTP client to use |
78 |
| - * @return a InfluxDB adapter suitable to access a InfluxDB. |
79 |
| - */ |
80 |
| - public static InfluxDB connect(final String url, final String username, final String password, |
81 |
| - final OkHttpClient.Builder client) { |
82 |
| - return connect(url, username, password, client, ResponseFormat.JSON); |
83 |
| - } |
| 108 | + try { |
| 109 | + URL urlObj = new URL(url); |
| 110 | + if (-1 == urlObj.getPort()) { |
| 111 | + throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url)); |
| 112 | + } |
| 113 | + } catch (MalformedURLException e) { |
| 114 | + throw new RuntimeException(e); |
| 115 | + } |
84 | 116 |
|
85 |
| - /** |
86 |
| - * Create a connection to a InfluxDB. |
87 |
| - * |
88 |
| - * @param url |
89 |
| - * the url to connect to. |
90 |
| - * @param username |
91 |
| - * the username which is used to authorize against the influxDB instance. |
92 |
| - * @param password |
93 |
| - * the password for the username which is used to authorize against the influxDB |
94 |
| - * instance. |
95 |
| - * @param client |
96 |
| - * the HTTP client to use |
97 |
| - * @param responseFormat |
98 |
| - * The {@code ResponseFormat} to use for response from InfluxDB server |
99 |
| - * @return a InfluxDB adapter suitable to access a InfluxDB. |
100 |
| - */ |
101 |
| - public static InfluxDB connect(final String url, final String username, final String password, |
102 |
| - final OkHttpClient.Builder client, final ResponseFormat responseFormat) { |
103 |
| - Preconditions.checkNonEmptyString(url, "url"); |
104 |
| - Preconditions.checkNonEmptyString(username, "username"); |
105 |
| - Objects.requireNonNull(client, "client"); |
106 |
| - return new InfluxDBImpl(url, username, password, client, responseFormat); |
107 |
| - } |
| 117 | + } |
108 | 118 | }
|
0 commit comments