public class BeanPopulator extends Object
Utility class that configures *EnvironmentBean objects using a PropertyManager, which is usually backed by a -properties.xml file.
Jonathan Halliday (
  • Constructor Details

    • BeanPopulator

      public BeanPopulator()
  • Method Details

    • getDefaultInstance

      public static <T> T getDefaultInstance(Class<T> beanClass) throws RuntimeException
    • getDefaultInstance

      public static <T> T getDefaultInstance(Class<T> beanClass, Properties properties) throws RuntimeException
    • getNamedInstance

      public static <T> T getNamedInstance(Class<T> beanClass, String name) throws RuntimeException
    • configureFromProperties

      public static void configureFromProperties(Object bean, Properties properties) throws Exception
      Only used in tests
    • configureFromProperties

      public static void configureFromProperties(Object bean, String instanceName, Properties properties) throws Exception
      Examine the properties of the provided bean and update them to match the values of the corresponding properties in the Properties. This will normally be used at startup to configure a freshly created default bean to match the configuration read from a properties file. The algorithm is as follows: for each field in the bean, which must have a getter and setter method matching according to the JavaBeans naming conventions, determine the corresponding property key. Several key names are tried, with the first match being used: For scalar properties: The FQN of the bean class, optionally followed by the bean name, followed by the field name, e.g. com.arjuna.FooBean.theField or com.arjuna.FooBean.theName.theField the short class name of the bean, optionally followed by the bean name, followed by the field name, e.g. FooBean.theField or FooBean.theName.theField and finally the bean classes' PropertyPrefix annotation value followed by the name of the field, the last being except in cases where the field has a FullPropertyName annotation, in which case its value is used instead. For vector (in the math sense - the type is actually normally List/Map) properties, a single property key matched according to the prior rules will be treated as having a compound value which will be tokenized into elements based on whitespace and inserted into the list in token order or further tokenized on = for Map key/value. If no such key is found, the value of the field's ConcatenationPrefix annotation is treated as a name prefix and the list elements are assembled from the values of any properties having key values starting with the prefix. These are inserted into the list in order of the key's name sort position. This allows for the convention that all properties in a given bean will share the same prefix e.g. whilst still allowing for changing of the property name in cases where this makes for more readable code. Obtain the value of the property from the Properties and if it's not null, type convert it to match the bean's property field type. Obtain the value of the property from the bean and if it's different from the value read from the Properties, use the setter to update the bean.
      bean - a JavaBean, the target of the property updates and source for defaults.
      instanceName - the (optional, use null for default) name for the bean instance.
      properties - a Properties object, the source of the configuration overrides.
      Exception - if the configuration of the bean fails.
    • setBeanInstanceIfAbsent

      public static Object setBeanInstanceIfAbsent(String name, Object beanInstance)
      Attempt to set a named bean instance. [Please note that this method is not guaranteed to exist longer term since it is provided in an internal API. In a later release a public API will be provided to support similar behaviour]. If the name is not already associated with a bean instance (or is mapped to null) then the name will be associated with the supplied bean and the method will return null, else the method returns the current instance associated with the name. Once a bean is associated with a name any other configuration mechanism (such as XML property files and system properties) will be ignored. Therefore this method can be useful if the caller wishes to avoid the need to parse XML property files.
      name - the name of the instance to set
      beanInstance - the instance to be associated with the name
      the previous instance associated with the specified name, or null if there was no mapping for the name.
    • printBean

      public static String printBean(Object bean)
    • printState

      public static String printState()
      Render the state of the known bean instances as text.