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 :)

Wednesday, December 31, 2008

From 2008 to 2009

So again its the last day of the year.Its been a long year for me.And now I am planning for what I should do at 2009.
1.Attend scrum master course at beijing at March.
2.Start my personal project,using groovy on grails and related technologies.
3.Get my SCBCD,I tried on 2008 but i failed the test,hopefully can pass the exam this year.
4.Go to travel alone.
5.Gain more knowledge on photo shooting.

That's all, folks.Happy new year...haha...

Wednesday, October 8, 2008

返學打油詩

生係中國人,死係中國魂

要我學英文,梗係冇可能

英語唔合格,更顯我性格

數學唔合格,老師負全責

語文唔合格,我都冇辦法

考試考得好,全靠隔離好

考試考唔好,隔離唔識做

這個中國人,真係失禮人

叫佢學語文,佢話佢低能

叫佢學英文,佢話冇可能

叫佢學計數,佢話會頭暈

考試考得好,全靠運氣好

考試考唔好,完全意料到

唔駛睇相佬,大家估得到

返學九個月 怨聲已不絕

晨早六點九 趕住著衫走

路口等小巴 車費又再加

爬到校門口 仲有幾層樓

望望班房內 竟然冇人在

聽到人示愛 遲到又要再

今日第一堂 學生已發狂

漏帶一本書 九族都要誅

空堂唱下歌 傾計都唔多

科學孖連擊 聽完都唔識

跟住中文堂 擦鞋仔最忙

最後英文科 變曬做睡魔

打鐘各自飛 就咁又一 Day

返學為左乜 日日被糟質

返學等放學 放學飲可樂

Thursday, June 12, 2008

马来西亚的中文

中國人 :今晚你有空嗎?我沒空!
馬來西亞華人 :今晚你得不得空?我不得空!
中國人 :餅干受潮了…。
馬來西亞華人 :餅干'漏風'了…。
中國人 :從上海去蘇州要多少個小時?
馬來西亞華人:從上海去蘇州要幾粒鍾?
中國人 :難道他不可以來嗎?
馬來西亞華人:你不給他不來啊?
中國人 :周傑倫不喜歡穿內褲。
馬來西亞華人:周傑倫不喜歡穿底褲。

中國人 :我一向都是這樣的
馬來西亞人:我一路來都是這樣的啦
中國人 :我的手機掉進溝渠了。
馬來西亞華人:我的手機掉進龍溝了。
中國人 :這樣你不是很不值得嗎?
馬來西亞華人:這樣你'馬'很不 '歹'?
中國人 :你真是聰明!
馬來西亞華人:你真是pan nai!(源自馬來語pandai,聰明的意思)
中國人 :你安靜!
馬來西亞華人:你diam diam!(源自馬來語diam,安靜的意思)


中國人 :我要去銀行取款。
馬來西亞華人:我要去銀行'按錢'。
中國人 :為什麼?
馬來西亞華人:做麼?
中國人 :你很強~
馬來西亞華人:你很夠力~
中國人 :明天也叫他一起去吧!
馬來西亞華人:明天叫'埋'他一起去!
中國人 :我很郁悶~~~
馬來西亞華人:我很'顯'(sien)啊~~~~('顯'比郁悶的境界更高)
中國人 :你再說我就打你!
馬來西亞華人:你再說我就hood你!(有點粗俗的)
中國人 :你在說什麼?
馬來西亞華人:你在說sommok?


中國人 :你不要令我丟臉~
馬來西亞華人:你不要'下水'我~
中國人 :真被你氣到…。
馬來西亞華人:被你炸到…。
中國人 :你別亂來~
馬來西亞華人:你表亂亂來~
中國人 :你很無聊
馬來西亞華人:你很廢
中國人 :XX你
馬來西亞華人:Kanasai(意思是像大便一樣,罵人的話)
中國人 :迫切
馬來西亞華人:bek chek


中國人 :我們一起吃這碗面~
馬來西亞華人:我們'公司'吃這碗面~(源自馬來語的kongsi,就是一起分享的意思)
中國人 :我們結婚吧!
馬來西亞華人:我們結'分'吧!('婚'字受粵語影響,所以音不標准)
中國人 :今天的天氣很熱~
馬來西亞華人:今天的天氣熱到啊。。。。。。。。。。。。。。~~~~~~~('啊'字要拉長,然後沒有下文了)
中國人 :哇!
馬來西亞華人:哇撈weh!!!!
中國人 :我受不了他!
馬來西亞華人:我behtahan他!

Tuesday, April 1, 2008

Some photos - Yong He Gong


Tree beside the temple



Temple

Where should Jason go next?Wondering....


The streets outside of the temple

Temple

Temple

The entrance


Mr Jason,stop blocking the view...haha


Temple



Jason still thinking whether wants to go in there..smoking~~


Turning this may give you better luck...


Thursday, March 6, 2008

Tuesday, March 4, 2008

What my name,Thor means?

You are a seeker. You often find yourself restless - and you have a lot of questions about life.
You tend to travel often, to fairly random locations. You're most comfortable when you're far away from home.
You are quite passionate and easily tempted. Your impulses sometimes get you into trouble.

You are truly an original person. You have amazing ideas, and the power to carry them out.
Success comes rather easily for you... especially in business and academia.
Some people find you to be selfish and a bit overbearing. You're a strong person.

You are well rounded, with a complete perspective on life.
You are solid and dependable. You are loyal, and people can count on you.
At times, you can be a bit too serious. You tend to put too much pressure on yourself.

You are wild, crazy, and a huge rebel. You're always up to something.
You have a ton of energy, and most people can't handle you. You're very intense.
You definitely are a handful, and you're likely to get in trouble. But your kind of trouble is a lot of fun.