001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.util.HashMap;
007    import java.util.Map;
008    
009    import org.apache.commons.logging.Log;
010    import org.apache.commons.logging.LogFactory;
011    import org.w3c.dom.Element;
012    import org.w3c.dom.NodeList;
013    
014    /**
015     * @author lakshman
016     * 
017     */
018    public class PrototypeFactory<X extends Object> {
019            private Log log = LogFactory.getLog(PrototypeFactory.class);
020    
021            private Map<String, X> myMap = new HashMap<String, X>();
022    
023            private void register(String name, X prototype) {
024                    myMap.put(name, prototype);
025            }
026    
027            /**
028             * Get the master
029             * 
030             * @return null if no such prototype was registered
031             */
032            public synchronized X getPrototypeMaster(String name) {
033                    return myMap.get(name);
034            }
035    
036            @SuppressWarnings("unchecked")
037            public PrototypeFactory(String configpath) {
038                    try {
039                            Element e = W2Config.getFileElement(configpath);
040                            NodeList nodes = e.getElementsByTagName("class");
041                            for (int i = 0; i < nodes.getLength(); ++i) {
042                                    Element c = (Element) nodes.item(i);
043                                    String name = c.getAttribute("name");
044                                    String proto = c.getAttribute("proto");
045                                    register(name, (X) Class.forName(proto).newInstance());
046                                    if (log.isDebugEnabled()) {
047                                            log.debug("Registered " + proto);
048                                    }
049                            }
050                    } catch (Exception e) {
051                            log.error(e);
052                    }
053            }
054    
055    }