001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.config;
019
020import java.util.HashMap;
021import java.util.Map;
022import java.util.logging.Level;
023import java.util.logging.Logger;
024
025import com.google.common.eventbus.EventBus;
026
027/**
028 * The event bus wraps the Google EventBus service for the
029 * daemon. It provides a publish/subscribe mechanism within a
030 * single JVM.
031 * 
032 * @author Philipp C. Heckel (philipp.heckel@gmail.com)
033 */
034//TODO [medium] This class belongs in the 'util' package
035public abstract class InternalEventBus {
036        protected static final Logger logger = Logger.getLogger(InternalEventBus.class.getSimpleName());
037        private static Map<Class<? extends InternalEventBus>, InternalEventBus> instances = new HashMap<>();
038        
039        protected EventBus eventBus;
040        
041        @SuppressWarnings("unchecked")
042        protected static <T extends InternalEventBus> T getInstance(Class<T> eventBusClass) {
043                T eventBusInstance = (T) instances.get(eventBusClass);
044                
045                if (eventBusInstance != null) {
046                        return eventBusInstance;
047                }
048                
049                try {
050                        eventBusInstance = eventBusClass.newInstance();
051                        instances.put(eventBusClass, eventBusInstance);
052                }
053                catch (Exception e) {
054                        throw new RuntimeException("Cannot create event bus with class " + eventBusClass);
055                }               
056                
057                return eventBusInstance;
058        }
059        
060        protected InternalEventBus() {
061                this.eventBus = new EventBus(this.getClass().getName());
062                logger.log(Level.INFO, "Event bus: Created event bus " + this.getClass().getName());
063        }
064        
065        public void register(Object object) {
066                logger.log(Level.INFO, "Event bus '" + this.getClass().getSimpleName() + "': Registering " + object.getClass().getSimpleName() + " (" + object + ") ...");
067                eventBus.register(object);
068        }
069        
070        public void unregister(Object object) {
071                logger.log(Level.INFO, "Event bus '" + this.getClass().getSimpleName() + "': Unregistering " + object.getClass().getSimpleName() + " (" + object + ") ...");
072                eventBus.unregister(object);
073        }       
074        
075        public void post(Object event) {
076                logger.log(Level.INFO, "Event bus '" + this.getClass().getSimpleName() + "': Posting event " + event.getClass().getSimpleName() + " (" + event + ") ...");
077                eventBus.post(event);
078        }
079}