|
| 1 | +/* |
| 2 | + * Copyright 2011-2022 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.neo4j.core.support; |
| 17 | + |
| 18 | +import org.neo4j.driver.Driver; |
| 19 | +import org.springframework.data.mapping.context.AbstractMappingContext; |
| 20 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 21 | +import org.springframework.lang.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Representation of a user agent containing sensible information to identify queries generated by or executed via Spring Data Neo4j. |
| 25 | + * |
| 26 | + * @author Michael J. Simons |
| 27 | + * @since 6.1.11 |
| 28 | + */ |
| 29 | +public enum UserAgent { |
| 30 | + |
| 31 | + INSTANCE( |
| 32 | + getVersionOf(Driver.class), |
| 33 | + getVersionOf(AbstractMappingContext.class), |
| 34 | + getVersionOf(EnableNeo4jRepositories.class) |
| 35 | + ); |
| 36 | + |
| 37 | + @Nullable |
| 38 | + private final String driverVersion; |
| 39 | + |
| 40 | + @Nullable |
| 41 | + private final String springDataVersion; |
| 42 | + |
| 43 | + @Nullable |
| 44 | + private final String sdnVersion; |
| 45 | + |
| 46 | + private final String representation; |
| 47 | + |
| 48 | + UserAgent(@Nullable String driverVersion, @Nullable String springDataVersion, @Nullable String sdnVersion) { |
| 49 | + int idxOfDash = driverVersion == null ? -1 : driverVersion.indexOf('-'); |
| 50 | + this.driverVersion = driverVersion == null ? |
| 51 | + null : |
| 52 | + driverVersion.substring(0, idxOfDash > 0 ? idxOfDash : driverVersion.length()); |
| 53 | + this.springDataVersion = springDataVersion; |
| 54 | + this.sdnVersion = sdnVersion; |
| 55 | + |
| 56 | + String unknown = "-"; |
| 57 | + this.representation = String.format("Java/%s (%s %s %s) neo4j-java/%s spring-data/%s spring-data-neo4j/%s", |
| 58 | + System.getProperty("java.version"), |
| 59 | + System.getProperty("java.vm.vendor"), |
| 60 | + System.getProperty("java.vm.name"), |
| 61 | + System.getProperty("java.vm.version"), |
| 62 | + this.driverVersion == null ? unknown : this.driverVersion, |
| 63 | + this.springDataVersion == null ? unknown : this.springDataVersion, |
| 64 | + this.sdnVersion == null ? unknown : this.sdnVersion |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @Nullable |
| 69 | + public String getDriverVersion() { |
| 70 | + return driverVersion; |
| 71 | + } |
| 72 | + |
| 73 | + @Nullable |
| 74 | + public String getSpringDataVersion() { |
| 75 | + return springDataVersion; |
| 76 | + } |
| 77 | + |
| 78 | + @Nullable |
| 79 | + public String getSdnVersion() { |
| 80 | + return sdnVersion; |
| 81 | + } |
| 82 | + |
| 83 | + @Nullable |
| 84 | + private static String getVersionOf(Class<?> type) { |
| 85 | + |
| 86 | + Package p = type.getPackage(); |
| 87 | + String version = p.getImplementationVersion(); |
| 88 | + if (!(version == null || version.trim().isEmpty())) { |
| 89 | + return version; |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String toString() { |
| 96 | + return this.representation; |
| 97 | + } |
| 98 | +} |
0 commit comments