Skip to content

Commit

Permalink
feat(*): add ligning talk with example code
Browse files Browse the repository at this point in the history
  • Loading branch information
bentolor committed Jun 30, 2016
1 parent c46ad87 commit 27a3c24
Show file tree
Hide file tree
Showing 26 changed files with 920 additions and 392 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,5 @@
# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
target/

11 changes: 11 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion java9-in-action.iml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/presentation/css" />
<excludeFolder url="file://$MODULE_DIR$/presentation/img" />
<excludeFolder url="file://$MODULE_DIR$/presentation/js" />
Expand Down
12 changes: 12 additions & 0 deletions playground-dependent/playground-dependent.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="playground" />
</component>
</module>
12 changes: 12 additions & 0 deletions playground-dependent/run-with-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

echo -e "\nHINT: Be sure to compile project with IDEA first\n"

echo -e "=== RetrieveModuleInfo"
java -modulepath ../target/production/playground:../target/production/playground-dependent \
-m anothermodule/de.exxcellent.anothermodule.RetrieveModuleInfo


echo -e "\n\n=== TestJigsawSPI"
java -modulepath ../target/production/playground:../target/production/playground-dependent \
-m anothermodule/de.exxcellent.anothermodule.TestJigsawSPI
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.exxcellent.anothermodule;

import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleDescriptor;
import static java.lang.System.out;

public class RetrieveModuleInfo {

/**
* Show module infos
*/
public static void main(String[] args) throws IOException {
ModuleDescriptor descriptor = RetrieveModuleInfo.class.getModule().getDescriptor();

if (descriptor == null) {
out.println("JVM started without '-mp' option. Trying to load module-info manually...");
descriptor = readModuleDescriptorFromBytecode();
}

printModule(descriptor);
}

static void printModule(ModuleDescriptor descriptor) {
out.println("Module " + descriptor.name());
descriptor.exports().forEach(
export -> out.println("\texports " + export.source())
);
descriptor.requires().forEach(
requires -> out.println("\trequires " + requires.name() + " " + requires.modifiers())
);
}

static ModuleDescriptor readModuleDescriptorFromBytecode() throws IOException {
InputStream moduleInfo = RetrieveModuleInfo.class.getClassLoader().getResourceAsStream("module-info.class");
return ModuleDescriptor.read(moduleInfo);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.exxcellent.anothermodule;

import de.exxcellent.java9.jigsaw.BillingService;
import static java.lang.System.out;

public class TestJigsawSPI {

public static void main(String[] args) {
BillingService s = BillingService.getInstance();
out.println(s.takeMoney());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.exxcellent.anothermodule;

import de.exxcellent.java9.collections.ImmutableCollections;

/**
* Test module encapsulation
*/
public class TestModuleEncapuslation {

public static void main(String[] args) {
ImmutableCollections.main(args);

// Would break, as it is not exported. Though it's public!
// de.exxcellent.java9.util.InputStreamExample.main(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module anothermodule {
exports de.exxcellent.anothermodule;
requires de.exxcellent.java9;

// Provide Service instance (SPI with Jigsaw modules)
provides de.exxcellent.java9.jigsaw.spi.BillingServiceProvider
with de.exxcellent.anothermodule.spi.MastercardBillingServiceProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.exxcellent.anothermodule.spi;

import de.exxcellent.java9.jigsaw.BillingService;

class MastercardBillingService extends BillingService {

@Override
public String takeMoney() {
return "Mastercard billed the money!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.exxcellent.anothermodule.spi;

import de.exxcellent.java9.jigsaw.BillingService;
import de.exxcellent.java9.jigsaw.spi.BillingServiceProvider;

public class MastercardBillingServiceProvider extends BillingServiceProvider {

@Override
public BillingService buildBillingService() {
return new MastercardBillingService();
}
}
8 changes: 4 additions & 4 deletions playground/playground.iml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9" inherit-compiler-output="true">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.exxcellent.java9.collections;

import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.lang.System.*;

public class ImmutableCollections {

/**
* Create immutable collections on the fly.
* They do not accept {@code null} or duplicate entries (Set/Map)
*/
public static void main(String args[]) {
List<Integer> listOfNumbers = List.of(1, 2, 3, 4, 5/*, null*/);
out.println(listOfNumbers);

Set<Integer> setOfNumbers = Set.of(1, 2, 3, 4, 5/*, 1*/);
out.println(setOfNumbers);

Map<String, String> mapOfString = Map.of("key1", "value1", "key2", "value2");
out.println(mapOfString);

Map<String, String> moreMapOfString = Map.ofEntries(
Map.entry("key1", "value1"),
Map.entry("key2", "value2")/*,
Map.entry("key1", "value3")*/
);
out.println(moreMapOfString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.exxcellent.java9.http;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import static java.lang.System.out;

public class HttpClientExample {

/**
* The HTTP API functions asynchronously & synchronously. In asynchronous mode,
* work is done on the threads supplied by the client's ExecutorService.
*/
public static void main(String[] args) throws Exception{
HttpClient.getDefault()
.request(URI.create("https://www.exxcellent.de"))
.GET()
.responseAsync() // CompletableFuture :D
.thenAccept(httpResponse ->
out.println(httpResponse.body(HttpResponse.asString()))
);

Thread.sleep(999); // Give worker thread some time.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.exxcellent.java9.jigsaw;

import java.util.Iterator;
import java.util.ServiceLoader;
import de.exxcellent.java9.jigsaw.spi.BillingServiceProvider;

public abstract class BillingService {

protected BillingService() {
}

public static BillingService getInstance() {
// Java SPI to find the instance
ServiceLoader<BillingServiceProvider> sl = ServiceLoader.load(BillingServiceProvider.class);

// Fetch first provider implementation
Iterator<BillingServiceProvider> providerIterator = sl.iterator();
if (!providerIterator.hasNext()) {
throw new RuntimeException("No service providers found!");
}

BillingServiceProvider provider = providerIterator.next();

return provider.buildBillingService();
}

public abstract String takeMoney();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.exxcellent.java9.jigsaw.spi;

import de.exxcellent.java9.jigsaw.BillingService;

public abstract class BillingServiceProvider {

public abstract BillingService buildBillingService();
}
12 changes: 12 additions & 0 deletions playground/src/main/java/de/exxcellent/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module de.exxcellent.java9 {
exports de.exxcellent.java9.collections;

// Depend on an offficial JDK modules
// full list see http://cr.openjdk.java.net/~mr/jigsaw/ea/module-summary.html
requires java.httpclient;

// Service example (SPI with Jigsaw modules)
exports de.exxcellent.java9.jigsaw;
exports de.exxcellent.java9.jigsaw.spi;
uses de.exxcellent.java9.jigsaw.spi.BillingServiceProvider;
}
Loading

0 comments on commit 27a3c24

Please sign in to comment.