1
package com.eucalyptus.configurable;
3
import java.lang.reflect.Field;
6
import org.apache.log4j.Logger;
7
import com.eucalyptus.event.PassiveEventListener;
8
import com.google.common.collect.Lists;
9
import com.google.common.collect.Maps;
10
import com.google.common.collect.Multimap;
11
import com.google.common.collect.Multimaps;
12
import edu.ucsb.eucalyptus.msgs.ComponentProperty;
14
public class PropertyDirectory {
15
private static Logger LOG = Logger.getLogger( PropertyDirectory.class );
16
private static Map<String, ConfigurableProperty> fqMap = Maps.newHashMap( );
17
private static Multimap<String, ConfigurableProperty> fqPrefixMap = Multimaps.newHashMultimap( );
18
private static Map<String, ConfigurableProperty> fqPendingMap = Maps.newHashMap( );
19
private static Multimap<String, ConfigurableProperty> fqPendingPrefixMap = Multimaps.newHashMultimap( );
21
private static List<ConfigurablePropertyBuilder> builders = Lists.newArrayList( new StaticPropertyEntry.StaticPropertyBuilder( ),
22
new SingletonDatabasePropertyEntry.DatabasePropertyBuilder( ),
23
new MultiDatabasePropertyEntry.DatabasePropertyBuilder( )); //FIXME: make this dynamic kkthx.
25
public static class NoopEventListener extends PassiveEventListener<ConfigurableProperty> {
26
public static NoopEventListener NOOP = new NoopEventListener( );
28
public void firingEvent( ConfigurableProperty t ) {}
30
@SuppressWarnings( { "unchecked" } )
31
public static ConfigurableProperty buildPropertyEntry( Class c, Field field ) {
32
for ( ConfigurablePropertyBuilder b : builders ) {
34
ConfigurableProperty prop = null;
36
prop = b.buildProperty( c, field );
37
} catch ( ConfigurablePropertyException e ) {
39
} catch ( Throwable t ) {
40
LOG.error( "Failed to prepare configurable field: " + c.getCanonicalName( ) + "." + field.getName( ) );
44
ConfigurableClass configurableAnnot = (ConfigurableClass) c.getAnnotation(ConfigurableClass.class);
45
if ( configurableAnnot.deferred() ) {
46
if ( !fqPendingMap.containsKey( prop.getQualifiedName( ) ) ) {
47
fqPendingMap.put( prop.getQualifiedName( ), prop );
48
fqPendingPrefixMap.put( prop.getEntrySetName( ), prop );
52
if ( !fqMap.containsKey( prop.getQualifiedName( ) ) ) {
53
fqMap.put( prop.getQualifiedName( ), prop );
54
fqPrefixMap.put( prop.getEntrySetName( ), prop );
57
RuntimeException r = new RuntimeException( "Duplicate configurable field in same config file: \n" + "-> "
58
+ fqMap.get( prop.getQualifiedName( ) ).getDefiningClass( ).getCanonicalName( ) + "." + field.getName( )
59
+ "\n" + "-> " + c.getCanonicalName( ) + "." + field.getName( ) + "\n" );
66
} catch ( ConfigurablePropertyException e ) {
73
public static List<String> getPropertyEntrySetNames( ) {
74
return Lists.newArrayList( fqPrefixMap.keySet( ) );
77
public static List<ConfigurableProperty> getPropertyEntrySet( ) {
78
List<ConfigurableProperty> props = Lists.newArrayList( );
79
for ( String fqPrefix : fqPrefixMap.keySet( ) ) {
80
props.addAll( getPropertyEntrySet( fqPrefix ) );
85
public static List<ConfigurableProperty> getPropertyEntrySet( String prefix ) {
86
List<ConfigurableProperty> props = Lists.newArrayList( );
87
for ( ConfigurableProperty fq : fqPrefixMap.get( prefix ) ) {
93
public static List<ConfigurableProperty> getPropertyEntrySet( String prefix, String alias ) {
94
List<ConfigurableProperty> props = Lists.newArrayList( );
95
for ( ConfigurableProperty fq : fqPrefixMap.get( prefix ) ) {
96
if(fq.getAlias().equals(alias))
102
public static ConfigurableProperty getPropertyEntry( String fq ) throws IllegalAccessException {
103
if ( !fqMap.containsKey( fq ) ) {
104
throw new IllegalAccessException( "No such property: " + fq );
106
return fqMap.get( fq );
110
public static List<ConfigurableProperty> getPendingPropertyEntrySet( String prefix ) {
111
List<ConfigurableProperty> props = Lists.newArrayList( );
112
for ( ConfigurableProperty fq : fqPendingPrefixMap.get( prefix ) ) {
118
public static String getEntrySetDescription( String entrySetName ) {
119
return "Temporary description";
122
public static List<ComponentProperty> getComponentPropertySet( String prefix ) {
123
List<ComponentProperty> componentProps = Lists.newArrayList();
124
List<ConfigurableProperty> props = getPropertyEntrySet( prefix );
125
for (ConfigurableProperty prop : props) {
126
componentProps.add(new ComponentProperty(prop.getWidgetType().toString(), prop.getDisplayName(), prop.getValue(), prop.getQualifiedName()));
128
return componentProps;
131
public static void addProperty(ConfigurableProperty prop) {
132
if ( !fqMap.containsKey( prop.getQualifiedName( ) ) ) {
133
fqMap.put( prop.getQualifiedName( ), prop );
134
fqPrefixMap.put( prop.getEntrySetName( ), prop );
138
public static void removeProperty(ConfigurableProperty prop) {
139
if ( fqMap.containsKey( prop.getQualifiedName( ) ) ) {
140
fqMap.remove( prop.getQualifiedName( ) );
141
fqPrefixMap.remove( prop.getEntrySetName( ), prop );
145
public static List<ComponentProperty> getComponentPropertySet(String prefix, String alias) {
146
List<ComponentProperty> componentProps = Lists.newArrayList();
147
List<ConfigurableProperty> props = getPropertyEntrySet( prefix, alias );
148
for (ConfigurableProperty prop : props) {
149
componentProps.add(new ComponentProperty(prop.getWidgetType().toString(), prop.getDisplayName(), prop.getValue(), prop.getQualifiedName()));
151
return componentProps;