มาลอง run Groovy บน Apache WebServer กัน
เมื่อวานนี้ผมไปเจอ project ใน GoogleCode คือ Groovy-LAMP
โดยคำอธิบายของ project นี้ก็ง่ายนิดเดียวคือ
คำถาม
ถ้าคุณรัก Groovy แต่เกลียด Java Web Servers ล่ะจะทำยังไงคำตอบ
เราจะทำให้การเขียน Groovy เหมือนกับการเขียน php เลย กล่าวคือ เมื่อทำการเขียน code และแก้ไข code groovy แล้วก็ทำงานได้เลย และที่สำคัญทำงานผ่าน Apache WebServer กันไปเลย
พออ่านมาได้นิดหน่อย ผมจึงตาลุกวาว เนื่องจากเป็นโลกที่ผมไม่เคยเข้าไปมาก่อน ดังนั้นเรามาเริ่มศึกษาไปพร้อมๆ กัน
โครงสร้างของ Groovy-LAMP project
- ทำการสร้าง java proxy ให้ Apache WebServer, Lighttpd ติดต่อมาโดยผ่าน Simple CGI ( SCGI ) โดย java proxy ที่สร้างขึ้นมานั้นจะรับ file ที่มีนามสกุล .groovy เท่านั้น
- เบื้องหลังการทำงานก็คือ GroovyScriptEngine เป็น unitilies class ของ groovy นั่นเอง
มาถึงตรงนี้แล้ว สิ่งที่ต้องการในการติดตั้ง Groovy-LAMP project มีดังนี้ [ Software เหล่านี้ผมใช้ในการทดสอบ ]
- Apache WebServer 2.x
- Java 1.5
- Simple CGI for Apache WebServer 2.x
- Groovy Library ( groovy-all.jar )
- Groovy-LAMP
ก่อนจะเริ่มผมเจอปัญหาตั้งแต่ต้น คือ Groovy-LAMP ที่ download มานั้นใช้งานบน Windows ไม่ได้เกิด error ตาม issue นี้
ดังนั้นผมจึงตัดสินในทำการ checkout sourcecode มาแก้ไขเองดีกว่า ด้วยคำสั่งนี้
svn checkout http://groovy-lamp.googlecode.com/svn/trunk/ groovy-lamp-read-only
หลังจากที่อ่าน sourcecode แล้วพบว่ามีปัญหาในการ parser request นั่นเอง ผมคิดว่าข้อมูลของ request ที่มาจาก Apache WebServer และ Lighttpd ไม่เหมือนกัน ดังนั้นผมจึงเพิ่มบรรทัดนี้ไปที่ท้าย method parse() ใน file groovyrun.scgi.SCGIParser.java ดังนี้
env.put("SCRIPT_FILENAME", "file:/" + env.get("DOCUMENT_ROOT").toString() + env.get("SCRIPT_NAME").toString() + env.get("PATH_INFO").toString());
แล้วก็ build เป็น jar file หรือทำการ run ได้เลยครับ ลอง run ด้วยคำสั่ง
java -jar GroovyLamp.jar -port=4444 -mode=script
หรือ
java -jar GroovyLamp.jar -port=4444หรือ
java Main -port=4444
ผลการทำงานจะค้างหน้าจอหรือ console ดังนี้
Fri Feb 05 13:45:53 PST 2010 : NOTICE : Starting server
Fri Feb 05 13:45:53 PST 2010 : NOTICE : SCGIApplicationServer running on port: 4444
กลับมาต่อที่การ config ให้ Apache Web Server ติดต่อไปยัง Groovy-LAMP โดยจะต้องไปหา SCGI ตาม link จากข้างต้นมาด้วย ดังนั้นทำการ download และ extract mod_scgi.so file ไปไว้ใน %APACHE_HOME%\modoles และทำการ config ใน file %APACHE_HOME%\conf\httpd.conf ดังนี้
1. ทำการ load scgi module ขึ้นมา
[sourcecode langiage="java"]
LoadModule scgi_module modules/mod_scgi.so
[/sourcecode]
2. เป็นการบอกให้รู้ว่า file ที่อยู่ใน folder groovy [ ใน www root ] ให้ส่ง request ไปที่ Groovy-LAMP ที่เราสร้างขึ้นมา ผ่าน port 4444 ที่เปิดเอาไว้
[sourcecode langiage="java"]
SCGIMount /groovy 127.0.0.1:4444
[/sourcecode]
ต่อไปทดลองโดยใช้ file ตัวอย่างจากการ Download Groovy-LAMP มา ซึ่งชื่อว่า test.groovy โดย copy ไปไว้ใน folder /groovy/ แล้วทำการ restart ตัว Apache WebServer และ run http://localhost/groovy/test.groovy ใน browser จะได้ผลลัพธ์ดังนี้
Sourcecode ตัวอย่าง
out.println "<h1>Server</h1>"
out.println "<table border=\"1\">"
request.env.each(){
k,v -> out.println "<tr><td>$k</td><td>$v</td></tr>"
}
out.println "</table>"
out.println "<h1>Get parameters</h1>"
out.println "<table border=\"1\">"
request.get.each(){
k,v -> out.println "<tr><td>$k</td><td>$v</td></tr>"
}
out.println "</table>"
ผลการทำงาน

เพียงเท่านี้ก็สามารถเริ่มใช้งาน Groovy-LAMP ได้แล้วครับ ซึ่งเป็นแนวคิดที่น่าสนใจเหมือนกันครับ ใครสนใจก็สามารถนำมาพัฒนาต่อยอดได้ครับ
Tags: groovy, groovy-lamp
เกรลส์รุ่น 1.2.1 ออกแล้วเมื่อวานนี้ครับ เป็นรุ่นแก้บั๊กและปรับปรุง
อ่านรีลีสโน้ตได้จากที่นี่ และดาวน์โหลดได้จาก grails.org/Download เช่นเคยครับ
ที่มา Blog เกรม รอชเชอร์
Tags: grails
เราคูณ String ได้ อาจจะเป็นเรื่องที่หลายท่านยังไม่ได้ลองทำดูในภาษา Groovy นะครับ แต่ก็อาจจะมีประโยชน์โดยเฉพาะเวลาที่ต้องการประมวลผลหรือแสดงผลข้อความ
พิมพ์
println ‘-’.multiply 80
จะทำให้เกิดสายอัขระเท่ากับจำนวน เป็นต้นครับ
หรือสามารถใช้ operator * ได้ในทำนองเดียวกับ (เพราะ operator * จะแปลงเป็น multiply method ในระดับ bytecode ครับ)
println (’-’ * 80)
จาก Groovy Goodness
Tags: groovy, groovy goodness
สวัสดีปีใหม่ทุกๆ ท่านนะครับ
ก่อนที่ผมจะไม่อยู่หลายวันเนื่องจากมีภารกิจสำคัญ ทำให้ห่างหายจากโลก internet ไปสัก 1-2 สัปดาห์ เลยขอฝาก Grails plugin ที่น่าสนใจสักตัวให้ลองใช้งานนะครับ
นั่นก็คือเครื่องมือสำหรับ Monitoring การใช้งาน resources ต่างๆ ของ grails application ที่ทำงานอยู่ โดยตัวที่แนะนำคือ Melody Grails Plugin
การใช้งานก็ง่ายๆ ครับ คือ ใช้คำสั่ง
>grails install-plugin grails-melody
สามารถใช้งานผ่าน url ดังนี้
http://localhost:8080/
/monitoring
ผลการทำงานจะแบ่งเป็นส่วนต่างๆ ดังนี้
1. ผลการทำงานโดยรวม ซึ่งแสดงเป็นรูปกราฟ

และยังมีส่วนอื่นๆ อีกเช่น System Error Log, System Information, จำนวน Thread การทำงาน, จำนวนข้อมูลที่ Cache ไว้ สามารถ export report ออกเป็น pdf file ได้ รวมทั้งยังสามารถดู report ตาม วัน เดือน และ ปี ได้อีกด้วย
ดังนั้น Melody Plugin ตัวนี้จึงเป็นที่น่าสนใจมากครับ แต่ถ้าเป็น JavaEE Application ก็สามารถใช้ได้เช่นกันนะครับ ซึ่งดูเพิ่มเติ่มได้ที่นี่
Tags: melody, monitoring
Groovy Community, นอกจากจะมี Grails ซึ่งเป็น web application platform ที่ทำมาให้เราใช้ในการพัฒนาเว็ปอย่าง รวดเร็วส์ แล้ว ได้มีกลุ่มคนอีกกลุ่มหนึ่ง นำโดย
คุณ Andres Almiray ที่ได้เห็นความซับซ้อนในอดึต สำหรับการพัฒนา Java GUI ด้วย Swing จึงได้คิดปรับปรุง วิธีคิด ในการพัฒนา Java Desktop Application ในรูปแบบใหม่ โดยใช้ความสามารถของตัวภาษา Groovy อย่างเต็มที่ และมีรูปแบบการพัฒนาคล้ายๆ กับ การพัฒนา web application บน Grails แต่ก็ยังคงยึดแบบแผนที่ดีที่มีการพัฒนามายาวนาน และต่อเนื่องของ Swing Application Framework(JSR 296) ซึ่งก็คือ “Griffon Framework” นั่นเอง
เนื่องจาก Griffon ใช้ตัวภาษา Groovy เป็นพื้นฐานในการพัฒนาดังนั้น แนวคิดหลักๆที่เราคุ้นเคยกันใน grails ก็จะมีมากับ Griffon เหมือนกัน เช่น
Convention over Configuration, MVC architecture, Command line interface และ มี plug-in system ให้ได้ใช้กันอย่างสนุกสนาน เหมือน grails อีกด้วย
เริ่มติดตั้ง Griffon บน Window
1. ติดตั้ง jdk 1.5 หรือ jdk 1.6
2. เนื่อง จาก Griffon 0.2 ยังมีปัญหาอยู่บ้าง จึงขอแนะนำให้ไปใช้ version: Griffon 0.3 SNAPSHOT เลย สามารถ download ได้ที่ griffon-bin-0.3-SNAPSHOT.zip
3. เพิ่มเติมค่าใน System variable setting บน Window
JAVA_HOME ชี้ไปที่ เราติดตั้ง jdk เอาไว้ ( D:\Java\jdk1.6.0_17)
GRIFFON_HOME ชี้ไปที่เราติดตั้ง griffon ไว้ ( D:\Java\griffon-0.3-SNAPSHOT )
และ เพิ่ม PATH variable : %JAVA_HOME%\bin;%GRIFFON_HOME%\bin
ทำการทดสอบ Griffon
1. เปิด command line ขึ้นมา แล้วพิมพ์ดังนี้
C:\Users\Prakart>griffon help
ก็จะเห็นคำสั่งต่าง ๆ ที่เราจะใช้ในการพัฒนา application ด้วย Griffon

ลอง สร้าง application และ เทส
1. C:\Users\Prakart> griffon create-app TestGriffon
2. cd TestGriffon
ภายใน จะมี folder ที่ถูกเตรียมไว้ให้แบบนี้

3. ทำการ run โดยใช้คำสั่ง griffon run-app
C:\Users\Prakart\TestGriffon\griffon run-app
จะปรากฏ หน้าต่าง window ของ griffon เล็กๆ ขึ้นมา

นอกจากนี้ griffon ยังได้เตรียม deployment options ต่างๆ ไว้ให้ด้วย(ดูได้จาก griffon help)
- griffon run-applet และ
- griffon run-webstart
ซึ่งเราสามารถลองเรียกดูได้เลย
C:\Users\Prakart\TestGriffon\griffon run-applet และ
C:\Users\Prakart\TestGriffon\griffon run-webstart
Griffon เป็นอีก community หนึ่งของ Groovy ที่น่าสนใจนะครับ
ขอให้สนุกสนาน กับภาษา Groovy และ ขอให้ทุกคนมีความสุขในปี 2010 และตลอดไป นะครับ
References:
1. Griffon’s website > http://griffon.codehaus.org/
2. Griffon in Action > http://www.manning.com/almiray/Almiray_MEAPch01_free.pdf
Tags: griffon
OAuth เป็น open standard สำหรับ APIs ของการ authentication ซึ่งจะทำให้ปลอดภัยและง่ายต่อการพัฒนาระบบงาน
โดยถ้าใครเคยใช้ web ที่มีการ authentication จาก twitter account แล้วนั้นจะเห็นหน้าจอเหล่านี้ ซึ่ง Twitter ก็เป็น OAuth Service Provider ที่ให้เราๆ ท่านๆใช้งานกัน และในบทความนี้ก็จะลองใช้ OAuth Service Provider ของ twitter เช่นกันครับ


ถ้าอยากทำแบบนี้ใน Grails application บ้างล่ะ ?? ทำอย่างไร ซึ่งแน่นอนว่าใน Grails มี OAuth plugin ให้ใช้เช่นกัน ดังนั้นเรามาดูว่าใช้งานกันอย่างไร
ขั้นตอนการพัฒนา
1. สร้าง Grails application ชื่อ my_oauth ดังนี้
>grails create-app my_oauth
2. ติดตั้ง OAuth plugin ดังนี้
>cd my_oauth
>grails install-plugin oauth-plugin
หรือ ทำการ download OAuth plugin มา แล้วติดตั้งดังนี้
>grails install-plugin c:\my-path\grails-oauth-xxx.zip
3. ทำการ config ค่า parameters ต่างๆ ที่จำเป็นในการใช้งาน OAuth service provider ของ Twitter ใน file /conf/Config.groovy ดังนี้
oauth {
twitter {
requestTokenUrl="http://twitter.com/oauth/request_token"
accessTokenUrl="http://twitter.com/oauth/access_token"
authUrl="http://twitter.com/oauth/authorize"
consumer.key="xxx"
consumer.secret="yyy"
}
}
คำถาม
ค่าของ consumer.key และ consumer.secret เอามาจากไหน ?
คำตอบ
เอามาจากการ register ใน twitter ตาม url นี้ http://twitter.com/oauth และทำการ register new application โดยจะมีแบบ form ให้ใส่ข้อมูลต่างๆ แล้วทำการ save ไปจะได้ consumer.key และ consumer.secret มานะครับ
ขั้นตอนการ Register เป็นดังรูป


4. สร้าง controller ชื่อ timeline ขึ้นมา เพื่อใช้สำหรับดึงข้อมูลจาก user timeline [ XML ] ใน twitter ดังนี้
>grails create-controller timeline
และแก้ไข code ใน file /grails-app/controllers/TimelineController.groovy ดังนี้
class TimelineController {
def apiURL = "http://twitter.com/statuses/user_timeline.xml"
def oauthService
def index = {
if (session.oauthToken == null) {
redirect(uri:"/")
}
if (params?.apiURL)
apiUrl = params.apiURL
def response = oauthService.accessResource(apiURL, 'twitter', [key:session.oauthToken.key, secret:session.oauthToken.secret])
def statuses = JSON.parse(response)
render statuses;
}
}
5. แก้ไข file /grails-app/views/index.gsp ดังนี้
<html>
<head>
<title>Welcome to Grails</title>
<meta name="layout" content="main" />
</head>
<body>
<div>
<g:oauthLink consumer="twitter" returnTo="[controller:'timeline']">
<h3>Login with</h3>
<img src="${resource(dir:'images',file:'twitter-logo.png')}" width="155" height="36"
style="border: 0; float: left" />
</g:oauthLink>
</div>
</body>
</html>
6. ทำการ run application ด้วยคำสั่ง
>grails run-app
และเข้าไปที่ http://localhhost:8080/my_oauth/ แล้วจะสามารถใช้งานดังรูป



โดยสุดท้ายจะวิ่งไปทำงานตาม callback ทีี่กำหนดไว้ครับ ถือเป็นอันสำเร็จของการใช้งานแล้ว เพียงเท่านี้ก็สามารถใช้งาน OAuth service provider จาก Twitter ได้แล้วครับ
ที่มา
- OAuth Plugin
- Grails OAuth and LinkedIn APIs
หลังจากที่นั่ง countdown การปล่อย Grails 1.2 มาเป็นเวลา 2 ชั่วโมง ตอนนี้ทางทีมพัฒนา Grails และ SpringSource ได้ปล่อยออกมาแล้วครับ
โดย feature สรุปได้ดังนี้ [ จะเหมือนกับ 1.2 RCx นะครับ ]
- Dependency Resolution DSL
- Better Spring Integration
- Named Query Support
- Improved Performance & Memory Consumption
- Named URL Mappings
- Refactored Testing Infrastructure
- Pluggable Web Containers
หลังจากที่ Spring Framwork 3.0 GA ออกมาไม่กี่ชั่วโมง ก็ได้ฤกษ์ของ Grails 1.2 RC2 กันบ้างโดยส่วนใหญ่จะเป็นการแก้ไขข้อผิดพลาด โดยสิ่งใหม่ที่เพิ่มมาคือ นำ Spring Framwork 3.0 GA เข้ามาใช้นั่นเองครับ
บทสัมภาษณ์ของ Guillaume Laforge and Graeme Rocher เกี่ยวกับ Groovy 1.7 และ Grails 1.2 โดยสัมภาษณ์จากงาน SpringOne 2GX
หัวข้อในการสัมภาษณ์ในครั้งนี้ประกอบไปด้วย
1. Features ใหม่ๆ ที่จะมีใน Groovy 1.7 และ Grails 1.2 รวมทั้งแผนการ release ใน version ใหม่ๆ
2. Spring Roo vs Grails
3. ผลกระทบของทีมพัฒนาจากการเข้ามาซื้อของ SpringSource
4. Grails + Cloud Computing
5. Groovy and Grails in Eclipse
ทาง SpringSource และทีมพัฒนา Grails ได้ปล่อย Grails เวอร์ชัน 1.2 RC 1 ( Release Candidate ) ออกมา
ในเวอร์ชันนี้ส่วนใหญ่จะเป็นการแก้ไขข้อผิดพลาด และ ปรับปรุงการทำงานให้ดียิ่งขึ้น ส่วน features ใหม่ๆ ก็มี เช่น
- Dependency Resolution DSL
- เพิ่ม file CheckPluginVersions.groovy เพื่อตรวจสอบ version ของ plugin ใน grails project ใช้สำหรับการ update plugin
- เพิ่ม argument -clean ในชุดคำสั่ง grails test-app
- Dynamic Finder สนับสนุน Named Criteria
- เพิ่ม dynamic properties ใน domain class
โดยรายละเอียดเพิ่มเติมที่นี่
- Changelog
- Download
- Documentation http://grails.org/doc/1.2.x
