1
package com.eucalyptus.configurable;
3
import java.lang.reflect.Field;
4
import java.lang.reflect.Modifier;
5
import org.apache.log4j.Logger;
6
import com.eucalyptus.configurable.PropertyDirectory.NoopEventListener;
7
import com.eucalyptus.event.PassiveEventListener;
9
public class StaticPropertyEntry extends AbstractConfigurableProperty {
10
static Logger LOG = Logger.getLogger( StaticPropertyEntry.class );
12
public StaticPropertyEntry( Class definingClass, String entrySetName, Field field, String description, String defaultValue, PropertyTypeParser typeParser, Boolean readOnly, String displayName, ConfigurableFieldType widgetType, String alias, PassiveEventListener changeListener ) {
13
super( definingClass, entrySetName, field.getName( ), defaultValue, description, typeParser, readOnly, displayName, widgetType, alias, changeListener );
16
public Field getField( ) {
20
public String getValue( ) {
22
return ""+this.field.get( null );
23
} catch ( Exception e ) {
25
return super.getDefaultValue();
29
public String setValue( String s ) {
31
Object o = super.getTypeParser( ).parse( s );
32
this.field.set( null, o );
34
LOG.info( "Set configurable property: " + super.getQualifiedName( ) + " to " + s );
35
} catch ( Throwable t ) {
36
LOG.warn( "Failed to set property: " + super.getQualifiedName( ) + " because of " + t.getMessage( ) );
39
return this.getValue( );
42
public static class StaticPropertyBuilder implements ConfigurablePropertyBuilder {
43
private static String qualifiedName( Class c, Field f ) {
44
ConfigurableClass annote = ( ConfigurableClass ) c.getAnnotation( ConfigurableClass.class );
45
return annote.root( ) + "." + f.getName( ).toLowerCase( );
49
public ConfigurableProperty buildProperty( Class c, Field field ) throws ConfigurablePropertyException {
50
if( c.isAnnotationPresent( ConfigurableClass.class ) && field.isAnnotationPresent( ConfigurableField.class ) ) {
51
ConfigurableClass classAnnote = ( ConfigurableClass ) c.getAnnotation( ConfigurableClass.class );
52
ConfigurableField annote = ( ConfigurableField ) field.getAnnotation( ConfigurableField.class );
53
String description = annote.description( );
54
String defaultValue = annote.initial( );
55
String fq = qualifiedName( c, field );
56
String fqPrefix = fq.replaceAll( "\\..*", "" );
57
String alias = classAnnote.alias();
58
PropertyTypeParser p = PropertyTypeParser.get( field.getType( ) );
59
ConfigurableProperty entry = null;
60
Class<? extends PassiveEventListener> changeListenerClass = annote.changeListener( );
61
PassiveEventListener changeListener;
62
if( !changeListenerClass.equals( NoopEventListener.class ) ) {
64
changeListener = changeListenerClass.newInstance( );
65
} catch ( Throwable e ) {
66
changeListener = NoopEventListener.NOOP;
69
changeListener = NoopEventListener.NOOP;
71
int modifiers = field.getModifiers( );
72
if ( Modifier.isPublic( modifiers ) && Modifier.isStatic( modifiers ) ) {
73
entry = new StaticPropertyEntry( c, fqPrefix, field, description, defaultValue, p, annote.readonly( ), annote.displayName(), annote.type(), alias, changeListener );
74
entry.setValue( defaultValue );