/**
   * Load the application context using a single classloader
   *
   * @param ac The spring application context
   * @param config a list of configurations represented as List of resources
   * @param loader the classloader to use
   */
  public void loadComponent(
      ConfigurableApplicationContext ac, List<Resource> config, ClassLoader loader) {
    ClassLoader current = Thread.currentThread().getContextClassLoader();

    Thread.currentThread().setContextClassLoader(loader);

    try {

      // make a reader
      XmlBeanDefinitionReader reader =
          new XmlBeanDefinitionReader((BeanDefinitionRegistry) ac.getBeanFactory());

      // In Spring 2, classes aren't loaded during bean parsing unless
      // this
      // classloader property is set.
      reader.setBeanClassLoader(loader);

      reader.loadBeanDefinitions(config.toArray(new Resource[0]));
    } catch (Throwable t) {
      log.warn("loadComponentPackage: exception loading: " + config + " : " + t, t);
    } finally {
      // restore the context loader
      Thread.currentThread().setContextClassLoader(current);
    }
  }
예제 #2
0
  /**
   * Load one component package into the AC
   *
   * @param packageRoot The file path to the component package
   * @param ac The ApplicationContext to load into
   */
  protected void loadComponentPackage(File dir, ConfigurableApplicationContext ac) {
    // setup the classloader onto the thread
    ClassLoader current = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = newPackageClassLoader(dir);

    M_log.info("loadComponentPackage: " + dir);

    Thread.currentThread().setContextClassLoader(loader);

    File xml = null;

    try {
      // load this xml file
      File webinf = new File(dir, "WEB-INF");
      xml = new File(webinf, "components.xml");

      // make a reader
      XmlBeanDefinitionReader reader =
          new XmlBeanDefinitionReader((BeanDefinitionRegistry) ac.getBeanFactory());

      // In Spring 2, classes aren't loaded during bean parsing unless this
      // classloader property is set.
      reader.setBeanClassLoader(loader);

      List<Resource> beanDefList = new ArrayList<Resource>();
      beanDefList.add(new FileSystemResource(xml.getCanonicalPath()));

      // Load the demo components, if necessary
      File demoXml = new File(webinf, "components-demo.xml");
      if ("true".equalsIgnoreCase(System.getProperty("sakai.demo"))) {
        if (M_log.isDebugEnabled()) M_log.debug("Attempting to load demo components");
        if (demoXml.exists()) {
          if (M_log.isInfoEnabled()) M_log.info("Loading demo components from " + dir);
          beanDefList.add(new FileSystemResource(demoXml.getCanonicalPath()));
        }
      } else {
        if (demoXml.exists()) {
          // Only log that we're skipping the demo components if they exist
          if (M_log.isInfoEnabled()) M_log.info("Skipping demo components from " + dir);
        }
      }

      reader.loadBeanDefinitions(beanDefList.toArray(new Resource[0]));
    } catch (Exception e) {
      M_log.warn("loadComponentPackage: exception loading: " + xml + " : " + e, e);
    } finally {
      // restore the context loader
      Thread.currentThread().setContextClassLoader(current);
    }
  }