001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.builder;
016:
017: import java.util.Properties;
018:
019: import org.strecks.context.ActionContextFactory;
020: import org.strecks.controller.ActionCreator;
021: import org.strecks.controller.ControllerProcessorDelegate;
022: import org.strecks.form.handler.FormPopulateSource;
023: import org.strecks.form.handler.FormValidationHandler;
024: import org.strecks.form.handler.FormWrapper;
025: import org.strecks.preprocess.RequestPreprocessor;
026: import org.strecks.util.ReflectHelper;
027:
028: /**
029: * The default <code>Builder</code> implementation, which loads configuration information from the
030: * <code>strecks.properties</code> file located on the class path
031: * @author Phil Zoio
032: */
033: public class PropertiesBuilder implements Builder {
034:
035: private ActionCreator actionCreator;
036:
037: private ControllerProcessorDelegate delegate;
038:
039: private ActionContextFactory actionContextFactory;
040:
041: private FormWrapper formHandler;
042:
043: private FormValidationHandler formValidationHandler;
044:
045: private FormPopulateSource formPopulateSource;
046:
047: private RequestPreprocessor requestPreprocessor;
048:
049: private ImplementationConfig implementationConfig;
050:
051: private InterceptorBuilder interceptorBuilder;
052:
053: public void build(String prefix) {
054: String name = getFileName(prefix);
055: Properties properties = ConfigUtils.loadProperties(name);
056: this .implementationConfig = getImplementationConfig(properties);
057: setActionCreator(ReflectHelper.createInstance(
058: implementationConfig.getActionCreatorClassName(),
059: ActionCreator.class));
060: setDelegate(ReflectHelper.createInstance(implementationConfig
061: .getDelegateClassName(),
062: ControllerProcessorDelegate.class));
063: setFormHandler(ReflectHelper.createInstance(
064: implementationConfig.getFormHandlerClassName(),
065: FormWrapper.class));
066: setActionContextFactory(ReflectHelper.createInstance(
067: implementationConfig.getContextFactoryClassName(),
068: ActionContextFactory.class));
069: setFormHandler(ReflectHelper.createInstance(
070: implementationConfig.getFormHandlerClassName(),
071: FormWrapper.class));
072: setFormValidationHandler(ReflectHelper.createInstance(
073: implementationConfig
074: .getFormValidationHandlerClassName(),
075: FormValidationHandler.class));
076: setFormPopulateSource(ReflectHelper.createInstance(
077: implementationConfig.getFormPopulateSourceClassName(),
078: FormPopulateSource.class));
079: setRequestPreprocessor(ReflectHelper.createInstance(
080: implementationConfig.getRequestPreprocessorClassName(),
081: RequestPreprocessor.class));
082:
083: this .interceptorBuilder = new InterceptorBuilderImpl();
084: interceptorBuilder.build(prefix);
085:
086: this .delegate.setBeforeInterceptors(interceptorBuilder
087: .getBeforeInterceptors());
088: this .delegate.setAfterInterceptors(interceptorBuilder
089: .getAfterInterceptors());
090: }
091:
092: protected String getFileName(String prefix) {
093: return ConfigUtils.getPropertiesFileName(prefix);
094: }
095:
096: ImplementationConfig getImplementationConfig(Properties properties) {
097:
098: ImplementationConfig config = new ImplementationConfig();
099:
100: String builderImpl = properties.getProperty("builder.impl");
101: if (builderImpl != null) {
102: config.setBuilderClassName(builderImpl);
103: }
104:
105: String actionCreatorImpl = properties
106: .getProperty("action.creator.impl");
107: if (actionCreatorImpl != null) {
108: config.setActionCreatorClassName(actionCreatorImpl);
109: }
110:
111: String delegateImpl = properties.getProperty("delegate.impl");
112: if (delegateImpl != null) {
113: config.setDelegateClassName(delegateImpl);
114: }
115:
116: String contextFactoryImpl = properties
117: .getProperty("context.factory.impl");
118: if (contextFactoryImpl != null) {
119: config.setContextFactoryClassName(contextFactoryImpl);
120: }
121:
122: String formHandlerImpl = properties
123: .getProperty("form.wrapper.impl");
124: if (formHandlerImpl != null) {
125: config.setFormHandlerClassName(formHandlerImpl);
126: }
127:
128: String formValidationHandlerImpl = properties
129: .getProperty("form.validation.handler.impl");
130: if (formValidationHandlerImpl != null) {
131: config
132: .setFormValidationHandlerClassName(formValidationHandlerImpl);
133: }
134:
135: String formPopulationSourceImpl = properties
136: .getProperty("form.populate.source.impl");
137: if (formPopulationSourceImpl != null) {
138: config
139: .setFormPopulateSourceClassName(formPopulationSourceImpl);
140: }
141:
142: String requestPreprocessorImpl = properties
143: .getProperty("request.preprocessor.impl");
144: if (requestPreprocessorImpl != null) {
145: config
146: .setRequestPreprocessorClassName(requestPreprocessorImpl);
147: }
148:
149: return config;
150:
151: }
152:
153: public InterceptorConfig getInterceptorConfig(Properties properties) {
154: InterceptorConfig config = new InterceptorConfig();
155: int index = 1;
156: String beforeInterceptor;
157:
158: while ((beforeInterceptor = properties
159: .getProperty("before.interceptor." + index)) != null) {
160: config.addBeforeInterceptor(beforeInterceptor);
161: index++;
162: }
163:
164: index = 1;
165: String afterInterceptor;
166:
167: while ((afterInterceptor = properties
168: .getProperty("after.interceptor." + index)) != null) {
169: config.addAfterInterceptor(afterInterceptor);
170: index++;
171: }
172:
173: return config;
174: }
175:
176: public ImplementationConfig getConfig() {
177: return implementationConfig;
178: }
179:
180: public ActionCreator getActionCreator() {
181: return actionCreator;
182: }
183:
184: public ControllerProcessorDelegate getControllerDelegate() {
185: return delegate;
186: }
187:
188: public FormWrapper getFormHandler() {
189: return formHandler;
190: }
191:
192: public ActionContextFactory getActionContextFactory() {
193: return actionContextFactory;
194: }
195:
196: public FormPopulateSource getFormPopulateSource() {
197: return formPopulateSource;
198: }
199:
200: public FormValidationHandler getFormValidationHandler() {
201: return formValidationHandler;
202: }
203:
204: public RequestPreprocessor getRequestPreprocessor() {
205: return requestPreprocessor;
206: }
207:
208: ControllerProcessorDelegate getDelegate() {
209: return delegate;
210: }
211:
212: void setDelegate(ControllerProcessorDelegate delegate) {
213: this .delegate = delegate;
214: }
215:
216: void setActionContextFactory(
217: ActionContextFactory actionContextFactory) {
218: this .actionContextFactory = actionContextFactory;
219: }
220:
221: void setActionCreator(ActionCreator actionCreator) {
222: this .actionCreator = actionCreator;
223: }
224:
225: void setFormHandler(FormWrapper formHandler) {
226: this .formHandler = formHandler;
227: }
228:
229: void setFormPopulateSource(FormPopulateSource formPopulateSource) {
230: this .formPopulateSource = formPopulateSource;
231: }
232:
233: void setFormValidationHandler(
234: FormValidationHandler formValidationHandler) {
235: this .formValidationHandler = formValidationHandler;
236: }
237:
238: void setRequestPreprocessor(RequestPreprocessor requestPreprocessor) {
239: this.requestPreprocessor = requestPreprocessor;
240: }
241: }
|