1 18 package org.apache.beehive.netui.util.iterator; 19 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.LinkedHashMap ; 24 import java.util.Map ; 25 import java.util.Collections ; 26 import java.sql.ResultSet ; 27 import javax.sql.RowSet ; 28 29 import org.apache.beehive.netui.util.config.ConfigUtil; 30 import org.apache.beehive.netui.util.config.bean.NetuiConfigDocument.NetuiConfig; 31 import org.apache.beehive.netui.util.config.bean.IteratorFactories; 32 import org.apache.beehive.netui.util.logging.Logger; 33 34 53 public class IteratorFactory { 54 55 58 public static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 59 60 private static final Logger LOGGER = Logger.getInstance(IteratorFactory.class); 61 private static final LinkedHashMap ITERATOR_FACTORIES; 62 63 static { 64 ITERATOR_FACTORIES = new LinkedHashMap (); 65 initialize(); 66 } 67 68 71 public abstract static class IteratorPlant { 72 76 public abstract Iterator createIterator(Object value); 77 } 78 79 85 public static final Iterator createIterator(Object object) { 86 LOGGER.debug("Create an iterator for class: " + (object == null ? "null" : object.getClass().getName())); 87 88 if(object == null) 89 return null; 90 91 if(object instanceof Iterator ) { 92 return (Iterator )object; 93 } else if(object instanceof Collection ) { 94 Collection collection = (Collection )object; 95 return collection.iterator(); 96 } else if(object instanceof Map ) { 97 return new MapIterator((Map )object); 98 } else if(object.getClass().isArray()) { 99 return new ArrayIterator(object); 100 } else if(object instanceof Enumeration ) 101 return new EnumerationIterator((Enumeration )object); 102 else if(object instanceof ResultSet && !(object instanceof RowSet )) 103 return new ResultSetIterator((ResultSet )object); 104 105 Iterator ret = null; 107 Iterator factories = ITERATOR_FACTORIES.keySet().iterator(); 108 while(factories.hasNext()) { 109 IteratorPlant plant = (IteratorPlant)ITERATOR_FACTORIES.get(factories.next()); 110 ret = plant.createIterator(object); 111 112 if(ret != null) return ret; 113 } 114 115 return new AtomicObjectIterator(object); 116 } 117 118 122 private static final void initialize() { 123 Map map = readFromConfig(); 124 if(map != null) 125 loadFactories(map); 126 } 127 128 private static final Map readFromConfig() { 129 NetuiConfig config = ConfigUtil.getConfig(); 130 if(config == null) 131 return null; 132 133 IteratorFactories factories = config.getIteratorFactories(); 134 if(factories == null) 135 return null; 136 137 org.apache.beehive.netui.util.config.bean.IteratorFactories.IteratorFactory[] factoryArray = 138 factories.getIteratorFactoryArray(); 139 if(factoryArray == null) 140 return null; 141 142 LinkedHashMap map = new LinkedHashMap (); 143 for(int i = 0; i < factoryArray.length; i++) { 144 map.put(factoryArray[i].getName(), factoryArray[i].getFactoryClass()); 145 } 146 147 return map; 148 } 149 150 private static final void loadFactories(Map factories) { 151 Iterator iterator = factories.keySet().iterator(); 152 while(iterator.hasNext()) { 153 String name = (String )iterator.next(); 154 String className = (String )factories.get(name); 155 IteratorPlant plant = null; 156 157 try { 158 Class type = Class.forName(className); 159 plant = (IteratorPlant)type.newInstance(); 160 } catch(ClassNotFoundException cnf) { 161 LOGGER.warn("Could not create an IteratorPlant for type \"" + className + "\" because the implementation class could not be found."); 162 continue; 163 } catch(Exception e) { 164 assert e instanceof InstantiationException || e instanceof IllegalAccessException ; 165 LOGGER.warn("Could not create an IteratorPlant for type \"" + className + "\" because an error occurred creating the plant. Cause: " + e, e); 166 continue; 167 } 168 169 if(ITERATOR_FACTORIES.containsKey(name)) { 170 LOGGER.warn("Overwriting a previously defined IteratorPlant named \"" + name + "\" with a new IteratorPlant of type \"" + className + "\""); 171 } else LOGGER.info("Adding an IteratorPlant named \"" + name + "\" with implementation \"" + className + "\""); 172 173 ITERATOR_FACTORIES.put(name, plant); 174 } 175 } 176 } 177 | Popular Tags |