1 /* 2 * Copyright (C) 2012-2024 RRiBbit.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.rribbit.creation; 17 18 import java.util.Collection; 19 import java.util.List; 20 import java.util.concurrent.CopyOnWriteArrayList; 21 22 import org.rribbit.Listener; 23 import org.rribbit.ListenerObject; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 27 import org.springframework.context.ApplicationContext; 28 import org.springframework.context.ApplicationContextAware; 29 import org.springframework.context.ApplicationListener; 30 import org.springframework.context.event.ContextRefreshedEvent; 31 32 /** 33 * This {@link ListenerObjectCreator} creates {@link ListenerObject}s from classes. Users can pass in {@link Class}es or packagenames and this class will scan the {@link Class}es 34 * and create {@link ListenerObject}s for the public methods that are annotated with {@link Listener}. Note that public methods inherited from superclasses and superinterfaces will also be 35 * scanned. This means that users must take care not to scan a method twice, once as a method of a class and once as a method of a superclass, by passing a class/interface and its 36 * superclass/superinterface separately to this {@link ListenerObjectCreator}. 37 * <p /> 38 * Please note that in Java, method annotations are NOT inherited. This means that, if you override/implement a method in a subclass or subinterface, and the overriding/implementing method 39 * does not have the annotation, then that method will not inherit it. If a class or interface just inherits a method, without overriding it, then the annotation WILL exist. 40 * <p /> 41 * To get the actual {@link Object}s that the listeners run on, this will attempt to get a bean for a certain {@link Class} from the Spring Configuration. 42 * When no suitable bean can be found, the {@link Class} will simply be ignored. 43 * <p /> 44 * When the classnames and/or packagenames are set, the classes/packagenames are accumulated into {@link Collection}s, but actual processing of them will be done once the entire Spring 45 * Configuration is initialized. This is because this class needs other Spring Beans from the configuration to initialize the {@link ListenerObject}s. 46 * <p /> 47 * By default, subpackages of packages are not scanned. This can be changed by using the {@link #setScanSubpackages(boolean)} method. 48 * <p /> 49 * NOTE: When using interface-based Spring Proxy's, you have to declare your {@link Listener} annotations on the interface-methods, not the class methods. RRiBbit does not even need to 50 * know about the implementing classes, it will simply ask Spring for a bean that implements the interface that the method is declared in. 51 * 52 * @author G.J. Schouten 53 * 54 */ 55 public class SpringBeanClassBasedListenerObjectCreator extends AbstractClassBasedListenerObjectCreator implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent> { 56 57 private static final Logger log = LoggerFactory.getLogger(SpringBeanClassBasedListenerObjectCreator.class); 58 59 protected Collection<Class<?>> classesToBeProcessed; 60 protected Collection<String> packageNamesToBeProcessed; 61 protected Collection<Class<?>> classesToBeExcluded; 62 protected boolean scanSubpackages; 63 64 protected ApplicationContext applicationContext; 65 66 /** 67 * Constructors of superclass are not copied, since that would make declaring a bean of this type in the Spring configuration quite verbose. Use the provided setters instead. 68 */ 69 public SpringBeanClassBasedListenerObjectCreator() { 70 classesToBeProcessed = new CopyOnWriteArrayList<>(); 71 packageNamesToBeProcessed = new CopyOnWriteArrayList<>(); 72 classesToBeExcluded = new CopyOnWriteArrayList<>(); 73 scanSubpackages = false; 74 } 75 76 /** 77 * Set the classNames that are to be processed by calling {@link #addClass(Class)} once the Spring Configuration is initialized. 78 * 79 * @param classNames 80 */ 81 public void setClassNames(List<String> classNames) { 82 83 for(String className : classNames) { 84 try { 85 classesToBeProcessed.add(Class.forName(className)); 86 } catch(Exception e) { 87 throw new RuntimeException("Instantiation of Class object for class '" + className + "' failed", e); 88 } 89 } 90 } 91 92 /** 93 * Set the packageNames that are to be processed by calling {@link #addPackage(String, boolean)} once the Spring Configuration is initialized. 94 * 95 * @param packageNames 96 */ 97 public void setPackageNames(List<String> packageNames) { 98 99 packageNamesToBeProcessed.addAll(packageNames); 100 } 101 102 /** 103 * Sets the classes that should be exluced when the packages set with {@link #setPackageNames(List)} are scanned. 104 * 105 * @param excludedClasses 106 */ 107 public void setExcludedClasses(Collection<Class<?>> excludedClasses) { 108 109 classesToBeExcluded.addAll(excludedClasses); 110 } 111 112 /** 113 * Sets whether subpackages need to be scanned too. The default is false. 114 * 115 * @param scanSubpackages 116 */ 117 public void setScanSubpackages(boolean scanSubpackages) { 118 this.scanSubpackages = scanSubpackages; 119 } 120 121 @Override 122 protected Object getTargetObjectForClass(Class<?> clazz) { 123 124 try { 125 log.debug("Attempting to get bean of class '{}'", clazz.getName()); 126 return applicationContext.getBean(clazz); 127 } catch(NoSuchBeanDefinitionException e) { 128 log.debug("No suitable bean found, returning null"); 129 return null; 130 } 131 } 132 133 @Override 134 public void setApplicationContext(ApplicationContext applicationContext) { 135 this.applicationContext = applicationContext; 136 } 137 138 /** 139 * When the Spring Configuration is reloaded, all {@link ListenerObject}s contained in this {@link ListenerObjectCreator} are dropped, and new ones are created from the classNames 140 * and packageNames set in this bean. This means that if you added listeners to this object with one of the methods of one of the superclasses, such as {@link #addClass(Class)}, 141 * {@link #addObject(Object)} or {@link #addPackage(String, boolean)}, all the {@link ListenerObject}s that resulted from those calls will no longer be there after this call. 142 * <p /> 143 * This is by design. The reason for doing this is because a {@link ContextRefreshedEvent} represents a total refresh of all objects in the application, erasing previous state. If you 144 * do not want this, you can extend this class and override the three mentioned add* methods. Each of them must then add the {@link Class}es and packageNames it processes to the 145 * {@link Collection}s in this object, before calling super. 146 * 147 * @param contextRefreshedEvent 148 */ 149 @Override 150 public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { 151 152 if(contextRefreshedEvent.getApplicationContext().equals(applicationContext)) { //Only do this for this ApplicationContext, not for child contexts 153 log.debug("Clearing existing ListenerObjects"); 154 listenerObjects.clear(); 155 156 log.debug("Adding original classes"); 157 for(Class<?> clazz : classesToBeProcessed) { 158 this.addClass(clazz); 159 } 160 161 log.debug("Adding original classes to be excluded"); 162 for(Class<?> clazz : classesToBeExcluded) { 163 this.excludeClass(clazz); 164 } 165 166 log.debug("Adding original packages"); 167 for(String packageName : packageNamesToBeProcessed) { 168 this.addPackage(packageName, scanSubpackages); 169 } 170 } 171 } 172 }