1 21 package net.sf.hajdbc; 22 23 import java.beans.Introspector ; 24 import java.beans.PropertyDescriptor ; 25 import java.beans.PropertyEditor ; 26 import java.beans.PropertyEditorManager ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 31 import net.sf.hajdbc.util.Collections; 32 33 37 public class SynchronizationStrategyBuilder 38 { 39 private String id; 40 private String className; 41 private Properties properties; 42 43 46 public SynchronizationStrategyBuilder() 47 { 48 } 50 51 55 public SynchronizationStrategyBuilder(String id) 56 { 57 this.id = id; 58 } 59 60 63 public String getClassName() 64 { 65 return this.className; 66 } 67 68 71 public void setClassName(String className) 72 { 73 this.className = className; 74 } 75 76 79 public String getId() 80 { 81 return this.id; 82 } 83 84 87 public void setId(String id) 88 { 89 this.id = id; 90 } 91 92 95 public Properties getProperties() 96 { 97 return this.properties; 98 } 99 100 103 public void setProperties(Properties properties) 104 { 105 this.properties = properties; 106 } 107 108 112 public SynchronizationStrategy buildStrategy() throws Exception 113 { 114 Class <?> strategyClass = Class.forName(this.className); 115 116 SynchronizationStrategy strategy = strategyClass.asSubclass(SynchronizationStrategy.class).newInstance(); 117 118 PropertyDescriptor [] descriptors = Introspector.getBeanInfo(strategyClass).getPropertyDescriptors(); 119 120 Map <String , PropertyDescriptor > propertyDescriptorMap = new HashMap <String , PropertyDescriptor >(); 121 122 for (PropertyDescriptor descriptor: descriptors) 123 { 124 if (descriptor.getName().equals("class")) continue; 125 126 propertyDescriptorMap.put(descriptor.getName(), descriptor); 127 } 128 129 for (String name: Collections.cast(this.properties.keySet(), String .class)) 130 { 131 PropertyDescriptor descriptor = propertyDescriptorMap.get(name); 132 133 if (descriptor == null) 134 { 135 throw new IllegalArgumentException (Messages.getMessage(Messages.INVALID_PROPERTY, name, this.getClass().getName())); 136 } 137 138 PropertyEditor editor = PropertyEditorManager.findEditor(descriptor.getPropertyType()); 139 140 String textValue = this.properties.getProperty(name); 141 142 try 143 { 144 if (editor == null) 145 { 146 throw new Exception (); 147 } 148 149 editor.setAsText(textValue); 150 } 151 catch (Exception e) 152 { 153 throw new IllegalArgumentException (Messages.getMessage(Messages.INVALID_PROPERTY_VALUE, textValue, name, this.className)); 154 } 155 156 descriptor.getWriteMethod().invoke(strategy, editor.getValue()); 157 } 158 159 return strategy; 160 } 161 162 168 public static SynchronizationStrategyBuilder getBuilder(String id, SynchronizationStrategy strategy) throws Exception 169 { 170 SynchronizationStrategyBuilder builder = new SynchronizationStrategyBuilder(); 171 172 builder.setId(id); 173 174 Class strategyClass = strategy.getClass(); 175 176 builder.setClassName(strategyClass.getName()); 177 178 Properties properties = new Properties (); 179 180 PropertyDescriptor [] descriptors = Introspector.getBeanInfo(strategyClass).getPropertyDescriptors(); 181 182 for (PropertyDescriptor descriptor: descriptors) 183 { 184 if (descriptor.getName().equals("class")) continue; 185 186 PropertyEditor editor = PropertyEditorManager.findEditor(descriptor.getPropertyType()); 187 188 if (editor == null) continue; 189 190 editor.setValue(descriptor.getReadMethod().invoke(strategy)); 191 192 properties.setProperty(descriptor.getName(), editor.getAsText()); 193 } 194 195 builder.setProperties(properties); 196 197 return builder; 198 } 199 } 200 | Popular Tags |