package io.undertow.servlet.api;
import java.lang.reflect.Constructor;
import java.util.EventListener;
import io.undertow.servlet.UndertowServletMessages;
import io.undertow.servlet.core.ApplicationListeners;
import io.undertow.servlet.util.ConstructorInstanceFactory;
public class ListenerInfo {
private final Class<? extends EventListener> listenerClass;
private volatile InstanceFactory<? extends EventListener> instanceFactory;
private final boolean programatic;
public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory) {
this(listenerClass, instanceFactory, false);
}
public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory, boolean programatic) {
this.listenerClass = listenerClass;
this.instanceFactory = instanceFactory;
this.programatic = programatic;
if(!ApplicationListeners.isListenerClass(listenerClass)) {
throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(listenerClass);
}
}
public ListenerInfo(final Class<? extends EventListener> listenerClass) {
this(listenerClass, false);
}
public ListenerInfo(final Class<? extends EventListener> listenerClass, boolean programatic) {
this.listenerClass = listenerClass;
this.programatic = programatic;
try {
final Constructor<EventListener> ctor = (Constructor<EventListener>) listenerClass.getDeclaredConstructor();
ctor.setAccessible(true);
this.instanceFactory = new ConstructorInstanceFactory<>(ctor);
} catch (NoSuchMethodException e) {
throw UndertowServletMessages.MESSAGES.componentMustHaveDefaultConstructor("Listener", listenerClass);
}
}
public InstanceFactory<? extends EventListener> getInstanceFactory() {
return instanceFactory;
}
public void setInstanceFactory(InstanceFactory<? extends EventListener> instanceFactory) {
this.instanceFactory = instanceFactory;
}
public boolean isProgramatic() {
return programatic;
}
public Class<? extends EventListener> getListenerClass() {
return listenerClass;
}
@Override
public String toString() {
return "ListenerInfo{" +
"listenerClass=" + listenerClass +
'}';
}
}