Skip to content

Commit b9e7609

Browse files
committed
feat: add python and java artifact lib
Signed-off-by: peefy <[email protected]>
1 parent 2bb2a6d commit b9e7609

File tree

41 files changed

+49906
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+49906
-13
lines changed

java/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "kcl-lib-jni"
3+
publish = false
4+
version = "0.1.0"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
doc = false
9+
10+
[dependencies]
11+
jni = "0.21.1"
12+
anyhow = "1"
13+
kcl-lang = {path = "../"}

java/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build:
2+
mvn -B install -U -DskipTests=true
3+
4+
test:
5+

java/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# KCL Artifact Library for Java

java/pom.xml

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.kcl</groupId>
8+
<artifactId>kcl-java</artifactId>
9+
<version>0.1.0</version>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<surefire.version>2.20</surefire.version>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
18+
<dependency>
19+
<groupId>com.google.protobuf</groupId>
20+
<artifactId>protobuf-java</artifactId>
21+
<version>3.21.1</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>1.7.30</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.alibaba</groupId>
30+
<artifactId>fastjson</artifactId>
31+
<version>1.2.68</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-lang3</artifactId>
36+
<version>3.4</version>
37+
</dependency>
38+
<!-- test dependencies -->
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<version>1.2.3</version>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.13</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.openjdk.jmh</groupId>
53+
<artifactId>jmh-core</artifactId>
54+
<version>1.28</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.openjdk.jmh</groupId>
59+
<artifactId>jmh-generator-annprocess</artifactId>
60+
<version>1.28</version>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-jar-plugin</artifactId>
69+
<version>3.2.0</version>
70+
<configuration>
71+
<archive>
72+
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
73+
</archive>
74+
</configuration>
75+
</plugin>
76+
<plugin>
77+
<groupId>org.apache.maven.plugins</groupId>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<configuration>
80+
<source>${java.version}</source>
81+
<target>${java.version}</target>
82+
<encoding>UTF-8</encoding>
83+
</configuration>
84+
<version>3.3</version>
85+
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<version>${surefire.version}</version>
90+
<configuration>
91+
<testFailureIgnore>true</testFailureIgnore>
92+
<threadCount>1</threadCount>
93+
<properties>
94+
<junit>false</junit>
95+
</properties>
96+
<includes>
97+
<include>**/*Tests.java</include>
98+
<include>**/*Test.java</include>
99+
</includes>
100+
<excludes>
101+
<exclude>**/Abstract*.java</exclude>
102+
</excludes>
103+
</configuration>
104+
<dependencies>
105+
<dependency>
106+
<groupId>org.apache.maven.surefire</groupId>
107+
<artifactId>surefire-junit47</artifactId>
108+
<version>${surefire.version}</version>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.apache.maven.surefire</groupId>
112+
<artifactId>surefire-testng</artifactId>
113+
<version>${surefire.version}</version>
114+
</dependency>
115+
</dependencies>
116+
</plugin>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-source-plugin</artifactId>
120+
<version>2.0.2</version>
121+
<executions>
122+
<execution>
123+
<id>attach-sources</id>
124+
<goals>
125+
<goal>jar</goal>
126+
</goals>
127+
</execution>
128+
</executions>
129+
</plugin>
130+
<plugin>
131+
<groupId>org.apache.maven.plugins</groupId>
132+
<artifactId>maven-eclipse-plugin</artifactId>
133+
<version>2.6</version>
134+
<configuration>
135+
<downloadSources>true</downloadSources>
136+
</configuration>
137+
</plugin>
138+
</plugins>
139+
</build>
140+
<profiles>
141+
<profile>
142+
<id>dev</id>
143+
<activation>
144+
<activeByDefault>true</activeByDefault>
145+
</activation>
146+
</profile>
147+
<profile>
148+
<id>test</id>
149+
<activation>
150+
<property>
151+
<name>env</name>
152+
<value>test</value>
153+
</property>
154+
</activation>
155+
</profile>
156+
<profile>
157+
<id>prod</id>
158+
<activation>
159+
<property>
160+
<name>env</name>
161+
<value>prod</value>
162+
</property>
163+
</activation>
164+
</profile>
165+
</profiles>
166+
</project>

java/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
extern crate anyhow;
2+
extern crate jni;
3+
extern crate kcl_lang;
4+
5+
use anyhow::Result;
6+
use jni::objects::{JByteArray, JClass, JObject};
7+
use jni::sys::jbyteArray;
8+
use jni::JNIEnv;
9+
10+
#[no_mangle]
11+
pub extern "system" fn Java_com_kcl_api_API_callNative(
12+
mut env: JNIEnv,
13+
_: JClass,
14+
name: JByteArray,
15+
args: JByteArray,
16+
) -> jbyteArray {
17+
intern_call_native(&mut env, name, args).unwrap_or_else(|e| {
18+
let _ = throw(&mut env, e);
19+
JObject::default().into_raw()
20+
})
21+
}
22+
23+
fn intern_call_native(env: &mut JNIEnv, name: JByteArray, args: JByteArray) -> Result<jbyteArray> {
24+
let api = kcl_lang::API::new()?;
25+
let name = env.convert_byte_array(name)?;
26+
let args = env.convert_byte_array(args)?;
27+
let result = api.call_native(&name, &args)?;
28+
let j_byte_array = env.byte_array_from_slice(result)?;
29+
Ok(j_byte_array.into_raw())
30+
}
31+
32+
fn throw(env: &mut JNIEnv, error: anyhow::Error) -> jni::errors::Result<()> {
33+
env.throw(("java/lang/Exception", error.to_string()))
34+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.kcl.api;
2+
3+
import com.kcl.api.Spec.*;
4+
5+
public class API implements Service {
6+
static {
7+
// Load the dynamic library (the .dll, .so, or .dylib file)
8+
System.loadLibrary("kcl_lib_jni");
9+
}
10+
11+
private native byte[] callNative(byte[] call, byte[] args);
12+
13+
// Example method that calls a native function
14+
public ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception {
15+
return ExecProgram_Result.parseFrom(call("KclvmService.ExecProgram", args.toByteArray()));
16+
}
17+
18+
@Override
19+
public OverrideFile_Result overrideFile(OverrideFile_Args args) throws Exception {
20+
return OverrideFile_Result.parseFrom(call("KclvmService.OverrideFile", args.toByteArray()));
21+
}
22+
23+
@Override
24+
public GetSchemaType_Result getFullSchemaType(GetFullSchemaType_Args args) throws Exception {
25+
return GetSchemaType_Result.parseFrom(call("KclvmService.GetSchemaType", args.toByteArray()));
26+
}
27+
28+
@Override
29+
public FormatCode_Result formatCode(FormatCode_Args args) throws Exception {
30+
return FormatCode_Result.parseFrom(call("KclvmService.FormatCode", args.toByteArray()));
31+
}
32+
33+
@Override
34+
public FormatPath_Result formatPath(FormatPath_Args args) throws Exception {
35+
return FormatPath_Result.parseFrom(call("KclvmService.FormatPath", args.toByteArray()));
36+
}
37+
38+
@Override
39+
public LintPath_Result lintPath(LintPath_Args args) throws Exception {
40+
return LintPath_Result.parseFrom(call("KclvmService.LintPath", args.toByteArray()));
41+
}
42+
43+
@Override
44+
public ValidateCode_Result validateCode(ValidateCode_Args args) throws Exception {
45+
return ValidateCode_Result.parseFrom(call("KclvmService.ValidateCode", args.toByteArray()));
46+
}
47+
48+
@Override
49+
public LoadSettingsFiles_Result loadSettingsFiles(LoadSettingsFiles_Args args) throws Exception {
50+
return LoadSettingsFiles_Result.parseFrom(call("KclvmService.LoadSettingsFiles", args.toByteArray()));
51+
}
52+
53+
@Override
54+
public Rename_Result rename(Rename_Args args) throws Exception {
55+
return Rename_Result.parseFrom(call("KclvmService.Rename", args.toByteArray()));
56+
}
57+
58+
@Override
59+
public RenameCode_Result renameCode(RenameCode_Args args) throws Exception {
60+
return RenameCode_Result.parseFrom(call("KclvmService.RenameCode", args.toByteArray()));
61+
}
62+
63+
@Override
64+
public Test_Result test(Test_Args args) throws Exception {
65+
return Test_Result.parseFrom(call("KclvmService.Test", args.toByteArray()));
66+
}
67+
68+
private byte[] call(String name, byte[] args) throws Exception {
69+
byte[] result = callNative(name.getBytes(), args);
70+
if (result != null && startsWith(result, "ERROR")) {
71+
throw new Error(result.toString());
72+
}
73+
return result;
74+
}
75+
76+
static boolean startsWith(byte[] array, String prefix) {
77+
byte[] prefixBytes = prefix.getBytes();
78+
if (array.length < prefixBytes.length) {
79+
return false;
80+
}
81+
82+
for (int i = 0; i < prefixBytes.length; i++) {
83+
if (array[i] != prefixBytes[i]) {
84+
return false;
85+
}
86+
}
87+
88+
return true;
89+
}
90+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.kcl.api;
2+
3+
import com.kcl.api.Spec.*;
4+
5+
public interface Service {
6+
// Execute KCL file with args
7+
ExecProgram_Result execProgram(ExecProgram_Args args) throws Exception;
8+
9+
// Override KCL file with args
10+
OverrideFile_Result overrideFile(OverrideFile_Args args) throws Exception;
11+
12+
// Service for getting the full schema type list
13+
GetSchemaType_Result getFullSchemaType(GetFullSchemaType_Args args) throws Exception;
14+
15+
// Service for formatting a code source
16+
FormatCode_Result formatCode(FormatCode_Args args) throws Exception;
17+
18+
// Service for formatting KCL file or directory path
19+
FormatPath_Result formatPath(FormatPath_Args args) throws Exception;
20+
21+
// Service for KCL Lint API
22+
LintPath_Result lintPath(LintPath_Args args) throws Exception;
23+
24+
// Service for validating the data string using the schema code string
25+
ValidateCode_Result validateCode(ValidateCode_Args args) throws Exception;
26+
27+
// Service for building setting file config from args
28+
LoadSettingsFiles_Result loadSettingsFiles(LoadSettingsFiles_Args args) throws Exception;
29+
30+
// Service for renaming all the occurrences of the target symbol in the files
31+
Rename_Result rename(Rename_Args args) throws Exception;
32+
33+
// Service for renaming all the occurrences of the target symbol and rename them
34+
RenameCode_Result renameCode(RenameCode_Args args) throws Exception;
35+
36+
// Service for the testing tool
37+
Test_Result test(Test_Args args) throws Exception;
38+
}
39+

0 commit comments

Comments
 (0)