View Javadoc
1   package com.atlassian.plugin.osgi.spring;
2   
3   import com.atlassian.plugin.osgi.spring.external.ApplicationContextPreProcessor;
4   import org.eclipse.gemini.blueprint.context.DelegatedExecutionOsgiBundleApplicationContext;
5   import org.eclipse.gemini.blueprint.extender.OsgiApplicationContextCreator;
6   import org.eclipse.gemini.blueprint.extender.support.ApplicationContextConfiguration;
7   import org.eclipse.gemini.blueprint.extender.support.DefaultOsgiApplicationContextCreator;
8   import org.eclipse.gemini.blueprint.extender.support.scanning.ConfigurationScanner;
9   import org.eclipse.gemini.blueprint.extender.support.scanning.DefaultConfigurationScanner;
10  import org.eclipse.gemini.blueprint.util.OsgiStringUtils;
11  import org.osgi.framework.Bundle;
12  import org.osgi.framework.BundleContext;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  import org.springframework.osgi.atlassian.NonValidatingOsgiBundleXmlApplicationContext;
16  import org.springframework.util.ObjectUtils;
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          this.applicationContextPreProcessors = applicationContextPreProcessors;
34      }
35  
36      /**
37       * Creates an application context that disables validation. Most of this code is copy/pasted from
38       * {@code DefaultOsgiApplicationContextCreator}
39       *
40       * @param bundleContext The bundle context for the application context
41       * @return The new application context
42       */
43      public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(BundleContext bundleContext) {
44          Bundle bundle = bundleContext.getBundle();
45          ApplicationContextConfiguration config = new ApplicationContextConfiguration(bundle, configurationScanner);
46          if (log.isTraceEnabled())
47              log.trace("Created configuration " + config + " for bundle "
48                      + OsgiStringUtils.nullSafeNameAndSymName(bundle));
49  
50          // it's not a spring bundle, ignore it
51          if (!isSpringPoweredBundle(bundle, config)) {
52              return null;
53          }
54  
55          log.info("Discovered configurations " + ObjectUtils.nullSafeToString(config.getConfigurationLocations())
56                  + " in bundle [" + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "]");
57  
58          // This is the one new line, which uses our application context and not the other one
59          DelegatedExecutionOsgiBundleApplicationContext sdoac = new NonValidatingOsgiBundleXmlApplicationContext(
60                  config.getConfigurationLocations());
61  
62          sdoac.setBundleContext(bundleContext);
63          sdoac.setPublishContextAsService(config.isPublishContextAsService());
64  
65          for (ApplicationContextPreProcessor processor : applicationContextPreProcessors) {
66              processor.process(bundle, sdoac);
67          }
68  
69          return sdoac;
70      }
71  
72      boolean isSpringPoweredBundle(Bundle bundle, ApplicationContextConfiguration config) {
73          // Check for the normal configuration xml files
74          if (config.isSpringPoweredBundle()) {
75              return true;
76          }
77  
78          // Check any preprocessors, as they may solely use annotations
79          else {
80              for (ApplicationContextPreProcessor processor : applicationContextPreProcessors) {
81                  if (processor.isSpringPoweredBundle(bundle)) {
82                      return true;
83                  }
84              }
85          }
86  
87          // Return false as the default
88          return false;
89      }
90  }