Advice on how to achieve setting properties on an arbitrary class

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Advice on how to achieve setting properties on an arbitrary class

Austin Brooks
What is the best way to achieve something that can resemble the following?

<healthCheck class="....urlCheck" serverPort="8080" path="/ping" />
<healthCheck class="....portCheck" serverPort="8080" />

Basically, I am trying to make an extendable health check task that takes a
class and is then configured using additional attributes or elements. I am
not overly restricted to a certain syntax.  The only real requirements I
have are:
1. Is extendable by passing any class to the class attribute
2. Custom class is not tied to ant, will pass configuration via hash map or
reflection property setting

So is there a way to configure this by sub-elements or defining a method
that is a collect all bucket for unknown attributes?

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Advice on how to achieve setting properties on an arbitrary class

Stefan Bodewig
On 2016-07-12, Austin Brooks wrote:

> What is the best way to achieve something that can resemble the following?

> <healthCheck class="....urlCheck" serverPort="8080" path="/ping" />
> <healthCheck class="....portCheck" serverPort="8080" />

> Basically, I am trying to make an extendable health check task that takes a
> class and is then configured using additional attributes or elements. I am
> not overly restricted to a certain syntax.  The only real requirements I
> have are:
> 1. Is extendable by passing any class to the class attribute
> 2. Custom class is not tied to ant, will pass configuration via hash map or
> reflection property setting

> So is there a way to configure this by sub-elements or defining a method
> that is a collect all bucket for unknown attributes?

There are two approaches, which one is better in your case is not
completely clear to me.

The first approach is to take advantage of as much of Ant as
possible. If the classes you want to use provide setters you can use
Ant's attribute setting logic[1].

(1) You typedef the classes

<typedef name="urlCheck" classname="....urlCheck"/>

(2) create an instance of it and let Ant set the properties

<urlCheck serverPort="8080" path="/ping" id="pingCheck"/>

(3) Use references to use them inside your task

<healthCheck refid="pingCheck"/>

or alternatively if there is a commin interface of your check classes
you could use an "void add(TheCommonInterface)" method in healthCheck
and nest them directly like in

<healthCheck>
  <urlCheck serverPort="8080" path="/ping"/>
</healthCheck>

The alternative approach is to implement
org.apache.tools.ant.DynamicAttribute or its extension
DynamicConfigurator (or the NS aware variants) in your task, collect the
attributes and configure your checks programmatically.

Stefan

[1] https://ant.apache.org/manual/develop.html#writingowntask

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...