1 package com.atlassian.config.lifecycle; 2 3 import com.atlassian.plugin.PluginManager; 4 import com.atlassian.johnson.event.Event; 5 import com.atlassian.johnson.event.EventType; 6 import com.atlassian.johnson.event.EventLevel; 7 import com.atlassian.event.EventManager; 8 import com.atlassian.config.lifecycle.events.ApplicationStoppedEvent; 9 import com.atlassian.config.lifecycle.events.ApplicationStartedEvent; 10 import com.atlassian.config.lifecycle.events.ApplicationStoppingEvent; 11 12 import javax.servlet.ServletContext; 13 import java.util.List; 14 import java.util.Collections; 15 import java.util.Iterator; 16 17 import org.apache.log4j.Logger; 18 19 public class DefaultLifecycleManager implements LifecycleManager 20 { 21 private static Logger log = Logger.getLogger(LifecycleManager.class); 22 23 private PluginManager pluginManager; 24 private EventManager eventManager; 25 26 public void startUp(ServletContext servletContext) 27 { 28 List moduleDescriptors = getLifecyclePluginModuleDescriptors(); 29 LifecycleContext context = new DefaultLifecycleContext(servletContext); 30 31 LifecyclePluginModuleDescriptor descriptor = null; 32 33 try 34 { 35 for (Iterator it = moduleDescriptors.iterator(); it.hasNext();) 36 { 37 descriptor = (LifecyclePluginModuleDescriptor) it.next(); 38 LifecycleItem item = (LifecycleItem) descriptor.getModule(); 39 log.info("Starting: " + descriptor); 40 item.startup(context); 41 } 42 43 eventManager.publishEvent(new ApplicationStartedEvent(this)); 44 } 45 catch (Throwable t) 46 { 47 panicAndShutdown(t, context, descriptor); 48 } 49 } 50 51 public void shutDown(ServletContext servletContext) 52 { 53 shutDown(servletContext, null); 54 } 55 56 private void panicAndShutdown(Throwable t, LifecycleContext context, LifecyclePluginModuleDescriptor descriptor) 57 { 58 String errorString = "Unable to start up Confluence. Fatal error during startup sequence: " + descriptor + " - " + t; 59 log.fatal(errorString, t); 60 context.getAgentJohnson().addEvent(new Event(EventType.get("startup"), errorString, EventLevel.FATAL)); 61 shutDown(context.getServletContext(), descriptor.getCompleteKey()); 62 } 63 64 private void shutDown(ServletContext servletContext, String startingPluginKey) 65 { 66 eventManager.publishEvent(new ApplicationStoppingEvent(this)); 67 68 List moduleDescriptors = getLifecyclePluginModuleDescriptors(); 69 Collections.reverse(moduleDescriptors); 70 LifecycleContext context = new DefaultLifecycleContext(servletContext); 71 72 boolean started = startingPluginKey == null; 73 74 for (Iterator it = moduleDescriptors.iterator(); it.hasNext();) 75 { 76 LifecyclePluginModuleDescriptor descriptor = (LifecyclePluginModuleDescriptor) it.next(); 77 78 if (!started) 79 { 80 if (descriptor.getCompleteKey().equals(startingPluginKey)) 81 { 82 started = true; 83 } 84 else 85 { 86 continue; 87 } 88 } 89 90 log.info("Shutting down: " + descriptor); 91 LifecycleItem item = (LifecycleItem) descriptor.getModule(); 92 try 93 { 94 item.shutdown(context); 95 } 96 catch (Throwable t) 97 { 98 log.error("Error running shutdown plugin: " + descriptor.getDescription() + " - " + t, t); 99 } 100 } 101 102 eventManager.publishEvent(new ApplicationStoppedEvent(this)); 103 } 104 105 private List getLifecyclePluginModuleDescriptors() 106 { 107 List modules = pluginManager.getEnabledModuleDescriptorsByClass(LifecyclePluginModuleDescriptor.class); 108 Collections.sort(modules); 109 return modules; 110 } 111 112 public void setPluginManager(PluginManager pluginManager) 113 { 114 this.pluginManager = pluginManager; 115 } 116 117 public void setEventManager(EventManager eventManager) 118 { 119 this.eventManager = eventManager; 120 } 121 }