Package jfun.yan.xml.nuts.spring

Source Code of jfun.yan.xml.nuts.spring.SpringNuts

package jfun.yan.xml.nuts.spring;

import java.io.IOException;

import jfun.yan.Container;
import jfun.yan.containers.ManualContainer;
import jfun.yan.lifecycle.DefaultLifecycleManager;
import jfun.yan.lifecycle.Procedure;
import jfun.yan.spring.Container2ApplicationContext;
import jfun.yan.xml.NutsProcessor;
import org.springframework.beans.factory.DisposableBean;

/**
* The utility class to decrate a Nuts Interpreter object with tags that are
* aware of Spring beans.
* <p>
* @author Ben Yu
* Nov 17, 2005 2:57:21 PM
*/
public class SpringNuts {

  /**
   * Make an processor spring-aware by registering spring-aware
   * nuts tags such as ctor, bean, method etc.
   * @param module_name the name of the current module.
   * @param processor the processor object.
   * @param loader the class loader used to load the spring-aware
   * nuts classes.
   */
  public static void setSpringAware(String module_name,
      NutsProcessor processor,
      ClassLoader loader){
    setSpringAware(module_name, processor, loader,
        "jfun/yan/xml/nuts/spring/spring.xml");
  }
  /**
   * Make an processor spring-aware by registering spring-aware
   * nuts tags such as ctor, bean, method etc.
   * @param module_name the name of the current module.
   * @param processor the processor object.
   * @param loader the class loader used to load the spring-aware
   * @param nuts_resource the resource name of the config file
   * that configures the nut classes.
   * nuts classes.
   */
  public static void setSpringAware(String module_name,
      NutsProcessor processor,
      ClassLoader loader, String nuts_resource){
    setSpringAware(module_name, processor, loader, nuts_resource,
        "jfun/yan/xml/nuts/spring/appcontext.xml");
  }

  /**
   * Make an processor spring-aware by registering spring-aware
   * nuts tags such as ctor, bean, method etc.
   * @param module_name the name of the current module.
   * @param processor the processor object.
   * @param loader the class loader used to load the spring-aware
   * @param nuts_resource the resource name of the config file
   * that configures the nut classes.
   * @param appcontext_resource the resource name of the config file
   * that configures the ApplicationContext.
   */
  public static void setSpringAware(String module_name,
      NutsProcessor processor,
      ClassLoader loader, String nuts_resource,
      String appcontext_resource){
    final NutsProcessor spring =
      processor.createMetaProcessor(loader);
    final Container yan = processor.getContainer();
    /*
    final ReloadableResourceBundleMessageSource msgsrc = new ReloadableResourceBundleMessageSource();
    msgsrc.setBasename("org/springframework/context/support/messages");
    final ApplicationEventPublisher aep = new ApplicationEventPublisher(){
     
    }*/
    final Container base = new ManualContainer();
    base.registerValue("modulename", module_name);
    base.registerValue("timestamp", new Long(System.currentTimeMillis()));
    base.registerValue("classloader", loader);
    base.registerValue("container", yan);
    final NutsProcessor cinterpreter = new NutsProcessor();
    //cinterpreter.setParameterWiring(Modes.params_bytype);
    try{
      cinterpreter.processResource(appcontext_resource);
    }
    catch(IOException e){
      throw new IllegalStateException("cannot startup spring integration.");
    }
   
    final Container context = cinterpreter.getContainer().inherit(base);
    final Container2ApplicationContext ctxt = (Container2ApplicationContext)
    context.getInstance("appcontext");
   
    final Container nuts = spring.getContainer();
    final DefaultLifecycleManager manager = processor.getLifecycleManager();
   
    //final BeanPostProcessorQueue bppq = new BeanPostProcessorQueue();
    //nuts.registerValue(newBeanLifecycle(manager));
    //nuts.registerValue(ctxt);
    //nuts.registerValue(yan);
    //nuts.registerValue(bppq);
    nuts.registerValue(new SpringService(ctxt, manager));
    final String resource = nuts_resource;
    try{
      spring.processResource(resource);
    }
    catch(IOException e){
      throw new IllegalStateException("cannot load spring nuts: "
          + resource);
    }
    processor.loadNutsFromContainer(nuts);
  }

  /**
   * Make an processor spring-aware by registering spring-aware
   * nuts tags such as ctor, bean, method etc.
   * <p>
   * The class loader for component classes is used to load
   * the spring nut classes.
   * </p>
   * @param module_name the current module name.
   * @param processor the processor object.
   */
  public static void setSpringAware(String module_name, NutsProcessor processor){
    setSpringAware(module_name, processor, processor.getClassLoader());
  }
  /**
   * Make an processor spring-aware by registering spring-aware
   * nuts tags such as ctor, bean, method etc.
   * <p>
   * The class loader for component classes is used to load
   * the spring nut classes.
   * </p>
   * @param module_name the current module name.
   * @param processor the processor object.
   * @param nuts_resource the resource name of the config file
   * that configures the nut classes.
   */
  public static void setSpringAware(String module_name, NutsProcessor processor,
      String nuts_resource){
    setSpringAware(module_name, processor, processor.getClassLoader(), nuts_resource);
  }
  private static DefaultLifecycleManager.DefaultLifecycle newBeanLifecycle(
      DefaultLifecycleManager man){
    return man.newLifecycle().disposer(new Procedure(){
      public void invoke(Object self, Object[] args) throws Throwable {
        ((DisposableBean)self).destroy();
      }
    });
  }
}
TOP

Related Classes of jfun.yan.xml.nuts.spring.SpringNuts

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.