Commit 3ab4626f authored by Peter Harrison's avatar Peter Harrison

Poor Example Application

parent dc2bab56
# poorexample
Vladimir Antanov has fled the country with his girlfriend Sonya. My company employed Antanov to maintain our critical communication systems. We urgently need to add a new feature the the existing software, and knowing you are a Master Java Developer we have chosen you to modify the software to communicate with our new clients.
Please find attached the project and source code. Currently the system processes notifications and sends them to email, database or a web service. We need to add another endpoint - JMS.
The JMS connection requires configuration details:
- Server Name
- Port
- Queue Name
You have one day to create a new fork of the project and apply your changes. Once complete please post the URL for your fork.
Note: We don't expect an actual JMS connector - follow the same pattern as the existing senders which simply output to the console.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>org.devcentre</groupId>
<artifactId>poorexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Poor Example</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.devcentre.poorexample;
public class Alert {
private String code;
private String message;
private boolean sendToEmail;
private boolean sendToDatabase;
private boolean sendToWebService;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isSendToEmail() {
return sendToEmail;
}
public void setSendToEmail(boolean sendToEmail) {
this.sendToEmail = sendToEmail;
}
public boolean isSendToDatabase() {
return sendToDatabase;
}
public void setSendToDatabase(boolean sendToDatabase) {
this.sendToDatabase = sendToDatabase;
}
public boolean isSendToWebService() {
return sendToWebService;
}
public void setSendToWebService(boolean sendToWebService) {
this.sendToWebService = sendToWebService;
}
}
package org.devcentre.poorexample;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BootStrap {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring/beans.xml");
BeanFactory factory = (BeanFactory) context;
MainApplication application = (MainApplication) factory.getBean("mainApp");
application.run();
}
}
package org.devcentre.poorexample;
import java.util.List;
import org.devcentre.poorexample.sender.DatabaseSender;
import org.devcentre.poorexample.sender.EmailSender;
import org.devcentre.poorexample.sender.WebServiceSender;
public class MainApplication {
public static String emailServer;
public static String emailPort;
public static String databaseServer;
public static String databaseUser;
public static String databasePassword;
public static String webServiceUrl;
public static String webServiceService;
private List<Alert> alerts;
private List<String> notifications;
private EmailSender emailSender = new EmailSender();
private DatabaseSender databaseSender = new DatabaseSender(this);
private WebServiceSender webServiceSender = new WebServiceSender();
public void run() {
for( String notification : notifications ){
Alert alert = findAlert( notification );
if( alert.isSendToDatabase()){
this.databaseSender.submit( alert );
}
if( alert.isSendToEmail()){
this.emailSender.send( alert );
}
if( alert.isSendToWebService()){
this.webServiceSender.transmit( alert, webServiceUrl, webServiceService );
}
}
}
public Alert findAlert( String code ){
for( Alert alert: alerts){
if( alert.getCode().equals( code)){
return alert;
}
}
return null;
}
public void setAlerts(List<Alert> alerts) {
this.alerts = alerts;
}
public List<Alert> getAlerts() {
return alerts;
}
public void setNotifications(List<String> notifications) {
this.notifications = notifications;
}
public List<String> getNotifications() {
return notifications;
}
public void setEmailServer(String server) {
emailServer = server;
}
public void setEmailPort(String port) {
emailPort = port;
}
public void setDatabaseServer(String server) {
databaseServer = server;
}
public void setDatabaseUser(String user) {
databaseUser = user;
}
public void setDatabasePassword(String password) {
databasePassword = password;
}
public void setWebServiceUrl(String url) {
webServiceUrl = url;
}
public void setWebServiceService(String service) {
webServiceService = service;
}
public void setEmailSender(EmailSender sender) {
emailSender = sender;
}
public void setDatabaseSender(DatabaseSender sender) {
databaseSender = sender;
}
public void setWebServiceSender(WebServiceSender sender) {
webServiceSender = sender;
}
}
package org.devcentre.poorexample.sender;
import org.devcentre.poorexample.Alert;
public abstract class AbstractSender {
protected void display( String type, Alert alert ){
System.out.println("\nSending " + type + " Alert: " + alert.getMessage());
}
}
package org.devcentre.poorexample.sender;
import org.devcentre.poorexample.Alert;
import org.devcentre.poorexample.MainApplication;
public class DatabaseSender extends AbstractSender{
private MainApplication app;
public DatabaseSender( MainApplication app ){
this.app = app;
}
public void submit( Alert alert ){
display( "Database", alert);
System.out.println("Server: " + this.app.databaseServer);
System.out.println("User: " + this.app.databaseUser);
System.out.println("Password: " + this.app.databasePassword);
}
}
package org.devcentre.poorexample.sender;
import org.devcentre.poorexample.Alert;
import org.devcentre.poorexample.MainApplication;
public class EmailSender extends AbstractSender{
public void send( Alert alert ){
display( "Email", alert);
System.out.println("Host: " + MainApplication.emailServer);
System.out.println("Port: " + MainApplication.emailPort);
}
}
package org.devcentre.poorexample.sender;
import org.devcentre.poorexample.Alert;
public class WebServiceSender {
public void transmit( Alert alert, String url, String service ){
System.out.println("\nSending Web Service Alert: " + alert.getMessage());
System.out.println("URL: " + url);
System.out.println("Service: " + service);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mainApp" class="org.devcentre.poorexample.MainApplication">
<property name="emailServer" value="devcentre.org"/>
<property name="emailPort" value="25"/>
<property name="databaseServer" value="localhost"/>
<property name="databaseUser" value="admin"/>
<property name="databasePassword" value="password"/>
<property name="webServiceUrl" value="http://devcentre.org/webservice"/>
<property name="webServiceService" value="getServices"/>
<property name="alerts">
<list>
<bean class="org.devcentre.poorexample.Alert">
<property name="code" value="EMAIL"/>
<property name="message" value="This is the EMAIL Alert - it sends an email"/>
<property name="sendToEmail" value="true"/>
</bean>
<bean class="org.devcentre.poorexample.Alert">
<property name="code" value="DATABASE"/>
<property name="message" value="This is the DATABASE Alert - it sends to Database"/>
<property name="sendToDatabase" value="true"/>
</bean>
<bean class="org.devcentre.poorexample.Alert">
<property name="code" value="WEB"/>
<property name="message" value="This is the WEB Alert - it sends to Web Service"/>
<property name="sendToWebService" value="true"/>
</bean>
<bean class="org.devcentre.poorexample.Alert">
<property name="code" value="ALL"/>
<property name="message" value="This is the ALL Alert - it sends to Everything"/>
<property name="sendToEmail" value="true"/>
<property name="sendToDatabase" value="true"/>
<property name="sendToWebService" value="true"/>
</bean>
</list>
</property>
<property name="notifications">
<list>
<value>EMAIL</value>
<value>DATABASE</value>
<value>WEB</value>
<value>ALL</value>
</list>
</property>
</bean>
</beans>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment