View Javadoc

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