|
| 1 | +/* |
| 2 | + * Copyright 2015 original authors |
| 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 | + * http://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.grails.events.spring |
| 17 | + |
| 18 | +import grails.events.bus.EventBus |
| 19 | +import grails.util.GrailsNameUtils |
| 20 | +import groovy.transform.CompileStatic |
| 21 | +import org.springframework.beans.BeansException |
| 22 | +import org.springframework.context.ApplicationContext |
| 23 | +import org.springframework.context.ApplicationContextAware |
| 24 | +import org.springframework.context.ApplicationEvent |
| 25 | +import org.springframework.context.ApplicationListener |
| 26 | +import org.springframework.context.event.ContextClosedEvent |
| 27 | +import org.springframework.context.support.GenericApplicationContext |
| 28 | +import java.util.concurrent.ConcurrentHashMap |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Translates Spring Events into Reactor events |
| 33 | + * |
| 34 | + * @author Graeme Rocher |
| 35 | + * @since 3.0.8 |
| 36 | + */ |
| 37 | +@CompileStatic |
| 38 | +class SpringEventTranslator implements ApplicationListener, ApplicationContextAware { |
| 39 | + |
| 40 | + public static final String GDM_EVENT_PACKAGE = 'org.grails.datastore' |
| 41 | + public static final String EVENT_SUFFIX = "Event" |
| 42 | + public static final String SPRING_PACKAGE = "org.springframework" |
| 43 | + |
| 44 | + private Map<Class, String> eventClassToName = new ConcurrentHashMap<Class, String>().withDefault { Class eventClass -> |
| 45 | + def clsName = eventClass.name |
| 46 | + def logicalName = GrailsNameUtils.getLogicalPropertyName(clsName, EVENT_SUFFIX) |
| 47 | + if(clsName.startsWith(GDM_EVENT_PACKAGE)) { |
| 48 | + return "gorm:${logicalName}".toString() |
| 49 | + } |
| 50 | + else if(clsName.startsWith(SPRING_PACKAGE)) { |
| 51 | + return "spring:${logicalName}".toString() |
| 52 | + } |
| 53 | + else { |
| 54 | + return "grails:${logicalName}".toString() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + private final EventBus eventBus |
| 60 | + |
| 61 | + SpringEventTranslator(EventBus eventBus) { |
| 62 | + this.eventBus = eventBus |
| 63 | + } |
| 64 | + |
| 65 | + void onApplicationEvent(ApplicationEvent event) { |
| 66 | + def eventName = eventClassToName[event.getClass()] |
| 67 | + |
| 68 | + if(eventBus.isActive()) { |
| 69 | + // don't relay context closed events because Reactor would have been shutdown |
| 70 | + if(!(event instanceof ContextClosedEvent)) { |
| 71 | + eventBus.notify(eventName, event) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 78 | + ((GenericApplicationContext)applicationContext).addApplicationListener(this) |
| 79 | + } |
| 80 | +} |
0 commit comments