package com.dianping.tavern.plugin.struts;

import com.dianping.tavern.Tavern;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.spring.SpringObjectFactory;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.spring.ClassReloadingXMLWebApplicationContext;
import org.apache.struts2.spring.StrutsSpringObjectFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yihua.huang@dianping.com
 */
public class TavernSpringObjectFactory extends StrutsSpringObjectFactory {

    private static final Logger LOG = LoggerFactory.getLogger(StrutsSpringObjectFactory.class);

    //@Inject
    //public StrutsSpringObjectFactory(
    //        @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
    //        @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
    //        @Inject ServletContext servletContext) {
    //    this(autoWire, "false", useClassCacheStr, servletContext);
    //}

    @Inject
    public TavernSpringObjectFactory(
            @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
            @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT,required=false) String alwaysAutoWire,
            @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
            @Inject ServletContext servletContext,
            @Inject(StrutsConstants.STRUTS_DEVMODE) String devMode,
            @Inject Container container) {

        super(autoWire,alwaysAutoWire,useClassCacheStr,servletContext,devMode,container);
    }

    @Override
	public Object buildBean(String beanName, Map<String, Object> extraContext, boolean injectInternal) throws Exception {
		Object o;
		ApplicationContext applicationContext = Tavern.getCurrentApplication(beanName).getApplicationContext();
		if (applicationContext.containsBean(beanName)) {
			o = applicationContext.getBean(beanName);
		} else {
			Class beanClazz = getClassInstance(beanName);
			o = buildBean(beanClazz, extraContext);
		}
		if (injectInternal) {
			injectInternalBeans(o);
		}
		return o;
	}

	public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFactory) {
		autoWiringFactory = findAutoWiringBeanFactory(Tavern.getCurrentApplication(bean.getClass())
				.getApplicationContext());
		if (autoWiringFactory != null) {
			autoWiringFactory.autowireBeanProperties(bean, autowireStrategy, false);
		}
		if (bean instanceof ApplicationContextAware) {
			((ApplicationContextAware) bean).setApplicationContext(appContext);
		}

		injectInternalBeans(bean);

		return bean;
	}

}

@@ -1332,535 +1332,8 @@
  %7B%0A%0A
-    private static final Logger LOG = LoggerFactory.getLogger(StrutsSpringObjectFactory.class);%0A%0A    //@Inject%0A    //public StrutsSpringObjectFactory(%0A    //        @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,%0A    //        @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,%0A    //        @Inject ServletContext servletContext) %7B%0A    //    this(autoWire, %22false%22, useClassCacheStr, servletContext);%0A    //%7D%0A%0A
     

clean code