
com.guicedee.guicedservlets.jsf.implementations.JsfNamedBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guiced-servlets-jsf Show documentation
Show all versions of guiced-servlets-jsf Show documentation
Configures Guice Servlet for JSF
package com.guicedee.guicedservlets.jsf.implementations;
import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.name.Names;
import com.guicedee.cdi.services.NamedBindings;
import com.guicedee.guicedinjection.GuiceContext;
import com.guicedee.guicedinjection.interfaces.IGuiceModule;
import io.github.classgraph.ClassInfo;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.guicedee.guicedinjection.json.StaticStrings.*;
public class JsfNamedBinder
extends AbstractModule
implements IGuiceModule
{
@SuppressWarnings("deprecation")
@Override
protected void configure()
{
for (ClassInfo classInfo : GuiceContext.instance()
.getScanResult()
.getClassesWithAnnotation(ManagedBean.class.getCanonicalName()))
{
if (classInfo.isInterfaceOrAnnotation()
|| classInfo.hasAnnotation("javax.enterprise.context.Dependent"))
{
continue;
}
Class> clazz = classInfo.loadClass();
ManagedBean nn = clazz.getAnnotation(ManagedBean.class);
String name = NamedBindings.cleanName(classInfo, nn.name());
if (nn.eager())
{
NamedBindings.bindToEagerSingleton(binder(), clazz, name);
}
else
{
boolean sessionScoped = clazz.isAnnotationPresent(SessionScoped.class);
boolean requestScoped = clazz.isAnnotationPresent(RequestScoped.class);
boolean applicationScoped = clazz.isAnnotationPresent(ApplicationScoped.class);
if (sessionScoped)
{
NamedBindings.bindToScope(binder(), clazz, name, com.google.inject.servlet.SessionScoped.class);
}
else if (requestScoped)
{
NamedBindings.bindToScope(binder(), clazz, name, com.google.inject.servlet.RequestScoped.class);
}
else if (applicationScoped)
{
NamedBindings.bindToScope(binder(), clazz, name, Singleton.class);
}
else
{
NamedBindings.bindToScope(binder(), clazz, name);
}
}
}
super.configure();
}
@Override
public Integer sortOrder()
{
return 151;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy