Skip to content

Commit f24282c

Browse files
Merge pull request eugenp#657 from christian-raedel/craedel-enterprise-patterns/front-controller
BAEL-305: enterprise patterns/front controller
2 parents 11292ac + 3b948c9 commit f24282c

11 files changed

Lines changed: 169 additions & 47 deletions

File tree

‎enterprise-patterns/front-controller-pattern/pom.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
<plugin>
3232
<groupId>org.eclipse.jetty</groupId>
3333
<artifactId>jetty-maven-plugin</artifactId>
34+
<<<<<<< HEAD
35+
<version>9.3.11.v20160721</version>
36+
=======
3437
<version>9.4.0.M1</version>
38+
>>>>>>> upstream/master
3539
<configuration>
3640
<webApp>
3741
<contextPath>/front-controller</contextPath>

‎enterprise-patterns/front-controller-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,17 @@ protected void doGet(
2222

2323
private FrontCommand getCommand(HttpServletRequest request) {
2424
try {
25-
return (FrontCommand) getCommandClass(request)
26-
.asSubclass(FrontCommand.class)
27-
.newInstance();
28-
} catch (Exception e) {
29-
throw new RuntimeException("Failed to get command!", e);
30-
}
31-
}
32-
33-
private Class getCommandClass(HttpServletRequest request) {
34-
try {
35-
return Class.forName(
25+
Class type = Class.forName(
3626
String.format(
3727
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
3828
request.getParameter("command")
3929
)
4030
);
41-
} catch (ClassNotFoundException e) {
42-
return UnknownCommand.class;
31+
return (FrontCommand) type
32+
.asSubclass(FrontCommand.class)
33+
.newInstance();
34+
} catch (Exception e) {
35+
return new UnknownCommand();
4336
}
4437
}
4538
}
Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
package com.baeldung.enterprise.patterns.front.controller.data;
22

3-
public class Book {
4-
private String author;
5-
private String title;
6-
private Double price;
3+
public interface Book {
4+
String getAuthor();
75

8-
public Book() {
9-
}
6+
void setAuthor(String author);
107

11-
public Book(String author, String title, Double price) {
12-
this.author = author;
13-
this.title = title;
14-
this.price = price;
15-
}
8+
String getTitle();
169

17-
public String getAuthor() {
18-
return author;
19-
}
10+
void setTitle(String title);
2011

21-
public void setAuthor(String author) {
22-
this.author = author;
23-
}
12+
Double getPrice();
2413

25-
public String getTitle() {
26-
return title;
27-
}
28-
29-
public void setTitle(String title) {
30-
this.title = title;
31-
}
32-
33-
public Double getPrice() {
34-
return price;
35-
}
36-
37-
public void setPrice(Double price) {
38-
this.price = price;
39-
}
14+
void setPrice(Double price);
4015
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
public class BookImpl implements Book {
4+
private String author;
5+
private String title;
6+
private Double price;
7+
8+
public BookImpl() {
9+
}
10+
11+
public BookImpl(String author, String title, Double price) {
12+
this.author = author;
13+
this.title = title;
14+
this.price = price;
15+
}
16+
17+
@Override
18+
public String getAuthor() {
19+
return author;
20+
}
21+
22+
@Override
23+
public void setAuthor(String author) {
24+
this.author = author;
25+
}
26+
27+
@Override
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
@Override
33+
public void setTitle(String title) {
34+
this.title = title;
35+
}
36+
37+
@Override
38+
public Double getPrice() {
39+
return price;
40+
}
41+
42+
@Override
43+
public void setPrice(Double price) {
44+
this.price = price;
45+
}
46+
}

‎enterprise-patterns/front-controller-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public interface Bookshelf {
44

55
default void init() {
6-
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
7-
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
6+
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
7+
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
88
}
99

1010
Bookshelf getInstance();

‎spring-cloud-config/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Spring Cloud Config ##
2+
3+
To get this example working, you have to initialize a new *Git* repository in
4+
the ```client-config``` directory first *and* you have to set the environment variable
5+
```CONFIG_REPO``` to an absolute path of that directory.
6+
7+
```
8+
$> cd client-config
9+
$> git init
10+
$> git add .
11+
$> git commit -m 'Initial commit'
12+
$> export CONFIG_REPO=$(pwd)
13+
```
14+
15+
Then you're able to run the examples with ```mvn install spring-boot:run```.
16+
17+
### Docker ###
18+
19+
To get the *Docker* examples working, you have to repackage the ```spring-cloud-config-server```
20+
and ```spring-cloud-config-client``` modules first:
21+
22+
```
23+
$> mvn install spring-boot:repackage
24+
```
25+
26+
Don't forget to download the *Java JCE* package from
27+
(Oracle)[http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html].
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM alpine-java:base
2+
MAINTAINER baeldung.com
3+
RUN apk --no-cache add netcat-openbsd
4+
COPY files/spring-cloud-config-client-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-client.jar
5+
COPY files/config-client-entrypoint.sh /opt/spring-cloud/bin/
6+
RUN chmod 755 /opt/spring-cloud/bin/config-client-entrypoint.sh
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM alpine-java:base
2+
MAINTAINER baeldung.com
3+
COPY files/spring-cloud-config-server-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-server.jar
4+
ENV SPRING_APPLICATION_JSON='{"spring": {"cloud": {"config": {"server": \
5+
{"git": {"uri": "/var/lib/spring-cloud/config-repo", "clone-on-start": true}}}}}}'
6+
ENTRYPOINT ["/usr/bin/java"]
7+
CMD ["-jar", "/opt/spring-cloud/lib/config-server.jar"]
8+
VOLUME /var/lib/spring-cloud/config-repo
9+
EXPOSE 8888
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/UnlimitedJCEPolicyJDK8
2+
/*.jar
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
3+
while ! nc -z config-server 8888 ; do
4+
echo "Waiting for upcoming Config Server"
5+
sleep 2
6+
done
7+
8+
java -jar /opt/spring-cloud/lib/config-client.jar

0 commit comments

Comments
 (0)