必要な環境 †今回は以下の環境で開発・確認を行っています。
作成するプラグインの概要 †今回作成するGoogleプラグインの概要はおおむね下の通りです。
プラグインの構成 †作成するGoogleプラグインは以下のようなプラグインで構成されています。
上のプラグインのうち、nu.mine.kino.plugin.log4j,nu.mine.kino.plugin.axisに関してはライブラリ的な扱いなので、詳細は割愛させていただきます。詳細はソースコードをご参照ください。またこの2プラグインは、Googleプラグインを作成していく上で使用しますので、あらかじめaxisとlog4jのプラグイン・プロジェクトをワークスペースにインポートしておいてください。 またGoogle Web APIs Developer's KitにはすでにWeb Services Proxyが同梱されていますが、今回はお勉強をかねてwsdlファイルからWeb Ser vices Proxyを自動生成してみました。詳細は割愛させていただきますがAxisのライブラリにクラスパスを通して java org.apache.axis.wsdl.WSDL2Java -p nu.mine.kino.googleapis GoogleSearch.wsdl とすることでProxyのJavaソースを作成する事ができます。今回はこのProxyを使ってGoogle検索を行いたいと思います。 まずはCoreをつくる †今回はGUIに依存しないコアの部分を作成します。 プラグイン・プロジェクトの作成 †まずはGUIに依存しない、コアのプラグインを作ります。Eclipseのウィザードでプラグイン開発>プラグイン・プロジェクトを選択します。 プラグイン・プロジェクトのページ
プラグイン・コンテンツのページ
終了をクリックします。以上でプラグインのプロジェクトが作成できました。 Proxyをソースディレクトリに配置 †先に作成しておいたProxy (nu.mine.kino.googleapisパッケージのソースコード)のソースをソースディレクトリに置いておきます。 plugin.xml,MANIFEST.MFを記述する †作成したプラグイン・プロジェクトに設定を追加していきます。作成したプロジェクトのMETA-INF/MANIFEST.MFをマニフェストエディタで開きます。依存関係タブを開いて、以下のプラグインを必須プラグインに追加しておきます。 org.eclipse.core.runtime nu.mine.kino.plugin.axis nu.mine.kino.plugin.log4j 次にランタイムタブを開いて、エクスポートされるパッケージに、 nu.mine.kino.googleapis nu.mine.kino.plugin.google.core を追加しておきます。 そのほか、プラグイン名やバージョンなど多少変更していますが、結局MANIFEST.MFは以下のようになりました。 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: nu.mine.kino.plugin.google.core Bundle-Version: 0.1.0 Bundle-Activator: nu.mine.kino.plugin.google.core.GoogleCorePlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, nu.mine.kino.plugin.log4j, nu.mine.kino.plugin.axis Eclipse-AutoStart: true Bundle-Vendor: %providerName Export-Package: nu.mine.kino.googleapis, nu.mine.kino.plugin.google.core プラグインにビジネスロジックを実装する †このプラグインで必要なビジネスロジックはGoogleの検索機能を呼び出す事ですが、Google Web APIsはスペルチェックを行う便利な機能があるので、
このメソッドをプラグインクラスnu.mine.kino.plugin.google.core.GoogleCorePlugin?に実装していきます。 まずはログ出力用を行うクラスLoggerと、Google検索を行うサービスインタフェースGoogleSearchPort_PortType?をフィールドに定義します。 private static final Logger logger = Logger.getLogger(GoogleCorePlugin.class); private GoogleSearchPort_PortType googleSearch; 検索メソッドやスペルチェックメソッドの実装は以下の通りです。 検索メソッドの実装のソース /**
* 引数の文字列でGoogle検索を行います。エラーが発生した場合nullが返るか、例外を投げます。
*
* @param myGoogleKey Googleのキー
* @param searchText 検索文字列
* @return GoogleSearchResult
* @throws CoreException 何らかのエラーが発生したとき。
*/
public GoogleSearchResult search(String myGoogleKey, String searchText)
throws CoreException {
logger.debug("search(String) - start");
GoogleSearchResult result = null;
if (googleSearch != null) {
try {
result = googleSearch.doGoogleSearch(myGoogleKey, searchText,
0, 10, false, "", false, "lang_ja", "", "");
} catch (RemoteException e) {
logger.error(e);
log(e);
IStatus status = new Status(IStatus.ERROR, getPluginId(),
IStatus.ERROR, e.toString(), e);
throw new CoreException(status);
}
}
logger.debug("search(String) - end");
return result;
}
スペルチェックメソッドの実装のソース /**
* 引数の文字列にスペルミスがないかをチェックするメソッドです。
* スペルミスがある場合、候補を返します。スペルミスがない場合、nullが返ります。
*
* @param myGoogleKey
* @param text
* @return
*/
public String getSpellingSuggestion(String myGoogleKey, String text) {
logger.debug("getSplellingSuggestion(String, String) - start");
String suggestionText = null;
try {
suggestionText = googleSearch.doSpellingSuggestion(myGoogleKey,text);
} catch (Exception e) {
// 文字によっては例外が発生するので、その場合は
// とりあえず候補がなかったことにして通り過ぎちゃう
logger.warn(e);
logger.warn("スペルミス補正でエラーが発生しました。");
}
logger.debug("getSplellingSuggestion(String, String) - end");
return suggestionText;
}
またログ出力用のメソッドやプラグインIDを返すメソッドを追加します。 public static void log(String message, Exception e) {
IStatus status = new Status(IStatus.ERROR, getPluginId(),
IStatus.ERROR, message, e);
getDefault().getLog().log(status);
}
public static void log(String message) {
log(message, null);
}
public static void log(Exception e) {
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String message = stringWriter.getBuffer().toString();
log(message, e);
}
プラグインIDを返すメソッド public static String getPluginId() {
return getDefault().getBundle().getSymbolicName();
}
続いて、startメソッドでサービスインタフェースを取得するようoverrideします。 public void start(BundleContext context) throws Exception {
logger.debug("start(BundleContext) - start");
super.start(context);
// Web Services Proxyの取得
try {
GoogleSearchServiceLocator locator = new GoogleSearchServiceLocator();
googleSearch = locator.getGoogleSearchPort();
} catch (ServiceException e) {
logger.error(e);
log(e);
}
logger.debug("start(BundleContext) - end");
}
以上でプラグインクラスにビジネスロジックを定義することができました。 テストプラグインの作成 †さて、作成したプラグインのメソッドをテストして今回は終わりにしようと思います。まずはテスト用のプラグイン・プロジェクトを作成します。 プロジェクト名:nu.mine.kino.plugin.google.core.test テスト用プラグインには、テスト対象のプラグインnu.mine.kino.plugin.google.coreとorg.junitを依存プラグインに追加しておきます。MANIFEST.MFは以下のようになりました。 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Test プラグイン Bundle-SymbolicName: nu.mine.kino.plugin.google.core.test Bundle-Version: 1.0.0 Bundle-Activator: nu.mine.kino.plugin.google.core.test.TestPlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.ui,org.eclipse.core.runtime, nu.mine.kino.plugin.google.core, org.junit Eclipse-AutoStart: true テストクラスを作成する †メソッドを実装したクラスnu.mine.kino.plugin.google.core.GoogleCorePlugin?をパッケージ・エクスプローラで右クリックし、新規>>その他を選択しウィザードを開きます。ウィザードでJUnitテスト・ケースを選択します。junit.jarをビルドパスに追加するかをEclipseが聞いてきますが、テストクラスはテスト用プラグインに配置するので、いいえを選択します。つづいてテストクラスを作成するダイアログが開くので、ソース・フォルダをnu.mine.kino.plugin.google.core.test/sourceに変更します。 次へをクリックするとテストメソッドの追加画面になりますが、先のメソッド getSplellingSuggestion(String myGoogleKey, String text) search(String myGoogleKey, String searchText) にチェックして終了をクリックします。 テストクラスGoogleCorePluginTest?には以下のようにメソッドを実装しました。 searchのテストメソッド public void testSearch() {
try {
GoogleSearchResult result = GoogleCorePlugin.getDefault().search(
"xxxxxxxxxxxx", "Eclipse");
// ↑Google SOAP Search API のサイトで取得したキー値をセットする
ResultElement[] resultElements = result.getResultElements();
for (int i = 0; i < resultElements.length; i++) {
ResultElement element = resultElements[i];
System.out.println(element.getTitle() + ":" + element.getURL());
}
} catch (CoreException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
getSplellingSuggestion?のテストメソッド public void testGetSplellingSuggestion() {
String spellingSuggestion = GoogleCorePlugin.getDefault()
.getSpellingSuggestion("xxxxxxxxxxxx","Eclipse");
if (spellingSuggestion != null) {
System.out.println("修正候補は:" + spellingSuggestion);
} else {
System.out.println("修正候補はありません");
}
spellingSuggestion = GoogleCorePlugin.getDefault()
.getSpellingSuggestion("xxxxxxxxxxxx","Eclipsee");
if (spellingSuggestion != null) {
System.out.println("修正候補は:" + spellingSuggestion);
} else {
System.out.println("修正候補はありません");
}
}
"xxxxxxxxxxxx"の箇所はあらかじめGoogle SOAP Search API のサイトで取得しておいたキーコードを設定してください。 さて、このテストメソッドを実行します。パッケージ・エクスプローラでGoogleCorePluginTest?を選択し、Eclipseのツールバーから実行>>JUnit プラグイン・テストを選択します。Eclipseが起動して、Googleプラグインのメソッドが実行され、コンソールに実行結果が表示されました。確かにプラグインが実行されていることが確認できました。 JUnit実行結果 2006-06-25 00:15:55,203 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - start(BundleContext) - start 2006/06/25 0:15:55 org.apache.axis.utils.JavaUtils isAttachmentSupported 警告: 要求されたクラス(javax.activation.DataHandler と javax.mail.internet.MimeMultipart)が見つかりません。Attachmentサポートが利用できません。 / [en]-(Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.) 2006-06-25 00:15:56,312 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - start(BundleContext) - end 2006-06-25 00:15:56,437 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - search(String) - start 2006-06-25 00:15:59,046 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - search(String) - end --- 以下、検索結果 --- エクリプス:http://eclipsewiki.net/eclipse/ 富士通テン : FUJITSU TEN Japan:http://www.fujitsu-ten.co.jp/ <b>ECLIPSE</b>:http://www.fujitsu-ten.co.jp/eclipse/index.html @IT:連載 <b>Eclipse</b>を使おう!(1): http://www.atmarkit.co.jp/fjava/rensai2/eclipse01/eclipse01.html @IT:<b>Eclipse</b>を使おう(1): http://www.atmarkit.co.jp/fjava/rensai3/eclipse31_01/eclipse31_01_1.html @IT:<b>Eclipse</b>ではじめるプログラミング(1): http://www.atmarkit.co.jp/fjava/rensai3/eclipsejava01/eclipse01.html www.<b>eclipse</b>-td.com:http://www.eclipse-td.com/ LIVE! <b>ECLIPSE</b> 2006:トップページ:http://www.live-eclipse.org/ <b>eclipse</b>を使ってみよう:http://muimi.com/j/eclipse/ <b>ECLIPSE</b> 桐原いづみ+KEIG Website:http://www.uni-sex.info/ --- 以上、検索結果 --- 2006-06-25 13:36:54,281 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - start 2006-06-25 13:36:54,828 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - end 修正候補はありません 2006-06-25 13:36:54,828 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - start 2006-06-25 13:36:55,250 [main] DEBUG nu.mine.kino.plugin.google.core.GoogleCorePlugin - getSpellingSuggestion(String, String) - end 修正候補は:Eclipse <- スペルミスを補正してくれている おわりに †今回はEclipseプラグイン開発のうち、GUIに依存しないコアな部分の開発手順をご紹介しました。Eclipseプラグインの開発といっても、通常のJavaプロジェクトやJ2EEのプロジェクトのやり方とそんなに変わらないことがご理解いただけたと思います。また単体テスト環境もEclipseに統合されていることもご紹介しました。 つぎは UI部のロジック を作成します。 参考資料 †この記事は 現在のアクセス:19222 |