1 package org.apache.tools.ant.taskdefs.optional.jmx.converter; 2 3 52 53 54 55 import java.util.Iterator ; 56 import java.util.Map ; 57 import java.util.TreeMap ; 58 import org.apache.tools.ant.BuildException; 59 60 61 74 public class ValueFactory implements ValueConverter { 75 76 private Map converterMap; 79 private static ValueFactory singleton = null; 80 private ValueConverter defaultConverter; 81 82 83 private ValueFactory(ValueConverter defaultConverter) { 84 converterMap = new TreeMap (); 85 this.defaultConverter = defaultConverter; 86 } 87 88 private ValueFactory() { 89 this(new DefaultValueConverter()); 90 converterMap = new TreeMap (); 91 } 92 93 97 public static ValueFactory getInstance() { 98 if (singleton == null) { 99 ValueConverter converter = new DefaultValueConverter(); 100 singleton = new ValueFactory(converter); 101 singleton.registerValueConverter(converter); 102 singleton.registerValueConverter(new PropertiesValueConverter()); 103 singleton.registerValueConverter(new SetValueConverter()); 104 } 105 return singleton; 106 } 107 108 116 public void registerValueConverter(ValueConverter converter) { 117 if (converter != null) { 118 String [] types = converter.getSupportedTypes(); 119 for (int counter = 0; counter < types.length; counter++) { 120 converterMap.put(types[counter],converter); 121 } 122 } 123 } 124 125 134 private ValueConverter findConverterForClass(Class theClass) { 135 136 ValueConverter converter = (ValueConverter) converterMap.get(theClass.getName()); 137 if (converter == null) { 138 Class superClass = theClass.getSuperclass(); 142 if (superClass != null) { 143 converter = findConverterForClass(theClass); 144 } 145 Class [] interfaces = theClass.getInterfaces(); 146 for (int counter = 0; counter < interfaces.length; counter++) { 147 converter = findConverterForClass(interfaces[counter]); 148 if (converter != null) { 149 break; 150 } 151 } 152 } 153 154 return converter; 155 } 156 157 public Object valueOf(String value, String type) throws Exception { 158 ValueConverter converter = (ValueConverter) converterMap.get(type); 161 if (converter == null) { 162 Class theClass = Thread.currentThread().getContextClassLoader().loadClass(type); 163 if ( (theClass != null) & (theClass.isArray()) ) { 164 return valueOfArray(value,type); 165 } else { 166 converter = findConverterForClass(theClass); 167 } 168 } 169 170 if (converter == null) { 171 converter = defaultConverter; 174 } 175 176 try { 177 return converter.valueOf(value,type); 178 } catch (Exception x) { 179 throw new org.apache.tools.ant.BuildException("Cannot convert property of type ["+type+"]. " 180 + "JMX4Ant provides support via reflection for any class with a static valueOf() method, and any class with a constructor " 181 + "with a single String argument. JMX4Ant also provides " 182 + "explicit support for values and arrays of the following types or subtypes: " 183 + converterMap.keySet().toString(),x); 184 } 185 186 187 } 188 189 202 private Object [] valueOfArray(String value, String type) throws Exception { 203 Class arrayClass = Thread.currentThread().getContextClassLoader().loadClass(type); 204 if ( (arrayClass != null) & (arrayClass.isArray()) ) { 205 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer (value," []{}\t\n"); 206 java.util.ArrayList result = new java.util.ArrayList (); 207 while (tokenizer.hasMoreTokens()) { 208 String arrayValue = tokenizer.nextToken(); 209 result.add(valueOf(arrayValue,arrayClass.getComponentType().getName())); 210 } 211 212 Object [] resultArray = (Object [])java.lang.reflect.Array.newInstance(arrayClass.getComponentType(), result.size()); 215 Iterator it = result.iterator(); 216 217 int counter = 0; 218 while (it.hasNext()) { 219 resultArray[counter] = it.next(); 220 counter++; 221 } 222 return resultArray; 223 } else { 224 throw new BuildException("Was expecting ["+type+"] to be an array."); 225 } 226 } 227 228 public String [] getSupportedTypes() { 229 return (String []) converterMap.keySet().toArray(); 230 } 231 232 233 234 237 public static String arrayToString(Object array) { 238 Object [] objectArray = (Object []) array; 239 StringBuffer result = new StringBuffer ("["); 240 241 for (int counter = 0; counter < objectArray.length; counter++) { 242 result.append(objectArray[counter].toString()); 243 if ((counter+1) < objectArray.length) { 244 result.append("\t"); 245 } 246 } 247 result.append("]"); 248 return result.toString(); 249 250 } 251 252 256 public static String toString(Object value) { 257 String stringValue = null; 258 if ( (value != null) && (value.getClass().isArray()) ) { 259 stringValue = arrayToString(value); 260 } else if (value != null) { 261 stringValue = value.toString(); 262 } 263 264 return stringValue; 265 } 266 } 267 268 293 | Popular Tags |