1 package com.atlassian.refapp.webitem;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.module.ContainerManagedPlugin;
5 import com.atlassian.plugin.web.Condition;
6 import com.atlassian.plugin.web.ContextProvider;
7 import com.atlassian.plugin.web.WebFragmentHelper;
8 import com.atlassian.plugin.web.conditions.ConditionLoadingException;
9 import com.atlassian.sal.api.message.I18nResolver;
10 import com.atlassian.templaterenderer.TemplateRenderer;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13
14 import java.io.Serializable;
15 import java.util.List;
16 import java.util.Map;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 public class WebFragmentHelperImpl implements WebFragmentHelper {
21 private final Log logger = LogFactory.getLog(getClass());
22
23 private final TemplateRenderer renderer;
24
25 public WebFragmentHelperImpl(TemplateRenderer renderer) {
26 this.renderer = checkNotNull(renderer);
27 }
28
29 public String getI18nValue(String key, List<?> arguments, Map<String, Object> context) {
30 if (!context.containsKey("i18n")) {
31 logger.info("context does not contain an I18nResolver as i18n, unable to get value");
32 return key;
33 }
34 I18nResolver i18n = (I18nResolver) context.get("i18n");
35 Serializable[] params = new Serializable[0];
36 if (arguments != null) {
37
38 params = arguments.toArray(params);
39 }
40 return i18n.getText(key, params);
41 }
42
43 public Condition loadCondition(String className, Plugin plugin) throws ConditionLoadingException {
44 try {
45 if (plugin instanceof ContainerManagedPlugin) {
46 Class conditionClass = plugin.loadClass(className, getClass());
47 return (Condition) ((ContainerManagedPlugin) plugin).getContainerAccessor().createBean(conditionClass);
48 } else {
49 throw new ConditionLoadingException("Plugin " + plugin.getKey() + " is not a ContainerManagedPlugin, could not load condition.");
50 }
51 } catch (IllegalArgumentException e) {
52 throw new ConditionLoadingException(e);
53 } catch (ClassNotFoundException e) {
54 throw new ConditionLoadingException(e);
55 }
56 }
57
58 public ContextProvider loadContextProvider(String className, Plugin plugin) throws ConditionLoadingException {
59 try {
60 if (plugin instanceof ContainerManagedPlugin) {
61 Class conditionClass = plugin.loadClass(className, getClass());
62 return (ContextProvider) ((ContainerManagedPlugin) plugin).getContainerAccessor().createBean(conditionClass);
63 } else {
64 throw new ConditionLoadingException("Plugin " + plugin.getKey() + " is not a ContainerManagedPlugin, could not load context.");
65 }
66 } catch (IllegalArgumentException e) {
67 throw new ConditionLoadingException(e);
68 } catch (ClassNotFoundException e) {
69 throw new ConditionLoadingException(e);
70 }
71 }
72
73 public String renderVelocityFragment(String fragment, Map<String, Object> context) {
74 return renderer.renderFragment(fragment, context);
75 }
76 }