View Javadoc

1   package com.atlassian.plugin.web.model;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugin.PluginAccessor;
5   import com.atlassian.plugin.web.descriptors.WebPanelRendererModuleDescriptor;
6   import com.atlassian.plugin.web.renderer.RendererException;
7   import com.atlassian.plugin.web.renderer.StaticWebPanelRenderer;
8   import com.atlassian.plugin.web.renderer.WebPanelRenderer;
9   import com.google.common.base.Preconditions;
10  import com.google.common.base.Predicate;
11  import com.google.common.collect.Iterables;
12  
13  import java.io.IOException;
14  import java.io.Writer;
15  import java.util.Map;
16  import java.util.NoSuchElementException;
17  
18  /**
19   * @since   2.5.0
20   */
21  public abstract class AbstractWebPanel implements WebPanel
22  {
23      private final PluginAccessor pluginAccessor;
24      protected Plugin plugin;
25      private String resourceType;
26  
27      protected AbstractWebPanel(PluginAccessor pluginAccessor)
28      {
29          this.pluginAccessor = pluginAccessor;
30      }
31  
32      public void setPlugin(Plugin plugin)
33      {
34          this.plugin = plugin;
35      }
36  
37      public void setResourceType(String resourceType)
38      {
39          this.resourceType = Preconditions.checkNotNull(resourceType);
40      }
41  
42      /**
43       * Default implementation of {@link WebPanel#writeHtml(java.io.Writer, java.util.Map)} that delegates to
44       * {@link WebPanel#getHtml(java.util.Map)}. This method is provided for backwards compatibility with
45       * pre-2.11 implementations of WebPanel. Panels SHOULD be upgraded to support this method natively
46       * for performance reasons.
47       *
48       * @param writer the writer to append the panel output to
49       * @param context the contextual information that can be used during
50       *  rendering. Context elements are not standardized and are
51       *  application-specific, so refer to your application's documentation to
52       *  learn what is available.
53       * @throws IOException
54       */
55      public void writeHtml(Writer writer, Map<String, Object> context) throws IOException
56      {
57          writer.write(getHtml(context));
58      }
59  
60      protected final WebPanelRenderer getRenderer()
61      {
62          if (StaticWebPanelRenderer.RESOURCE_TYPE.equals(resourceType))
63          {
64              return StaticWebPanelRenderer.RENDERER;
65          }
66          else
67          {
68              // PERF hit the cache
69              try
70              {
71                  return Iterables.find(pluginAccessor.getEnabledModuleDescriptorsByClass(
72                      WebPanelRendererModuleDescriptor.class),
73                      new Predicate<WebPanelRendererModuleDescriptor>()
74                      {
75                          @Override
76                          public boolean apply(WebPanelRendererModuleDescriptor descriptor)
77                          {
78                              WebPanelRenderer webPanelRenderer = descriptor.getModule();
79                              return (Preconditions.checkNotNull(resourceType).equals(webPanelRenderer.getResourceType()));
80                          }
81                      }).getModule();
82              }
83              catch (NoSuchElementException e)
84              {
85                  throw new RendererException("No renderer found for resource type: " + resourceType, e);
86              }
87          }
88      }
89  }