1 18 19 package sync4j.syncclient.spds.engine; 20 21 import java.lang.reflect.Method ; 22 23 import java.util.Hashtable ; 24 import java.util.Enumeration ; 25 26 import sync4j.syncclient.common.logging.Logger; 27 import sync4j.syncclient.common.SimpleClassLoader; 28 import sync4j.syncclient.spds.source.SPSSyncSource; 29 30 40 public class SyncSourceFactory { 41 42 private Hashtable syncSourcesDefinition = null ; 43 private Hashtable syncSources = null ; 44 45 private String libDir = null ; 46 private Logger logger = new Logger() ; 47 48 57 public SyncSourceFactory (String libDir, Hashtable syncSourcesDefinition) { 58 59 this.syncSourcesDefinition = syncSourcesDefinition; 60 this.syncSources = new Hashtable (); 61 62 this.libDir = libDir; 63 64 } 65 66 69 public SyncSource getSyncSource(String dataStoreName) { 70 71 SyncSource s = (SyncSource)syncSources.get(dataStoreName); 72 73 if (s != null) { 74 return s; 75 } 76 77 String syncSourceClass = null; 78 SyncSource syncSource = null; 79 80 SyncSourceDefinition def = (SyncSourceDefinition) syncSourcesDefinition.get(dataStoreName); 81 82 syncSourceClass = def.getSourceClass(); 83 84 try { 85 syncSource = (SyncSource) (Class.forName(syncSourceClass)).newInstance(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 89 String msg = "Error loading class " + 90 syncSourceClass + 91 ": " + 92 e.getMessage() ; 93 94 if (logger.isLoggable(Logger.INFO)) { 95 logger.info(msg); 96 } 97 98 return null; 99 } 100 101 106 if (syncSourceClass.endsWith(".SPSSyncSource")) { 107 ((SPSSyncSource)syncSource).setClassLoader(new SimpleClassLoader(this.libDir)); 108 } 109 110 111 Hashtable properties = def.getProperties(); 112 113 Enumeration e = properties.keys(); 114 115 String key = null; 116 while (e.hasMoreElements()) { 117 key = (String )e.nextElement(); 118 setProperty(syncSource, key, (String )properties.get(key)); 119 } 120 121 syncSources.put((Object ) dataStoreName, (Object ) syncSource); 122 123 return syncSource; 124 } 125 126 128 private void setProperty(SyncSource source, String key, String value) { 129 String methodName = ""; 130 131 char firstLetter = key.toUpperCase().charAt(0); 132 if (key.length() > 1) { 133 methodName = key.substring(1); 134 } 135 136 methodName = "set" + firstLetter + methodName; 137 138 Class sourceClass = source.getClass(); 139 140 try { 141 Method m = sourceClass.getMethod(methodName, new Class [] { String .class }); 142 m.invoke(source, new String [] {value}); 143 } catch (Exception e) { 144 String msg = "Property " + 145 key + 146 " not set to " + 147 value + 148 ". Method " + 149 methodName + 150 "(String s) not found in " + 151 sourceClass.getName() ; 152 153 if (logger.isLoggable(Logger.DEBUG)) { 154 logger.debug(msg); 155 } 156 157 } 158 } 159 } | Popular Tags |