|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.junit.jupiter; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URL; |
| 20 | + |
| 21 | +import org.elasticsearch.node.Node; |
| 22 | +import org.elasticsearch.node.NodeValidationException; |
| 23 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.data.elasticsearch.Utils; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.util.StringUtils; |
| 29 | + |
| 30 | +/** |
| 31 | + * This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary. The information |
| 32 | + * about the ClusterConnection is stored in a static ThreadLocal<ClusterConnectionInfo> and can be accessed with the |
| 33 | + * {@link ClusterConnection#getClusterConnectionInfo()} method. |
| 34 | + * |
| 35 | + * @author Peter-Josef Meisch |
| 36 | + */ |
| 37 | +class ClusterConnection implements ExtensionContext.Store.CloseableResource { |
| 38 | + private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class); |
| 39 | + |
| 40 | + private static final ThreadLocal<ClusterConnectionInfo> clusterConnectionInfo = new ThreadLocal<>(); |
| 41 | + |
| 42 | + private Node node; |
| 43 | + |
| 44 | + /** |
| 45 | + * creates the ClusterConnection, starting a local node if necessary. |
| 46 | + * |
| 47 | + * @param clusterUrl if null or empty a local cluster is tarted |
| 48 | + */ |
| 49 | + public ClusterConnection(@Nullable String clusterUrl) { |
| 50 | + ClusterConnectionInfo cci = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl); |
| 51 | + |
| 52 | + if (cci != null) { |
| 53 | + LOGGER.debug(cci.toString()); |
| 54 | + clusterConnectionInfo.set(cci); |
| 55 | + } else { |
| 56 | + LOGGER.error("could not create ClusterConnectionInfo"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Nullable |
| 61 | + public static ClusterConnectionInfo getClusterConnectionInfo() { |
| 62 | + return clusterConnectionInfo.get(); |
| 63 | + } |
| 64 | + |
| 65 | + @Nullable |
| 66 | + private ClusterConnectionInfo parseUrl(String clusterUrl) { |
| 67 | + try { |
| 68 | + URL url = new URL(clusterUrl); |
| 69 | + |
| 70 | + if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) { |
| 71 | + throw new IllegalArgumentException("invalid url " + clusterUrl); |
| 72 | + } |
| 73 | + |
| 74 | + return ClusterConnectionInfo.builder() // |
| 75 | + .withHostAndPort(url.getHost(), url.getPort()) // |
| 76 | + .useSsl(url.getProtocol().equals("https")) // |
| 77 | + .build(); |
| 78 | + } catch (Exception e) { |
| 79 | + LOGGER.warn("could not parse URL {}", clusterUrl, e); |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private @Nullable ClusterConnectionInfo startLocalNode() { |
| 86 | + LOGGER.debug("starting local node"); |
| 87 | + |
| 88 | + try { |
| 89 | + node = Utils.getNode(); |
| 90 | + node.start(); |
| 91 | + return ClusterConnectionInfo.builder() // |
| 92 | + .withHostAndPort("localhost", 9200) // |
| 93 | + .withClient(node.client()) // |
| 94 | + .build(); |
| 95 | + } catch (NodeValidationException e) { |
| 96 | + LOGGER.error("could not start local node", e); |
| 97 | + } |
| 98 | + |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void close() throws Exception { |
| 104 | + |
| 105 | + if (node != null) { |
| 106 | + LOGGER.debug("closing node"); |
| 107 | + try { |
| 108 | + node.close(); |
| 109 | + } catch (IOException ignored) {} |
| 110 | + } |
| 111 | + LOGGER.debug("closed"); |
| 112 | + } |
| 113 | +} |
0 commit comments