|
| 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.MalformedURLException; |
| 20 | +import java.net.URL; |
| 21 | + |
| 22 | +import org.elasticsearch.node.Node; |
| 23 | +import org.elasticsearch.node.NodeValidationException; |
| 24 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.springframework.data.elasticsearch.Utils; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary. The information |
| 33 | + * about the ClusterConnection is stored botha s a varaible in the instance for direct aaces from JUnit 5 and in a |
| 34 | + * static ThreadLocal<ClusterConnectionInfo> acessible with the {@link ClusterConnection#clusterConnectionInfo()} method |
| 35 | + * to be integrated in the Spring setup |
| 36 | + * |
| 37 | + * @author Peter-Josef Meisch |
| 38 | + */ |
| 39 | +class ClusterConnection implements ExtensionContext.Store.CloseableResource { |
| 40 | + private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class); |
| 41 | + |
| 42 | + private static final ThreadLocal<ClusterConnectionInfo> clusterConnectionInfoThreadLocal = new ThreadLocal<>(); |
| 43 | + |
| 44 | + private Node node; |
| 45 | + private final ClusterConnectionInfo clusterConnectionInfo; |
| 46 | + |
| 47 | + /** |
| 48 | + * creates the ClusterConnection, starting a local node if necessary. |
| 49 | + * |
| 50 | + * @param clusterUrl if null or empty a local cluster is tarted |
| 51 | + */ |
| 52 | + public ClusterConnection(@Nullable String clusterUrl) { |
| 53 | + clusterConnectionInfo = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl); |
| 54 | + |
| 55 | + if (clusterConnectionInfo != null) { |
| 56 | + LOGGER.debug(clusterConnectionInfo.toString()); |
| 57 | + clusterConnectionInfoThreadLocal.set(clusterConnectionInfo); |
| 58 | + } else { |
| 59 | + LOGGER.error("could not create ClusterConnectionInfo"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return the {@link ClusterConnectionInfo} from the ThreadLocal storage. |
| 65 | + */ |
| 66 | + @Nullable |
| 67 | + public static ClusterConnectionInfo clusterConnectionInfo() { |
| 68 | + return clusterConnectionInfoThreadLocal.get(); |
| 69 | + } |
| 70 | + |
| 71 | + public ClusterConnectionInfo getClusterConnectionInfo() { |
| 72 | + return clusterConnectionInfo; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param clusterUrl the URL to parse |
| 77 | + * @return the connection information |
| 78 | + */ |
| 79 | + private ClusterConnectionInfo parseUrl(String clusterUrl) { |
| 80 | + try { |
| 81 | + URL url = new URL(clusterUrl); |
| 82 | + |
| 83 | + if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) { |
| 84 | + throw new ClusterConnectionException("invalid url " + clusterUrl); |
| 85 | + } |
| 86 | + |
| 87 | + return ClusterConnectionInfo.builder() // |
| 88 | + .withHostAndPort(url.getHost(), url.getPort()) // |
| 89 | + .useSsl(url.getProtocol().equals("https")) // |
| 90 | + .build(); |
| 91 | + } catch (MalformedURLException e) { |
| 92 | + throw new ClusterConnectionException(e); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + private @Nullable ClusterConnectionInfo startLocalNode() { |
| 98 | + LOGGER.debug("starting local node"); |
| 99 | + |
| 100 | + try { |
| 101 | + node = Utils.getNode(); |
| 102 | + node.start(); |
| 103 | + return ClusterConnectionInfo.builder() // |
| 104 | + .withHostAndPort("localhost", 9200) // |
| 105 | + .withClient(node.client()) // |
| 106 | + .build(); |
| 107 | + } catch (NodeValidationException e) { |
| 108 | + LOGGER.error("could not start local node", e); |
| 109 | + } |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void close() throws Exception { |
| 116 | + |
| 117 | + if (node != null) { |
| 118 | + LOGGER.debug("closing node"); |
| 119 | + try { |
| 120 | + node.close(); |
| 121 | + } catch (IOException ignored) {} |
| 122 | + } |
| 123 | + LOGGER.debug("closed"); |
| 124 | + } |
| 125 | +} |
0 commit comments