View Javadoc

1   package com.atlassian.config.lifecycle;
2   
3   import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.StateAware;
7   import com.atlassian.spring.container.ContainerManager;
8   import org.dom4j.Element;
9   import org.dom4j.Attribute;
10  
11  import javax.servlet.ServletContextListener;
12  
13  /**
14   * Descriptor for lifecycle plugin modules.
15   *
16   * <p>A lifecycle plugin module must either implement {@link LifecycleItem}, or {@link ServletContextListener}. The
17   * latter will automatically be wrapped in a {@link ServletContextListenerWrapper} by the descriptor, so
18   * {@link #getModule()} will <i>always</i> return a LifecycleItem instance.
19   *
20   * <p>Each module has a sequence number. On startup, the modules will be invoked in ascending order of sequence (lowest
21   * to highest), and then on shutdown, the order will be reversed.
22   */
23  public class LifecyclePluginModuleDescriptor extends AbstractModuleDescriptor implements Comparable
24  {
25      private Object module;
26      private int sequence;
27  
28      /**
29       * Default no-arg constructor
30       */
31      public LifecyclePluginModuleDescriptor()
32      {
33      }
34  
35      /**
36       * Helpful constructor for tests
37       */
38      LifecyclePluginModuleDescriptor(Object module, int sequence)
39      {
40          this.module = module;
41          this.sequence = sequence;
42      }
43  
44      public void init(Plugin plugin, Element element) throws PluginParseException
45      {
46          super.init(plugin, element);
47          sequence = determineSequenceNumber(element);
48      }
49  
50      private void ensureCompatibleModuleType() throws PluginParseException
51      {
52          Class moduleClass = getModuleClass();
53          if (!LifecycleItem.class.isAssignableFrom(moduleClass) && !ServletContextListener.class.isAssignableFrom(moduleClass))
54              throw new PluginParseException("Lifecycle classes must extend LifecycleItem or ServletContextListener. Module class: " + moduleClass.getName());
55      }
56  
57      private int determineSequenceNumber(Element element) throws PluginParseException
58      {
59          Attribute att = element.attribute("sequence");
60          if (att != null)
61          {
62              String value = att.getValue();
63              try
64              {
65                  return Integer.parseInt(value);
66              }
67              catch (NumberFormatException e)
68              {
69                  throw new PluginParseException("Could not determine sequence from: " + value);
70              }
71          }
72  
73          throw new PluginParseException("Missing required attribute: sequence");
74      }
75  
76      public Object getModule()
77      {
78          return module;
79      }
80  
81      private Object makeModule()
82      {
83          Object module = ContainerManager.getInstance().getContainerContext().createComponent(getModuleClass());
84  
85          if (module instanceof ServletContextListener)
86              module = new ServletContextListenerWrapper((ServletContextListener) module);
87  
88          return module;
89      }
90  
91      public void enabled()
92      {
93          super.enabled();
94          ensureCompatibleModuleType();
95          module = makeModule();
96          if  (module instanceof StateAware)
97              ((StateAware) module).enabled();
98      }
99  
100     public void disabled()
101     {
102         if  (module instanceof StateAware)
103             ((StateAware) module).disabled();
104         module = null;
105         super.disabled();
106     }
107 
108     public int getSequence()
109     {
110         return sequence;
111     }
112 
113     public int compareTo(Object o)
114     {
115         int otherSequence = ((LifecyclePluginModuleDescriptor)o).sequence;
116 
117         return (sequence == otherSequence) ? 0 : (sequence < otherSequence) ? -1 : 1;
118     }
119 }