From 783be0cd2a6064f1629b61407bbae27b83fc4d02 Mon Sep 17 00:00:00 2001
From: sukidayou <77531556+sukidayou@users.noreply.github.com>
Date: Thu, 12 Dec 2024 23:44:26 +0800
Subject: [PATCH] feat: support "-v/--version" to get version of
casbin-java-cli (#28)
---
pom.xml | 43 +++++++
src/main/java/org/casbin/Client.java | 7 ++
.../org/casbin/util/DependencyHandler.java | 111 ++++++++++++++++++
.../java/org/casbin/util/VersionUtil.java | 85 ++++++++++++++
4 files changed, 246 insertions(+)
create mode 100644 src/main/java/org/casbin/util/DependencyHandler.java
create mode 100644 src/main/java/org/casbin/util/VersionUtil.java
diff --git a/pom.xml b/pom.xml
index 1ac83cf..e628cf9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -78,6 +78,49 @@
+
+ org.apache.maven.plugins
+ maven-resources-plugin
+ 3.3.0
+
+
+ generate-version-info
+ validate
+
+ copy-resources
+
+
+ ${project.build.outputDirectory}/META-INF
+
+
+ ${project.basedir}
+
+ pom.xml
+
+
+
+
+
+
+
+
+ pl.project13.maven
+ git-commit-id-plugin
+ 4.9.10
+
+
+
+ revision
+
+
+
+
+ 7
+ true
+ ${project.build.outputDirectory}/META-INF/git.properties
+ properties
+
+
\ No newline at end of file
diff --git a/src/main/java/org/casbin/Client.java b/src/main/java/org/casbin/Client.java
index 09af32a..a2dadf2 100644
--- a/src/main/java/org/casbin/Client.java
+++ b/src/main/java/org/casbin/Client.java
@@ -7,6 +7,8 @@
import java.util.*;
+import static org.casbin.util.VersionUtil.*;
+
public class Client {
public static String run(String... args) {
@@ -21,6 +23,11 @@ public static String run(String... args) {
if(Objects.equals(commandName, "-h") || Objects.equals(commandName, "--help")){
printHelpMessage();
return result;
+ } else if(Objects.equals(commandName, "-v") || Objects.equals(commandName, "--version")){
+ String cliVersion = getCliVersion();
+ String jcasbinVersion = getCasbinVersion("org.casbin","jcasbin");
+ System.out.printf("casbin-java-cli %s\njcasbin %s%n",cliVersion,jcasbinVersion);
+ return result;
}
// processing line breaks in parameters
diff --git a/src/main/java/org/casbin/util/DependencyHandler.java b/src/main/java/org/casbin/util/DependencyHandler.java
new file mode 100644
index 0000000..f971300
--- /dev/null
+++ b/src/main/java/org/casbin/util/DependencyHandler.java
@@ -0,0 +1,111 @@
+// Copyright 2024 The casbin Authors. All Rights Reserved.
+//
+// Licensed 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.
+
+package org.casbin.util;
+
+import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.*;
+
+/***
+ * A custom handler for parsing XML dependencies (POM file format) using SAX (Simple API for XML).
+ * This handler looks for a specific dependency identified by the group ID and artifact ID,
+ * and extracts its version information.
+ */
+public class DependencyHandler extends DefaultHandler {
+ private final String targetGroupId;
+ private final String targetArtifactId;
+ private String currentElement;
+ private String currentGroupId;
+ private String currentArtifactId;
+ private String currentVersion;
+ private String dependencyVersion;
+
+ /**
+ * Constructor to initialize the handler with the target groupId and artifactId.
+ *
+ * @param groupId The groupId of the dependency to search for.
+ * @param artifactId The artifactId of the dependency to search for.
+ */
+ public DependencyHandler(String groupId, String artifactId) {
+ this.targetGroupId = groupId;
+ this.targetArtifactId = artifactId;
+ }
+
+ /**
+ * Called when a new element is encountered during the XML parsing.
+ *
+ * @param uri The namespace URI (if any).
+ * @param localName The local name of the element.
+ * @param qName The qualified name of the element.
+ * @param attributes The attributes of the element.
+ */
+ @Override
+ public void startElement(String uri, String localName, String qName, Attributes attributes) {
+ currentElement = qName;
+ if ("dependency".equals(currentElement)) {
+ currentGroupId = null;
+ currentArtifactId = null;
+ currentVersion = null;
+ }
+ }
+
+ /**
+ * Called when the end of an element is reached during XML parsing.
+ *
+ * @param uri The namespace URI (if any).
+ * @param localName The local name of the element.
+ * @param qName The qualified name of the element.
+ */
+ @Override
+ public void endElement(String uri, String localName, String qName) {
+ if ("dependency".equals(qName)) {
+ if (targetGroupId.equals(currentGroupId) && targetArtifactId.equals(currentArtifactId)) {
+ dependencyVersion = currentVersion;
+ }
+ }
+ currentElement = null;
+ }
+
+ /**
+ * Called to process the character data inside an element during XML parsing.
+ *
+ * @param ch The character array containing the text.
+ * @param start The start index of the text.
+ * @param length The length of the text.
+ */
+ @Override
+ public void characters(char[] ch, int start, int length) {
+ String content = new String(ch, start, length).trim();
+ if (content.isEmpty()) {
+ return;
+ }
+
+ if ("groupId".equals(currentElement)) {
+ currentGroupId = content;
+ } else if ("artifactId".equals(currentElement)) {
+ currentArtifactId = content;
+ } else if ("version".equals(currentElement)) {
+ currentVersion = content;
+ }
+ }
+
+ /**
+ * Returns the version of the dependency if found, otherwise returns "Unknown".
+ *
+ * @return The version of the target dependency.
+ */
+ public String getDependencyVersion() {
+ return dependencyVersion != null ? dependencyVersion : "Unknown";
+ }
+}
diff --git a/src/main/java/org/casbin/util/VersionUtil.java b/src/main/java/org/casbin/util/VersionUtil.java
new file mode 100644
index 0000000..60aa048
--- /dev/null
+++ b/src/main/java/org/casbin/util/VersionUtil.java
@@ -0,0 +1,85 @@
+// Copyright 2024 The casbin Authors. All Rights Reserved.
+//
+// Licensed 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.
+
+package org.casbin.util;
+
+import org.casbin.Client;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+/***
+ * Utility class for retrieving version-related information.
+ */
+public class VersionUtil {
+
+ /**
+ * Retrieves Cli version information.
+ *
+ * @return The Cli version info as a string (commit ID or tag name).
+ * @throws IOException If an error occurs while reading the git.properties file.
+ */
+ public static String getCliVersion() throws IOException {
+ Properties properties = new Properties();
+ InputStream input = Client.class.getResourceAsStream("/META-INF/git.properties");
+ if (input != null) {
+ properties.load(input);
+ }
+
+ if (properties.isEmpty()) {
+ throw new IOException("Git properties not found!");
+ }
+
+ String commitId = properties.getProperty("git.commit.id.abbrev", "Unknown");
+ String tag = properties.getProperty("git.closest.tag.name", "Unknown");
+ String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");
+
+ if(tag.isEmpty()) {
+ tag = properties.getProperty("git.tags", "Unknown");
+ }
+ if (commitCount.isEmpty()) {
+ return tag;
+ }
+ return commitId;
+ }
+
+ /**
+ * Retrieves Casbin version information.
+ *
+ * @param groupId The group ID of the dependency to search for.
+ * @param artifactId The artifact ID of the dependency to search for.
+ * @return The version of the specified dependency, or "Unknown" if not found.
+ * @throws ParserConfigurationException If a configuration error occurs during parsing.
+ * @throws SAXException If a SAX error occurs during parsing.
+ * @throws IOException If an error occurs while reading the POM file.
+ */
+ public static String getCasbinVersion(String groupId, String artifactId) throws ParserConfigurationException, SAXException, IOException {
+ InputStream inputStream = Client.class.getResourceAsStream("/META-INF/pom.xml");
+ if (inputStream == null) {
+ throw new IOException("POM file not found!");
+ }
+
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ SAXParser parser = factory.newSAXParser();
+ DependencyHandler handler = new DependencyHandler(groupId, artifactId);
+ parser.parse(inputStream, handler);
+
+ return handler.getDependencyVersion();
+ }
+}