s2とDWRの連携

s2を使ってDWRと連携したかったので、以下のページを参考にした。
http://www3.vis.ne.jp/~asaki/p_diary/diary.cgi?Date=2005-11-04#2005110400
DWRを使う - n-ichimuraの日記

Sessionの情報などにアクセスしたかったのだが、2番目の記事の方法だとgetTypeがDWRの初期化時点で呼び出されてうまく動かなかったのと、手動でBeanにコンポーネントを詰めているのがイヤンだったので改造した。

package sample.dwr;

import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
import uk.ltd.getahead.dwr.WebContextFactory;
import uk.ltd.getahead.dwr.create.AbstractCreator;

public class S2Creator extends AbstractCreator {
    
    private String beanName;
    private Class clazz;
    
    public void setBeanName(String beanName){
        this.beanName = beanName;
    }
    
    public Class getType() {
        if(clazz==null){
            try {
                clazz = SingletonS2ContainerFactory.getContainer().getComponentDef(beanName).getComponentClass();
            } catch(Exception ex){
                clazz = Object.class;
            }
        }
        return clazz;
    }

    public Object getInstance() throws InstantiationException {
        S2Container container = SingletonS2ContainerFactory.getContainer();
        WebContext webContext = WebContextFactory.get();
        container.setRequest(webContext.getHttpServletRequest());
        container.setResponse(webContext.getHttpServletResponse());
        return container.getComponent(beanName);
    }
}

これでだいぶシンプルに連携できた。