Merge pull request #657 from christian-raedel/craedel-enterprise-patterns/front-controller

BAEL-305: enterprise patterns/front controller
This commit is contained in:
slavisa-baeldung 2016-09-25 13:33:13 +03:00 committed by GitHub
commit f24282cb27
11 changed files with 169 additions and 47 deletions

View File

@ -31,7 +31,11 @@
<plugin> <plugin>
<groupId>org.eclipse.jetty</groupId> <groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId> <artifactId>jetty-maven-plugin</artifactId>
<<<<<<< HEAD
<version>9.3.11.v20160721</version>
=======
<version>9.4.0.M1</version> <version>9.4.0.M1</version>
>>>>>>> upstream/master
<configuration> <configuration>
<webApp> <webApp>
<contextPath>/front-controller</contextPath> <contextPath>/front-controller</contextPath>

View File

@ -22,24 +22,17 @@ public class FrontControllerServlet extends HttpServlet {
private FrontCommand getCommand(HttpServletRequest request) { private FrontCommand getCommand(HttpServletRequest request) {
try { try {
return (FrontCommand) getCommandClass(request) Class type = Class.forName(
.asSubclass(FrontCommand.class)
.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to get command!", e);
}
}
private Class getCommandClass(HttpServletRequest request) {
try {
return Class.forName(
String.format( String.format(
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand", "com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
request.getParameter("command") request.getParameter("command")
) )
); );
} catch (ClassNotFoundException e) { return (FrontCommand) type
return UnknownCommand.class; .asSubclass(FrontCommand.class)
.newInstance();
} catch (Exception e) {
return new UnknownCommand();
} }
} }
} }

View File

@ -1,40 +1,15 @@
package com.baeldung.enterprise.patterns.front.controller.data; package com.baeldung.enterprise.patterns.front.controller.data;
public class Book { public interface Book {
private String author; String getAuthor();
private String title;
private Double price;
public Book() { void setAuthor(String author);
}
public Book(String author, String title, Double price) { String getTitle();
this.author = author;
this.title = title;
this.price = price;
}
public String getAuthor() { void setTitle(String title);
return author;
}
public void setAuthor(String author) { Double getPrice();
this.author = author;
}
public String getTitle() { void setPrice(Double price);
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
} }

View File

@ -0,0 +1,46 @@
package com.baeldung.enterprise.patterns.front.controller.data;
public class BookImpl implements Book {
private String author;
private String title;
private Double price;
public BookImpl() {
}
public BookImpl(String author, String title, Double price) {
this.author = author;
this.title = title;
this.price = price;
}
@Override
public String getAuthor() {
return author;
}
@Override
public void setAuthor(String author) {
this.author = author;
}
@Override
public String getTitle() {
return title;
}
@Override
public void setTitle(String title) {
this.title = title;
}
@Override
public Double getPrice() {
return price;
}
@Override
public void setPrice(Double price) {
this.price = price;
}
}

View File

@ -3,8 +3,8 @@ package com.baeldung.enterprise.patterns.front.controller.data;
public interface Bookshelf { public interface Bookshelf {
default void init() { default void init() {
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99)); add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88)); add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
} }
Bookshelf getInstance(); Bookshelf getInstance();

View File

@ -0,0 +1,27 @@
## Spring Cloud Config ##
To get this example working, you have to initialize a new *Git* repository in
the ```client-config``` directory first *and* you have to set the environment variable
```CONFIG_REPO``` to an absolute path of that directory.
```
$> cd client-config
$> git init
$> git add .
$> git commit -m 'Initial commit'
$> export CONFIG_REPO=$(pwd)
```
Then you're able to run the examples with ```mvn install spring-boot:run```.
### Docker ###
To get the *Docker* examples working, you have to repackage the ```spring-cloud-config-server```
and ```spring-cloud-config-client``` modules first:
```
$> mvn install spring-boot:repackage
```
Don't forget to download the *Java JCE* package from
(Oracle)[http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html].

View File

@ -0,0 +1,6 @@
FROM alpine-java:base
MAINTAINER baeldung.com
RUN apk --no-cache add netcat-openbsd
COPY files/spring-cloud-config-client-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-client.jar
COPY files/config-client-entrypoint.sh /opt/spring-cloud/bin/
RUN chmod 755 /opt/spring-cloud/bin/config-client-entrypoint.sh

View File

@ -0,0 +1,9 @@
FROM alpine-java:base
MAINTAINER baeldung.com
COPY files/spring-cloud-config-server-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-server.jar
ENV SPRING_APPLICATION_JSON='{"spring": {"cloud": {"config": {"server": \
{"git": {"uri": "/var/lib/spring-cloud/config-repo", "clone-on-start": true}}}}}}'
ENTRYPOINT ["/usr/bin/java"]
CMD ["-jar", "/opt/spring-cloud/lib/config-server.jar"]
VOLUME /var/lib/spring-cloud/config-repo
EXPOSE 8888

View File

@ -0,0 +1,2 @@
/UnlimitedJCEPolicyJDK8
/*.jar

View File

@ -0,0 +1,8 @@
#!/bin/sh
while ! nc -z config-server 8888 ; do
echo "Waiting for upcoming Config Server"
sleep 2
done
java -jar /opt/spring-cloud/lib/config-client.jar

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud-config</artifactId>
<packaging>pom</packaging>
<modules>
<module>spring-cloud-config-server</module>
<module>spring-cloud-config-client</module>
</modules>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.0.RELEASE</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>