The Dragon Scroll

Be just and fear not.

GAE/JでStruts2を動かす

GAE/Jで、Strutsを動かそうとして、はまった。
「Struts1.2が動いた」「Strutsは動かない」などのさまざまな情報があって混乱したが、Struts2の動作が
確認できた。


Download a ReleaseからStrutsをダウンロード。
GAEのプロジェクトを作成後、Struts2のライブラリをWEB-INF/libに配置。
通常のstruts2アプリの準備を進める。割愛。
で、ローカルでGAEアプリを動かしてみるが…

WARNING: Caught OgnlException while setting property 'location' on type 'org.apache.struts2.dispatcher.ServletDispatcherResult'.
java.lang.IllegalAccessException: Method [public void org.apache.struts2.dispatcher.StrutsResultSupport.setLocation(java.lang.String)]
cannot be accessed.
   at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:508)
   at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:812) 

実行時にエラーとなる。ここでしばらく前に進めなくなる。
調べていると、GAE/JのGoogleGroupに以下の文章を見つける。

I am running locally using the Eclipse plugins. The first exception I
get is:

WARNING: Caught OgnlException while setting property 'location' on type
'org.apache.struts2.dispatcher.ServletDispatcherResult'.
java.lang.IllegalAccessException: Method [public void
org.apache.struts2.dispatcher.StrutsResultSupport.setLocation(java.lang.String)]
cannot be accessed.
   at ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:508)
   at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:812) 

同じだ。で、優秀なこの人は、解決策を見つけていた。

It involves creating a servlet context listener that invokes OgnlRuntime.setSecurityManager(null). 

Google グループ
OgnlRuntimeのinvokeMethodで、SecurityManagerのcheckPermissionメソッドを実行している。
SecurityManagerを無効にする。これはServletContextListenerに実装する。

public class InitListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    public void contextInitialized(ServletContextEvent sce)  {
        OgnlRuntime.setSecurityManager(null);
    }
  // 以下、略

web.xmlに、Listenerを追加。

    
        com.papanda.InitListener
    

これで、ローカルの環境で動くようになった。GAE/Jの本番環境でも動く。作業完了。