
br.com.objectos.way.base.log.AbstractWayLogModule Maven / Gradle / Ivy
/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.base.log;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.matcher.Matcher;
import com.google.inject.matcher.Matchers;
/**
* @author [email protected] (Marcio Endo)
*/
public abstract class AbstractWayLogModule extends AbstractModule {
@Override
protected final void configure() {
configureLoggers();
}
protected abstract void configureLoggers();
protected MethodMatcherBuilder log(Class> type) {
@SuppressWarnings("rawtypes")
Matcher classMatcher = Matchers.subclassesOf(type);
return new BindInterceptorBuilder(classMatcher);
}
protected MethodMatcherBuilder log(Package pkg) {
@SuppressWarnings("rawtypes")
Matcher classMatcher = Matchers.inPackage(pkg);
return new BindInterceptorBuilder(classMatcher);
}
protected MethodMatcherBuilder log(String pkg) {
@SuppressWarnings("rawtypes")
Matcher classMatcher = Matchers.inSubpackage(pkg);
return new BindInterceptorBuilder(classMatcher);
}
protected interface MethodMatcherBuilder {
InterceptorBuilder anyMethod();
InterceptorBuilder methodsAnnotatedWith(Annotation annotation);
InterceptorBuilder methodsAnnotatedWith(Class extends Annotation> annotationType);
InterceptorBuilder methodsReturning(Class> type);
}
protected interface InterceptorBuilder {
void withDefaultLogger();
void with(AbstractMethodLogger... loggers);
}
private class BindInterceptorBuilder
implements
MethodMatcherBuilder,
InterceptorBuilder {
private final Binder binder = binder();
private final Matcher super Class>> classMatcher;
private Matcher super Method> methodMatcher;
public BindInterceptorBuilder(Matcher super Class>> classMatcher) {
this.classMatcher = classMatcher;
}
@Override
public InterceptorBuilder anyMethod() {
methodMatcher = Matchers.any();
return this;
}
@Override
public InterceptorBuilder methodsAnnotatedWith(Annotation annotation) {
methodMatcher = Matchers.annotatedWith(annotation);
return this;
}
@Override
public InterceptorBuilder methodsAnnotatedWith(Class extends Annotation> annotationType) {
methodMatcher = Matchers.annotatedWith(annotationType);
return this;
}
@Override
public InterceptorBuilder methodsReturning(Class> type) {
@SuppressWarnings("rawtypes")
Matcher returnType = Matchers.subclassesOf(type);
methodMatcher = Matchers.returns(returnType);
return this;
}
@Override
public void withDefaultLogger() {
DefaultMethodLogger interceptor = new DefaultMethodLogger(getProvider(LogProcessor.class));
binder.bindInterceptor(classMatcher, methodMatcher, interceptor);
}
@Override
public void with(AbstractMethodLogger... loggers) {
binder.bindInterceptor(classMatcher, methodMatcher, loggers);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy