JJUG CCC 2011 Spring

2,322 views
2,189 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,322
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
14
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

JJUG CCC 2011 Spring

  1. 1. G*JJUG CCC 2011 Spring @kiy0taka
  2. 2. ※ kiy0taka 0
  3. 3. G*
  4. 4. Groovy?
  5. 5. HelloWorld.javapublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }}
  6. 6. HelloWorld.groovypublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); }} Java
  7. 7. HelloWorld.groovypublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!") }}
  8. 8. HelloWorld.groovypublic class HelloWorld { public static void main(String[] args) { System.out.println "Hello, World!" }}
  9. 9. HelloWorld.groovypublic static void main(String[] args) { System.out.println "Hello, World!"}
  10. 10. HelloWorld.groovySystem.out.println "Hello, World!" main
  11. 11. HelloWorld.groovy println "Hello, World!" System.out.println → println
  12. 12. One-Linergroovy -e "println Hello, World!"
  13. 13. Java def file = new File("./hoge.txt")
  14. 14. def text = new File("./hoge.txt").text
  15. 15. Webnew URL(http://www.java-users.jp/contents/).getText(UTF-8)
  16. 16. Stringprintln ls -la /opt/local.execute().texttotal 8drwxr-xr-x 12 root admin 408 11 23 19:32 .drwxr-xr-x 3 root admin 102 11 7 14:34 ..drwxr-xr-x 5 root wheel 170 11 24 13:02 Librarydrwxr-xr-x 686 root admin 23324 11 27 18:06 bindrwxr-xr-x 25 root admin 850 11 24 17:01 etcdrwxr-xr-x 177 root admin 6018 11 24 17:01 includedrwxr-xr-x 988 root admin 33592 11 24 17:01 libdrwxr-xr-x 22 root admin 748 11 24 16:17 libexeclrwxr-xr-x 1 65534 wheel 9 11 23 18:42 man -> share/mandrwxr-xr-x 5 root admin 170 11 24 13:37 sbindrwxr-xr-x 76 root admin 2584 11 27 18:06 sharedrwxr-xr-x 8 root admin 272 11 24 13:02 var
  17. 17. import import java.lang import java.math import java.io import java.net import java.util import groovy.lang import groovy.util
  18. 18. Javaimport org.apache.poi.hssf.usermodel.*def workBook = new HSSFWorkbook(new File(./foo.xls)workBook.newInputStream()).sheets.each { sheet -> sheet.firstRowNum.upto(sheet.lastRowNum) { sheet.getRow(it).with { row -> row.firstCellNum.upto(row.lastCellNum - 1) { println row.getCell(it).stringCellValue } } }}
  19. 19. jar@Grab(org.apache.poi:poi:3.2-FINAL)import org.apache.poi.hssf.usermodel.*def workBook = new HSSFWorkbook(new File(./foo.xls)workBook.newInputStream()).sheets.each { sheet -> sheet.firstRowNum.upto(sheet.lastRowNum) { sheet.getRow(it).with { row -> row.firstCellNum.upto(row.lastCellNum - 1) { println row.getCell(it).stringCellValue } } }}
  20. 20. Ant def ant = new AntBuilder() ant.unzip(src: xxx.zip, dest:dest) ant.mail(mailhost:hostname, subject:hello, charset:utf-8, user:user, password:password) { from address:xxx@example.com to address:kiy0taka333@gmail.com message Hello World! }
  21. 21. // Groovy$ time groovy -e "println Hello"Helloreal!0m1.292suser!0m1.283ssys! 0m0.192s// GroovyServ$ time groovyclient -e "println Hello"Helloreal!0m0.036suser!0m0.001ssys! 0m0.003s
  22. 22. Swing(Java) contentPane.add(button);package sample; setDefaultCloseOperation(EXIT_ON_CLOSE);import java.awt.Container; pack();import java.awt.GridLayout; setVisible(true);import java.awt.event.ActionEvent; }import java.awt.event.ActionListener; public static void main(String[] args) {import javax.swing.JButton; SwingUtilities.invokeLater(new Runnable() {import javax.swing.JFrame; public void run() {import javax.swing.JLabel; new Hello();import javax.swing.JTextArea; }import javax.swing.SwingUtilities; }); }public class Hello extends JFrame { } public Hello() { super("Hello"); Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(3, 1)); JLabel label = new JLabel("Label"); contentPane.add(label); JTextArea textArea = new JTextArea("Text Area"); textArea.setColumns(20); textArea.setRows(2); contentPane.add(textArea); JButton button = new JButton("Button"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { ... } });
  23. 23. Groovyimport groovy.swing.SwingBuildernew SwingBuilder().edt { frame(title:Hello, show:true, pack:true) { gridLayout(cols:1, rows:3) label Label textArea(Text Area, rows:2, columns:20) button(Button, actionPerformed:{ evt -> ... }) }}
  24. 24. SwingBuilderimport groovy.swing.SwingBuildernew SwingBuilder().edt { frame(show:true, pack:true) { tableLayout { tr { td { label UserName: } td { textField columns:20 } } tr { td { label Password: } td { passwordField columns:20 } } tr { td(colspan:2) { button Login } } } }}
  25. 25. https://gist.github.com/913279
  26. 26. h2console.groovy@Grab(org.mortbay.jetty:jetty-embedded:6.1.25)@Grab(com.h2database:h2:1.2.144)@Grab(mysql:mysql-connector-java:5.1.13)import org.mortbay.jetty.Serverimport org.mortbay.jetty.servlet.Contextimport org.h2.server.web.WebServletdef server = new Server(8080)new Context(server, "/", Context.SESSIONS) .addServlet(WebServlet, "/*")server.start() https://gist.github.com/717932
  27. 27. please show thesquare_root of 100
  28. 28. show = { println it }square_root = { Math.sqrt(it) }def please(action) { [the: { what -> [of: { n -> action(what(n)) }] }]}please(show).the(square_root).of(100)
  29. 29. show = { println it }square_root = { Math.sqrt(it) }def please(action) { [the: { what -> [of: { n -> action(what(n)) }] }]}please show the square_root of 100
  30. 30. Object.metaClass. =Object.metaClass. ={ clos -> clos(delegate) } = { it } = { println it } = { Math.sqrt(it) } 100
  31. 31. import static groovyx.gpars.GParsPool.withPooldef list = [1, 2, 3, 4, 5]withPool { def result = list.collectParallel { it * 2 } assert result == [2, 4, 6, 8, 10]}
  32. 32. import groovy.json.* def slurper = new JsonSlurper() def json = slurper.parseText(text)def text = [ assert json == [ ["aaa", "bbb", "ccc"], ["aaa", "bbb", "ccc"], { [ "key1" : "value1", "key1" : "value1", "key2" : "value2", "key2" : "value2", "key3" : "value3", "key3" : "value3", "key4" : "" "key4" : "" }, ], ["ddd", "eee", "fff"] ["ddd", "eee", "fff"]] ]
  33. 33. import groovy.json.*def json = new JsonBuilder()json ( ["aaa", "bbb", "ccc"], [ "key1" : "value1", "key2" : "value2", "key3" : "value3", "key4" : "" ], ["ddd", "eee", "fff"])println json
  34. 34. import org.gcontracts.annotations.*@Invariant({ speed() >= 0 })class Rocket { @Requires({ isStarted() }) @Ensures({ old.speed < speed }) def accelerate() { ... } boolean isStarted() { ... } def speed() { ... }}
  35. 35. def plus2 = { it + 2 }def times3 = { it * 3 }def times3plus2 = plus2 << times3assert times3plus2(3) == 11assert times3plus2(4) == plus2(times3(4))def plus2times3 = times3 << plus2assert plus2times3(3) == 15assert plus2times3(5) == times3(plus2(5))assert times3plus2(3) == (times3 >> plus2)(3)
  36. 36. @Logimport groovy.util.logging.*@Logclass Car { Car() { log.info Car constructed }}def c = new Car() @Commons @Log4j @Slf4j
  37. 37. @Log @Commons @Log4j @Slf4j @Field @PackageScope @AutoClone @AutoExternalizable @ThreadInterrupt @TimedInterrupt @ConditionalInterrupt @ToString@EqualsAndHashCode @TupleConstructor @Canonical @InheritConstructors @WithReadLock @WithWriteLock @ListenerList
  38. 38. Grails
  39. 39. create-app generate-allcreate-controller generate-controllercreate-domain-class generate-viewscreate-script packagecreate-service run-appcreate-tag-lib run-warcreate-unit-test test-app
  40. 40. package myappclass Message { String text static constraints = { text blank:false, maxSize:500 }}
  41. 41. package myappclass MessageController { static scaffold = true}
  42. 42. Gaelyk:Controller log.info "Setting attribute datetime" request.datetime = new Date().toString() log.info "Forwarding to the template" forward /datetime.gtpl
  43. 43. Gaelyk:View<% include /WEB-INF/includes/header.gtpl %><h1>Date / time</h1><p> <% log.info "outputing the datetime attribute" %> The current date and time: ${request.datetime}</p><% include /WEB-INF/includes/footer.gtpl %>
  44. 44. Gaelyk:Datastoreimport com.google.appengine.api.datastore.Entitydef entity = new Entity("person")entity.name = "Kiyotaka Oku"entity.age = 31entity.save()
  45. 45. datastore blobstore memcache oauth urlFetch namespace mail capabilities images channel users files user backendsdefaultQueue lifecycle queues localMode xmpp app
  46. 46. Gaelyk:Datastoreimport com.google.appengine.api.datastore.*import static com.google.appengine.api.datastore.FetchOptions.Builder.*import static com.google.appengine.api.datastore.Query.FilterOperator.*import static com.google.appengine.api.datastore.Query.SortDirection.*def query = new Query(person).with { addFilter age, GREATER_THAN_OR_EQUAL, 30 addSort name, ASCENDING}def persons = datastore.prepare(query).asList(withLimit(21))
  47. 47. Gaelyk:Mailmail.send sender: "kiy0taka333@gmail.com", to: "xxx@example.com", subject: "Hello", textBody: "Hello, how are you doing?"
  48. 48. Gaelyk:TaskQueuedefaultQueue << [ countdownMillis: 1000, url: "/task/dailyEmail", taskName: "Send daily email newsletter", method: PUT, params: [date: 20090914], payload: content]
  49. 49. Griffon
  50. 50. Getting Started$ griffon create-app myapp$ cd myapp$ griffon run-app
  51. 51. Griffon MVCMVC
  52. 52. Gradle
  53. 53. apply plugin: groovyrepositories { mavenCentral()}dependencies { groovy org.codehaus.groovy:groovy:1.8.0 compile junit:junit:4.8.2}
  54. 54. $ gradle clean build:clean:compileJava:compileGroovy:processResources:classes:jar:assemble:compileTestJava:compileTestGroovy:processTestResources:testClasses:test:check:buildBUILD SUCCESSFULTotal time: 13.766 secs
  55. 55. task checksum << { fileList(../antLoadfileResources).each {File file -> ant.checksum(file: file, property: "cs_$file.name") println "$file.name Checksum: ${ant.properties["cs_$file.name"]}" }}task loadfile << { fileList(../antLoadfileResources).each {File file -> ant.loadfile(srcFile: file, property: file.name) println "Im fond of $file.name" }}File[] fileList(String dir) { file(dir).listFiles({file -> file.isFile() } as FileFilter).sort()}
  56. 56. G*Magazine
  57. 57. G*

×