001    package com.hammurapi.config.runtime;
002    
003    import java.util.Iterator;
004    
005    import org.eclipse.emf.ecore.EObject;
006    
007    import com.hammurapi.config.bootstrap.Destroyable;
008    import com.hammurapi.util.Context;
009    
010    /**
011     * Default context factory.
012     * @author Pavel Vlasov
013     *
014     */
015    public class DefaultContextFactory implements ContextFactory {
016            
017    
018            public ConfigurationContext<?> createContext(final EObject definition, final Component<?> component, final FactoryConfig config, final DestroyableSink destroyableSink) {
019                    final Context context = config.getContext();
020                    final Object[] path = config.getObjectPath();
021                    final ClassLoader classLoader = config.getClassLoader();
022                    
023                    return new ConfigurationContext<EObject>() {
024    
025                            @SuppressWarnings("unchecked")
026                            public <T> T findParent(Class<T> type) {
027                                    for (int i=path==null ? -1 : path.length-2; i>=0; --i) {
028                                            if (type.isInstance(path[i])) {
029                                                    return (T) path[i];
030                                            }
031                                    }
032                                    return null;
033                            }
034    
035                            public EObject getDefinition() {
036                                    return definition;
037                            }
038    
039                            public Object[] getPath() {
040                                    return path;
041                            }
042    
043                            public <T> T lookup(Class<T> type) {
044                                    return context==null ? null : context.lookup(type);
045                            }
046    
047                            public <T> Iterator<T> lookupAll(Class<T> type) {
048                                    return context==null ? null : context.lookupAll(type);
049                            }
050    
051                            public ClassLoader getClassLoader() {
052                                    return classLoader;
053                            }
054    
055                            public Object lookup(String name) {
056                                    return context==null ? null : context.lookup(name);
057                            }
058    
059                            public void addDestroyable(Destroyable child) {
060                                    destroyableSink.addDestroyable(child);                          
061                            }
062    
063                            public FactoryConfig getFactoryConfig() {
064                                    return config;
065                            }
066                            
067                    };
068            }
069    
070    }