1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.api;
20
21 import java.lang.reflect.Constructor;
22 import java.util.EventListener;
23
24 import io.undertow.servlet.UndertowServletMessages;
25 import io.undertow.servlet.core.ApplicationListeners;
26 import io.undertow.servlet.util.ConstructorInstanceFactory;
27
28 /**
29  * @author Stuart Douglas
30  */

31 public class ListenerInfo {
32
33
34     private final Class<? extends EventListener> listenerClass;
35     private volatile InstanceFactory<? extends EventListener> instanceFactory;
36     private final boolean programatic;
37
38     public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory) {
39         this(listenerClass, instanceFactory, false);
40     }
41
42     public ListenerInfo(final Class<? extends EventListener> listenerClass, final InstanceFactory<? extends EventListener> instanceFactory, boolean programatic) {
43         this.listenerClass = listenerClass;
44         this.instanceFactory = instanceFactory;
45         this.programatic = programatic;
46         if(!ApplicationListeners.isListenerClass(listenerClass)) {
47             throw UndertowServletMessages.MESSAGES.listenerMustImplementListenerClass(listenerClass);
48         }
49     }
50
51     public ListenerInfo(final Class<? extends EventListener> listenerClass) {
52         this(listenerClass, false);
53     }
54
55     public ListenerInfo(final Class<? extends EventListener> listenerClass, boolean programatic) {
56         this.listenerClass = listenerClass;
57         this.programatic = programatic;
58
59         try {
60             final Constructor<EventListener> ctor = (Constructor<EventListener>) listenerClass.getDeclaredConstructor();
61             ctor.setAccessible(true);
62             this.instanceFactory = new ConstructorInstanceFactory<>(ctor);
63         } catch (NoSuchMethodException e) {
64             throw UndertowServletMessages.MESSAGES.componentMustHaveDefaultConstructor("Listener", listenerClass);
65         }
66     }
67
68     public InstanceFactory<? extends EventListener> getInstanceFactory() {
69         return instanceFactory;
70     }
71
72     public void setInstanceFactory(InstanceFactory<? extends EventListener> instanceFactory) {
73         this.instanceFactory = instanceFactory;
74     }
75
76     public boolean isProgramatic() {
77         return programatic;
78     }
79
80     public Class<? extends EventListener> getListenerClass() {
81         return listenerClass;
82     }
83
84     @Override
85     public String toString() {
86         return "ListenerInfo{" +
87                 "listenerClass=" + listenerClass +
88                 '}';
89     }
90 }
91