net.sf.andromedaioc.context.builder.ContextBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of andromeda-ioc Show documentation
Show all versions of andromeda-ioc Show documentation
Inversion of Control Framework for Android
The newest version!
package net.sf.andromedaioc.context.builder;
import android.content.Context;
import net.sf.andromedaioc.bean.wrapper.BeanWrapper;
import net.sf.andromedaioc.bean.BeanWrapperCreatorFactory;
import net.sf.andromedaioc.context.AndromedaContext;
import net.sf.andromedaioc.context.AndromedaContextImpl;
import net.sf.andromedaioc.model.beans.BeanModel;
import net.sf.andromedaioc.model.beans.ContextModel;
import net.sf.andromedaioc.model.beans.ReferenceKey;
import net.sf.andromedaioc.model.builder.BasicModelBuilder;
import net.sf.andromedaioc.resource.ResourceProvider;
import net.sf.andromedaioc.resource.ResourceProviderImpl;
import net.sf.andromedaioc.util.BeanUtils;
import java.util.*;
/**
* Build andromeda context
*
* @author Alexey Mitrov
*/
public class ContextBuilder {
private final Context context;
private ResourceProvider resourceProvider;
private String xmlContexts;
private String scanPackages;
private String propertiesFiles;
/**
* Create andromeda context builder
* @param context android context
*/
public ContextBuilder(Context context) {
this.context = context;
}
/**
* Build andromeda context
* @return andromeda context
* @throws Exception
*/
public AndromedaContext build() throws Exception {
resourceProvider = new ResourceProviderImpl(context, Collections.emptyList(), BeanUtils.splitAndTrimCSV(propertiesFiles));
ContextModel contextModel = getModel();
Map beans = createBeans(contextModel);
return new AndromedaContextImpl(beans, resourceProvider);
}
/**
* Use this method to set xml context files
* @param xmlResources coma separated list of xml files
* @return context builder
*/
public ContextBuilder withXmlResource(String xmlResources) {
this.xmlContexts = xmlResources;
return this;
}
/**
* Use this method to set scan packages with annotated beans you want to be added to context
* @param scanPackages coma separated packages. All sub packages of provided packages will be scanned too
* @return context builder
*/
public ContextBuilder withScanPackages(String scanPackages) {
this.scanPackages = scanPackages;
return this;
}
/**
* Use this method to set properties files
* @param propertiesFiles list of comma separated properties files
* @return
*/
public ContextBuilder withPropertiesFiles(String propertiesFiles) {
this.propertiesFiles = propertiesFiles;
return this;
}
private ContextModel getModel() throws Exception {
return (new BasicModelBuilder())
.withResourceProviderFactory(resourceProvider)
.withXmlContext(xmlContexts)
.withScanPackages(scanPackages)
.build();
}
private Map createBeans(ContextModel contextModel) throws Exception {
Map beans = new HashMap();
for(Map.Entry entry : contextModel.getBeans().entrySet()) {
BeanModel beanModel = entry.getValue();
BeanWrapper bean = BeanWrapperCreatorFactory.getBeanCreator(beanModel, beans, resourceProvider).createWrapper();
beans.put(entry.getKey(), bean);
}
return beans;
}
}