001    package de.deepamehta.core.osgi;
002    
003    import de.deepamehta.core.impl.EmbeddedService;
004    import de.deepamehta.core.impl.StorageDecorator;
005    import de.deepamehta.core.impl.WebPublishingService;
006    import de.deepamehta.core.service.DeepaMehtaService;
007    import de.deepamehta.core.storage.spi.DeepaMehtaStorage;
008    
009    import org.osgi.framework.BundleActivator;
010    import org.osgi.framework.BundleContext;
011    import org.osgi.framework.ServiceReference;
012    import org.osgi.service.http.HttpService;
013    import org.osgi.util.tracker.ServiceTracker;
014    
015    import java.util.logging.Level;
016    import java.util.logging.Logger;
017    
018    
019    
020    public class CoreActivator implements BundleActivator {
021    
022        // ---------------------------------------------------------------------------------------------- Instance Variables
023    
024        private BundleContext bundleContext;
025    
026        // consumed services
027        private DeepaMehtaStorage storageService;
028        private HttpService httpService;
029    
030        private ServiceTracker storageServiceTracker;
031        private ServiceTracker httpServiceTracker;
032    
033        private Logger logger = Logger.getLogger(getClass().getName());
034    
035        // -------------------------------------------------------------------------------------------------- Public Methods
036    
037    
038    
039        // **************************************
040        // *** BundleActivator Implementation ***
041        // **************************************
042    
043    
044    
045        @Override
046        public void start(BundleContext bundleContext) {
047            try {
048                logger.info("========== Starting \"DeepaMehta 4 Core\" ==========");
049                this.bundleContext = bundleContext;
050                //
051                storageServiceTracker = createServiceTracker(DeepaMehtaStorage.class);
052                httpServiceTracker = createServiceTracker(HttpService.class);
053                //
054                storageServiceTracker.open();
055                httpServiceTracker.open();
056            } catch (Throwable e) {
057                logger.log(Level.SEVERE, "Starting \"DeepaMehta 4 Core\" failed", e);
058                // Note: here we catch anything, also errors (like NoClassDefFoundError).
059                // If thrown through the OSGi container it would not print out the stacktrace.
060                // File Install would retry to start the bundle endlessly.
061            }
062        }
063    
064        @Override
065        public void stop(BundleContext bundleContext) {
066            try {
067                logger.info("========== Stopping \"DeepaMehta 4 Core\" ==========");
068                storageServiceTracker.close();
069                httpServiceTracker.close();
070                //
071                // Note: we do not shutdown the DB here.
072                // The DB shuts down itself through the storage bundle's stop() method.
073            } catch (Throwable e) {
074                logger.log(Level.SEVERE, "Stopping \"DeepaMehta 4 Core\" failed", e);
075                // Note: here we catch anything, also errors (like NoClassDefFoundError).
076                // If thrown through the OSGi container it would not print out the stacktrace.
077            }
078        }
079    
080    
081    
082        // ------------------------------------------------------------------------------------------------- Private Methods
083    
084        private ServiceTracker createServiceTracker(final Class serviceInterface) {
085            //
086            return new ServiceTracker(bundleContext, serviceInterface.getName(), null) {
087    
088                @Override
089                public Object addingService(ServiceReference serviceRef) {
090                    Object service = null;
091                    try {
092                        service = super.addingService(serviceRef);
093                        addService(service);
094                    } catch (Throwable e) {
095                        logger.log(Level.SEVERE, "Adding service " + serviceInterface.getName() +
096                            " to \"DeepaMehta 4 Core\" failed", e);
097                        // Note: here we catch anything, also errors (like NoClassDefFoundError).
098                        // If thrown through the OSGi container it would not print out the stacktrace.
099                    }
100                    return service;
101                }
102    
103                @Override
104                public void removedService(ServiceReference ref, Object service) {
105                    try {
106                        removeService(service);
107                        super.removedService(ref, service);
108                    } catch (Throwable e) {
109                        logger.log(Level.SEVERE, "Removing service " + serviceInterface.getName() +
110                            " from \"DeepaMehta 4 Core\" failed", e);
111                        // Note: here we catch anything, also errors (like NoClassDefFoundError).
112                        // If thrown through the OSGi container it would not print out the stacktrace.
113                    }
114                }
115            };
116        }
117    
118        // ---
119    
120        private void addService(Object service) {
121            if (service instanceof DeepaMehtaStorage) {
122                logger.info("Adding storage service to DeepaMehta 4 Core");
123                storageService = (DeepaMehtaStorage) service;
124                checkRequirementsForActivation();
125            } else if (service instanceof HttpService) {
126                logger.info("Adding HTTP service to DeepaMehta 4 Core");
127                httpService = (HttpService) service;
128                checkRequirementsForActivation();
129            }
130        }
131    
132        private void removeService(Object service) {
133            if (service == storageService) {
134                logger.info("Removing storage service from DeepaMehta 4 Core");
135                storageService = null;
136            } else if (service == httpService) {
137                logger.info("Removing HTTP service from DeepaMehta 4 Core");
138                // ### TODO: unregister WebPublishingService
139                httpService = null;
140            }
141        }
142    
143        // ---
144    
145        private void checkRequirementsForActivation() {
146            if (storageService != null && httpService != null) {
147                EmbeddedService dms = new EmbeddedService(new StorageDecorator(storageService), bundleContext);
148                logger.info("Registering DeepaMehta 4 core service at OSGi framework");
149                bundleContext.registerService(DeepaMehtaService.class.getName(), dms, null);
150                //
151                WebPublishingService wpService = new WebPublishingService(dms, httpService);
152                logger.info("Registering Web Publishing service at OSGi framework");
153                bundleContext.registerService(WebPublishingService.class.getName(), wpService, null);
154            }
155        }
156    }