001    package de.deepamehta.core.osgi;
002    
003    import de.deepamehta.core.service.DeepaMehtaService;
004    import de.deepamehta.core.service.PluginService;
005    import de.deepamehta.core.service.SecurityHandler;
006    import de.deepamehta.core.impl.PluginImpl;
007    
008    import org.osgi.framework.Bundle;
009    import org.osgi.framework.BundleActivator;
010    import org.osgi.framework.BundleContext;
011    
012    import java.util.logging.Logger;
013    
014    
015    
016    /**
017     * Base class for all DeepaMehta plugins.
018     * All DeepaMehta plugins are derived from this class, directly or indirectly.
019     */
020    public class PluginActivator implements BundleActivator, PluginContext {
021    
022        // ---------------------------------------------------------------------------------------------- Instance Variables
023    
024        protected DeepaMehtaService dms;
025        protected Bundle bundle;
026    
027        private BundleContext bundleContext;
028        private PluginImpl plugin;
029    
030        private Logger logger = Logger.getLogger(getClass().getName());
031    
032        // -------------------------------------------------------------------------------------------------- Public Methods
033    
034    
035    
036        // **************************************
037        // *** BundleActivator Implementation ***
038        // **************************************
039    
040    
041    
042        @Override
043        public void start(BundleContext context) {
044            this.bundleContext = context;
045            this.bundle = context.getBundle();
046            this.plugin = new PluginImpl(this);
047            //
048            try {
049                // Note: logging "this" requires "plugin" to be initialzed already
050                logger.info("========== Starting " + this + " ==========");
051                plugin.start();
052            } catch (Exception e) {
053                logger.severe("Starting " + this + " failed:");
054                e.printStackTrace();
055                // Note: we don't throw through the OSGi container here. It would not print out the stacktrace.
056                // File Install would retry to start the bundle endlessly.
057            }
058        }
059    
060        @Override
061        public void stop(BundleContext context) {
062            try {
063                logger.info("========== Stopping " + this + " ==========");
064                shutdown();
065                plugin.stop();
066            } catch (Exception e) {
067                logger.severe("Stopping " + this + " failed:");
068                e.printStackTrace();
069                // Note: we don't throw through the OSGi container here. It would not print out the stacktrace.
070            }
071        }
072    
073    
074    
075        // ************************************
076        // *** PluginContext Implementation ***
077        // ************************************
078    
079    
080    
081        @Override
082        public void init() {
083        }
084    
085        @Override
086        public void shutdown() {
087        }
088    
089        @Override
090        public void postInstall() {
091        }
092    
093        @Override
094        public void serviceArrived(PluginService service) {
095        }
096    
097        @Override
098        public void serviceGone(PluginService service) {
099        }
100    
101        // ---
102    
103        @Override
104        public BundleContext getBundleContext() {
105            return bundleContext;
106        }
107    
108        @Override
109        public void setCoreService(DeepaMehtaService dms) {
110            this.dms = dms;
111        }
112    
113    
114    
115        // ===
116    
117        @Override
118        public String toString() {
119            return plugin.toString();
120        }
121    
122    
123    
124        // ----------------------------------------------------------------------------------------------- Protected Methods
125    
126        protected String getUri() {
127            return plugin.getUri();
128        }
129    
130        /**
131         * @param   securityHandler     Optional. If null no security is provided.
132         */
133        protected void publishDirectory(String directoryPath, String uriNamespace, SecurityHandler securityHandler) {
134            plugin.publishDirectory(directoryPath, uriNamespace, securityHandler);
135        }
136    }