Skip to content

Commit

Permalink
Merge pull request #657 from christian-raedel/craedel-enterprise-patt…
Browse files Browse the repository at this point in the history
…erns/front-controller

BAEL-305: enterprise patterns/front controller
  • Loading branch information
slavisa-baeldung authored Sep 25, 2016
2 parents 11292ac + 3b948c9 commit f24282c
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 47 deletions.
4 changes: 4 additions & 0 deletions enterprise-patterns/front-controller-pattern/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<<<<<<< HEAD
<version>9.3.11.v20160721</version>
=======
<version>9.4.0.M1</version>
>>>>>>> upstream/master
<configuration>
<webApp>
<contextPath>/front-controller</contextPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@ protected void doGet(

private FrontCommand getCommand(HttpServletRequest request) {
try {
return (FrontCommand) getCommandClass(request)
.asSubclass(FrontCommand.class)
.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to get command!", e);
}
}

private Class getCommandClass(HttpServletRequest request) {
try {
return Class.forName(
Class type = Class.forName(
String.format(
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
request.getParameter("command")
)
);
} catch (ClassNotFoundException e) {
return UnknownCommand.class;
return (FrontCommand) type
.asSubclass(FrontCommand.class)
.newInstance();
} catch (Exception e) {
return new UnknownCommand();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
package com.baeldung.enterprise.patterns.front.controller.data;

public class Book {
private String author;
private String title;
private Double price;
public interface Book {
String getAuthor();

public Book() {
}
void setAuthor(String author);

public Book(String author, String title, Double price) {
this.author = author;
this.title = title;
this.price = price;
}
String getTitle();

public String getAuthor() {
return author;
}
void setTitle(String title);

public void setAuthor(String author) {
this.author = author;
}
Double getPrice();

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}
void setPrice(Double price);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public interface Bookshelf {

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

Bookshelf getInstance();
Expand Down
27 changes: 27 additions & 0 deletions spring-cloud-config/README.md
Original file line number Diff line number Diff line change
@@ -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].
6 changes: 6 additions & 0 deletions spring-cloud-config/docker/Dockerfile.client
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spring-cloud-config/docker/Dockerfile.server
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spring-cloud-config/docker/files/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/UnlimitedJCEPolicyJDK8
/*.jar
8 changes: 8 additions & 0 deletions spring-cloud-config/docker/files/config-client-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions spring-cloud-config/pom.xml
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit f24282c

Please sign in to comment.