1 package org.columba.core.scripting.service; 19 20 import java.util.Enumeration ; 21 import java.util.logging.Level ; 22 import java.util.logging.Logger ; 23 24 import org.columba.api.plugin.IExtensionHandler; 25 import org.columba.api.plugin.IExtensionHandlerKeys; 26 import org.columba.api.plugin.IExtensionInterface; 27 import org.columba.api.plugin.PluginException; 28 import org.columba.api.plugin.PluginHandlerNotFoundException; 29 import org.columba.core.logging.Logging; 30 import org.columba.core.plugin.Extension; 31 import org.columba.core.plugin.PluginManager; 32 import org.columba.core.scripting.service.api.IColumbaService; 33 34 public class ServiceManager { 35 36 private static final Logger LOG = Logger 37 .getLogger("org.columba.core.scripting.service.ServiceManager"); 38 39 private static ServiceManager instance = new ServiceManager(); 40 41 private IExtensionHandler handler; 42 43 private ServiceManager() { 44 try { 45 handler = PluginManager.getInstance().getExtensionHandler( 46 IExtensionHandlerKeys.ORG_COLUMBA_CORE_SERVICE); 47 } catch (PluginHandlerNotFoundException e) { 48 e.printStackTrace(); 49 } 50 } 51 52 public static ServiceManager getInstance() { 53 return instance; 54 } 55 56 64 private IColumbaService getServiceInstance(Extension extension) { 65 66 IExtensionInterface service = null; 67 try { 68 service = (IExtensionInterface) extension 69 .instanciateExtension(new Object [] {}); 70 } catch (PluginException e1) { 71 LOG.severe("Failed to load service: " + e1.getMessage()); 72 73 if (Logging.DEBUG) 74 e1.printStackTrace(); 75 76 return null; 77 } 78 79 if (!(service instanceof IColumbaService)) { 80 LOG.log(Level.WARNING, 81 "Service plugin doesn't explicitly declare an " 82 + "IColumbaService interface. Service ignored..."); 83 return null; 84 } 85 86 return (IColumbaService) service; 87 88 } 89 90 94 public void initServices() { 95 Enumeration e = handler.getExtensionEnumeration(); 96 while (e.hasMoreElements()) { 97 Extension extension = (Extension) e.nextElement(); 98 99 IColumbaService service = getServiceInstance(extension); 104 service.initService(); 105 106 } 107 108 } 109 110 public void disposeServices() { 111 Enumeration e = handler.getExtensionEnumeration(); 112 while (e.hasMoreElements()) { 113 Extension extension = (Extension) e.nextElement(); 114 IColumbaService service = getServiceInstance(extension); 115 service.disposeService(); 116 } 117 } 118 119 public void startServices() { 120 Enumeration e = handler.getExtensionEnumeration(); 121 while (e.hasMoreElements()) { 122 Extension extension = (Extension) e.nextElement(); 123 IColumbaService service = getServiceInstance(extension); 124 service.startService(); 125 } 126 127 } 128 129 public void stopServices() { 130 Enumeration e = handler.getExtensionEnumeration(); 131 while (e.hasMoreElements()) { 132 Extension extension = (Extension) e.nextElement(); 133 IColumbaService service = getServiceInstance(extension); 134 service.stopService(); 135 } 136 } 137 } 138 | Popular Tags |