felix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Clement Escoffier (JIRA)" <j...@apache.org>
Subject [jira] Commented: (FELIX-227) iPOJO should allow configuration and service properties to be bound via setter/getter methods, not just via direct fields
Date Mon, 05 Mar 2007 17:28:50 GMT

    [ https://issues.apache.org/jira/browse/FELIX-227?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12478110
] 

Clement Escoffier commented on FELIX-227:
-----------------------------------------

iPOJO follow the dependency injection pattern (as Spring). So when you read your code, you
don't have to focus on which value a container-managed field has. You should guess : "it will
have a consistent value". The JEE annotation are another good (and famous) example of dependency
injections. But the advantages, is that these annotation are in the code. So user know that
the field will be container-managed. iPOJO does not provide annotations because it brings
compilation dependency that I don't want (it implies that each handler must provide its annotation
classes and be in the classpath). A parser using "doclet" is currently under study. Doctlet
have the advantage to not create compilation dependency but cannot be checked statically.

However, you point up an interisting limitation. There is no way in the POJO to be notified
of the injection of a value in a property. Perhaps callback can help.

Imagine something like : 
<property name="foo.bound" field="bound" method="setBound'>
and in your code
setBound(String newvalue) { ...}

When the container injects a value inside the "bound" property, it should notify the pojo
by invoking the setBound method. What do you think of this solution ? 



> iPOJO should allow configuration and service properties to be bound via setter/getter
methods, not just via direct fields
> -------------------------------------------------------------------------------------------------------------------------
>
>                 Key: FELIX-227
>                 URL: https://issues.apache.org/jira/browse/FELIX-227
>             Project: Felix
>          Issue Type: Improvement
>          Components: iPOJO
>         Environment: Not relevant.
>            Reporter: Steven E. Harris
>
> iPOJO's binding of configuration properties and service properties directly to component
fields causes a few difficult situations that could be eased by optionally binding the reading
and writing to getter and setter methods instead.
> First, consider a component class with invariants that span multiple fields. iPOJO can
change the value of a field without the owning class knowing, depriving it the chance to update
dependent fields that participate in the invariant. Examples include recalculating and caching
an expensive result whenever some input field changes, or validating a changed value and updating
other fields in response.
> Second, at present iPOJO sets the value of bound fields before the component class constructor
runs. If the constructor attempts to initialize some fields, it may be inadvertantly overwriting
the initial values supplied by iPOJO. Writing the constructor to deliberately ignore values
that might be bound by iPOJO flies in the face of the very name of the project: a POJO isn't
supposed to know that it's being silently manipulated like this, and hence a POJO should be
written in standard form: Initialize variables to sane defaults, including constructor parameters,
expecting them to be overwritten later.
> Take for example a class that has two fields, one of which is bound to a configuration
property:
> public class Example {
>   private String bound;
>   private String dependent;
>   public Example() {
>     bound = null;
>     dependent = "empty";
>   }
>   public void setBound(String s) {
>     bound = s;
>     dependent = null == s || 0 == s.length() ? "empty" : "full";
>   }
>   public String getDependent() {
>     return dependent;
>   }
> }
> If written in this manner, the constructor mistakenly overwrites the initial iPOJO-provided
value for "bound" by initializing it to null. But to resist initializing "bound' is also dangerous;
how would one reading this code have any idea that "bound" might get set to a different value
before the constructor runs, or while the instance is live?
> Also, consider that if "bound' changes silently once the instance is live, "dependent"
will fall out of step, as the invariant maintained in setBound() can be violated.
> If iPOJO would allow property binding to optionally work by way of getter and setter
methods, one and a half of these problems could be avoided. The missing half relates to construction.
If we ask iPOJO to defer setting the "initial values" until the constructor completes, we
may have to defer some initialization that would use the values not yet available.
> Trying to write a POJO class that gets manipulated on the sly by iPOJO is proving to
be more tricky than just writing some of the ManagedServiceFactory code myself, as I'm forced
to adjust my would-be POJO service class to deal with these weird initialization and invariant
maintenance problems normally solved by member variable encapsulation. Perhaps we should look
at Spring's example that better acknowledges not just the technical possibilities, but the
logical difficulties in iPOJO's kind of silent injection and manipulation.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message