001/* Copyright 2011-2012 the original author or authors: 002 * 003 * Marc Palmer (marc@grailsrocks.com) 004 * Stéphane Maldini (smaldini@vmware.com) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.grails.plugin.platform.events.publisher; 019 020import org.apache.log4j.Logger; 021import org.grails.plugin.platform.events.EventReply; 022import org.grails.plugin.platform.events.Events; 023import org.grails.plugin.platform.events.dispatcher.GormTopicSupport; 024import org.springframework.context.ApplicationEvent; 025import org.springframework.context.ApplicationListener; 026 027import java.util.HashMap; 028import java.util.Map; 029 030/** 031 * @author Stephane Maldini <smaldini@vmware.com> 032 * @version 1.0 033 * @file 034 * @date 29/05/12 035 * @section DESCRIPTION 036 * <p/> 037 * [Does stuff] 038 */ 039public class GormBridgePublisher implements ApplicationListener { 040 041 private GormTopicSupport gormTopicSupport; 042 private Events grailsEvents; 043 044 private final static Logger log = Logger.getLogger(GormBridgePublisher.class); 045 static final private String GORM_EVENT_PACKAGE = "org.grails.datastore.mapping.engine.event"; 046 047 public void setGrailsEvents(Events grailsEvents) { 048 this.grailsEvents = grailsEvents; 049 } 050 051 public void setGormTopicSupport(GormTopicSupport gormTopicSupport) { 052 this.gormTopicSupport = gormTopicSupport; 053 } 054 055 public void onApplicationEvent(ApplicationEvent applicationEvent) { 056 //fixme horrible hack to support grails 1.3.x 057 if (applicationEvent.getClass().getName().startsWith(GORM_EVENT_PACKAGE)) { 058 String topic = gormTopicSupport.convertTopic(applicationEvent); 059 if (topic != null) { 060 log.debug("sending " + applicationEvent + " to topic " + topic); 061 062 Map<String, Object> params = new HashMap<String, Object>(); 063 params.put(EventsPublisher.GORM, false); 064 params.put(EventsPublisher.FORK, false); 065 066 EventReply reply = grailsEvents.event(GormTopicSupport.GORM_SOURCE, topic, 067 gormTopicSupport.extractEntity(applicationEvent), params); 068 try { 069 gormTopicSupport.processCancel(applicationEvent, reply != null ? reply.getValues() : null); 070 } catch (Throwable e) { 071 throw new RuntimeException(e);//shouldn't happen as its sync event 072 } 073 } 074 } 075 } 076}