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 plugin.stop(); 065 } catch (Exception e) { 066 logger.severe("Stopping " + this + " failed:"); 067 e.printStackTrace(); 068 // Note: we don't throw through the OSGi container here. It would not print out the stacktrace. 069 } 070 } 071 072 073 074 // ************************************ 075 // *** PluginContext Implementation *** 076 // ************************************ 077 078 079 080 @Override 081 public void init() { 082 } 083 084 @Override 085 public void postInstall() { 086 } 087 088 @Override 089 public void serviceArrived(PluginService service) { 090 } 091 092 @Override 093 public void serviceGone(PluginService service) { 094 } 095 096 // --- 097 098 @Override 099 public BundleContext getBundleContext() { 100 return bundleContext; 101 } 102 103 @Override 104 public void setCoreService(DeepaMehtaService dms) { 105 this.dms = dms; 106 } 107 108 109 110 // === 111 112 public String toString() { 113 return plugin.toString(); 114 } 115 116 117 118 // ----------------------------------------------------------------------------------------------- Protected Methods 119 120 /** 121 * @param securityHandler Optional. If null no security is provided. 122 */ 123 protected void publishDirectory(String directoryPath, String uriNamespace, SecurityHandler securityHandler) { 124 plugin.publishDirectory(directoryPath, uriNamespace, securityHandler); 125 } 126 }