Skip to content

Commit 884b47c

Browse files
committed
init dev
0 parents  commit 884b47c

File tree

47 files changed

+2899
-0
lines changed

Some content is hidden

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

47 files changed

+2899
-0
lines changed

.gitattributes

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Handle line endings automatically for files detected as text
2+
# and leave all files detected as binary untouched.
3+
* text=auto
4+
5+
#
6+
# The above will handle all files NOT found below
7+
#
8+
# These files are text and should be normalized (Convert crlf => lf)
9+
*.css text
10+
*.df text
11+
*.htm text
12+
*.html text
13+
*.java text
14+
*.js text
15+
*.json text
16+
*.jsp text
17+
*.jspf text
18+
*.jspx text
19+
*.properties text
20+
*.sh text
21+
*.tld text
22+
*.txt text
23+
*.tag text
24+
*.tagx text
25+
*.xml text
26+
*.yml text
27+
28+
# These files are binary and should be left untouched
29+
# (binary is a macro for -text -diff)
30+
*.class binary
31+
*.dll binary
32+
*.ear binary
33+
*.gif binary
34+
*.ico binary
35+
*.jar binary
36+
*.jpg binary
37+
*.jpeg binary
38+
*.png binary
39+
*.so binary
40+
*.war binary
41+
42+
#linguist-documentation
43+
docs/* linguist-documentation

Department/pom.xml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" 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+
<parent>
6+
<artifactId>EmployeeAdmin</artifactId>
7+
<groupId>org.puremvc</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>Department</artifactId>
13+
<packaging>war</packaging>
14+
15+
<name>EmployeeAdmin - Department</name>
16+
<url>http://www.puremvc.org</url>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.source>1.7</maven.compiler.source>
21+
<maven.compiler.target>1.7</maven.compiler.target>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>4.11</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.mockito</groupId>
33+
<artifactId>mockito-core</artifactId>
34+
<version>3.0.0</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet</groupId>
39+
<artifactId>javax.servlet-api</artifactId>
40+
<version>4.0.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.json</groupId>
44+
<artifactId>javax.json-api</artifactId>
45+
<version>1.1.4</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>javax.ws.rs</groupId>
49+
<artifactId>javax.ws.rs-api</artifactId>
50+
<version>2.1</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.mongodb</groupId>
54+
<artifactId>mongo-java-driver</artifactId>
55+
<version>3.10.2</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.puremvc</groupId>
59+
<artifactId>puremvc</artifactId>
60+
<version>2.1</version>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<finalName>Department</finalName>
66+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
67+
<plugins>
68+
<plugin>
69+
<artifactId>maven-clean-plugin</artifactId>
70+
<version>3.1.0</version>
71+
</plugin>
72+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
73+
<plugin>
74+
<artifactId>maven-resources-plugin</artifactId>
75+
<version>3.0.2</version>
76+
</plugin>
77+
<plugin>
78+
<artifactId>maven-compiler-plugin</artifactId>
79+
<version>3.8.0</version>
80+
</plugin>
81+
<plugin>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>2.22.1</version>
84+
</plugin>
85+
<plugin>
86+
<artifactId>maven-war-plugin</artifactId>
87+
<version>3.2.2</version>
88+
</plugin>
89+
<plugin>
90+
<artifactId>maven-install-plugin</artifactId>
91+
<version>2.5.2</version>
92+
</plugin>
93+
<plugin>
94+
<artifactId>maven-deploy-plugin</artifactId>
95+
<version>2.8.2</version>
96+
</plugin>
97+
</plugins>
98+
</pluginManagement>
99+
</build>
100+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// ApplicationFacade.java
3+
// PureMVC Java Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2019 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
package org.puremvc.java.multicore.demos.microservice.employeeadmin.department;
10+
11+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.controller.StartupCommand;
12+
import org.puremvc.java.patterns.facade.Facade;
13+
14+
import javax.servlet.Servlet;
15+
16+
public class ApplicationFacade extends Facade {
17+
18+
private static final String STARTUP = "startup";
19+
public static final String SERVICE = "service";
20+
public static final String SERVICE_RESULT = "serviceResult";
21+
public static final String SERVICE_FAULT = "serviceFault";
22+
23+
@Override
24+
protected void initializeController() {
25+
super.initializeController();
26+
registerCommand(STARTUP, () -> new StartupCommand());
27+
}
28+
29+
public static ApplicationFacade getInstance() {
30+
return (ApplicationFacade) Facade.getInstance(() -> new ApplicationFacade());
31+
}
32+
33+
public void startup(Servlet servlet) {
34+
sendNotification(ApplicationFacade.STARTUP, servlet);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// ServiceCommand.java
3+
// PureMVC Java Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2019 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
package org.puremvc.java.multicore.demos.microservice.employeeadmin.department.controller;
10+
11+
import org.puremvc.java.interfaces.INotification;
12+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.ApplicationFacade;
13+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.model.ServiceProxy;
14+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.model.request.ServiceRequest;
15+
import org.puremvc.java.patterns.command.SimpleCommand;
16+
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.ws.rs.NotAllowedException;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
public class ServiceCommand extends SimpleCommand {
23+
24+
@Override
25+
public void execute(INotification notification) {
26+
ServiceRequest serviceRequest = (ServiceRequest) notification.getBody();
27+
ServiceProxy serviceProxy = (ServiceProxy) facade.retrieveProxy(ServiceProxy.NAME);
28+
HttpServletRequest request = serviceRequest.getRequest();
29+
30+
try {
31+
switch (request.getPathInfo()) {
32+
case "/departments":
33+
if(request.getMethod().equals("GET")) {
34+
Object result = serviceProxy.findAll();
35+
serviceRequest.setResultData(200, result);
36+
} else {
37+
throw new NotAllowedException("HTTP 405 Method Not Allowed");
38+
}
39+
break;
40+
41+
default:
42+
throw new NotAllowedException("HTTP 405 Method Not Allowed");
43+
}
44+
} catch (Exception exception) {
45+
serviceRequest.setResultData(500, exception);
46+
sendNotification(ApplicationFacade.SERVICE_FAULT, serviceRequest);
47+
return;
48+
}
49+
50+
sendNotification(ApplicationFacade.SERVICE_RESULT, serviceRequest);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// StartupCommand.java
3+
// PureMVC Java Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2019 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
package org.puremvc.java.multicore.demos.microservice.employeeadmin.department.controller;
10+
11+
import com.mongodb.MongoClientSettings;
12+
import com.mongodb.MongoCredential;
13+
import com.mongodb.ServerAddress;
14+
import com.mongodb.client.MongoClient;
15+
import com.mongodb.client.MongoClients;
16+
import org.puremvc.java.interfaces.INotification;
17+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.ApplicationFacade;
18+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.model.ServiceProxy;
19+
import org.puremvc.java.multicore.demos.microservice.employeeadmin.department.view.ServiceMediator;
20+
import org.puremvc.java.patterns.command.SimpleCommand;
21+
22+
import javax.servlet.Servlet;
23+
import java.util.Arrays;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
public class StartupCommand extends SimpleCommand {
28+
29+
@Override
30+
public void execute(INotification notification) {
31+
32+
Map<String, String> env = new HashMap<String, String>() {
33+
{
34+
put("MONGO_HOST", System.getenv("MONGO_HOST"));
35+
put("MONGO_PORT", System.getenv("MONGO_PORT"));
36+
put("MONGO_USERNAME", System.getenv("MONGO_USERNAME"));
37+
put("MONGO_PASSWORD", System.getenv("MONGO_PASSWORD"));
38+
put("MONGO_DATABASE", System.getenv("MONGO_DATABASE"));
39+
put("MONGO_AUTHDB", System.getenv("MONGO_AUTHDB"));
40+
}
41+
};
42+
43+
env.forEach((k, v) -> {
44+
if(v == null) {
45+
throw new RuntimeException("Please set the " + k + " in env variables and try again");
46+
}
47+
});
48+
49+
MongoCredential credential = MongoCredential.createCredential(env.get("MONGO_USERNAME"), env.get("MONGO_AUTHDB"), env.get("MONGO_PASSWORD").toCharArray());
50+
51+
while(true) { // wait for mongo to boot up, mongoclient waits 30000 ms before timing out
52+
MongoClient mongoClient = MongoClients.create(
53+
MongoClientSettings.builder()
54+
.applyToClusterSettings(builder -> builder.hosts(Arrays.asList(new ServerAddress(env.get("MONGO_HOST"), Integer.parseInt(env.get("MONGO_PORT"))))))
55+
.credential(credential)
56+
.build());
57+
58+
if(mongoClient.getDatabase(env.get("MONGO_DATABASE")).getCollection("user").find().iterator() != null) {
59+
this.facade.registerCommand(ApplicationFacade.SERVICE, () -> new ServiceCommand());
60+
this.facade.registerProxy(new ServiceProxy(() -> mongoClient.getDatabase(env.get("MONGO_DATABASE"))));
61+
this.facade.registerMediator(new ServiceMediator((Servlet) notification.getBody()));
62+
break;
63+
}
64+
}
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ServiceProxy.java
3+
// PureMVC Java Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2019 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
package org.puremvc.java.multicore.demos.microservice.employeeadmin.department.model;
10+
11+
import com.mongodb.client.MongoCursor;
12+
import com.mongodb.client.MongoDatabase;
13+
import org.bson.Document;
14+
import org.puremvc.java.patterns.proxy.Proxy;
15+
16+
import java.util.function.Supplier;
17+
18+
public class ServiceProxy extends Proxy {
19+
20+
public static String NAME = "ServiceProxy";
21+
22+
private Supplier<MongoDatabase> databaseSupplier;
23+
24+
public ServiceProxy(Supplier<MongoDatabase> databaseSupplier) {
25+
super(NAME, null);
26+
this.databaseSupplier = databaseSupplier;
27+
}
28+
29+
public MongoCursor<Document> findAll() {
30+
return databaseSupplier.get()
31+
.getCollection("department")
32+
.find()
33+
.iterator();
34+
}
35+
36+
}

0 commit comments

Comments
 (0)