You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Adam Zimowski <zi...@gmail.com> on 2006/02/12 20:16:15 UTC

LinkFactory problem

Hello,

I'm trying automatically generate a URL for image source on the custom
service I wrote which provides Captcha image through jCaptcha. It
worked when I had a hard coded URL <img src="?service=captcha"/> but I
want the URL to be provided by the service itself. So I created a
Hivemind service-point to set linkFactory, but when I try to run the
application I'm getting NullPointerException from service's getLink()
method when it tries to return it thru the linkFactory. Apparently
linkFactory is never getting set, because the setter for it is never
called (I would have seen my log4j debug otherwise). I also noticed
that the signature of getLink has changed with version 4.0 as it no
longer takes IRequestCycle as a parameter. Could that have something
to do with my NullPointer problem? I never coded Tapestry 3.0 as I am
new to Tapestry, and 4.0 is all I know. Is there something obvious I
may be missing?

hivemodule.xml
<?xml version="1.0"?>
<module id="org.opendating" version="1.0.0">
    <service-point id="CaptchaService"
interface="org.apache.tapestry.engine.IEngineService">
        <invoke-factory>
            <construct class="org.opendating.services.CaptchaService">
                <set-object property="linkFactory"
value="service:tapestry.url.LinkFactory"/>
            </construct>
        </invoke-factory>
    </service-point>
    <contribution configuration-id="tapestry.services.ApplicationServices">
        <service name="captcha"
object="instance:org.opendating.services.CaptchaService"/>
    </contribution>
</module>
------------------------------------------------------------------------------------------
public class CaptchaService implements IEngineService {

    // relevant part

    private LinkFactory linkFactory;

    public ILink getLink(boolean aIsPost, Object aParameter) {

        String captchaId = (String) aParameter;
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("service", getName());
        parameters.put("captchaId", captchaId);
        if (_aLog.isDebugEnabled())
            _aLog.debug("captchaId: " + captchaId);

        return linkFactory.constructLink(this, aIsPost, parameters, false);
    }

  // etc..
----------------------------------------------------------------------------------------------
public abstract class Captcha extends BaseComponent {

    private static Logger _pLog;
    static {
        _pLog = LogEngine.getPageLogger();
    }

    @InjectObject("engine-service:captcha")
    public abstract IEngineService getCaptchaService();

    public String getImageUrl() {

        String captchaId = new Long(new Date().getTime()).toString();
        if(_pLog.isDebugEnabled()) _pLog.debug("captchaId: " + captchaId);

        return getCaptchaService().getLink(false, captchaId).getURL();
    }

}
----------------------------------------------------------------------------------------------

<html>
<head>
<title>Captcha Component</title>
</head>
<body>
<h1>@Captcha</h1>
<p/>
Generates an image with distorted text which only humans can easily read but
automated programs have trouble with. Next to the image is a text input box
which accepts the string of text that matches that displayed on the image.
The text entered is then compared to that on the image and validated.
<hr/>
 <form>

 <div jwcid="$content$">

 <!-- Captcha component -->
  <img jwcid="@Any" src="ognl:getImageUrl()"/>
 <!-- EndOf: Captcha -->

 </div>

 </form>
</body>
</html>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org