-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guiced Rabbit Configure Connections and Exchanges by Package
- Loading branch information
Showing
6 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/guicedee/guicedinjection/InjectLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.guicedee.guicedinjection; | ||
|
||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({METHOD, CONSTRUCTOR, FIELD}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
public @interface InjectLogger | ||
{ | ||
String value(); | ||
String level() default ""; | ||
|
||
String rollingFileName() default ""; | ||
String fileName() default ""; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/guicedee/guicedinjection/logging/Log4JMembersInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.guicedee.guicedinjection.logging; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.inject.MembersInjector; | ||
import com.guicedee.guicedinjection.InjectLogger; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class Log4JMembersInjector<T> implements MembersInjector<T> { | ||
private final Field field; | ||
private final InjectLogger injectLogger; | ||
private final Logger logger; | ||
|
||
Log4JMembersInjector(Field field) { | ||
this.field = field; | ||
this.injectLogger = field.getAnnotation(InjectLogger.class); | ||
String logName = Strings.isNullOrEmpty(injectLogger.value()) ? field.getDeclaringClass().getCanonicalName() : injectLogger.value(); | ||
this.logger = LogManager.getLogger(logName); | ||
field.setAccessible(true); | ||
} | ||
|
||
public void injectMembers(T t) { | ||
try { | ||
field.set(t, logger); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/guicedee/guicedinjection/logging/Log4JTypeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.guicedee.guicedinjection.logging; | ||
|
||
import com.google.inject.TypeLiteral; | ||
import com.google.inject.spi.TypeEncounter; | ||
import com.google.inject.spi.TypeListener; | ||
import com.guicedee.guicedinjection.InjectLogger; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class Log4JTypeListener implements TypeListener { | ||
public <T> void hear(TypeLiteral<T> typeLiteral, TypeEncounter<T> typeEncounter) { | ||
Class<?> clazz = typeLiteral.getRawType(); | ||
while (clazz != null) { | ||
for (Field field : clazz.getDeclaredFields()) { | ||
if (field.getType() == Logger.class && | ||
field.isAnnotationPresent(InjectLogger.class)) { | ||
typeEncounter.register(new Log4JMembersInjector<T>(field)); | ||
} | ||
} | ||
clazz = clazz.getSuperclass(); | ||
} | ||
} | ||
} |