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  {
22      private final static Logger log = LoggerFactory.getLogger(PluginContextClassLoaderStrategyBeanDefinitionDecorator.class);
23  
24      /**
25       * Called when the Spring parser encounters an "available" attribute.
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      {
33          final String contextClassLoaderStrategy = ((Attr) source).getValue();
34          if (contextClassLoaderStrategy != null)
35          {
36              new PluginBeanDefinitionRegistry(ctx.getRegistry()).addContextClassLoaderStrategy(holder.getBeanName(), getContextClassLoaderStrategy(contextClassLoaderStrategy));
37          }
38          return holder;
39  
40      }
41  
42      private ContextClassLoaderStrategy getContextClassLoaderStrategy(String contextClassLoaderStrategy)
43      {
44          try
45          {
46              return ContextClassLoaderStrategy.valueOf(contextClassLoaderStrategy);
47          }
48          catch (IllegalArgumentException e)
49          {
50              log.warn("Cannot parse '{}' to a valid context class loader strategy, will use default '{}'", contextClassLoaderStrategy, ContextClassLoaderStrategy.USE_HOST);
51              return ContextClassLoaderStrategy.USE_HOST;
52          }
53      }
54  }