View Javadoc

1   package com.atlassian.plugin.descriptors.servlet;
2   
3   import com.atlassian.plugin.descriptors.servlet.util.DefaultPathMapper;
4   import com.atlassian.plugin.descriptors.servlet.util.PathMapper;
5   
6   import javax.servlet.ServletConfig;
7   import javax.servlet.ServletException;
8   import java.util.HashMap;
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  /**
13   * A simple manager to track and retrieve the loaded servlet plugin modules.
14   */
15  public class ServletModuleManager
16  {
17      PathMapper mapper = new DefaultPathMapper();
18      Map<String,ServletModuleDescriptor> descriptors = new HashMap<String,ServletModuleDescriptor>();
19      Map<String,DelegatingPluginServlet> inittedServlets = new HashMap<String,DelegatingPluginServlet>();
20  
21      public DelegatingPluginServlet getServlet(String path, final ServletConfig servletConfig) throws ServletException
22      {
23          String completeKey = mapper.get(path);
24          DelegatingPluginServlet servlet = null;
25  
26          if (completeKey != null)
27          {
28              servlet = inittedServlets.get(completeKey);
29  
30              if (servlet == null)
31              {
32                  final ServletModuleDescriptor descriptor = descriptors.get(completeKey);
33  
34                  if (descriptor != null)
35                  {
36                      servlet = new DelegatingPluginServlet(descriptor);
37                      servlet.init(new PluginServletConfig(descriptor, servletConfig));
38                      inittedServlets.put(completeKey, servlet);
39                  }
40              }
41          }
42  
43          return servlet;
44      }
45  
46      public void addModule(ServletModuleDescriptor descriptor)
47      {
48          descriptors.put(descriptor.getCompleteKey(), descriptor);
49  
50          for (String path : descriptor.getPaths())
51              mapper.put(descriptor.getCompleteKey(), path);
52      }
53  
54      public void removeModule(ServletModuleDescriptor descriptor)
55      {
56          descriptors.remove(descriptor.getCompleteKey());
57  
58          inittedServlets.remove(descriptor.getCompleteKey());
59  
60          mapper.put(descriptor.getCompleteKey(), null);
61      }
62  }