CUBA Environment Entries


CUBA supports the same basic mechanism for component configuration as it is known from the EJB standard. Configuration parameters can be declared as environment entries in a component's deployment descriptor and accessed at runtime with function ComponentContextI.getEnvironment(). Environment entries can be of any primitive Java object type and can be addressed by a symbolic identifier. An environment entry can be declared as follows:
 
<?xml version="1.0" encoding="UTF-8"?>
<component-jar>
  <component>
    <component-name>env</component-name>
    <external-interface>env.Env</external-interface>
    <component-class>env.EnvImpl</component-class>

    <env-entries>
      <env-entry name="email" type="java.lang.String" value="jlessner@gmx.de"/> 
    </env-entries>
  </component>
</component-jar>

Accessing the environment entry at runtime by a lookup operation looks like this
 
public class EnvImpl extends AbstractComponent implements Env {
  public String getEmail() throws ReferenceException {
    return getContext().getEnvironment("email").toString();
  }
}

Alternatively, environment entries can be initialized by the container, using dependency injection. The component's implementation class has to define a public member or setter function which can be refered to in the descriptor, e.g

...
    <env-entries>

      <env-entry name="email" type="java.lang.String"
         value="jlessner@gmx.de" injection-target="emailAddress"/>

    </env-entries>
...


The environment entry doesn't have to be looked up any more in the code but can be used as a pre-initialized member:
 
public class EnvImpl extends AbstractComponent implements Env {
  public String emailAddress; // will be set by container
  public String getEmail() throws ReferenceException {

    return emailAddress;
  }
}

Environment entries can also be defined by the @Resource and @Resources code annotations in an equivalent way as defined in the EJB 3 standard, e.g.
 
public class EnvImpl extends AbstractComponent implements Env {
  @Resource(name="emailAddress") public String emailAddress;
}

As an extension over EJB 3 environment entries, CUBA allows to specify an initial value by means of the annotation's link attribute. The value will not only be injected but is also available by lookup from the component context. Environment entries which are not injected are defined at the class level. For further details, see the EJB specification. An example for the usage of environment entries in CUBA is available under examples/env.


Home Introduction Javadoc