Top / Eclipse / プラグイン開発のTIPS集 / GooglePlugin / まずはCoreをつくる

まずはGUIに関係ない、Coreな部分を作ります。Eclipseプラグイン開発では、GUI部分とCoreな部分に分けて設計するのが基本みたいですね。とりあえず、それに則ってみます。

開発時、実行時に必要なクラスは

です。Apache AxisはWSDLからクラスを生成したり、ランタイム時に使用されたりします。Log4jはログ制御をLog4jで行っているため必要です。Google Web APIs はWSDLファイルとAxisより自動生成されるクラスライブラリ群です。

plugin.xml

このプラグインのplugin.xmlはこんな感じです。

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin
   id="nu.mine.kino.plugin.google.core"
   name="%pluginName"
   version="0.1.1"
   provider-name="%providerName"
   class="nu.mine.kino.plugin.google.core.GoogleCorePlugin">

   <runtime>
      <library name="core.jar">
         <export name="*"/>
      </library>
   </runtime>

   <requires>
      <import plugin="org.eclipse.core.runtime"/>
      <import plugin="nu.mine.kino.axis"/>
      <import plugin="nu.mine.kino.log4j"/>
   </requires>

</plugin>

nu.mine.kino.axis

このプラグインはApache Axisのライブラリを利用するためにプラグインにしておきました。ソース自体はこちらのCVSを見てみて下さい。使うときはApache Axisのランタイムライブラリをlibディレクトリに置いておきます。

nu.mine.kino.log4j

このプラグインはLog4jのライブラリを利用するためにプラグインにしておきました。ソース自体はこちらのCVSを見てみて下さい。あとはLog4jの設定ファイルを指定する程度の機能をつけてあります。

プラグインクラス nu.mine.kino.plugin.google.core.GoogleCorePlugin

次にプラグインクラスにビジネスロジックを記述していきます。必要なメソッドは

などです。必要な箇所だけ抜粋しておきます。

package nu.mine.kino.plugin.google.core;

/**
 * Google検索を行うプラグインです。
 * 
 * @author Masatomi KINO
 * @version $Revision$
 */
public class GoogleCorePlugin extends Plugin {
   // いろいろ省略
  private GoogleSearchPort googleSearch;

  /**
   * プラグインの活性化時に呼ばれます。
   * This method is called upon plug-in activation
   */
  public void start(BundleContext context) throws Exception {
    logger.debug("start(BundleContext) - start");
    super.start(context);
    try {
      GoogleSearchServiceLocator locator = new GoogleSearchServiceLocator();
      googleSearch = locator.getGoogleSearchPort();
    } catch (ServiceException e) {
      logger.error(e);
      log(e);
    }

    logger.debug("start(BundleContext) - end");
  }

  /**
   * 引数の文字列で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,
            1, 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 getSplellingSuggestion(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;
  }
}

Coreについてはとりあえず、こんなもんでしょうか。

つぎは -UI部のロジック を作成します。


この記事は

選択肢 投票
おもしろかった 1  
そうでもない 0  

Top / Eclipse / プラグイン開発のTIPS集 / GooglePlugin / まずはCoreをつくる

現在のアクセス:18755


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS