1 22 package org.jboss.system.metadata; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.lang.reflect.Constructor ; 27 28 import org.jboss.system.ConfigurationException; 29 import org.jboss.util.Classes; 30 import org.jboss.util.StringPropertyReplacer; 31 32 42 public class ServiceConstructorMetaData 43 { 44 45 public static final Object [] EMPTY_PARAMETERS = {}; 46 47 48 public static final String [] EMPTY_PARAMS = {}; 49 50 51 public static final String [] EMPTY_SIGNATURE = {}; 52 53 54 private String [] signature = EMPTY_SIGNATURE; 55 56 57 private String [] params = EMPTY_PARAMS; 58 59 60 private Object [] parameters; 61 62 67 public String [] getParams() 68 { 69 return params; 70 } 71 72 77 public void setParams(String [] params) 78 { 79 if (params == null) 80 throw new IllegalArgumentException ("Null params"); 81 this.params = params; 82 } 83 84 90 public String [] getSignature() throws ConfigurationException 91 { 92 for (String string : signature) 93 { 94 if (string == null || string.trim().length() == 0) 95 throw new ConfigurationException("Missing or empty 'type' attribute in constructor arg"); 96 } 97 return signature; 98 } 99 100 105 public void setSignature(String [] signature) 106 { 107 if (signature == null) 108 throw new IllegalArgumentException ("Null signature"); 109 this.signature = signature; 110 } 111 112 119 public Object [] getParameters(ClassLoader cl) throws Exception 120 { 121 if (parameters != null) 122 return parameters; 123 124 if (params.length == 0) 125 return EMPTY_PARAMETERS; 126 127 String [] signature = getSignature(); 128 129 Object [] result = new Object [params.length]; 130 for (int i = 0; i < result.length; ++i) 131 { 132 if (params[i] == null) 133 throw new ConfigurationException("Missing 'value' attribute in constructor arg"); 134 135 String value = StringPropertyReplacer.replaceProperties(params[i]); 136 Object realValue = value; 137 138 if (signature[i] != null) 139 { 140 Class typeClass = Classes.getPrimitiveTypeForName(signature[i]); 142 if (typeClass == null) 143 typeClass = cl.loadClass(signature[i]); 144 145 PropertyEditor editor = PropertyEditorManager.findEditor(typeClass); 147 if (editor == null) 148 { 149 try 150 { 151 Class [] sig = {String .class}; 153 Constructor ctor = typeClass.getConstructor(sig); 154 Object [] args = {value}; 155 realValue = ctor.newInstance(args); 156 } 157 catch (Exception e) 158 { 159 throw new ConfigurationException("No property editor for type: " + typeClass); 160 } 161 } 162 else 163 { 164 editor.setAsText(value); 165 realValue = editor.getValue(); 166 } 167 } 168 result[i] = realValue; 169 } 170 return result; 171 } 172 173 178 public void setParameters(Object [] parameters) 179 { 180 this.parameters = parameters; 181 } 182 } 183 | Popular Tags |