Tuesday, January 6, 2009

ClassNotFoundException when running Grails with Acegi plugin

Recently, I've been playing around with Groovy on Grails. I have to admit that its kinda hard for me to learn groovy since I already work as a Java programmer for the past 4 years. Anyway, I was trying the Acegi plugin. Everything works fine, until I come to the generate-manager stage. The generate-manager script is to generate controllers and view files for all the acegi domains. After I run the generate-manager script, and I start the server, 2 exceptions are thrown.

[16] commons.DefaultGrailsApplication Class not found attempting to load class RoleController
java.lang.ClassNotFoundException: RoleController
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.security.AccessController.doPrivileged(Native Method)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
at Init_groovy$_run_closure6.doCall(Init_groovy:131)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs(Gant.groovy:372)
[32] commons.DefaultGrailsApplication Class not found attempting to load class UserController
java.lang.ClassNotFoundException: UserController
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.security.AccessController.doPrivileged(Native Method)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
at Init_groovy$_run_closure6.doCall(Init_groovy:131)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs(Gant.groovy:372)

Obviously, the compiled class UserController and RoleController are not found. How come? I checked the grails-app/controllers, both UserController and RoleController are there as groovy code. I always assume that the grails will compile the groovy code and turn them into java class file. Then why is this happening?

After spending hours of errors and trial, I finally figured it out. In UserController.groovy, the class name is actually PersonController, which is so different from Java. In Java, if the class name and the file name is different, a compile error will be thrown, but this is not the case in groovy. So, I checked the class folder, only PersonController and AuthorityController are found. Then, another question is, why does grails choose to load UserController and RoleController, why not PersonController and AuthorityController? If you look at the applicationContext.xml in WEB-INF folder, you will come to this very bean

<bean id="grailsResourceHolder" scope="prototype"
class="org.codehaus.groovy.grails.commons.spring.GrailsResourceHolder">
<property name="resources">
<value>classpath*:**/grails-app/**/*.groovy</value>
</property>
</bean>

What does this bean do? This bean will actually collect the name of all the groovy files under the grails-app folder, and try to class load them. For example, if UserController.groovy is found, then grails will try to load UserController.class, and this is the cause to it.

Therefore, I modified the GenerateManager script under Acegi plugin to make it work. If you are facing the same problem as I did, feel free to copy the script, but kindly leave a message here so that I know that I am not alone.

includeTargets << new File("$acegiPluginDir/scripts/_SecurityTargets.groovy")

pluginTemplatePath = "$templateDir/manager"

target('default': 'Generates view and controller for Spring Security user management') {
loadConfig()

generateControllerAndViews (resourceName='User', targetName=personClassName)
generateControllerAndViews (resourceName='Role', targetName=authorityClassName)
generateControllerAndViews (resourceName='Requestmap', targetName=requestmapClassName)
}

private void generateControllerAndViews(String resourceName, String targetName) {

String path = "$basedir/grails-app/controllers/${targetName}Controller.groovy"
def outFile = new File(path)
if (outFile.exists()) {
Ant.input addProperty: 'overwrite', message: "$outFile.name exists - overwrite? y/n"
if ('y' == Ant.antProject.properties.'overwrite') {
overwrite = true
}
}
else {
overwrite = true
}

println "generating files for $targetName ......."

println "generating file $path"
generateFile "$pluginTemplatePath/controllers/_${resourceName}Controller.groovy", path

path = "$basedir/grails-app/views/${targetName.toLowerCase()}"
String viewPath = "$pluginTemplatePath/views/${resourceName.toLowerCase()}"
println "generating view files - $path/* "
Ant.mkdir dir: path
generateFile "$viewPath/list.gsp", "$path/list.gsp"
generateFile "$viewPath/edit.gsp", "$path/edit.gsp"
generateFile "$viewPath/create.gsp", "$path/create.gsp"
generateFile "$viewPath/show.gsp", "$path/show.gsp"
}

You can find this file under $Your_application/plugins/acegi_$VERSION/scripts
My script might not be the best solution around, so feel free to give me any feedback.

Happy coding :)

41 comments:

Scott Frederick said...

I believe the correct way to do the customization you did is documented here: http://www.grails.org/AcegiSecurity+Plugin+-+Customizing+with+SecurityConfig

愛你 said...

一葉晴貼圖區,影音視訊聊天室,玩美女人,免費視訊,ut 聊天室,美女寫真,微風成人,玩美女人,同志交友,成人卡通,援交友留言,免費視訊,美女影片,情色視訊,伊莉成人,成人光碟,tt1069同志交友網,85cc成人片,新人淚成人色網,日本美女寫真集,免費聊天,聊天室080,999成人性站,av成人網,情色貼圖區,情色論壇,成人聊天室,視訊美女,同志貼圖,成人視訊,視訊交友90739,視訊聊天室,比基尼泳裝美女,杜雷斯成人,色情影片,免費視訊,交友戀愛進行室,玩美女人,美女交友,正妹時鐘,

美麗新世界 said...

Knowledge is a treasure, but practice is the key to it...................................................

yahoo said...

快樂的微笑是保持生命康健的唯一藥石,它的價值千萬,卻不要花費一文錢..............................

便當 said...

春宮性愛教學taiwan sex大奶子台灣情色論壇台灣av女優一夜情留言板女生裸體色情電話a網情色片美女裸照成人笑話巨乳學院裸體寫真av寫真走光照巨奶做愛技巧a圖片淫亂撫摸情色聊天少婦自拍淫娃一夜情聊天av情色本土av女生高潮色情av成人情色貼圖一夜情留言成人圖庫亞洲成人口交技巧性經驗成人18三點全露蕩婦情色自拍貼圖成人漫畫成人漫畫

friend said...

我愛那些使自己的德行成為自己的目標或命定的人 ..................................................

茂一 said...

辣妹貼圖0401影音視訊bonbonsex日本avdvd光碟f 罩杯美女圖片情色a片免費無碼a片小魔女影片交流成人 影片sex明星露點鏡頭日本戀愛卡通日本成人聊天視訊dxlive日本成人遊戲日本成人遊戲節目日本成人部落格日本成人電影免費日本成人電影下載日本成人視訊聊天室日本成人AV視訊 辣妹視訊 辣妹0401線上影城視訊交友cleansing18禁成人影片嘿咻kiss168z6成人色站杜雷斯正妹牆成人論壇ss369成人色網38ga成人台灣kiss倩色網18成人影城919sex色片直播網5278成人色論免費線上影片觀看最新卡通a片最新加勒比 潮吹教學講座最新av女優排名最新av女優介紹最新無碼av

PokCalaway0710 said...

thx u very much, i learn a lot

TerryMontanez2289 said...

Nice Post~!!!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

函松 said...

Well done!........................................

蘇pet0701em_halvorsen said...

以簡單的行為愉悅他人的心靈,勝過千人低頭禱告........................................

劉claren0812ceb_brunette said...

唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們...............................................................

B120ryantScipio1 said...

may the blessing be always with you!! ...............................

韋于倫成 said...

有趣有趣~還ok的啦(。・`ω´・)........................................

奎峰 said...

成人 免費視訊影音觀賞 聊天網 免費h卡通 0401交友 台灣女優 性感裸體寫真 免費影片線上觀看 本土自拍偷拍露點照 免費豆豆聊天室 愛愛 情趣丁字褲 成人遊戲18禁 少女裸體圖片 嘟嘟影城85c 免費a圖a片 美眉情色網 麗的色情遊戲 一夜情短片 av383tv影片 完美女人聊天室 玩美女人影音視訊 性感熱辣美女寫真 裸睡的辣妹圖片 人妻自拍畫廊 超熟女性愛 視訊聊天 空姐免費a片 a圖網情色貼圖 寫真 85cc卡通影城 杜蕾斯成人 微風 嘟嘟 av女優777 情色貼圖片 台灣援交自拍影片 情人視訊聊天室 鋼管辣妹 免費av 洪爺性愛貼圖 大奶裸體辣妹 www.sex520.com 電話下載片 日本巨乳貼圖 性愛自拍圖貼 後宮電影院女優 限制級 0204 性愛影片觀看 內褲走光

清民 said...

天生我材必有用,千金散盡還復來。 ..................................................

熙辰 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。...........................................................................

reeselane said...

微風成人片 a視訊 一夜情情色聊天室 免費視訊 視訊影音交友 av女優寫真集 性感圖片 影片免費觀賞 偷拍影片 ut男女聊天室 如何做愛愛 情趣美女真 做愛18招 少女裸睡故事 免費色情片影城 成人免費a片 情色成人電影 線上免費色情影片 一夜情留言板 aio交友 名模寫真 玩美女影片 情色美女 辣妹按摩影片 人妻線上免費影片欣賞 聊天室 視訊聊天 ut a圖 女寫真 85cc 免費影城 貼圖區 78論壇 線上漫畫777 色貼圖片 台北援交妹留言版 性感情人趣味 鋼管秀影片 avdvd影片 洪爺影城 大奶頭美女 sex貼圖區 a片免費下載 日本巨乳av女優 免費性卡通影片觀賞 後宮電影院 限制級 偷拍0204 性愛罐頭影片 比基尼走光

SadeRa盈君iford0412 said...

要持續更新下去喲!!祝你心情愉快.................................................................

怡伶 said...

果然很有意思呀....這當然要頂一頂呀.................................................................

廷雯 said...

死亡是悲哀的,但活得不快樂更悲哀。......................................................................

asdas said...

成熟,就是有能力適應生活中的模糊。....................................................................

于庭 said...

It takes all kinds to make a world.............................................................

張怡 said...

Hello~Nice meet you~~............................................................

童雅 said...

一個人的際遇在第一次總是最深刻的,有時候甚至會讓人的心變成永遠的絕緣。............................................................

ju吳phe宇te佳ns said...

晚上好,很喜歡你的blog哦...............................................................

于倫 said...

不願彎腰撿拾一根針的人,永遠不值得一塊錢。..................................................

楊偉馨楊偉馨 said...

人生的苦惱,不在擁有太少,而在奢望太多。..................................................

美娟美娟 said...

要經常發表文章 最愛你了呦............................................................

王美妹 said...

人應該做自己認為對的事,而不是一味跟著群眾的建議走。..................................................

蕙春蕙春 said...

字是活的,人和環境的觀察是活的,思想是活的。不管怎樣,就是要有一兩樣是活的。否則都是平庸。..................................................

佳瑩佳瑩 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

劉士賢劉士賢 said...

愛情是一位偉大的導師,教我們重新作人.................................................................

張v李佳羽嘉旺 said...

寫文章需要心情~~期待你再一次的好文章...............................................................

家唐銘 said...

一個人的價值,應該看他貢獻了什麼,而不是他取得了什麼....................................................

凱許倫 said...

人生中最好的禮物就是屬於自己的一部份............................................................

文王廷 said...

愛情不是慈善事業,不能隨便施捨。.................................................................

por said...

人自幼就應該通過完美的教育,去建立一種好的習慣。......................................................

于庭吳 said...

初次拜訪,祝你人氣一百分..................................................................

陳欣陳玟珮陳玟珮琦 said...

好的部落格就要和好朋友分享--感謝分享..................................................

群育航學 said...

一棵樹除非在春天開了花,否則難望在秋天結果。............................................................