You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hivemind.apache.org by Olivier Bourgeois <ze...@wanadoo.fr> on 2005/02/05 18:35:32 UTC
Recursive call in AdaptaterRegistry ?
Hello all,
I'm playing with hivemind 1.1-alpha AdapterRegistry and now that I got
some simple example working, I'm asking myself a question I can't find
an answer :)
Is there a way to do- kind of - recursion with the AdaptaterRegistry? I
explain myself: suppose I want to create an XmlAdapter service with this
interface :
public interface XmlAdapter {
toXml (Object o);
}
I could define then :
public class StringAdapter {
public String toXml (Object o) {
String value = (String) o;
return value.trim();
}
}
This works fine for "converting" a String to XML.
But now suppose I got this object to adapt :
public class Name {
private String firstName;
private String lastName;
// + getters & setters
}
A great trick would be to be able to do something like this :
public class NameAdapter {
public String toXml (Object o) {
Name value = (Name) o;
String ret = new String("<Name><FirstName>");
// Here do some trick to get a StringAdapter from the registry
ret.append(stringAdapter.toXml(value.getFirstName()));
ret.append("</FirstName><LastName>");
ret.append(stringAdapter.toXml(value.getLastName()));
ret.append("</LastName></Name>");
return ret;
}
}
Of course I tried to inject the service in
itself but this leads only to nullpointerexception ( well, I expected
something like this anyway...).
So, is there a way to do this ? Or should I try another way ? ( with
the new Chain service maybe ? )
thanks in advance for any help/pointers,
Olivier.
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
Re: Recursive call in AdaptaterRegistry [Solved]
Posted by Olivier Bourgeois <ze...@wanadoo.fr>.
Thanks a lot !
I tried again today, works like a charm.
For the little story, in fact I was trying to do something like this :
<module id="test" version="1.0.0">
<configuration-point id="HtmlAdapters"
schema-id="hivemind.lib.AdapterRegistry"/>
<contribution configuration-id="HtmlAdapters">
<adapter class="java.lang.String"
object="instance:test..StringAdapter"/>
<adapter class="java.lang.Integer"
object="instance:test.IntegerAdapter"/>
</contribution>
<service-point id="HtmlAdapter"
interface="test.IHtmlAdapter">
<invoke-factory
service-id="hivemind.lib.AdapterRegistryFactory">
<construct configuration-id="HtmlAdapters" >
<service>HtmlAdapter</service>
</construct>
</invoke-factory>
</service-point>
</module>
which cannot work.
I didn't knew I could use object="service: ...." in the contribution
point and put each adapter in a service point. (BTW I found out that a
good sample for adapters is the new Tapestry AssetFactories).
This is radically changing the whole thing and making the
solution simple but powerfull ;)
Olivier.
Le Sun, 6 Feb 2005 09:30:52 -0500
Howard Lewis Ship <hl...@gmail.com> a écrit:
> On Sun, 6 Feb 2005 10:28:16 +0100, Olivier Bourgeois
> <ze...@wanadoo.fr> wrote:
> > Thanks for the quick answer Howard,
> >
> > I tried again to inject the service into itself, but I
> > stumbled onto
> > two problems again, I solved only the first one :
> >
> > - first you were right there was an error in the beginning
> > of
> > the console that I did not saw. It said that it was impossible to
> > instantiate my adapters.
>
> There's a "brittle" error handler that will throw a fatal exception at
> the first error, rather than just logging it. It may be a good
> practice to start using that.
>
> I prefer log-and-continue (the default), since I may be able to catch
> and fix multiple errors.
>
>
>
> > I finally found out that it was because I was missing a
> > constructor
> > without parameters ( I was using constructor only injection ) in my
> > adapter. I find this a little bit odd since I thought Java was
> > always providing one constrcutor by default even if not explicitly
> > written.
>
> Java compiler creates a public no-args constructor for you if and only
> if you don't
> define a constructor yourself. It's syntactic sugar.
>
>
> >
> > - second problem now that the adapters are instantiated
> > again, the
> > service is still not injected in itself. I tried all the ways I am
> > aware of (adding also getter/setter in my adapter) :
> > -
> > <construct><service>XmlAdapter</service></construct>
> > - <construct service-id-property="">... -
> > <construct><set-service
> > property=""service-id=""/>...
> >
> > But none of them is working : I got an "element not allowed here"
> > error in the console. So I had a look at the hivemodule.xml in the
> > hivemind-lib.jar, and for the service-point AdapterRegistryFactory
> > element the schema is as follow:
> >
> > <parameters-schema>
> > <element name="construct">
> > <attribute name="configuration-id"
> > translator="configuration" required="true"> A
> > configuration utilizing the
> > hivemind.lib.AdapterRegistry schema, which defines the
> > classes and matching adapters. </attribute>
> > <conversion
> > class="org.apache.hivemind.lib.adapter.AdapterRegistryParameter">
> > <map attribute="configuration-id" property="contributions"/>
> > </conversion>
> > </element>
> > </parameters-schema>
> >
> > So I suppose that means I can't because the schema definition for
> > AdaptaterRegistry is a subset of BuilderFactory's one.
> >
> > Or am I missing some other way to do this ?
> >
>
> I think you are missing something here. You have a specific adapter,
> implemented as a service, that needs to know about the adapter
> registry which it is registered into.
>
> Therefore, you should inject the adapter registry service into the
> adapter. So if AR is the service-id, your service-point shoudl look
> like:
>
> <service-point id="MyAdaptor" interface="....">
> <invoke-factory>
> <construct class="...MyAdaptorImpl".>
> <set-service property="registry" service-id="AR"/>
> </construct>
> </invoke-factory>
> </service-point>
>
> Or the correct constructor-inject variation thereof.
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
Re: Recursive call in AdaptaterRegistry ?
Posted by Howard Lewis Ship <hl...@gmail.com>.
On Sun, 6 Feb 2005 10:28:16 +0100, Olivier Bourgeois <ze...@wanadoo.fr> wrote:
> Thanks for the quick answer Howard,
>
> I tried again to inject the service into itself, but I stumbled onto
> two problems again, I solved only the first one :
>
> - first you were right there was an error in the beginning of
> the console that I did not saw. It said that it was impossible to
> instantiate my adapters.
There's a "brittle" error handler that will throw a fatal exception at
the first error, rather than just logging it. It may be a good
practice to start using that.
I prefer log-and-continue (the default), since I may be able to catch
and fix multiple errors.
> I finally found out that it was because I was missing a constructor
> without parameters ( I was using constructor only injection ) in my
> adapter. I find this a little bit odd since I thought Java was always
> providing one constrcutor by default even if not explicitly written.
Java compiler creates a public no-args constructor for you if and only
if you don't
define a constructor yourself. It's syntactic sugar.
>
> - second problem now that the adapters are instantiated again, the
> service is still not injected in itself. I tried all the ways I am aware
> of (adding also getter/setter in my adapter) :
> - <construct><service>XmlAdapter</service></construct>
> - <construct service-id-property="">...
> - <construct><set-service property=""service-id=""/>...
>
> But none of them is working : I got an "element not allowed here" error
> in the console. So I had a look at the hivemodule.xml in the
> hivemind-lib.jar, and for the service-point AdapterRegistryFactory
> element the schema is as follow:
>
> <parameters-schema>
> <element name="construct">
> <attribute name="configuration-id" translator="configuration"
> required="true"> A configuration utilizing the
> hivemind.lib.AdapterRegistry schema, which defines the classes
> and matching adapters. </attribute>
> <conversion
> class="org.apache.hivemind.lib.adapter.AdapterRegistryParameter">
> <map attribute="configuration-id" property="contributions"/>
> </conversion>
> </element>
> </parameters-schema>
>
> So I suppose that means I can't because the schema definition for
> AdaptaterRegistry is a subset of BuilderFactory's one.
>
> Or am I missing some other way to do this ?
>
I think you are missing something here. You have a specific adapter,
implemented as a service, that needs to know about the adapter
registry which it is registered into.
Therefore, you should inject the adapter registry service into the
adapter. So if AR is the service-id, your service-point shoudl look
like:
<service-point id="MyAdaptor" interface="....">
<invoke-factory>
<construct class="...MyAdaptorImpl".>
<set-service property="registry" service-id="AR"/>
</construct>
</invoke-factory>
</service-point>
Or the correct constructor-inject variation thereof.
> Olivier.
>
> Le Sat, 5 Feb 2005 15:52:24 -0500
> Howard Lewis Ship <hl...@gmail.com> a écrit:
>
> > You should be able to inject a service into itself. It's a proxy that
> > gets injected. Make sure there weren't any errors in the console.
> >
> >
> > On Sat, 5 Feb 2005 18:35:32 +0100, Olivier Bourgeois
> > <ze...@wanadoo.fr> wrote:
> > > Hello all,
> > >
> > > I'm playing with hivemind 1.1-alpha AdapterRegistry and now
> > > that I got
> > > some simple example working, I'm asking myself a question I can't
> > > find an answer :)
> > > Is there a way to do- kind of - recursion with the
> > > AdaptaterRegistry? I
> > > explain myself: suppose I want to create an XmlAdapter service with
> > > this interface :
> > >
> > > public interface XmlAdapter {
> > > toXml (Object o);
> > > }
> > >
> > > I could define then :
> > >
> > > public class StringAdapter {
> > > public String toXml (Object o) {
> > > String value = (String) o;
> > > return value.trim();
> > > }
> > > }
> > >
> > > This works fine for "converting" a String to XML.
> > > But now suppose I got this object to adapt :
> > >
> > > public class Name {
> > > private String firstName;
> > > private String lastName;
> > > // + getters & setters
> > > }
> > >
> > > A great trick would be to be able to do something like this :
> > >
> > > public class NameAdapter {
> > > public String toXml (Object o) {
> > > Name value = (Name) o;
> > > String ret = new String("<Name><FirstName>");
> > >
> > > // Here do some trick to get a StringAdapter from
> > > the registry
> > >
> > > ret.append(stringAdapter.toXml(value.getFirstName()
> > > )); ret.append("</FirstName><LastName>");
> > > ret.append(stringAdapter.toXml(value.getLastName())
> > > ); ret.append("</LastName></Name>");
> > > return ret;
> > > }
> > > }
> > >
> > > Of course I tried to inject the service in
> > > itself but this leads only to nullpointerexception ( well, I
> > > expected something like this anyway...).
> > >
> > > So, is there a way to do this ? Or should I try another way ? ( with
> > > the new Chain service maybe ? )
> > >
> > > thanks in advance for any help/pointers,
> > >
> > > Olivier.
> > >
> > > -------------------------------------------------------------------
> > > -- To unsubscribe, e-mail:
> > > hivemind-user-unsubscribe@jakarta.apache.org For additional
> > > commands, e-mail: hivemind-user-help@jakarta.apache.org
> > >
> > >
> >
> >
> > --
> > Howard M. Lewis Ship
> > Independent J2EE / Open-Source Java Consultant
> > Creator, Jakarta Tapestry
> > Creator, Jakarta HiveMind
> >
> > Professional Tapestry training, mentoring, support
> > and project work. http://howardlewisship.com
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
>
>
--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind
Professional Tapestry training, mentoring, support
and project work. http://howardlewisship.com
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
Re: Recursive call in AdaptaterRegistry ?
Posted by Olivier Bourgeois <ze...@wanadoo.fr>.
Thanks for the quick answer Howard,
I tried again to inject the service into itself, but I stumbled onto
two problems again, I solved only the first one :
- first you were right there was an error in the beginning of
the console that I did not saw. It said that it was impossible to
instantiate my adapters.
I finally found out that it was because I was missing a constructor
without parameters ( I was using constructor only injection ) in my
adapter. I find this a little bit odd since I thought Java was always
providing one constrcutor by default even if not explicitly written.
- second problem now that the adapters are instantiated again, the
service is still not injected in itself. I tried all the ways I am aware
of (adding also getter/setter in my adapter) :
- <construct><service>XmlAdapter</service></construct>
- <construct service-id-property="">...
- <construct><set-service property=""service-id=""/>...
But none of them is working : I got an "element not allowed here" error
in the console. So I had a look at the hivemodule.xml in the
hivemind-lib.jar, and for the service-point AdapterRegistryFactory
element the schema is as follow:
<parameters-schema>
<element name="construct">
<attribute name="configuration-id" translator="configuration"
required="true"> A configuration utilizing the
hivemind.lib.AdapterRegistry schema, which defines the classes
and matching adapters. </attribute>
<conversion
class="org.apache.hivemind.lib.adapter.AdapterRegistryParameter">
<map attribute="configuration-id" property="contributions"/>
</conversion>
</element>
</parameters-schema>
So I suppose that means I can't because the schema definition for
AdaptaterRegistry is a subset of BuilderFactory's one.
Or am I missing some other way to do this ?
Olivier.
Le Sat, 5 Feb 2005 15:52:24 -0500
Howard Lewis Ship <hl...@gmail.com> a écrit:
> You should be able to inject a service into itself. It's a proxy that
> gets injected. Make sure there weren't any errors in the console.
>
>
> On Sat, 5 Feb 2005 18:35:32 +0100, Olivier Bourgeois
> <ze...@wanadoo.fr> wrote:
> > Hello all,
> >
> > I'm playing with hivemind 1.1-alpha AdapterRegistry and now
> > that I got
> > some simple example working, I'm asking myself a question I can't
> > find an answer :)
> > Is there a way to do- kind of - recursion with the
> > AdaptaterRegistry? I
> > explain myself: suppose I want to create an XmlAdapter service with
> > this interface :
> >
> > public interface XmlAdapter {
> > toXml (Object o);
> > }
> >
> > I could define then :
> >
> > public class StringAdapter {
> > public String toXml (Object o) {
> > String value = (String) o;
> > return value.trim();
> > }
> > }
> >
> > This works fine for "converting" a String to XML.
> > But now suppose I got this object to adapt :
> >
> > public class Name {
> > private String firstName;
> > private String lastName;
> > // + getters & setters
> > }
> >
> > A great trick would be to be able to do something like this :
> >
> > public class NameAdapter {
> > public String toXml (Object o) {
> > Name value = (Name) o;
> > String ret = new String("<Name><FirstName>");
> >
> > // Here do some trick to get a StringAdapter from
> > the registry
> >
> > ret.append(stringAdapter.toXml(value.getFirstName()
> > )); ret.append("</FirstName><LastName>");
> > ret.append(stringAdapter.toXml(value.getLastName())
> > ); ret.append("</LastName></Name>");
> > return ret;
> > }
> > }
> >
> > Of course I tried to inject the service in
> > itself but this leads only to nullpointerexception ( well, I
> > expected something like this anyway...).
> >
> > So, is there a way to do this ? Or should I try another way ? ( with
> > the new Chain service maybe ? )
> >
> > thanks in advance for any help/pointers,
> >
> > Olivier.
> >
> > -------------------------------------------------------------------
> > -- To unsubscribe, e-mail:
> > hivemind-user-unsubscribe@jakarta.apache.org For additional
> > commands, e-mail: hivemind-user-help@jakarta.apache.org
> >
> >
>
>
> --
> Howard M. Lewis Ship
> Independent J2EE / Open-Source Java Consultant
> Creator, Jakarta Tapestry
> Creator, Jakarta HiveMind
>
> Professional Tapestry training, mentoring, support
> and project work. http://howardlewisship.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
Re: Recursive call in AdaptaterRegistry ?
Posted by Howard Lewis Ship <hl...@gmail.com>.
You should be able to inject a service into itself. It's a proxy that
gets injected. Make sure there weren't any errors in the console.
On Sat, 5 Feb 2005 18:35:32 +0100, Olivier Bourgeois <ze...@wanadoo.fr> wrote:
> Hello all,
>
> I'm playing with hivemind 1.1-alpha AdapterRegistry and now that I got
> some simple example working, I'm asking myself a question I can't find
> an answer :)
> Is there a way to do- kind of - recursion with the AdaptaterRegistry? I
> explain myself: suppose I want to create an XmlAdapter service with this
> interface :
>
> public interface XmlAdapter {
> toXml (Object o);
> }
>
> I could define then :
>
> public class StringAdapter {
> public String toXml (Object o) {
> String value = (String) o;
> return value.trim();
> }
> }
>
> This works fine for "converting" a String to XML.
> But now suppose I got this object to adapt :
>
> public class Name {
> private String firstName;
> private String lastName;
> // + getters & setters
> }
>
> A great trick would be to be able to do something like this :
>
> public class NameAdapter {
> public String toXml (Object o) {
> Name value = (Name) o;
> String ret = new String("<Name><FirstName>");
>
> // Here do some trick to get a StringAdapter from the registry
>
> ret.append(stringAdapter.toXml(value.getFirstName()));
> ret.append("</FirstName><LastName>");
> ret.append(stringAdapter.toXml(value.getLastName()));
> ret.append("</LastName></Name>");
> return ret;
> }
> }
>
> Of course I tried to inject the service in
> itself but this leads only to nullpointerexception ( well, I expected
> something like this anyway...).
>
> So, is there a way to do this ? Or should I try another way ? ( with
> the new Chain service maybe ? )
>
> thanks in advance for any help/pointers,
>
> Olivier.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: hivemind-user-help@jakarta.apache.org
>
>
--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind
Professional Tapestry training, mentoring, support
and project work. http://howardlewisship.com
---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-user-help@jakarta.apache.org