1 29 30 package nextapp.echo2.app.util; 31 32 import java.io.IOException ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 import nextapp.echo2.app.util.PropertiesDiscovery; 38 39 49 public class PeerFactory { 50 51 private final Map objectClassNameToPeerMap = new HashMap (); 52 53 64 public PeerFactory(String resourceName, ClassLoader classLoader) { 65 try { 66 Map peerNameMap = PropertiesDiscovery.loadProperties(resourceName, classLoader); 67 Iterator it = peerNameMap.keySet().iterator(); 68 while (it.hasNext()) { 69 String objectClassName = (String ) it.next(); 70 String peerClassName = (String ) peerNameMap.get(objectClassName); 71 Class peerClass = classLoader.loadClass(peerClassName); 72 Object peer = peerClass.newInstance(); 73 objectClassNameToPeerMap.put(objectClassName, peer); 74 } 75 } catch (ClassNotFoundException ex) { 76 throw new RuntimeException ("Unable to load synchronize peer bindings: " + ex); 77 } catch (IOException ex) { 78 throw new RuntimeException ("Unable to load synchronize peer bindings: " + ex); 79 } catch (InstantiationException ex) { 80 throw new RuntimeException ("Unable to load synchronize peer bindings: " + ex); 81 } catch (IllegalAccessException ex) { 82 throw new RuntimeException ("Unable to load synchronize peer bindings: " + ex); 83 } 84 } 85 86 97 public Object getPeerForObject(Class objectClass, boolean searchSuperClasses) { 98 Object peer = null; 99 do { 100 peer = objectClassNameToPeerMap.get(objectClass.getName()); 101 if (peer != null) { 102 return peer; 103 } 104 objectClass = objectClass.getSuperclass(); 105 } while (searchSuperClasses && objectClass != null); 106 return null; 107 } 108 } 109 | Popular Tags |