users@glassfish.java.net

Re: injecting com.sun.enterprise.config.serverbeans.Domain to osgi bundle

From: Pavel Bucek <Pavel.Bucek_at_Sun.COM>
Date: Wed, 29 Jul 2009 10:53:00 +0200

Hello Marina,

thanks for the hint! It seems to be working :-)

for future reference:

add dependencies:

        <dependency>
            <groupId>org.glassfish.common</groupId>
            <artifactId>internal-api</artifactId>
            <version>3.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.sun.enterprise</groupId>
            <artifactId>hk2</artifactId>
        <version>0.3.9</version>
        <scope>provided</scope>
        </dependency>



code:

import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.component.Habitat;
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.Domain;

....

    private String getApplicationName(String contextRoot) {

        Habitat habitat = Globals.getDefaultHabitat();

        Domain domain = habitat.getInhabitantByType(Domain.class).get();

        List<Application> applicationList =
domain.getApplications().getApplications();

        for(Application app : applicationList) {

            if(app.getContextRoot().equals(contextRoot)) {
                return app.getName();
            }
        }
       
        return null;
    }



Pavel



Marina Vatkina wrote:
> Pavel,
>
> Will getting habitat from Globals help?
>
> thanks,
> -marina
>
> Kedar Mhaswade wrote:
>>
>>
>> Pavel Bucek wrote:
>>
>>> Kedar Mhaswade wrote:
>>>
>>>> Pavel Bucek wrote:
>>>>
>>>>>
>>>>> Hello,
>>>>>
>>>>> I have an osgi bundle and I need to determine application name
>>>>> from context root. I can do something like:
>>>>>
>>>>> public static String getAppName(String contextRoot) {
>>>>> if (contextRoot == null)
>>>>> return null;
>>>>> // first check in web modules
>>>>> List<WebModule> lm =
>>>>> domain.getApplications().getModules(WebModule.class);
>>>>> for (WebModule wm : lm) {
>>>>> if (contextRoot.equals(wm.getContextRoot())) {
>>>>> return (wm.getName());
>>>>> }
>>>>> }
>>>>> // then check under applications (introduced in V3 not j2ee
>>>>> app)
>>>>> List<Application> la =
>>>>> domain.getApplications().getModules(Application.class);
>>>>> for (Application sapp : la) {
>>>>> if (contextRoot.equals(sapp.getContextRoot())) {
>>>>> return (sapp.getName());
>>>>> }
>>>>> }
>>>>> return null;
>>>>> }
>>>>>
>>>>>
>>>>> but I dont have a instance of Domain. Can it be injected somehow
>>>>> like in hk2 class with @Service annotation?
>>>>
>>>>
>>>> I believe
>>>> @Inject
>>>> volatile Domain domain = null;
>>>>
>>>> should work. Have you tried it?
>>>
>>> Yes, I did but domain is not injected.. I thought that these
>>> injections works only in @Services and its because the are
>>> instanciated in some special way. This is not my case, its service
>>> (declared in META.INF/services) but not hk2 service.
>>>
>>> (and.. I tried to annotate class with @Service annotation and domain
>>> was still null).
>>
>>
>> Oh I did not know that it's not an @Service. My understanding is that it
>> needs to be an hk2 @Service.
>>
>> I could be wrong here, but annotating the class as @Service
>> alone is not enough. Your module's "build" needs to use the
>> hk2-maven-plugin to
>> generate the hk2 magic for classes annotated as @Service.
>>
>>>
>>> If it helps - I instanciate my "service" via ServiceFinder:
>>> https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/jersey-core/src/main/java/com/sun/jersey/spi/service/ServiceFinder.java?rev=2400&view=markup
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>