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  /**
14   * Processes an "contextClassLoader" strategy attribute in the plugin namespace.
15   * Also handles registering the {@link com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider} through
16   * the {@link com.atlassian.plugin.spring.SpringHostComponentProviderFactoryBean}.
17   *
18   * In the case of hierarchical contexts we will put the host component provider in the lowest possible context.
19   */
20  public class PluginContextClassLoaderStrategyBeanDefinitionDecorator implements BeanDefinitionDecorator {
21      private final static Logger log = LoggerFactory.getLogger(PluginContextClassLoaderStrategyBeanDefinitionDecorator.class);
22  
23      /**
24       * Called when the Spring parser encounters an "available" attribute.
25       *
26       * @param source The attribute
27       * @param holder The containing bean definition
28       * @param ctx    The parser context
29       * @return The containing bean definition
30       */
31      public BeanDefinitionHolder decorate(Node source, BeanDefinitionHolder holder, ParserContext ctx) {
32          final String contextClassLoaderStrategy = ((Attr) source).getValue();
33          if (contextClassLoaderStrategy != null) {
34              new PluginBeanDefinitionRegistry(ctx.getRegistry()).addContextClassLoaderStrategy(holder.getBeanName(), getContextClassLoaderStrategy(contextClassLoaderStrategy));
35          }
36          return holder;
37  
38      }
39  
40      private ContextClassLoaderStrategy getContextClassLoaderStrategy(String contextClassLoaderStrategy) {
41          try {
42              return ContextClassLoaderStrategy.valueOf(contextClassLoaderStrategy);
43          } catch (IllegalArgumentException e) {
44              log.warn("Cannot parse '{}' to a valid context class loader strategy, will use default '{}'", contextClassLoaderStrategy, ContextClassLoaderStrategy.USE_HOST);
45              return ContextClassLoaderStrategy.USE_HOST;
46          }
47      }
48  }