001 package com.hammurapi.config.runtime;
002
003 import java.util.Map;
004
005 import org.eclipse.emf.ecore.resource.Resource;
006
007 import com.hammurapi.config.Factory;
008 import com.hammurapi.config.bootstrap.BootstrapFactory;
009 import com.hammurapi.config.bootstrap.ConfigurationException;
010 import com.hammurapi.config.bootstrap.FactoryClosure;
011 import com.hammurapi.config.bootstrap.TokenExpander.TokenSource;
012 import com.hammurapi.util.CompositeContext;
013 import com.hammurapi.util.DefaultContext;
014 import com.hammurapi.util.FacadeContext;
015 import com.hammurapi.util.SimpleMutableContext;
016
017 /**
018 * Implementation of BootstrapFactory.
019 * @author Pavel Vlasov
020 *
021 * @param <T>
022 */
023 public class BootstrapFactoryImpl<T> implements BootstrapFactory<T> {
024
025 @SuppressWarnings("unchecked")
026 public FactoryClosure<T> create(
027 String uri,
028 TokenSource tokens,
029 String[] profilePath,
030 Map<Class<?>, Iterable<?>> services,
031 Map<String, ?> bindings) throws ConfigurationException {
032 Resource resource = ResourceLoader.load(uri, this.getClass().getClassLoader());
033 Factory root = (Factory) resource.getContents().get(0);
034 FactoryConfig fConfig = new FactoryConfig();
035 fConfig.setProfilePath(profilePath);
036 fConfig.setTokenSource(tokens);
037 if (services!=null) {
038 FacadeContext fc = new FacadeContext(services, bindings);
039 fConfig.setContext(new CompositeContext(fc, new SimpleMutableContext(), new DefaultContext(this.getClass().getClassLoader(), null)));
040 } else {
041 fConfig.setContext(new CompositeContext(new SimpleMutableContext(), new DefaultContext(this.getClass().getClassLoader(), null)));
042 }
043 return (FactoryClosure<T>) root.create(fConfig);
044 }
045
046 }