View Javadoc

1   package com.atlassian.plugin.osgi.spring;
2   
3   import com.atlassian.plugin.osgi.spring.external.ApplicationContextPreProcessor;
4   import org.osgi.framework.Bundle;
5   import org.osgi.framework.BundleContext;
6   import org.springframework.osgi.context.DelegatedExecutionOsgiBundleApplicationContext;
7   import org.springframework.osgi.extender.OsgiApplicationContextCreator;
8   import org.springframework.osgi.extender.support.ApplicationContextConfiguration;
9   import org.springframework.osgi.extender.support.DefaultOsgiApplicationContextCreator;
10  import org.springframework.osgi.extender.support.scanning.ConfigurationScanner;
11  import org.springframework.osgi.extender.support.scanning.DefaultConfigurationScanner;
12  import org.springframework.osgi.util.OsgiStringUtils;
13  import org.springframework.osgi.atlassian.NonValidatingOsgiBundleXmlApplicationContext;
14  import org.springframework.util.ObjectUtils;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import java.util.List;
19  
20  /**
21   * Application context creator that will use a special application context that disables XML Schema validation
22   *
23   * @since 2.5.0
24   */
25  public class NonValidatingOsgiApplicationContextCreator implements OsgiApplicationContextCreator
26  {
27      private static final Logger log = LoggerFactory.getLogger(DefaultOsgiApplicationContextCreator.class);
28      private final List<ApplicationContextPreProcessor> applicationContextPreProcessors;
29  
30  	private ConfigurationScanner configurationScanner = new DefaultConfigurationScanner();
31  
32      public NonValidatingOsgiApplicationContextCreator(List<ApplicationContextPreProcessor> applicationContextPreProcessors)
33      {
34          this.applicationContextPreProcessors = applicationContextPreProcessors;
35      }
36  
37      /**
38       * Creates an application context that disables validation.  Most of this code is copy/pasted from
39       * {@link DefaultOsgiApplicationContextCreator}
40       * @param bundleContext The bundle context for the application context
41       * @return The new application context
42       * @throws Exception If anything goes wrong
43       */
44      public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext) throws Exception
45      {
46          Bundle bundle = bundleContext.getBundle();
47  		ApplicationContextConfiguration config = new ApplicationContextConfiguration(bundle, configurationScanner);
48  		if (log.isTraceEnabled())
49  			log.trace("Created configuration " + config + " for bundle "
50  					+ OsgiStringUtils.nullSafeNameAndSymName(bundle));
51  
52  		// it's not a spring bundle, ignore it
53          if (!isSpringPoweredBundle(bundle, config))
54          {
55              return null;
56          }
57  
58          log.info("Discovered configurations " + ObjectUtils.nullSafeToString(config.getConfigurationLocations())
59  				+ " in bundle [" + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]");
60  
61          // This is the one new line, which uses our application context and not the other one
62  		DelegatedExecutionOsgiBundleApplicationContext sdoac = new NonValidatingOsgiBundleXmlApplicationContext(
63  			config.getConfigurationLocations());
64  
65  		sdoac.setBundleContext(bundleContext);
66  		sdoac.setPublishContextAsService(config.isPublishContextAsService());
67  
68          for (ApplicationContextPreProcessor processor : applicationContextPreProcessors)
69          {
70              processor.process(bundle, sdoac);
71          }
72  
73  		return sdoac;
74      }
75  
76      boolean isSpringPoweredBundle(Bundle bundle, ApplicationContextConfiguration config)
77      {
78          // Check for the normal configuration xml files
79          if (config.isSpringPoweredBundle())
80          {
81              return true;
82          }
83  
84          // Check any preprocessors, as they may solely use annotations
85          else
86          {
87              for (ApplicationContextPreProcessor processor : applicationContextPreProcessors)
88              {
89                  if (processor.isSpringPoweredBundle(bundle))
90                  {
91                      return true;
92                  }
93              }
94          }
95  
96          // Return false as the default
97          return false;
98      }
99  }