/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.builder;
import java.util.Properties;
import org.strecks.context.ActionContextFactory;
import org.strecks.controller.ActionCreator;
import org.strecks.controller.ControllerProcessorDelegate;
import org.strecks.form.handler.FormPopulateSource;
import org.strecks.form.handler.FormValidationHandler;
import org.strecks.form.handler.FormWrapper;
import org.strecks.preprocess.RequestPreprocessor;
import org.strecks.util.ReflectHelper;
/**
* The default <code>Builder</code> implementation, which loads configuration information from the
* <code>strecks.properties</code> file located on the class path
* @author Phil Zoio
*/
public class PropertiesBuilder implements Builder
{
private ActionCreator actionCreator;
private ControllerProcessorDelegate delegate;
private ActionContextFactory actionContextFactory;
private FormWrapper formHandler;
private FormValidationHandler formValidationHandler;
private FormPopulateSource formPopulateSource;
private RequestPreprocessor requestPreprocessor;
private ImplementationConfig implementationConfig;
private InterceptorBuilder interceptorBuilder;
public void build(String prefix)
{
String name = getFileName(prefix);
Properties properties = ConfigUtils.loadProperties(name);
this.implementationConfig = getImplementationConfig(properties);
setActionCreator(ReflectHelper.createInstance(implementationConfig.getActionCreatorClassName(), ActionCreator.class));
setDelegate(ReflectHelper.createInstance(implementationConfig.getDelegateClassName(), ControllerProcessorDelegate.class));
setFormHandler(ReflectHelper.createInstance(implementationConfig.getFormHandlerClassName(), FormWrapper.class));
setActionContextFactory(ReflectHelper.createInstance(implementationConfig.getContextFactoryClassName(),
ActionContextFactory.class));
setFormHandler(ReflectHelper.createInstance(implementationConfig.getFormHandlerClassName(),
FormWrapper.class));
setFormValidationHandler(ReflectHelper.createInstance(implementationConfig.getFormValidationHandlerClassName(),
FormValidationHandler.class));
setFormPopulateSource(ReflectHelper.createInstance(implementationConfig.getFormPopulateSourceClassName(),
FormPopulateSource.class));
setRequestPreprocessor(ReflectHelper.createInstance(implementationConfig.getRequestPreprocessorClassName(),
RequestPreprocessor.class));
this.interceptorBuilder = new InterceptorBuilderImpl();
interceptorBuilder.build(prefix);
this.delegate.setBeforeInterceptors(interceptorBuilder.getBeforeInterceptors());
this.delegate.setAfterInterceptors(interceptorBuilder.getAfterInterceptors());
}
protected String getFileName(String prefix)
{
return ConfigUtils.getPropertiesFileName(prefix);
}
ImplementationConfig getImplementationConfig(Properties properties)
{
ImplementationConfig config = new ImplementationConfig();
String builderImpl = properties.getProperty("builder.impl");
if (builderImpl != null)
{
config.setBuilderClassName(builderImpl);
}
String actionCreatorImpl = properties.getProperty("action.creator.impl");
if (actionCreatorImpl != null)
{
config.setActionCreatorClassName(actionCreatorImpl);
}
String delegateImpl = properties.getProperty("delegate.impl");
if (delegateImpl != null)
{
config.setDelegateClassName(delegateImpl);
}
String contextFactoryImpl = properties.getProperty("context.factory.impl");
if (contextFactoryImpl != null)
{
config.setContextFactoryClassName(contextFactoryImpl);
}
String formHandlerImpl = properties.getProperty("form.wrapper.impl");
if (formHandlerImpl != null)
{
config.setFormHandlerClassName(formHandlerImpl);
}
String formValidationHandlerImpl = properties.getProperty("form.validation.handler.impl");
if (formValidationHandlerImpl != null)
{
config.setFormValidationHandlerClassName(formValidationHandlerImpl);
}
String formPopulationSourceImpl = properties.getProperty("form.populate.source.impl");
if (formPopulationSourceImpl != null)
{
config.setFormPopulateSourceClassName(formPopulationSourceImpl);
}
String requestPreprocessorImpl = properties.getProperty("request.preprocessor.impl");
if (requestPreprocessorImpl != null)
{
config.setRequestPreprocessorClassName(requestPreprocessorImpl);
}
return config;
}
public InterceptorConfig getInterceptorConfig(Properties properties)
{
InterceptorConfig config = new InterceptorConfig();
int index = 1;
String beforeInterceptor;
while ((beforeInterceptor = properties.getProperty("before.interceptor." + index)) != null)
{
config.addBeforeInterceptor(beforeInterceptor);
index++;
}
index = 1;
String afterInterceptor;
while ((afterInterceptor = properties.getProperty("after.interceptor." + index)) != null)
{
config.addAfterInterceptor(afterInterceptor);
index++;
}
return config;
}
public ImplementationConfig getConfig()
{
return implementationConfig;
}
public ActionCreator getActionCreator()
{
return actionCreator;
}
public ControllerProcessorDelegate getControllerDelegate()
{
return delegate;
}
public FormWrapper getFormHandler()
{
return formHandler;
}
public ActionContextFactory getActionContextFactory()
{
return actionContextFactory;
}
public FormPopulateSource getFormPopulateSource()
{
return formPopulateSource;
}
public FormValidationHandler getFormValidationHandler()
{
return formValidationHandler;
}
public RequestPreprocessor getRequestPreprocessor()
{
return requestPreprocessor;
}
ControllerProcessorDelegate getDelegate()
{
return delegate;
}
void setDelegate(ControllerProcessorDelegate delegate)
{
this.delegate = delegate;
}
void setActionContextFactory(ActionContextFactory actionContextFactory)
{
this.actionContextFactory = actionContextFactory;
}
void setActionCreator(ActionCreator actionCreator)
{
this.actionCreator = actionCreator;
}
void setFormHandler(FormWrapper formHandler)
{
this.formHandler = formHandler;
}
void setFormPopulateSource(FormPopulateSource formPopulateSource)
{
this.formPopulateSource = formPopulateSource;
}
void setFormValidationHandler(FormValidationHandler formValidationHandler)
{
this.formValidationHandler = formValidationHandler;
}
void setRequestPreprocessor(RequestPreprocessor requestPreprocessor)
{
this.requestPreprocessor = requestPreprocessor;
}
}