Search code examples
javaspringosgihtmlunitblueprint

OSGi Aries Blueprint static field


I need to create a bean that takes a class instance as a constructor parameter. The useful values of this class instance are created by the class as static fields.

Specifically, I am trying to create WebClient beans from HtmlUnit. http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/WebClient.html

You can see that WebClient takes a no-arg constructor (do not want), as well as one that takes a BrowserVersion instance. http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/BrowserVersion.html

BrowserVersion defines the useful instances as static fields, eg: CHROME, FIREFOX_24, INTERNET_EXPLORER_11.

I need to reference one of these BrowserVersion instances in an Aries Blueprint bean.

Here is what the blueprint setup looks like:

<bean id="webClient" class="com.gargoylesoftware.htmlunit.WebClient">
    <argument>??</argument>
</bean>

I have tried:

<argument><bean class="com.gargoylesoftware.htmlunit.BrowserVersion.FIREFOX_24" /></argument>
<argument value="com.gargoylesoftware.htmlunit.BrowserVersion.FIREFOX_24" />

I also tried just creating a BrowserVersion bean in the blueprint config, and I can't seem to make that work either.

<bean id="webClient" class="com.gargoylesoftware.htmlunit.WebClient">
    <argument>
        <bean class="com.gargoylesoftware.htmlunit.BrowserVersion">
            <argument index="0" type="java.lang.String" value="Netscape" />
            <argument index="1" type="java.lang.String" value="5.0 (Windows)" />
            <argument index="2" type="java.lang.String" value="Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0" />
            <argument index="3" type="java.lang.Float"  value="24.0" />
        </bean>
    </argument>
</bean>

That should match a public constructor on BrowserVersion.

Any ideas what I am doing wrong on either count?


Solution

  • Worked around it by using a helper method.

    <bean id="webClient" class="com.stackoverflow.HtmlUnitHelper" factory-method="getChromeClient" />
    

    The helper method is simply:

    public static WebClient getChromeClient() {
        return new WebClient(BrowserVersion.CHROME);
    }
    

    Perhaps there is a better solution, but this works!