1 16 package scriptella.driver.spring; 17 18 import org.springframework.beans.BeansException; 19 import org.springframework.beans.factory.BeanFactory; 20 import org.springframework.beans.factory.BeanFactoryAware; 21 import org.springframework.beans.factory.InitializingBean; 22 import org.springframework.core.io.Resource; 23 import scriptella.configuration.ConfigurationFactory; 24 import scriptella.execution.EtlExecutor; 25 import scriptella.execution.EtlExecutorException; 26 import scriptella.execution.ExecutionStatistics; 27 import scriptella.interactive.ProgressIndicator; 28 29 import java.io.IOException ; 30 import java.net.URL ; 31 import java.util.Map ; 32 import java.util.concurrent.Callable ; 33 34 43 public class EtlExecutorBean extends EtlExecutor implements InitializingBean, BeanFactoryAware, Callable <ExecutionStatistics> { 44 45 private static final ThreadLocal <BeanFactory> LOCAL_BEAN_FACTORY = new ThreadLocal <BeanFactory>(); 46 private BeanFactory beanFactory; 47 private ProgressIndicator progressIndicator; 48 private boolean autostart; 49 private Map <String , ?> properties; 50 private URL configLocation; 51 52 53 56 public EtlExecutorBean() { 57 } 58 59 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 60 this.beanFactory = beanFactory; 61 } 62 63 69 public void setAutostart(boolean autostart) { 70 this.autostart = autostart; 71 } 72 73 79 public void setProgressIndicator(ProgressIndicator progressIndicator) { 80 this.progressIndicator = progressIndicator; 81 } 82 83 88 public void setConfigLocation(Resource resource) throws IOException { 89 configLocation = resource.getURL(); 90 } 91 92 public Map <String , ?> getProperties() { 93 return properties; 94 } 95 96 public void setProperties(Map <String , ?> properties) { 97 this.properties = properties; 98 } 99 100 public void afterPropertiesSet() throws Exception { 101 102 if (getConfiguration() == null) { 103 if (configLocation == null) { 104 throw new IllegalStateException ("configLocation must be specified"); 105 } else { ConfigurationFactory cf = new ConfigurationFactory(); 107 cf.setResourceURL(configLocation); 108 cf.setExternalProperties(properties); 109 setConfiguration(cf.createConfiguration()); 110 } 111 } 112 if (autostart) { 113 execute(); 114 } 115 } 116 117 120 public ExecutionStatistics call() throws EtlExecutorException { 121 return execute(); 122 } 123 124 125 @Override 126 public ExecutionStatistics execute() throws EtlExecutorException { 127 if (progressIndicator != null) { 128 return execute(progressIndicator); 129 } else { 130 return super.execute(); 131 } 132 } 133 134 @Override 135 public ExecutionStatistics execute(final ProgressIndicator indicator) throws EtlExecutorException { 136 LOCAL_BEAN_FACTORY.set(beanFactory); 137 try { 138 return super.execute(indicator); 139 } finally { 140 LOCAL_BEAN_FACTORY.remove(); 141 } 142 } 143 144 145 148 static BeanFactory getContextBeanFactory() { 149 BeanFactory f = LOCAL_BEAN_FACTORY.get(); 150 if (f == null) { 151 throw new IllegalStateException ("No beanfactory associated with the current thread"); 152 } 153 return f; 154 } 155 } 156 | Popular Tags |