Skip to content

Commit 4649086

Browse files
authored
Merge pull request #18 from lvivJavaClub/dsl
Init dsl demo
2 parents 8704f84 + 6b73704 commit 4649086

File tree

16 files changed

+340
-9
lines changed

16 files changed

+340
-9
lines changed

dsl-executor-service/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.lohika.jclub.dsl.service</groupId>
7+
<artifactId>dsl-executor-service</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>DSL Executor service</name>
12+
<description>DSL Executor Service</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.lohika.jclub.storage.client</groupId>
31+
<artifactId>storage-service-client</artifactId>
32+
<version>0.0.1-SNAPSHOT</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.lohika.jclub.rating.client</groupId>
36+
<artifactId>rating-service-client</artifactId>
37+
<version>0.0.1-SNAPSHOT</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.lohika.jclub.dsl</groupId>
41+
<artifactId>dsl</artifactId>
42+
<version>0.0.1-SNAPSHOT</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-actuator</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-starter-eureka</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.codehaus.groovy</groupId>
60+
<artifactId>groovy</artifactId>
61+
<version>2.4.11</version>
62+
</dependency>
63+
</dependencies>
64+
65+
<dependencyManagement>
66+
<dependencies>
67+
<dependency>
68+
<groupId>org.springframework.cloud</groupId>
69+
<artifactId>spring-cloud-dependencies</artifactId>
70+
<version>${spring-cloud.version}</version>
71+
<type>pom</type>
72+
<scope>import</scope>
73+
</dependency>
74+
</dependencies>
75+
</dependencyManagement>
76+
77+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.lohika.jclub.dsl.service;
2+
3+
import groovy.lang.GroovyShell;
4+
import groovy.util.DelegatingScript;
5+
6+
import com.lohika.jclub.dsl.MyDsl;
7+
import com.lohika.jclub.rating.client.RatingServiceClient;
8+
import com.lohika.jclub.storage.client.StorageServiceClient;
9+
10+
import org.codehaus.groovy.control.CompilerConfiguration;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PathVariable;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.nio.file.Paths;
22+
23+
@RestController
24+
@RequestMapping(path = "/dsl")
25+
public class DslController {
26+
27+
@Value("${dsl.basepath}")
28+
private String basepath;
29+
30+
@Autowired
31+
private StorageServiceClient storageServiceClient;
32+
33+
@Autowired
34+
private RatingServiceClient ratingServiceClient;
35+
36+
@GetMapping(path = "/{scriptName}")
37+
public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException {
38+
File file = new File(basepath + scriptName + ".groovy");
39+
String script = new String(Files.readAllBytes(Paths.get(file.getPath())));
40+
41+
MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient);
42+
43+
CompilerConfiguration configuration = new CompilerConfiguration();
44+
configuration.setScriptBaseClass(DelegatingScript.class.getName());
45+
46+
GroovyShell groovy = new GroovyShell(configuration);
47+
48+
DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script);
49+
delegatingScript.setDelegate(dsl);
50+
delegatingScript.run();
51+
52+
return dsl;
53+
}
54+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lohika.jclub.dsl.service;
2+
3+
import com.lohika.jclub.rating.client.EnableRatingServiceClient;
4+
import com.lohika.jclub.rating.client.RatingServiceClient;
5+
import com.lohika.jclub.storage.client.EnableStorageServiceClient;
6+
import com.lohika.jclub.storage.client.StorageServiceClient;
7+
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
11+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
12+
13+
@EnableDiscoveryClient
14+
@EnableStorageServiceClient
15+
@EnableRatingServiceClient
16+
@EnableFeignClients(clients = {StorageServiceClient.class, RatingServiceClient.class})
17+
@SpringBootApplication
18+
public class DslServiceApplication {
19+
public static void main(String[] args) {
20+
SpringApplication.run(DslServiceApplication.class, args);
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
server.port=8088
2+
spring.application.name=dsl-executor-service
3+
4+
dsl.basepath=dsl-scripts/
5+
6+
eureka.instance.preferIpAddress=true
7+
8+
feign.hystrix.enabled=true
9+
10+
endpoints.info.id=info
11+
endpoints.info.sensitive=false
12+
endpoints.info.enabled=true
13+
14+
info.app.name=DSL executor service
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
____ _____ __ _
2+
/ __ \/ ___// / ________ ______ __(_)_______
3+
/ / / /\__ \/ / / ___/ _ \/ ___/ | / / / ___/ _ \
4+
/ /_/ /___/ / /___ (__ ) __/ / | |/ / / /__/ __/
5+
/_____//____/_____/ /____/\___/_/ |___/_/\___/\___/
6+
v.${application.version}

dsl-scripts/demo.mydsl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
String myPhone = '123'
2+
String myName = 'bot'
3+
String myEmail = 'bot@company.name'
4+
5+
(1..100).each {
6+
String location = "location-${it}"
7+
double myPrice = 2
8+
double mySqft = 1 + (it as int)
9+
def rating = rating location, myPrice, mySqft
10+
11+
if (rating > 400) {
12+
apartment(location) {
13+
price myPrice
14+
sqft mySqft
15+
phone myPhone
16+
realtorName myName
17+
mail myEmail
18+
}
19+
}
20+
}

dsl-scripts/simple.mydsl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apartment {
2+
location "location"
3+
price 1
4+
sqft 1
5+
phone 'phone'
6+
realtorName 'realtorName'
7+
mail 'mail'
8+
}
9+
10+
apartment("location", {
11+
price 1
12+
sqft 1
13+
phone 'phone'
14+
realtorName 'realtorName'
15+
mail 'mail'
16+
})
17+
18+
19+
apartment("location") {
20+
price 1
21+
sqft 1
22+
phone 'phone'
23+
realtorName 'realtorName'
24+
mail 'mail'
25+
}

dsl/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.lohika.jclub.dsl</groupId>
7+
<artifactId>dsl</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>DSL</name>
12+
<description>DSL</description>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17+
<groovy.version>2.4.3</groovy.version>
18+
<groovy-eclipse-batch.version>2.4.3-01</groovy-eclipse-batch.version>
19+
<groovy-eclipse-compiler.version>2.9.2-01</groovy-eclipse-compiler.version>
20+
21+
</properties>
22+
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-compiler-plugin</artifactId>
28+
<configuration>
29+
<compilerId>groovy-eclipse-compiler</compilerId>
30+
</configuration>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.codehaus.groovy</groupId>
34+
<artifactId>groovy-eclipse-compiler</artifactId>
35+
<version>${groovy-eclipse-compiler.version}</version>
36+
</dependency>
37+
38+
<!-- the compiler version should match the groovy dependency version -->
39+
<dependency>
40+
<groupId>org.codehaus.groovy</groupId>
41+
<artifactId>groovy-eclipse-batch</artifactId>
42+
<version>${groovy-eclipse-batch.version}</version>
43+
</dependency>
44+
</dependencies>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
<dependencies>
50+
<dependency>
51+
<groupId>org.codehaus.groovy</groupId>
52+
<artifactId>groovy</artifactId>
53+
<version>${groovy.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.lohika.jclub.storage.client</groupId>
57+
<artifactId>storage-service-client</artifactId>
58+
<version>0.0.1-SNAPSHOT</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.lohika.jclub.rating.client</groupId>
62+
<artifactId>rating-service-client</artifactId>
63+
<version>0.0.1-SNAPSHOT</version>
64+
</dependency>
65+
</dependencies>
66+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.lohika.jclub.dsl
2+
3+
import com.lohika.jclub.rating.client.Apartment
4+
import com.lohika.jclub.rating.client.RatingServiceClient
5+
import com.lohika.jclub.storage.client.StorageServiceClient
6+
import groovy.transform.ToString
7+
import groovy.util.logging.Log
8+
9+
@Log
10+
@ToString(excludes = ["ratingServiceClient", "storageServiceClient"])
11+
class MyDsl {
12+
13+
private RatingServiceClient ratingServiceClient
14+
private StorageServiceClient storageServiceClient
15+
16+
MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) {
17+
this.storageServiceClient = storageServiceClient
18+
this.ratingServiceClient = ratingServiceClient
19+
}
20+
}

dsl/src/main/java/package-info.java

Whitespace-only changes.

0 commit comments

Comments
 (0)