4

私はJavaでTomcatサーバーを使用しており、キャッシュされたDataSourceオブジェクトにアクセスするために(mysql接続をプールするために)レストレットリソースからServletContextにアクセスできるようにしたいと考えていました。org.restlet.resource.ResourceにはContextオブジェクトが付属していますが、これはServletContextとはまったく関係ありません。それで、いくつかのグーグルの後で、私は以下を見つけました:

final String contextKey = "org.restlet.ext.servlet.ServletContext";
final String poolKey = "MyCachedDBPool";
final Map<String, Object> attrs = getContext().getAttributes();
final ServletContext ctx = (ServletContext) attrs.get(contextKey);
if (ctx == null) {
  throw new Exception("Cannot find ServletContext: " + contextKey);
}
final DataSource ds = (DataSource) ctx.getAttribute(poolKey);
if (ds == null) {
  throw new DetourQAException("DataSource not stored in context" 
    + poolKey + "attr");
}

ただし、ServletContextに対してはnullを返します。誰かがレストレットリソース内からServletContextに正常にアクセスしましたか?どのようにアクセスしましたか?

これが接続プールを行うための推奨される方法ではない場合、レストレットで接続プールを行うための最良の方法は何ですか?

4

1 に答える 1

6

これは、Restlet 2.0 より前の方法でした (実際には、2.0-M5 前後で変更されたと思います)。とにかく、あなたが今それをする方法は次のとおりです:

ServletContext sc = (ServletContext) getContext().getServerDispatcher().getContext().getAttributes().get( "org.restlet.ext.servlet.ServletContext" );

それが役立つことを願っています。

于 2010-11-30T02:25:34.647 に答える