KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > spring > EtlExecutorBean


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.concurrent.Callable JavaDoc;
33
34 /**
35  * Implementation of {@link EtlExecutor} for Spring IoC container.
36  * <p>This class exposes a set of configurable properties and provides
37  * a {@link Callable} invocation interface to avoid dependency on Scriptella
38  * in application code.
39  *
40  * @author Fyodor Kupolov
41  * @version 1.0
42  */

43 public class EtlExecutorBean extends EtlExecutor implements InitializingBean, BeanFactoryAware, Callable JavaDoc<ExecutionStatistics> {
44
45     private static final ThreadLocal JavaDoc<BeanFactory> LOCAL_BEAN_FACTORY = new ThreadLocal JavaDoc<BeanFactory>();
46     private BeanFactory beanFactory;
47     private ProgressIndicator progressIndicator;
48     private boolean autostart;
49     private Map JavaDoc<String JavaDoc, ?> properties;
50     private URL JavaDoc configLocation;
51
52
53     /**
54      * Creates scripts executor.
55      */

56     public EtlExecutorBean() {
57     }
58
59     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
60         this.beanFactory = beanFactory;
61     }
62
63     /**
64      * Sets autostart property.
65      *
66      * @param autostart true if executor must be automatically runned after initialization.
67      * Default value is <code>false</code>.
68      */

69     public void setAutostart(boolean autostart) {
70         this.autostart = autostart;
71     }
72
73     /**
74      * Sets progress indicator to use.
75      * <p>By default no progress shown.
76      *
77      * @param progressIndicator progress indicator to use.
78      */

79     public void setProgressIndicator(ProgressIndicator progressIndicator) {
80         this.progressIndicator = progressIndicator;
81     }
82
83     /**
84      * Sets configuration location.
85      *
86      * @param resource configuration resource.
87      */

88     public void setConfigLocation(Resource resource) throws IOException JavaDoc {
89         configLocation = resource.getURL();
90     }
91
92     public Map JavaDoc<String JavaDoc, ?> getProperties() {
93         return properties;
94     }
95
96     public void setProperties(Map JavaDoc<String JavaDoc, ?> properties) {
97         this.properties = properties;
98     }
99
100     public void afterPropertiesSet() throws Exception JavaDoc {
101
102         if (getConfiguration() == null) {
103             if (configLocation == null) {
104                 throw new IllegalStateException JavaDoc("configLocation must be specified");
105             } else { //Initialize configuration
106
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     /**
118      * Simply calls {@link #execute()}.
119      */

120     public ExecutionStatistics call() throws EtlExecutorException {
121         return execute();
122     }
123
124
125     @Override JavaDoc
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 JavaDoc
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     /**
146      * @return bean factory associated with the current thread.
147      */

148     static BeanFactory getContextBeanFactory() {
149         BeanFactory f = LOCAL_BEAN_FACTORY.get();
150         if (f == null) {
151             throw new IllegalStateException JavaDoc("No beanfactory associated with the current thread");
152         }
153         return f;
154     }
155 }
156
Popular Tags