View Javadoc
1   package com.atlassian.plugin.spring.pluginns;
2   
3   import com.atlassian.plugin.osgi.hostcomponents.ContextClassLoaderStrategy;
4   import com.atlassian.plugin.spring.PluginBeanDefinitionRegistry;
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   import org.springframework.beans.factory.config.BeanDefinitionHolder;
8   import org.springframework.beans.factory.xml.BeanDefinitionDecorator;
9   import org.springframework.beans.factory.xml.ParserContext;
10  import org.w3c.dom.Attr;
11  import org.w3c.dom.Node;
12  
13  import javax.annotation.Nonnull;
14  
15  /**
16   * Processes an "contextClassLoader" strategy attribute in the plugin namespace.
17   * Also handles registering the {@link com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider} through
18   * the {@link com.atlassian.plugin.spring.SpringHostComponentProviderFactoryBean}.
19   *
20   * In the case of hierarchical contexts we will put the host component provider in the lowest possible context.
21   */
22  public class PluginContextClassLoaderStrategyBeanDefinitionDecorator implements BeanDefinitionDecorator {
23      private final static Logger log = LoggerFactory.getLogger(PluginContextClassLoaderStrategyBeanDefinitionDecorator.class);
24  
25      /**
26       * Called when the Spring parser encounters an "available" attribute.
27       *
28       * @param source The attribute
29       * @param holder The containing bean definition
30       * @param ctx    The parser context
31       * @return The containing bean definition
32       */
33      @Nonnull
34      @Override
35      public BeanDefinitionHolder decorate(@Nonnull Node source, @Nonnull BeanDefinitionHolder holder,
36                                           @Nonnull ParserContext ctx) {
37          final String contextClassLoaderStrategy = ((Attr) source).getValue();
38          if (contextClassLoaderStrategy != null) {
39              new PluginBeanDefinitionRegistry(ctx.getRegistry()).addContextClassLoaderStrategy(holder.getBeanName(), getContextClassLoaderStrategy(contextClassLoaderStrategy));
40          }
41          return holder;
42  
43      }
44  
45      private ContextClassLoaderStrategy getContextClassLoaderStrategy(String contextClassLoaderStrategy) {
46          try {
47              return ContextClassLoaderStrategy.valueOf(contextClassLoaderStrategy);
48          } catch (IllegalArgumentException e) {
49              log.warn("Cannot parse '{}' to a valid context class loader strategy, will use default '{}'", contextClassLoaderStrategy, ContextClassLoaderStrategy.USE_HOST);
50              return ContextClassLoaderStrategy.USE_HOST;
51          }
52      }
53  }