001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    package org.granite.osgi.adaptor;
021    
022    import java.util.Iterator;
023    import java.util.Set;
024    
025    import org.granite.config.flex.ServicesConfig;
026    import org.granite.logging.Logger;
027    import org.granite.osgi.constants.OSGIConstants;
028    import org.osgi.service.event.Event;
029    import org.osgi.service.event.EventHandler;
030    
031    public class ServiceEventHandler implements EventHandler {
032    
033            private static final Logger log = Logger.getLogger(ServiceEventHandler.class);
034    
035        private ServicesConfig servicesConfig;
036        
037        public ServiceEventHandler(ServicesConfig servicesConfig) {
038            this.servicesConfig = servicesConfig;
039        }
040        
041            public void handleEvent(Event event) {
042                    Class<?> c=(Class<?>) event.getProperty(OSGIConstants.SERVICE_CLASS);
043                    @SuppressWarnings("unchecked")
044                    Set<Class<?>>classes=(Set<Class<?>>)event.getProperty(OSGIConstants.SERVICE_CLASS_SET);
045                    //add service
046                    if(event.getTopic().equals(OSGIConstants.TOPIC_GDS_ADD_SERVICE)){
047                            if(c!=null)
048                                    servicesConfig.handleClass(c);
049                            if(classes!=null){
050                                    Iterator<Class<?>> it=classes.iterator();
051                                     while(it.hasNext()){
052                                             servicesConfig.handleClass(it.next());
053                                     }
054                            }else{
055                                    log.warn("Class NOT Found!!");
056                            }
057                    }
058                    //remove service
059               if(event.getTopic().equals(OSGIConstants.TOPIC_GDS_REMOVE_SERVICE)){
060                            if(c!=null)
061                                    servicesConfig.handleRemoveService(c);
062                            if(classes!=null){
063                                    Iterator<Class<?>> it=classes.iterator();
064                                     while(it.hasNext()){
065                                             servicesConfig.handleRemoveService(it.next());
066                                     }
067                            }else{
068                                    log.warn("Class NOT Found!!");
069                            }
070                    }
071            }
072    
073    }