Skip to content

Commit 108aea5

Browse files
committed
some technical tasks
* move javascript support to his own module * simplify jooby:run bootstrap and always use a main method * prepare for kotlin integration * fix/move Issue659.java to correct location
1 parent b541b27 commit 108aea5

27 files changed

Lines changed: 5462 additions & 201 deletions

File tree

coverage-report/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<source>${project.parent.basedir}/jooby-unbescape/src/main/java</source>
8888
<source>${project.parent.basedir}/jooby-thymeleaf/src/main/java</source>
8989
<source>${project.parent.basedir}/jooby-filewatcher/src/main/java</source>
90+
<source>${project.parent.basedir}/jooby-lang-js/src/main/java</source>
9091
</sources>
9192
</configuration>
9293
</execution>
@@ -148,6 +149,7 @@
148149
<source>${project.parent.basedir}/jooby-unbescape/src/test/java</source>
149150
<source>${project.parent.basedir}/jooby-thymeleaf/src/test/java</source>
150151
<source>${project.parent.basedir}/jooby-filewatcher/src/test/java</source>
152+
<source>${project.parent.basedir}/jooby-lang-js/src/test/java</source>
151153
</sources>
152154
</configuration>
153155
</execution>
@@ -348,6 +350,12 @@
348350
<version>${project.version}</version>
349351
</dependency>
350352

353+
<dependency>
354+
<groupId>org.jooby</groupId>
355+
<artifactId>jooby-lang-js</artifactId>
356+
<version>${project.version}</version>
357+
</dependency>
358+
351359
<dependency>
352360
<groupId>org.jooby</groupId>
353361
<artifactId>jooby-spymemcached</artifactId>

coverage-report/src/test/java/org/jooby/issues/Issue636.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.jooby.issues;
22

3-
import java.nio.channels.ClosedChannelException;
43
import java.util.concurrent.CountDownLatch;
54
import java.util.concurrent.TimeUnit;
65

coverage-report/src/test/java/org/jooby/js/JsApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import java.nio.file.Paths;
55

66
import org.jooby.Jooby;
7-
import org.jooby.internal.js.JsJooby;
7+
import org.jooby.JoobyJs;
88

99
public class JsApp {
1010

1111
public static void main(final String[] args) throws Throwable {
1212
File appjs = Paths.get("src", "test", "resources", "org", "jooby", "js", "app.js")
1313
.toFile();
14-
Jooby.run(new JsJooby().run(appjs), args);
14+
Jooby.run(new JoobyJs().run(appjs), args);
1515
}
1616
}

coverage-report/src/test/java/org/jooby/js/JsAppFeature.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import java.io.InputStreamReader;
55
import java.nio.file.Paths;
66

7-
import org.jooby.Jooby;
8-
import org.jooby.internal.js.JsJooby;
7+
import org.jooby.JoobyJs;
98
import org.junit.Test;
109

1110
public class JsAppFeature {
@@ -21,11 +20,11 @@ public void appfile() throws Throwable {
2120
}
2221

2322
private void run(final String filename) throws Exception {
24-
new JsJooby().run(new InputStreamReader(getClass().getResourceAsStream(filename))).get();
23+
new JoobyJs().run(new InputStreamReader(getClass().getResourceAsStream(filename))).get();
2524
}
2625

2726
private void run(final File filename) throws Throwable {
28-
Jooby.main(new String[]{filename.getAbsolutePath() });
27+
JoobyJs.main(new String[]{filename.getAbsolutePath() });
2928
}
3029

3130
}

jooby-lang-js/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# javascript
2+
3+
[Jooby](http://jooby.org) is available in JavaScript via [Nashorn](http://openjdk.java.net/projects/nashorn/).
4+
5+
## hello world
6+
7+
```js
8+
9+
var app = jooby();
10+
11+
app.get('/', function () 'Hey Jooby!');
12+
13+
```
14+
15+
## jooby function
16+
17+
The ```jooby``` function always returns a new application:
18+
19+
```js
20+
var app = jooby();
21+
```
22+
23+
or pass a function to ```jooby```:
24+
25+
```js
26+
jooby(function () {
27+
28+
this.get('/', function () 'Hey Jooby!');
29+
30+
})();
31+
```
32+
33+
Another minor but useful feature is the: **import of classes** and **packages** when you go with the functional version:
34+
35+
```js
36+
jooby(function (Jackson) {
37+
38+
use(new Jackson());
39+
40+
this.get('/', function () 'Hey Jooby!');
41+
42+
})(org.jooby.json.Jackson);
43+
```
44+
45+
Or import an entire package:
46+
47+
```js
48+
jooby(function (Jackson) {
49+
50+
use(new Jackson());
51+
52+
this.get('/', function () 'Hey Jooby!');
53+
54+
})(org.jooby.json);
55+
```
56+
57+
Import of packages is done via: ```importPackage``` function from in ```nashorn:mozilla_compat.js```.
58+
59+
## routes
60+
61+
Routes work as in Java, but it is worth to mention what are the available alternatives at the time you need to write a route in JavaScript:
62+
63+
```js
64+
jooby(function () {
65+
66+
// returns a constant value, using Function expression closures
67+
this.get('/', function () 'Hey Jooby!');
68+
69+
// returns a constant value
70+
this.get('/', function () {
71+
return 'Hey Jooby!';
72+
});
73+
74+
// returns a value but access to request object
75+
this.get('/', function (req) {
76+
var x = require(X)
77+
return x.doSomething();
78+
});
79+
80+
// access to request and rsp (like in express.js)
81+
this.get('/', function (req, rsp) {
82+
rsp.send('Hey Jooby!');
83+
});
84+
85+
// access to request and rsp
86+
this.get('/', function (req, rsp, chain) {
87+
chain.next();
88+
});
89+
90+
})();
91+
```
92+
93+
## running a javascript app
94+
95+
* via maven: ```mvn jooby:run```
96+
* java: ```java org.jooby.Jooby```. The ```app.js``` file must be present in the app directory

jooby-lang-js/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<parent>
6+
<groupId>org.jooby</groupId>
7+
<artifactId>jooby-project</artifactId>
8+
<version>1.0.4-SNAPSHOT</version>
9+
</parent>
10+
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>jooby-lang-js</artifactId>
13+
14+
<name>lang-js</name>
15+
16+
<build>
17+
<plugins>
18+
<!-- sure-fire -->
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-surefire-plugin</artifactId>
22+
<configuration>
23+
<includes>
24+
<include>**/*Test.java</include>
25+
<include>**/*Feature.java</include>
26+
<include>**/Issue*.java</include>
27+
</includes>
28+
</configuration>
29+
</plugin>
30+
31+
</plugins>
32+
</build>
33+
34+
<dependencies>
35+
<!-- Jooby -->
36+
<dependency>
37+
<groupId>org.jooby</groupId>
38+
<artifactId>jooby</artifactId>
39+
<version>${project.version}</version>
40+
</dependency>
41+
42+
<!-- Test dependencies -->
43+
<dependency>
44+
<groupId>org.jooby</groupId>
45+
<artifactId>jooby</artifactId>
46+
<version>${project.version}</version>
47+
<scope>test</scope>
48+
<classifier>tests</classifier>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.easymock</groupId>
59+
<artifactId>easymock</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.powermock</groupId>
65+
<artifactId>powermock-api-easymock</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.powermock</groupId>
71+
<artifactId>powermock-module-junit4</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.jacoco</groupId>
77+
<artifactId>org.jacoco.agent</artifactId>
78+
<classifier>runtime</classifier>
79+
<scope>test</scope>
80+
</dependency>
81+
82+
</dependencies>
83+
84+
</project>

jooby/src/main/java/org/jooby/internal/js/JsJooby.java renamed to jooby-lang-js/src/main/java/org/jooby/JoobyJs.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
@@ -16,8 +16,13 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.jooby.internal.js;
19+
package org.jooby;
2020

21+
import com.google.common.io.Closeables;
22+
import javaslang.control.Try;
23+
24+
import javax.script.ScriptEngine;
25+
import javax.script.ScriptEngineManager;
2126
import java.io.File;
2227
import java.io.FileReader;
2328
import java.io.InputStream;
@@ -27,20 +32,10 @@
2732
import java.util.function.Consumer;
2833
import java.util.function.Supplier;
2934

30-
import javax.script.ScriptEngine;
31-
import javax.script.ScriptEngineManager;
32-
33-
import org.jooby.Jooby;
34-
35-
import com.google.common.io.Closeables;
36-
37-
import javaslang.control.Try;
38-
39-
public class JsJooby {
40-
35+
public class JoobyJs {
4136
private ScriptEngine engine;
4237

43-
public JsJooby() throws Exception {
38+
public JoobyJs() throws Exception {
4439
ScriptEngineManager sem = new ScriptEngineManager();
4540
engine = sem.getEngineByName("nashorn");
4641
eval(Jooby.class.getResourceAsStream("/org/jooby/jooby.js"));
@@ -59,12 +54,22 @@ void eval(final InputStream stream) throws Exception {
5954
eval(new InputStreamReader(stream, StandardCharsets.UTF_8));
6055
}
6156

62-
@SuppressWarnings({"rawtypes", "unchecked" })
57+
@SuppressWarnings({"rawtypes", "unchecked"})
6358
void eval(final Reader reader) throws Exception {
6459
Consumer closer = x -> Closeables.closeQuietly(reader);
6560
Try.run(() -> engine.eval(reader))
6661
.onFailure(closer)
6762
.onSuccess(closer);
6863
}
6964

65+
public static void main(String[] mainargs) throws Throwable {
66+
String[] args = mainargs;
67+
String filename = "app.js";
68+
if (args.length > 0 && args[0].endsWith(".js")) {
69+
filename = args[0];
70+
args = new String[Math.max(0, mainargs.length - 1)];
71+
System.arraycopy(mainargs, 1, args, 0, args.length);
72+
}
73+
Jooby.run(new JoobyJs().run(new File(filename)), args);
74+
}
7075
}

jooby/src/main/java/org/jooby/internal/js/JsRequest.java renamed to jooby-lang-js/src/main/java/org/jooby/internal/js/JsRequest.java

File renamed without changes.

jooby/src/main/java/org/jooby/internal/js/JsResponse.java renamed to jooby-lang-js/src/main/java/org/jooby/internal/js/JsResponse.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
99
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
10+
* http://www.apache.org/licenses/LICENSE-2.0
1111
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
@@ -18,14 +18,12 @@
1818
*/
1919
package org.jooby.internal.js;
2020

21-
import java.util.Map;
22-
import java.util.stream.Collectors;
23-
24-
import org.jooby.Response;
25-
2621
import com.google.common.collect.ImmutableMap;
27-
2822
import jdk.nashorn.api.scripting.ScriptObjectMirror;
23+
import org.jooby.Response;
24+
25+
import java.util.Map;
26+
import java.util.stream.Collectors;
2927

3028
public class JsResponse extends Response.Forwarding {
3129

0 commit comments

Comments
 (0)