diff --git a/hugegraph-commons/hugegraph-common/pom.xml b/hugegraph-commons/hugegraph-common/pom.xml index 4c84b30c99..a57bcf59cd 100644 --- a/hugegraph-commons/hugegraph-common/pom.xml +++ b/hugegraph-commons/hugegraph-common/pom.xml @@ -238,6 +238,12 @@ + + + src/main/resources + true + + org.apache.maven.plugins diff --git a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/VersionUtil.java b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/VersionUtil.java index b49adda87a..87667347ec 100644 --- a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/VersionUtil.java +++ b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/util/VersionUtil.java @@ -19,14 +19,18 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.Objects; +import java.util.Properties; import java.util.jar.Attributes; import java.util.jar.Manifest; public final class VersionUtil { + private static volatile Properties CACHED_PROPERTIES = null; + private static final Object VERSION_FILE_LOCK = new Object(); /** * Compare if a version is inside a range [begin, end) * @param version The version to be compared @@ -102,6 +106,72 @@ public static String getImplementationVersion(String manifestPath) { .getValue(Attributes.Name.IMPLEMENTATION_VERSION); } + public static Properties loadProperties() { + if (CACHED_PROPERTIES == null) { + synchronized (VERSION_FILE_LOCK) { + if (CACHED_PROPERTIES == null) { + final Properties props = new Properties(); + try (InputStream is = VersionUtil.class.getResourceAsStream( + "/version.properties")) { + if (is == null) { + throw new RuntimeException( + "version.properties file not found in classpath"); + } + props.load(is); + CACHED_PROPERTIES = props; + } catch (IOException e) { + throw new RuntimeException("Could not load version.properties", e); + } + } + } + } + return CACHED_PROPERTIES; + } + + /** + * Get version from properties + * @return The common version + */ + public static String getVersionFromProperties() { + Properties props = loadProperties(); + String version = props.getProperty("Version"); + if (version == null) { + throw new RuntimeException("Version property not found in version.properties"); + } + return version; + } + + /** + * Get api version from properties + * @return The api version + */ + public static String getApiVersionFromProperties() { + Properties props = loadProperties(); + String apiVersion = props.getProperty("ApiVersion"); + if (apiVersion == null) { + throw new RuntimeException("ApiVersion property not found in version.properties"); + } + return apiVersion; + } + + public static String getApiCheckBeginVersionFromProperties() { + Properties props = loadProperties(); + String apiVersion = props.getProperty("ApiCheckBeginVersion"); + if (apiVersion == null) { + throw new RuntimeException("ApiCheckBeginVersion property not found in version.properties"); + } + return apiVersion; + } + + public static String getApiCheckEndVersionFromProperties() { + Properties props = loadProperties(); + String apiVersion = props.getProperty("ApiCheckEndVersion"); + if (apiVersion == null) { + throw new RuntimeException("ApiCheckEndVersion property not found in version.properties"); + } + return apiVersion; + } + /** * Get version from pom.xml * @return The pom version diff --git a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java index 73342fdaaa..1bf90ab782 100644 --- a/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java +++ b/hugegraph-commons/hugegraph-common/src/main/java/org/apache/hugegraph/version/CommonVersion.java @@ -17,6 +17,7 @@ package org.apache.hugegraph.version; +import org.apache.hugegraph.util.VersionUtil; import org.apache.hugegraph.util.VersionUtil.Version; public class CommonVersion { @@ -24,5 +25,5 @@ public class CommonVersion { public static final String NAME = "hugegraph-common"; // The second parameter of Version.of() is for all-in-one JAR - public static final Version VERSION = Version.of(CommonVersion.class, "1.5.0"); + public static final Version VERSION = Version.of(CommonVersion.class, VersionUtil.getVersionFromProperties()); } diff --git a/hugegraph-commons/hugegraph-common/src/main/resources/version.properties b/hugegraph-commons/hugegraph-common/src/main/resources/version.properties new file mode 100644 index 0000000000..b413bf04e3 --- /dev/null +++ b/hugegraph-commons/hugegraph-common/src/main/resources/version.properties @@ -0,0 +1,23 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# When updating the version, Version can be read from the pom file. +# When hugegraph-common is updated, hugegraph-commons.version in the pom file needs to be updated, +# and VersionInBash needs to be updated in this file. +Version=${revision} +ApiVersion=0.71 +ApiCheckBeginVersion=1.0 +ApiCheckEndVersion=1.7 +VersionInBash=1.5.0 diff --git a/hugegraph-commons/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java b/hugegraph-commons/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java index a2dd3d72c1..e2c41d8e29 100644 --- a/hugegraph-commons/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java +++ b/hugegraph-commons/hugegraph-rpc/src/main/java/org/apache/hugegraph/version/RpcVersion.java @@ -17,6 +17,7 @@ package org.apache.hugegraph.version; +import org.apache.hugegraph.util.VersionUtil; import org.apache.hugegraph.util.VersionUtil.Version; public class RpcVersion { @@ -24,5 +25,5 @@ public class RpcVersion { public static final String NAME = "hugegraph-rpc"; // The second parameter of Version.of() is for all-in-one JAR - public static final Version VERSION = Version.of(RpcVersion.class, "1.5.0"); + public static final Version VERSION = Version.of(RpcVersion.class, VersionUtil.getVersionFromProperties()); } diff --git a/hugegraph-pd/hg-pd-service/pom.xml b/hugegraph-pd/hg-pd-service/pom.xml index 37c90fe869..81b4568701 100644 --- a/hugegraph-pd/hg-pd-service/pom.xml +++ b/hugegraph-pd/hg-pd-service/pom.xml @@ -124,7 +124,7 @@ org.apache.hugegraph hugegraph-common - 1.2.0 + ${hugegraph-commons.version} org.apache.logging.log4j diff --git a/hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/API.java b/hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/API.java index a2287cb83e..70fea99f2b 100644 --- a/hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/API.java +++ b/hugegraph-pd/hg-pd-service/src/main/java/org/apache/hugegraph/pd/rest/API.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.hugegraph.pd.common.PDException; +import org.apache.hugegraph.util.VersionUtil; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -33,7 +34,7 @@ public class API { // TODO: use a flexible way to define the version // refer: https://github.com/apache/hugegraph/pull/2528#discussion_r1573823996 - public static final String VERSION = "1.5.0"; + public static final String VERSION = VersionUtil.getVersionFromProperties(); public static final String PD = "PD"; public static final String STORE = "STORE"; public static String STATUS_KEY = "status"; diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java index 7784613f53..9f9134ffa3 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/server/ApplicationConfig.java @@ -36,18 +36,23 @@ import org.glassfish.jersey.server.monitoring.ApplicationEventListener; import org.glassfish.jersey.server.monitoring.RequestEvent; import org.glassfish.jersey.server.monitoring.RequestEventListener; +import org.glassfish.jersey.servlet.ServletProperties; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.jersey3.InstrumentedResourceMethodApplicationListener; +import io.swagger.v3.oas.integration.OpenApiConfigurationException; +import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder; import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource; -import io.swagger.v3.oas.annotations.OpenAPIDefinition; import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; -import io.swagger.v3.oas.annotations.info.Contact; -import io.swagger.v3.oas.annotations.info.Info; -import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityScheme; +import io.swagger.v3.oas.integration.SwaggerConfiguration; +import io.swagger.v3.oas.models.OpenAPI; +import jakarta.servlet.ServletConfig; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.security.SecurityRequirement; import jakarta.ws.rs.ApplicationPath; +import jakarta.ws.rs.core.Context; @SecurityScheme( name = "basic", @@ -60,16 +65,9 @@ scheme = "bearer" ) @ApplicationPath("/") -@OpenAPIDefinition( - info = @Info( - title = "HugeGraph RESTful API", - version = CoreVersion.DEFAULT_VERSION, - description = "All management API for HugeGraph", - contact = @Contact(url = "https://github.com/apache/hugegraph", name = "HugeGraph") - ), - security = {@SecurityRequirement(name = "basic"), @SecurityRequirement(name = "bearer")} -) public class ApplicationConfig extends ResourceConfig { + @Context + private ServletConfig servletConfig; public ApplicationConfig(HugeConfig conf, EventHub hub) { packages("org.apache.hugegraph.api"); @@ -95,10 +93,56 @@ public ApplicationConfig(HugeConfig conf, EventHub hub) { MetricRegistry registry = MetricManager.INSTANCE.getRegistry(); register(new InstrumentedResourceMethodApplicationListener(registry)); - // Register OpenApi file to support display on swagger-ui + // Set OpenApi in runtime + registerOpenApi(); + register(OpenApiResource.class); } + + void registerOpenApi() { + OpenAPI openAPI = new OpenAPI(); + Info info = new Info() + .title("HugeGraph RESTful API") + .version(CoreVersion.DEFAULT_VERSION) + .description("All management API for HugeGraph") + .contact(new io.swagger.v3.oas.models.info.Contact() + .name("HugeGraph") + .url("https://github.com/apache/hugegraph")); + + openAPI.setInfo(info); + openAPI.addSecurityItem(new SecurityRequirement().addList("basic")); + openAPI.addSecurityItem(new SecurityRequirement().addList("bearer")); + + SwaggerConfiguration oasConfig = new SwaggerConfiguration() + .openAPI(openAPI) + .prettyPrint(true); + register(new ApplicationEventListener() { + @Override + public void onEvent(ApplicationEvent event) { + if (event.getType() == ApplicationEvent.Type.INITIALIZATION_FINISHED) { + try { + JaxrsOpenApiContextBuilder builder = + (JaxrsOpenApiContextBuilder) new JaxrsOpenApiContextBuilder() + .application(ApplicationConfig.this) + .openApiConfiguration(oasConfig); + if (servletConfig != null) { + builder.servletConfig(servletConfig); + } + builder.buildContext(true); + } catch (OpenApiConfigurationException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + } + + @Override + public RequestEventListener onRequest(RequestEvent requestEvent) { + return null; + } + }); + } + private class ConfFactory extends AbstractBinder implements Factory { private final HugeConfig conf; diff --git a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java index bbc06ad309..7e314f9ed6 100644 --- a/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java +++ b/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/version/ApiVersion.java @@ -127,10 +127,10 @@ public final class ApiVersion { * The second parameter of Version.of() is for IDE running without JAR * Note: Also update the version number in hugegraph-server/hugegraph-api/pom.xml */ - public static final Version VERSION = Version.of(ApiVersion.class, "0.71"); + public static final Version VERSION = Version.of(ApiVersion.class, VersionUtil.getApiVersionFromProperties()); public static void check() { // Check the version of hugegraph-core. Do first check from version 0.3 - VersionUtil.check(CoreVersion.VERSION, "1.0", "1.6", CoreVersion.NAME); + VersionUtil.check(CoreVersion.VERSION, VersionUtil.getApiCheckBeginVersionFromProperties(), VersionUtil.getApiCheckEndVersionFromProperties(), CoreVersion.NAME); } } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/version/CoreVersion.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/version/CoreVersion.java index 468663c924..2eff71487a 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/version/CoreVersion.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/version/CoreVersion.java @@ -23,7 +23,7 @@ public class CoreVersion { public static final String NAME = "hugegraph-core"; - public static final String DEFAULT_VERSION = "1.5.0"; + public static final String DEFAULT_VERSION = VersionUtil.getVersionFromProperties(); /** * The second parameter of Version.of() is for IDE running without JAR */ diff --git a/hugegraph-server/hugegraph-dist/src/assembly/travis/start-pd.sh b/hugegraph-server/hugegraph-dist/src/assembly/travis/start-pd.sh index 9f694d5c3c..bab4adcc8d 100755 --- a/hugegraph-server/hugegraph-dist/src/assembly/travis/start-pd.sh +++ b/hugegraph-server/hugegraph-dist/src/assembly/travis/start-pd.sh @@ -18,7 +18,18 @@ set -ev HOME_DIR=$(pwd) -PD_DIR=$HOME_DIR/hugegraph-pd/apache-hugegraph-pd-incubating-1.5.0 + +PROPERTIES_FILE="$HOME_DIR/hugegraph-commons/hugegraph-common/src/main/resources/version.properties" +if [ -f "$PROPERTIES_FILE" ]; then + set -a + source "$PROPERTIES_FILE" + set +a +else + echo "Error: properties file not found at $PROPERTIES_FILE" + exit 1 +fi + +PD_DIR=$HOME_DIR/hugegraph-pd/apache-hugegraph-pd-incubating-$VersionInBash pushd $PD_DIR . bin/start-hugegraph-pd.sh diff --git a/hugegraph-server/hugegraph-dist/src/assembly/travis/start-store.sh b/hugegraph-server/hugegraph-dist/src/assembly/travis/start-store.sh index 23e8f2297c..8882df3a8e 100755 --- a/hugegraph-server/hugegraph-dist/src/assembly/travis/start-store.sh +++ b/hugegraph-server/hugegraph-dist/src/assembly/travis/start-store.sh @@ -18,7 +18,18 @@ set -ev HOME_DIR=$(pwd) -STORE_DIR=$HOME_DIR/hugegraph-store/apache-hugegraph-store-incubating-1.5.0 + +PROPERTIES_FILE="$HOME_DIR/hugegraph-commons/hugegraph-common/src/main/resources/version.properties" +if [ -f "$PROPERTIES_FILE" ]; then + set -a + source "$PROPERTIES_FILE" + set +a +else + echo "Error: properties file not found at $PROPERTIES_FILE" + exit 1 +fi + +STORE_DIR=$HOME_DIR/hugegraph-store/apache-hugegraph-store-incubating-$VersionInBash pushd $STORE_DIR . bin/start-hugegraph-store.sh