Morph開発専用のプラグインを作ってみよう

Grails開発用に、開発支援プラグイン作ってみたよ | ゲンゾウ用ポストイット で取り上げた開発支援用のプラグインですが、 Morph eXchange を使用した開発のニーズが増えてきているようなので、専用プラグインとして作成して見ようと思っています。

機能

grails deploy-morph コマンド以外は、今回作ってみたものです。

  • grails deploy-morph コマンド( Morph eXchange にアップロード、デプロイ)
  • grails war コマンド強化機能
    • war作成時に、 web.xml にメールセッション情報を追加
    • war作成時に、spring管理beanとして mailSession、mailSender ビーンを追加

プラグイン名

morph-utilities morph-plugin

コーディング

DeployMorph.groovy ファイルは、以前のブログで作ったものを利用しました。

MorphUtilitiesGrailsPlugin.groovy MorphPluginGrailsPlugin.groovy ファイルには、以下のように記述しました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import grails.util.*
import org.codehaus.groovy.grails.commons.*
import org.springframework.mail.*


class MorphPluginGrailsPlugin {
    def version = 0.1
    def dependsOn = [:]

    def author = "genzou"
    def authorEmail = "genzouw@gmail.com"
    def title = "Plugin summary/headline"
    def description = '''
This plugin help you to develop the application on the morph appspace.
'''

    // URL to the plugin's documentation
    def documentation = "http://code.google.com/p/grails-morph-plugin/"

    def doWithSpring = {
        switch (GrailsUtil.environment) {
            case GrailsApplication.ENV_PRODUCTION:
                mailSession(org.springframework.jndi.JndiObjectFactoryBean)
                        {
                            jndiName = 'java:/comp/env/mail/Session'
                        }
                mailSender(org.springframework.mail.javamail.JavaMailSenderImpl)
                        {
                            session = mailSession
                        }
                break
            default:
                break
        }
    }

    def doWithApplicationContext = { applicationContext ->
        // TODO Implement post initialization spring config (optional)
    }

    def doWithWebDescriptor = { xml ->
        switch (GrailsUtil.environment) {
            case GrailsApplication.ENV_PRODUCTION:
                def servlets = xml.'servlet'

                servlets[servlets.size() - 1] + {
                    "resource-ref" {
                        "description"("Morphlabs Mail Session")
                        "res-ref-name"("mail/Session")
                        "res-type"("javax.mail.Session")
                        "res-auth"("Container")
                    }
                }
                break
            default:
                break
        }
    }

    def doWithDynamicMethods = { ctx ->
        // TODO Implement registering dynamic methods to classes (optional)
    }

    def onChange = { event ->
        // TODO Implement code that is executed when any artefact that this plugin is
        // watching is modified and reloaded. The event contains: event.source,
        // event.application, event.manager, event.ctx, and event.plugin.
    }

    def onConfigChange = { event ->
        // TODO Implement code that is executed when the project configuration changes.
        // The event is the same as for 'onChange'.
    }
}

実行時のパラメータによって、web.xml、springに情報を設定するかしないかを判断しています。 これにより、warファイル作成時にのみ実行されるようになります。

課題

これで、プラグインインストール後に Morph Appspace にサーバーをアップした際にメール送信可能になるはずです。でも、なんど試してもうまくいきません。 warファイル解凍後の web.xml にはメール設定が追記されているようなので、問題はspring管理beanに対する情報追加が失敗しているのかな?? 完成はまだまだ先みたいです。

どなたか、一緒に作ってみませんか??

追記 2008-10-15

投稿を若干変更しました。Google Codeプロジェクトに配置し、不具合をいくつか修正しました。