001    package biz.hammurapi.registry;
002    
003    import java.net.URL;
004    import java.util.ArrayList;
005    import java.util.Arrays;
006    import java.util.Collection;
007    import java.util.List;
008    import java.util.Set;
009    
010    import biz.hammurapi.config.ConfigurationException;
011    import biz.hammurapi.configx.XmlConfigFactory;
012    
013    /**
014     * Registry entry which groups other entries.
015     * @author Pavel
016     *
017     */
018    public class Directory implements RegistryEntry {
019            
020            private List<RegistryEntry> children = new ArrayList<RegistryEntry>();
021            private String description;
022            private String name;
023            private String tooltip;
024            
025            public Directory() {
026                    
027            }
028            
029            public Directory(String name, Collection<RegistryEntry> children) {
030                    this.children.addAll(children);
031                    this.name = name;
032            }
033            
034            public Directory(String name, RegistryEntry ... children) {
035                    this(name, Arrays.asList(children));
036            }
037            
038            public void addEntry(RegistryEntry child) {
039                    children.add(child);
040            }
041            
042            public void addEntry(URL childConfigUrl) throws ConfigurationException {
043                    RegistryEntry child = (RegistryEntry) XmlConfigFactory.create(childConfigUrl, getClass().getClassLoader());
044                    children.add(child);
045            }
046    
047            public List<RegistryEntry> getChildren(
048                            Class<?>[] componentTypes,
049                            Class<?> factoryTypes, 
050                            Set<String> filterTokens) {
051    
052                    List<RegistryEntry> ret = new ArrayList<RegistryEntry>();
053                    for (RegistryEntry child: children) {
054                            if (child.match(componentTypes, factoryTypes, filterTokens)) {
055                                    ret.add(child);
056                            }
057                    }
058                    return ret;
059            }
060            
061            private static final Class<?>[] EMPTY_CLASS_ARRAY = {};
062    
063            public Class<?>[] getComponentTypes() {
064                    return EMPTY_CLASS_ARRAY;
065            }
066    
067            public String getDescription() {
068                    return description;
069            }
070            
071            public void setDescription(String description) {
072                    this.description = description;
073            }
074    
075            public Class<?>[] getFactoryTypes() {
076                    return EMPTY_CLASS_ARRAY;
077            }
078    
079            public String getName() {
080                    return name;
081            }
082            
083            public void setName(String name) {
084                    this.name = name;
085            }
086    
087            public String getTooltip() {
088                    return tooltip;
089            }
090            
091            public void setTooltip(String tooltip) {
092                    this.tooltip = tooltip;
093            }
094    
095            public boolean hasChildren(
096                            Class<?>[] componentTypes,
097                            Class<?> factoryTypes, 
098                            Set<String> filterTokens) {
099                    
100                    for (RegistryEntry child: children) {
101                            if (child.match(componentTypes, factoryTypes, filterTokens)) {
102                                    return true;
103                            }
104                            
105                            if (child.hasChildren(componentTypes, factoryTypes, filterTokens)) {
106                                    return true;
107                            }
108                    }
109                    return false;
110            }
111    
112            /**
113             * Directory never matches.
114             */
115            public boolean match(
116                            Class<?>[] componentTypes, 
117                            Class<?> factoryTypes,
118                            Set<String> filterTokens) {
119                    return false;
120            }
121    
122            /**
123             * Directory cannot be instantiated.
124             * @return null
125             */
126            public <T> FactoryConfigurator<T> newFactory(Class<T> factoryClass) {
127                    return null;
128            }
129    
130            // TODO implement merge() to load Registry service from config files.
131    }