| 1 |
package org.apache.commons.jcs3.utils.config; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import java.beans.BeanInfo; |
| 23 |
import java.beans.IntrospectionException; |
| 24 |
import java.beans.Introspector; |
| 25 |
import java.beans.PropertyDescriptor; |
| 26 |
import java.io.File; |
| 27 |
import java.lang.reflect.Method; |
| 28 |
import java.util.Properties; |
| 29 |
|
| 30 |
import org.apache.commons.jcs3.log.Log; |
| 31 |
import org.apache.commons.jcs3.log.LogManager; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class PropertySetter |
| 55 |
{ |
| 56 |
|
| 57 |
private static final Log log = LogManager.getLog( PropertySetter.class ); |
| 58 |
|
| 59 |
|
| 60 |
private final Object obj; |
| 61 |
|
| 62 |
|
| 63 |
private PropertyDescriptor[] props; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public PropertySetter( final Object obj ) |
| 71 |
{ |
| 72 |
this.obj = obj; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
protected void introspect() |
| 79 |
{ |
| 80 |
try |
| 81 |
{ |
| 82 |
final BeanInfo bi = Introspector.getBeanInfo( obj.getClass() ); |
| 83 |
props = bi.getPropertyDescriptors(); |
| 84 |
} |
| 85 |
catch ( final IntrospectionException ex ) |
| 86 |
{ |
| 87 |
log.error( "Failed to introspect {0}", obj, ex ); |
| 88 |
props = new PropertyDescriptor[0]; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static void setProperties( final Object obj, final Properties properties, final String prefix ) |
| 101 |
{ |
| 102 |
new PropertySetter( obj ).setProperties( properties, prefix ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void setProperties( final Properties properties, final String prefix ) |
| 112 |
{ |
| 113 |
final int len = prefix.length(); |
| 114 |
|
| 115 |
for (final String key : properties.stringPropertyNames()) |
| 116 |
{ |
| 117 |
|
| 118 |
if ( key.startsWith( prefix ) ) |
| 119 |
{ |
| 120 |
|
| 121 |
if ( key.indexOf( '.', len + 1 ) > 0 ) |
| 122 |
{ |
| 123 |
|
| 124 |
|
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
final String value = OptionConverter.findAndSubst( key, properties ); |
| 129 |
|
| 130 |
setProperty( key.substring( len ), value ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public void setProperty( String name, final String value ) |
| 150 |
{ |
| 151 |
if ( value == null ) |
| 152 |
{ |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
name = Introspector.decapitalize( name ); |
| 157 |
final PropertyDescriptor prop = getPropertyDescriptor( name ); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if ( prop == null ) |
| 162 |
{ |
| 163 |
log.warn( "No such property [{0}] in {1}.", name, obj.getClass().getName() ); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
try |
| 168 |
{ |
| 169 |
setProperty( prop, name, value ); |
| 170 |
} |
| 171 |
catch ( final PropertySetterException ex ) |
| 172 |
{ |
| 173 |
log.warn( "Failed to set property {0} to value \"{1}\".", name, value, ex ); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void setProperty( final PropertyDescriptor prop, final String name, final String value ) |
| 187 |
throws PropertySetterException |
| 188 |
{ |
| 189 |
final Method setter = prop.getWriteMethod(); |
| 190 |
if ( setter == null ) |
| 191 |
{ |
| 192 |
throw new PropertySetterException( "No setter for property" ); |
| 193 |
} |
| 194 |
final Class<?>[] paramTypes = setter.getParameterTypes(); |
| 195 |
if ( paramTypes.length != 1 ) |
| 196 |
{ |
| 197 |
throw new PropertySetterException( "#params for setter != 1" ); |
| 198 |
} |
| 199 |
|
| 200 |
final Object arg; |
| 201 |
try |
| 202 |
{ |
| 203 |
arg = convertArg( value, paramTypes[0] ); |
| 204 |
} |
| 205 |
catch ( final Throwable t ) |
| 206 |
{ |
| 207 |
throw new PropertySetterException( "Conversion to type [" + paramTypes[0] + "] failed. Reason: " + t ); |
| 208 |
} |
| 209 |
if ( arg == null ) |
| 210 |
{ |
| 211 |
throw new PropertySetterException( "Conversion to type [" + paramTypes[0] + "] failed." ); |
| 212 |
} |
| 213 |
log.debug( "Setting property [{0}] to [{1}].", name, arg ); |
| 214 |
try |
| 215 |
{ |
| 216 |
setter.invoke( obj, arg ); |
| 217 |
} |
| 218 |
catch ( final Exception ex ) |
| 219 |
{ |
| 220 |
throw new PropertySetterException( ex ); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected Object convertArg( final String val, final Class<?> type ) |
| 231 |
{ |
| 232 |
if ( val == null ) |
| 233 |
{ |
| 234 |
return null; |
| 235 |
} |
| 236 |
|
| 237 |
final String v = val.trim(); |
| 238 |
if ( String.class.isAssignableFrom( type ) ) |
| 239 |
{ |
| 240 |
return val; |
| 241 |
} |
| 242 |
if ( Integer.TYPE.isAssignableFrom( type ) ) |
| 243 |
{ |
| 244 |
return Integer.valueOf( v ); |
| 245 |
} |
| 246 |
if ( Long.TYPE.isAssignableFrom( type ) ) |
| 247 |
{ |
| 248 |
return Long.valueOf( v ); |
| 249 |
} |
| 250 |
if ( Boolean.TYPE.isAssignableFrom( type ) ) |
| 251 |
{ |
| 252 |
if ( "true".equalsIgnoreCase( v ) ) |
| 253 |
{ |
| 254 |
return Boolean.TRUE; |
| 255 |
} |
| 256 |
if ( "false".equalsIgnoreCase( v ) ) |
| 257 |
{ |
| 258 |
return Boolean.FALSE; |
| 259 |
} |
| 260 |
} |
| 261 |
else if( type.isEnum() ) |
| 262 |
{ |
| 263 |
Enum<?> valueOf = Enum.valueOf(type.asSubclass(Enum.class), v); |
| 264 |
return valueOf; |
| 265 |
} |
| 266 |
else if ( File.class.isAssignableFrom( type ) ) |
| 267 |
{ |
| 268 |
return new File( v ); |
| 269 |
} |
| 270 |
return null; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
protected PropertyDescriptor getPropertyDescriptor( final String name ) |
| 279 |
{ |
| 280 |
if ( props == null ) |
| 281 |
{ |
| 282 |
introspect(); |
| 283 |
} |
| 284 |
|
| 285 |
for (final PropertyDescriptor prop : props) |
| 286 |
{ |
| 287 |
if ( name.equals( prop.getName() ) ) |
| 288 |
{ |
| 289 |
return prop; |
| 290 |
} |
| 291 |
} |
| 292 |
return null; |
| 293 |
} |
| 294 |
} |