Skip to content

Commit 6356fb6

Browse files
k0l0ssusKevinGilmore
authored andcommitted
Guice Intro (eugenp#1177)
* Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml
1 parent 23489a0 commit 6356fb6

15 files changed

Lines changed: 429 additions & 0 deletions

‎guice-intro/pom.xml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung.examples.guice</groupId>
5+
<artifactId>guice-intro</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<dependencies>
9+
<dependency>
10+
<groupId>com.google.inject</groupId>
11+
<artifactId>guice</artifactId>
12+
<version>${guice.version}</version>
13+
</dependency>
14+
</dependencies>
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-compiler-plugin</artifactId>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
<properties>
28+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29+
<maven.compiler.source>1.8</maven.compiler.source>
30+
<maven.compiler.target>1.8</maven.compiler.target>
31+
<guice.version>4.1.0</guice.version>
32+
</properties>
33+
<name>guice-intro</name>
34+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
package com.baeldung.examples;
3+
4+
import com.baeldung.examples.guice.Communication;
5+
import com.baeldung.examples.guice.binding.AOPModule;
6+
import com.baeldung.examples.guice.modules.BasicModule;
7+
import com.google.inject.Guice;
8+
import com.google.inject.Injector;
9+
import java.util.Scanner;
10+
11+
/**
12+
*
13+
* @author Baeldung
14+
*/
15+
public class RunGuice {
16+
17+
public static void main(String[] args) {
18+
Injector injector = Guice.createInjector(new BasicModule(), new AOPModule());
19+
Communication comms = injector.getInstance(Communication.class);
20+
Scanner scanner = new Scanner(System.in);
21+
System.out.println("Enter your message to be sent; press Q to quit and P to print the message log");
22+
while (true) {
23+
String input = scanner.nextLine();
24+
if (input.equalsIgnoreCase("q")) {
25+
System.exit(0);
26+
}
27+
if (input.equalsIgnoreCase("p")) {
28+
comms.print();
29+
} else {
30+
comms.sendMessage(input);
31+
}
32+
33+
}
34+
35+
}
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
package com.baeldung.examples.guice;
3+
4+
import com.google.inject.Inject;
5+
import com.google.inject.name.Named;
6+
import java.util.Date;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
import java.util.logging.Logger;
10+
11+
/**
12+
*
13+
* @author Baeldung
14+
*/
15+
public class Communication {
16+
17+
final Date start = new Date();
18+
19+
@Inject
20+
private Logger logger;
21+
22+
private Queue<String> messageLog;
23+
24+
@Named("CommsUUID")
25+
private String commsID;
26+
27+
@Inject
28+
private DefaultCommunicator communicator;
29+
30+
public Communication(Boolean keepRecords) {
31+
if (keepRecords) {
32+
messageLog = new LinkedList();
33+
}
34+
}
35+
36+
public boolean sendMessage(String message) {
37+
if (!message.isEmpty() && messageLog != null) {
38+
messageLog.add(message);
39+
}
40+
return communicator.sendMessage(message);
41+
}
42+
43+
public void print() {
44+
if (messageLog != null) {
45+
for (String message : messageLog) {
46+
logger.info(message);
47+
}
48+
} else {
49+
logger.info("Message logging wasn't enabled");
50+
}
51+
}
52+
53+
public DefaultCommunicator getCommunicator() {
54+
return this.communicator;
55+
}
56+
57+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package com.baeldung.examples.guice;
3+
4+
import com.baeldung.examples.guice.constant.CommunicationModel;
5+
6+
public interface CommunicationMode {
7+
8+
public CommunicationModel getMode();
9+
10+
public boolean sendMessage(String message);
11+
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
package com.baeldung.examples.guice;
3+
4+
import com.baeldung.examples.guice.marker.Communicator;
5+
import com.google.inject.Inject;
6+
import com.google.inject.name.Named;
7+
8+
9+
public class DefaultCommunicator implements Communicator {
10+
11+
private CommunicationMode defaultCommsMode;
12+
@Inject
13+
@Named("SMSComms")
14+
CommunicationMode smsCommsMode;
15+
@Inject
16+
@Named("EmailComms")
17+
CommunicationMode emailCommsMode;
18+
@Inject
19+
@Named("IMComms")
20+
CommunicationMode imCommsMode;
21+
22+
protected DefaultCommunicator(CommunicationMode defaultComms) {
23+
this.defaultCommsMode = defaultComms;
24+
}
25+
26+
public DefaultCommunicator() {
27+
28+
}
29+
30+
public boolean sendMessage(String message) {
31+
boolean sent = false;
32+
if (defaultCommsMode != null) {
33+
sent = sendMessageByDefault(message);
34+
} else {
35+
sent = smsCommsMode.sendMessage(message);
36+
}
37+
return sent;
38+
}
39+
40+
private boolean sendMessageByDefault(String message) {
41+
boolean sent = false;
42+
if (message != null && !message.trim().equals("")) {
43+
return defaultCommsMode.sendMessage(message);
44+
}
45+
return sent;
46+
}
47+
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.examples.guice;
2+
3+
import com.baeldung.examples.guice.aop.MessageSentLoggable;
4+
import com.baeldung.examples.guice.constant.CommunicationModel;
5+
6+
/**
7+
*
8+
* @author Baekdung
9+
*/
10+
public class EmailCommunicationMode implements CommunicationMode {
11+
12+
@Override
13+
public CommunicationModel getMode() {
14+
return CommunicationModel.EMAIL;
15+
}
16+
17+
@Override
18+
@MessageSentLoggable
19+
public boolean sendMessage(String Message) {
20+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
21+
}
22+
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
package com.baeldung.examples.guice;
3+
4+
import com.baeldung.examples.guice.aop.MessageSentLoggable;
5+
import com.baeldung.examples.guice.constant.CommunicationModel;
6+
import com.google.inject.Inject;
7+
import java.util.logging.Logger;
8+
9+
/**
10+
*
11+
* @author Baeldung
12+
*/
13+
public class IMCommunicationMode implements CommunicationMode {
14+
15+
@Inject
16+
private Logger logger;
17+
18+
@Override
19+
public CommunicationModel getMode() {
20+
return CommunicationModel.IM;
21+
}
22+
23+
@Override
24+
@MessageSentLoggable
25+
public boolean sendMessage(String message) {
26+
logger.info("IM Message Sent");
27+
return true;
28+
}
29+
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.examples.guice;
2+
3+
import com.baeldung.examples.guice.aop.MessageSentLoggable;
4+
import com.baeldung.examples.guice.constant.CommunicationModel;
5+
import com.google.inject.Inject;
6+
import java.util.logging.Logger;
7+
8+
/**
9+
*
10+
* @author Baeldung
11+
*/
12+
public class SMSCommunicationMode implements CommunicationMode {
13+
14+
@Inject
15+
private Logger logger;
16+
17+
@Override
18+
public CommunicationModel getMode() {
19+
return CommunicationModel.SMS;
20+
}
21+
22+
@Override
23+
@MessageSentLoggable
24+
public boolean sendMessage(String message) {
25+
logger.info("SMS message sent");
26+
return true;
27+
}
28+
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.examples.guice.aop;
2+
3+
import java.util.logging.Logger;
4+
import org.aopalliance.intercept.MethodInterceptor;
5+
import org.aopalliance.intercept.MethodInvocation;
6+
7+
/**
8+
*
9+
* @author Baeldung
10+
*/
11+
public class MessageLogger implements MethodInterceptor {
12+
13+
@Override
14+
public Object invoke(MethodInvocation invocation) throws Throwable {
15+
Object[] objectArray = invocation.getArguments();
16+
int i = 0;
17+
for (Object object : objectArray) {
18+
Logger.getAnonymousLogger().info("Sending message: " + object.toString());
19+
}
20+
return invocation.proceed();
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.examples.guice.aop;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
*
10+
* @author Baeldung
11+
*/
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.METHOD)
14+
public @interface MessageSentLoggable {
15+
16+
}

0 commit comments

Comments
 (0)