1 46 package org.mr; 47 48 import java.lang.reflect.Constructor ; 49 import java.util.ArrayList ; 50 import java.util.HashMap ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 import org.mr.core.configuration.ConfigurationElement; 55 56 61 public class DynamicRepository { 62 Log log = null; 63 private HashMap implementations = new HashMap (); 64 65 public DynamicRepository(){ 66 67 } 68 69 73 protected void init(){ 74 MantaAgent agent = MantaAgent.getInstance(); 75 log=LogFactory.getLog("DynamicRepository"); 76 ConfigurationElement drConf = agent.getSingletonRepository().getConfigManager().getConfigurationElement("dynamicRepository"); 77 78 if(drConf == null ||drConf.getSubConfigurationElements() == null|| drConf.getSubConfigurationElements().size() == 0 ){ 79 if(log.isInfoEnabled()) { 80 log.info("DynamicRepository has no object to init."); 81 } return; 83 } 84 ArrayList impelList =drConf.getSubConfigurationElements(); 85 for (int i = 0; i < impelList.size(); i++) { 86 ConfigurationElement implConf = (ConfigurationElement) impelList.get(i); 87 ConfigurationElement implKey = implConf.getSubConfigurationElementByName("implementationKey"); 88 ConfigurationElement implClass = implConf.getSubConfigurationElementByName("class"); 89 try { 90 Class c =Class.forName(implClass.getValue()); 91 Constructor constructor = c.getConstructor(null); 92 Object impl = constructor.newInstance(null); 93 String key = implKey.getValue(); 94 implementations.put(key,impl ); 95 } catch (Exception e) { 96 log.error("DynamicRepository fail to load "+implClass.getValue()+ ".",e); 97 98 } 99 100 } 101 102 } 103 104 109 public Object getImplementation(String implementationKey){ 110 return implementations.get(implementationKey); 111 } 112 } 113 | Popular Tags |