次にHelloGBeanの冒頭です(リスト3)。HelloGBeanもGBeanLifcycleのほかにHelloを実装しています。これはクライアントコードから呼ばれるインタフェースです。ちなみに、GBeanであることを知らなくても良いようにJNDI*経由で取得できるように15行目でContextにバインドしています。
1: public class HelloGBean implements Hello, GBeanLifecycle {
2: private static GBeanInfo createGBeanInfo() {
3: final GBeanInfoBuilder builder = new GBeanInfoBuilder(HelloGBean.class);
4: builder.addAttribute("contextName", String.class, true);
5: builder.addReference("invoker", Invoker.class);
6: builder.addAttribute("objectName", String.class, false);
7: builder.addAttribute("kernel", Kernel.class, false);
8: builder.setConstructor(new String[]{"contextName", "invoker", "objectName", "kernel"});
9: return builder.getBeanInfo();
10: }
11: public HelloGBean(String contextName, Invoker invoker, String objectName, Kernel kernel) {
12: log.debug("parameter objectName: " + objectName);
13: }
14: public synchronized void doStart() throws Exception {
15: GeronimoContextManager.bind(contextName, this);
16: }
さて、ここで注目すべきは、5、8、11行目です。これでHelloGBeanを生成するときにInvokerインスタンスが必要であり、第2引数として渡されることを示しています。ほかに6行目で「objectName」、7行目で「kernel」という属性が現れていますが、これは何でしょうか? 実はGeronimoでは、magic属性というものが定義されており、「objectName」は自分自身の(JMX仕様の)ObjectNameを表す文字列、「kernel」はGeronimoのカーネルをそれぞれ取得できる属性です。この属性をプランファイルに書く必要はありません。HelloGBean用のプランファイルはリスト4のようになります。
1: <configuration ..
2: configId="configure/helloGBean"
3: parentId="configure/rubyGBean">
4: <gbean name="gbean/helloGBean" class="HelloGBean">
5: <attribute name="contextName">/sample/helloGBean</attribute>
6: <reference name="invoker">
7: <domain>geronimo.server</domain>
8: <server>geronimo</server>
9: <application>null</application>
10: <module>configure/rubyGBean</module>
11: <type>GBean</type>
12: <name>gbean/rubyGBean</name>
13: </reference>
14: </gbean>
15:</configuration>
3行目では、HelloGBeanがRubyGBeanに依存するので親子関係を定義しています。次に注目すべきところとして、6〜13行目があります。これはRubyGBeanのObjectNameを書き下したものです。
ここまでできれば、後はクライアントコードです。これはHelloServlet(リスト5)でHelloインタフェースをlookupするようにしてみました。そのため、geronimo-application.xml(リスト6)、geronimo-web.xml(リスト7)でHelloGBean用のコンフィギュアを親に持つようにしています。
public class HelloServlet extends HttpServlet implements Servlet {
private Hello getHello() {
try {
return (Hello) new InitialContext().lookup("geronimo:/sample/helloGBean");
} catch (NamingException e) { throw new RuntimeException(e); }
}
}
<application ... configId="sample.enterprize/sample.enterprize" parentId="configure/helloGBean">
<web-app ... configId="sample.web/sample.web" parentId="configure/helloGBean">
いよいよ次回が本連載の最終回となります。連載第3回、第4回、そして今回解説した内容に基づいて実際に手を動かし、挙動を確かめてみましょう。
Java Naming and Directory Interface。Javaでネーミングおよびディレクトリ機能を利用できるようにするAPI。
本記事は、オープンソースマガジン2006年5月号「注目のJ2EEサーバー Apacheの戦士Geronimoが持つ実力」を再構成したものです。
日本アイ・ビー・エム システムズエンジニアリング株式会社
万仲龍樹・中山清喬・杉田直哉・津田嘉孝
日本アイ・ビー・エム システムズ・エンジニアリング株式会社(ISE)は、日本アイ・ビー・エムグループにおけるSE技術者の専門家集団として1992年7月に設立された。
発足以来、IBM製品を中心とした難易度の高い複雑なシステム構築や先進技術の適用場面において、卓越したITスキルによりお客様と開発現場を支援してきた。
近年は、仮想化技術・グリッドコンピューティング・Web 2.0・オープンソースソフトウェア等々の先進技術分野での支援も展開している。
Copyright © ITmedia, Inc. All Rights Reserved.