001    package com.hammurapi.util;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    /**
008     * Context delegating to other contexts "parts".
009     * @author Pavel Vlasov
010     *
011     */
012    public class CompositeContext implements MutableContext {
013            
014            private Context[] parts;
015            
016            public CompositeContext() {
017                    // 
018            }
019    
020            public CompositeContext(Context...parts) {
021                    this.parts = parts;
022            }
023            
024            public void setParts(Context...parts) {
025                    this.parts = parts;
026            }
027            
028            public <T> Iterator<T> lookupAll(Class<T> serviceClass) {
029                    List<T> all = new ArrayList<T>();
030                    for (Context part: parts) {
031                            if (part!=null) {
032                                    Iterator<T> cit = part.lookupAll(serviceClass);
033                                    while (cit.hasNext()) {
034                                            all.add(cit.next());
035                                    }
036                            }
037                    }
038                    return all.iterator();
039            }
040            
041            public <T> T lookup(Class<T> serviceClass) {
042                    for (Context part: parts) {
043                            if (part!=null) {
044                                    T ret = part.lookup(serviceClass);
045                                    if (ret!=null) {
046                                            return ret;
047                                    }
048                            }
049                    }
050                    return null;
051            }
052            
053            public Object lookup(String name) {
054                    for (Context part: parts) {
055                            if (part!=null) {
056                                    Object ret = part.lookup(name);
057                                    if (ret!=null) {
058                                            return ret;
059                                    }
060                            }
061                    }
062                    return null;
063            }
064    
065            public void bind(String name, Object obj) {
066                    for (Context part: parts) {
067                            if (part instanceof MutableContext) {
068                                    ((MutableContext) part).bind(name, obj);
069                                    return;
070                            }
071                    }
072                    throw new IllegalStateException("Composite context doesn't contain mutable parts.");
073            }
074    
075            public <T> void register(Class<? super T> type, T service) {
076                    for (Context part: parts) {
077                            if (part instanceof MutableContext) {
078                                    ((MutableContext) part).register(type, service);
079                                    return;
080                            }
081                    }
082                    throw new IllegalStateException("Composite context doesn't contain mutable parts.");
083            }
084    
085            public Object unbind(String name) {
086                    for (Context part: parts) {
087                            if (part instanceof MutableContext) {
088                                    return ((MutableContext) part).unbind(name);
089                            }
090                    }
091                    throw new IllegalStateException("Composite context doesn't contain mutable parts.");
092            }
093    
094            public <T> void unregister(Class<? super T> type, T service) {
095                    for (Context part: parts) {
096                            if (part instanceof MutableContext) {
097                                    ((MutableContext) part).unregister(type, service);
098                                    return;
099                            }
100                    }
101                    throw new IllegalStateException("Composite context doesn't contain mutable parts.");
102            }
103    }
104