-
1. Re: Lookup up a POJO service without injecting it inside -beans.xml?
alesj Jan 26, 2010 3:16 AM (in response to martinn)I cannot gather from your description on how exactly you're trying to do this.
e.g. where would you need to do this lookup (ok, from some interface method that cannot be changed)?
Why a simple setter injection doesn't work for you?
Of either the service you're loooking for or the Controller instance.
-
2. Re: Lookup up a POJO service without injecting it inside -beans.xml?
martinn Jan 26, 2010 9:17 PM (in response to alesj)Zdravo Ales...
here's my classes:
package com.raf.test;
import org.jboss.kernel.spi.dependency.KernelControllerContext;
import org.jboss.kernel.spi.dependency.KernelControllerContextAware;
import org.jboss.logging.Logger;public class ServiceA implements KernelControllerContextAware {
private KernelControllerContext controllerContext;
private Logger log;
private String val;
private ServiceB svcB;public ServiceA() {
log = Logger.getLogger(getClass().getName());
}
public String getValue() {
return val;
}public void setValue(String val) {
this.val = val;
}
public ServiceB getServiceB() {
return this.svcB;
}
public void setServiceB(ServiceB val) {
this.svcB = val;
}public void init() {
try {
log.info("lookup = " + controllerContext.getController().getInstalledContext("SvcB"));
} catch (Throwable e) {
e.printStackTrace();
}
log.info("init-ed");
}
public void create() {
log.info("created");
}
public void start() {
log.info("started");
}
public void stop() {
log.info("stopped");
}
public void destroy() {
log.info("destroyed");
}@Override
public void setKernelControllerContext(KernelControllerContext arg0)
throws Exception {
this.controllerContext = arg0;
}@Override
public void unsetKernelControllerContext(KernelControllerContext arg0)
throws Exception {
this.controllerContext = null;
}
}package com.raf.test;
import org.jboss.logging.Logger;
public class ServiceB {
private Logger log;
private String val;public ServiceB() {
log = Logger.getLogger(getClass().getName());
}
public String getValue() {
return val;
}public void setValue(String val) {
this.val = val;
}public void init() {
log.info("init-ed");
}
public void create() {
log.info("created");
}
public void start() {
log.info("started");
}
public void stop() {
log.info("stopped");
}
public void destroy() {
log.info("destroyed");
}
}and the descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
xmlns="urn:jboss:bean-deployer:2.0"><bean name="SvcA" class="com.raf.test.ServiceA">
<install method="init"/>
</bean>
<bean name="SvcB" class="com.raf.test.ServiceB">
<install method="init"/>
</bean>
</deployment>The "getInstalledContext("SvcB")" from ServiceA.java fails. It suggest some sort of scoping is at play because if I look up SvcA from ServiceA, it works just fine. I want to be able to look up a service just the same as if it is injected to me via "<inject bean=" from any service. Is this possible? Thanks!
Martin
-
3. Re: Lookup up a POJO service without injecting it inside -beans.xml?
alesj Jan 27, 2010 10:20 AM (in response to martinn)Zdravo :-)
The "getInstalledContext("SvcB")" from ServiceA.java fails. It suggest some sort of scoping is at play because if I look up SvcA from ServiceA, it works just fine. I want to be able to look up a service just the same as if it is injected to me via "<inject bean=" from any service. Is this possible? Thanks!
No scoping. ;-)
I think I know what the problem is.
You're doing the lookup too soon.
At the moment you try to find installed SvcB, that one is not installed yet.
You can either
* try to do lookup via getContext("SvcB", null)
* make sure when SvcA invoked init, SvcB is already fully installed; SvcA depends on SvcB at Installed state
If this is not it, post the stack trace you're catching in init.
-
4. Re: Lookup up a POJO service without injecting it inside -beans.xml?
martinn Jan 27, 2010 2:26 PM (in response to alesj)Ales -
getContext("SvcB", null) worked great, and so did the depends - the only caveat with the depends that I experienced in my other code (which I can't paste verbatim) is a circular dependency. However, the manual, thankfully, is clear on how to resolve those by depending on a different state (e.g. Instantiated) than 'installed', so I was able to resolve it that way too.
hvala za vaš nasvet,
Martin
p.s. I'm from Macedonia (ex-YU).
-
5. Re: Lookup up a POJO service without injecting it inside -beans.xml?
alesj Jan 28, 2010 6:45 AM (in response to martinn)Why do you actually have to go over KernelControllerContextAware interface?
Why not inject the service directly, via setter?
Or in the worst case, inject the Controller directly.
-
6. Re: Lookup up a POJO service without injecting it inside -beans.xml?
martinn Jan 29, 2010 4:07 PM (in response to alesj)That's what I did eventually. I had a misunderstanding with the software architect - I understood him that I wasn't supposed to modify his interface, which I thought meant, no adding of injection setters/getters on the implementation classes - thus I asked you how to get at the kernelcontrollercontext via the locator pattern... Turns out, after talking to him, he only meant not modifying the _actual_ interface (public interface xxx...) and he was fine with me adding injectors (setters) on the impl....
He was trying to develop some sort of a 'generic' interface for some of our products that he didn't want bound to anything jboss-y, that we could reuse anywhere, outside jboss or any other container.... long story that I can't quite fill you in on unless you come work for us .
Hvala.
Martin
-
7. Re: Lookup up a POJO service without injecting it inside -beans.xml?
dimonv Jun 3, 2010 3:34 AM (in response to alesj)Hi,
I'm interresting on the same use case and tried to implement a kind of bean lookup. But in the version 2.0.6.GA of MC there is no method org.jboss.kernel.spi.dependency.KernelController.getContext(String , ...).
Is there an API for looking up MC for beans?
Thanks
-
8. Re: Lookup up a POJO service without injecting it inside -beans.xml?
alesj Jun 3, 2010 4:05 AM (in response to dimonv)I'm interresting on the same use case and tried to implement a kind of bean lookup. But in the version 2.0.6.GA of MC there is no method org.jboss.kernel.spi.dependency.KernelController.getContext(String , ...).
Is there an API for looking up MC for beans?