Your SlideShare is downloading. ×
 
Introduction to  Dynamic Languages <ul><li>Tugdual Grall </li></ul><ul><li>Oracle Application Server </li></ul><ul><li>Pro...
Simple Example <ul><li>public class FilterApp { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><...
Simple Example <ul><li>def list = [&quot;Olaf&quot;,&quot;Tug&quot;,&quot;John&quot;,&quot;Dave&quot;] </li></ul><ul><li>d...
Why Scripting? <ul><li>Scripting languages are in fashion (again?) </li></ul><ul><ul><li>AJAX with JavaScript™ technology,...
Three Main Use Cases of Integration <ul><li>Prototyping/testing/scripting </li></ul><ul><ul><li>Shell or build scripting, ...
Java Technology & Scripting <ul><li>Leverage Java Platform </li></ul><ul><ul><li>Use scripts inside Java SE and Java EE </...
Why Groovy Is Relevant? <ul><li>Groovy is a dynamic and agile  scripting language for the Java VM </li></ul><ul><li>OSS pr...
Why another Language? <ul><li>Want complete binary compatibility with Java </li></ul><ul><ul><li>No difference from Java a...
Groovy Features <ul><li>Dynamic and (optional) static typing </li></ul><ul><ul><li>int a = 2 </li></ul></ul><ul><ul><li>de...
Groovy Features (Cont.) <ul><li>Regex built-in </li></ul><ul><ul><li>if ( &quot;name&quot; ==~ &quot;na.*&quot; ) { printl...
GDK Software <ul><li>Adds convenient methods to JDK </li></ul><ul><li>String </li></ul><ul><ul><li>contains(), count(), ex...
Groovy Markup <ul><li>Native support for hierarchical structures in code </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul>...
AntBuilder <ul><li>Reuse hundreds of Ant tasks as simple method calls </li></ul><ul><li>Even create your own build system!...
Groovy SQL <ul><li>Easy to use JDBC software thanks to closures </li></ul><ul><li>def sql = Sql.newInstance(url, usr, pwd,...
What is the magic? <ul><li>The dynamic part is coming from the: </li></ul><ul><ul><li>MOP: Meta Object Protocol </li></ul>...
Oracle and Groovy <ul><li>Manage your application server with Groovy </li></ul><ul><li>Groovy Editor for Jdeveloper </li><...
Rapid Web Application Development with
Grails: Introduction <ul><li>Grails: MVC web framework inspired by: </li></ul><ul><ul><ul><li>Convention over Configuratio...
Demonstration <ul><li>Create an application </li></ul>
Project Infrastructure + PROJECT_HOME + grails-app + conf + controllers + domain + i18n + services + taglib + views + lib ...
The Application Domain <ul><li>Domain classes hold state and implement behaviour </li></ul><ul><li>They are linked togethe...
Grails ORM (GORM) <ul><li>Extremely simple. No special class to extend or file to configure! </li></ul><ul><li>class Expen...
Grails ORM (GORM) <ul><li>We’ve got this far, so lets define the other side! </li></ul><ul><li>class ExpenseItem { </li></...
Grails Constraints <ul><li>Validation constraints can be defined using the ‘constraints’ property </li></ul><ul><ul><ul><u...
The Controller <ul><li>The controller and action name map to the URI:  /expenseReport/list </li></ul><ul><li>class Expense...
Data Binding & Flow Control <ul><li>// save action </li></ul><ul><li>@Property  save  = { </li></ul><ul><li>  def e = Expe...
Groovy Server Pages <ul><li>A view technology very similar to JSP, but with Groovy as the primary language  </li></ul><ul>...
A GSP Example <ul><li>The GSP for the  list  action is named according to convention:  grails-app/views/expenseItem/list.g...
Oracle and Grails <ul><li>Grails Application Running on OracleAS </li></ul><ul><li>Development team is working with Spring...
A Q & Q U E S T I O N S A N S W E R S
 
Upcoming SlideShare
Loading in...5
×

Introduction To Groovy 2005

2,004

Published on

A quick introduction to Groovy and Grails made in 2005 to Oracle Consulting folks

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

No Downloads
Views
Total Views
2,004
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Transcript of "Introduction To Groovy 2005"

    1. 2. Introduction to Dynamic Languages <ul><li>Tugdual Grall </li></ul><ul><li>Oracle Application Server </li></ul><ul><li>Product Management </li></ul>
    2. 3. Simple Example <ul><li>public class FilterApp { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>List<String> list = new ArrayList(); </li></ul><ul><li>list.add(&quot;Olaf&quot;); list.add(&quot;Tug&quot;); list.add(&quot;John&quot;); list.add(&quot;Dave&quot;); </li></ul><ul><li> FilterApp filter = new FilterApp(); </li></ul><ul><li>List<String> data = filter.filterLongerThan(list,4); </li></ul><ul><li>System.out.println(data.size()); </li></ul><ul><li>Iterator it = data.iterator(); </li></ul><ul><li>while (it.hasNext()) {System.out.println(it.next());} </li></ul><ul><li>} </li></ul><ul><li>public List filterLongerThan(List list, int length) { </li></ul><ul><li>List<String> result = new ArrayList(); </li></ul><ul><li>Iterator it = list.iterator(); </li></ul><ul><li>while (it.hasNext()) { </li></ul><ul><li>String item = (String)it.next(); </li></ul><ul><li>if (item.length()>= length) {result.add(item);} </li></ul><ul><li>} </li></ul><ul><li>return result; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    3. 4. Simple Example <ul><li>def list = [&quot;Olaf&quot;,&quot;Tug&quot;,&quot;John&quot;,&quot;Dave&quot;] </li></ul><ul><li>def data = list.findAll { it.size() >= 4 } </li></ul><ul><li>println data.size() </li></ul><ul><li>data.each { println it } </li></ul>Java 25-30 Lines / Groovy 4 lines
    4. 5. Why Scripting? <ul><li>Scripting languages are in fashion (again?) </li></ul><ul><ul><li>AJAX with JavaScript™ technology, and Ruby on </li></ul></ul><ul><ul><li>Rails with Ruby </li></ul></ul><ul><li>Java SE 6 integrates JSR 223 with Rhino </li></ul><ul><li>JCP agreed to standardize Groovy (JSR 241) and BeanShell (JSR 274) </li></ul><ul><li>Upcoming bytecode invokeDynamic (JSR 292) </li></ul><ul><li>Microsoft also believe in dynamic languages and hire IronPython lead </li></ul><ul><li>Need simplicity to overcome enterprise development complexity </li></ul>
    5. 6. Three Main Use Cases of Integration <ul><li>Prototyping/testing/scripting </li></ul><ul><ul><li>Shell or build scripting, data manipulation, unit testing, code generation, driving native applications </li></ul></ul><ul><li>Building standalone applications </li></ul><ul><ul><li>Small to mid-sized non-critical applications </li></ul></ul><ul><li>Integrating scripting in Java EE applications </li></ul><ul><ul><li>Programmatic configuration (less XML) </li></ul></ul><ul><ul><li>Business rules externalization </li></ul></ul><ul><ul><li>UI or application customizations </li></ul></ul>
    6. 7. Java Technology & Scripting <ul><li>Leverage Java Platform </li></ul><ul><ul><li>Use scripts inside Java SE and Java EE </li></ul></ul><ul><ul><li>Reuse existing Java technology skills and components </li></ul></ul><ul><li>Many existing languages on the Java VM </li></ul><ul><ul><li>Groovy, Rhino (JavaScript), BeanShell, Jython, JRuby, Pnuts, Scala… </li></ul></ul><ul><li>Current standardization effort around scripting languages and Java technology </li></ul><ul><ul><li>JSR 223, JSR 241, JSR 274, JSR 292 </li></ul></ul><ul><li>Let’s dive in Groovy as an example… </li></ul>
    7. 8. Why Groovy Is Relevant? <ul><li>Groovy is a dynamic and agile scripting language for the Java VM </li></ul><ul><li>OSS project hosted by Codehaus </li></ul><ul><li>Inspired by Ruby,Python and SmallTalk </li></ul><ul><li>Generates bytecode for the Java VM: integrates well with Java libraries </li></ul><ul><li>However, Groovy differentiates itself </li></ul><ul><ul><li>GDK: additional libraries to simplify complex APIs </li></ul></ul><ul><ul><li>MOP: advanced meta-programming features </li></ul></ul>+ + = Expressive Language Powerful Libraries Meta Programming
    8. 9. Why another Language? <ul><li>Want complete binary compatibility with Java </li></ul><ul><ul><li>No difference from Java at JVM/bytecode level </li></ul></ul><ul><ul><li>No wrappers or separate islands of APIs </li></ul></ul><ul><li>Java code-friendly syntax </li></ul><ul><ul><li>Don’t want something totally foreign to Java developers </li></ul></ul><ul><li>Want a scripting language designed: </li></ul><ul><ul><li>For the Java Platform </li></ul></ul><ul><ul><li>By Java developers </li></ul></ul><ul><ul><li>For Java developers </li></ul></ul>
    9. 10. Groovy Features <ul><li>Dynamic and (optional) static typing </li></ul><ul><ul><li>int a = 2 </li></ul></ul><ul><ul><li>def str = &quot;Hello&quot; </li></ul></ul><ul><li>Native syntax for lists, maps, arrays, beans, etc. </li></ul><ul><ul><li>def list = [&quot;Rod&quot;, 3, new Date()] </li></ul></ul><ul><ul><li>def myMap = [Neeta:32, Eric:34] </li></ul></ul><ul><li>Closures </li></ul><ul><ul><li>myMap.each( {name, age -> println &quot;$name is $age years old&quot; }) </li></ul></ul><ul><ul><li>>Eric is 34 years old </li></ul></ul><ul><ul><li>>Neeta is 32 years old </li></ul></ul>
    10. 11. Groovy Features (Cont.) <ul><li>Regex built-in </li></ul><ul><ul><li>if ( &quot;name&quot; ==~ &quot;na.*&quot; ) { println &quot;match!&quot; } </li></ul></ul><ul><ul><li>-> match! </li></ul></ul><ul><li>Operator overloading </li></ul><ul><ul><li>def list = [1, 2, 3] + [4, 5, 6] </li></ul></ul><ul><ul><li>list.each { print it } </li></ul></ul><ul><ul><li>-> 123456 </li></ul></ul><ul><li>Autoboxing and polymorphism across collection, array, map, bean, String, iterators, etc. </li></ul><ul><ul><li>String[] array = ['cat', 'dog', 'mouse'] </li></ul></ul><ul><ul><li>def str = 'hello' </li></ul></ul><ul><ul><li>Println&quot;${array.size()},${str.size()}, ${list.size()} -> 3,5,6 </li></ul></ul>
    11. 12. GDK Software <ul><li>Adds convenient methods to JDK </li></ul><ul><li>String </li></ul><ul><ul><li>contains(), count(), execute(), padLeft(), center(), padRight(), reverse(), tokenize(), each(), etc. </li></ul></ul><ul><li>Collection </li></ul><ul><ul><li>count(), collect(), join(), each(), reverseEach(), find/All(), min(), max(), inject(), sort(), etc. </li></ul></ul><ul><li>File </li></ul><ul><ul><li>eachFile(), eachLine(), withPrintWriter(), write(), getText(), etc. </li></ul></ul><ul><li>Lots there and growing all the time </li></ul><ul><li>You can add methods programmatically </li></ul>
    12. 13. Groovy Markup <ul><li>Native support for hierarchical structures in code </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>XHTML </li></ul></ul><ul><ul><li>Ant </li></ul></ul><ul><ul><li>Swing </li></ul></ul><ul><ul><li>SWT </li></ul></ul><ul><li>Relatively easy to add your own </li></ul>
    13. 14. AntBuilder <ul><li>Reuse hundreds of Ant tasks as simple method calls </li></ul><ul><li>Even create your own build system! </li></ul><ul><li>def ant = new AntBuilder() def patterns = [&quot;**/*.groovy&quot;, &quot;**/*.java&quot;] ant. sequential { mkdir (dir: &quot;dir1&quot;) copy (todir: &quot;dir1&quot;) { fileset (dir: &quot;dir2&quot;) { for(pattern in patterns) include (name: pattern) } } echo (&quot;done&quot;) } </li></ul>
    14. 15. Groovy SQL <ul><li>Easy to use JDBC software thanks to closures </li></ul><ul><li>def sql = Sql.newInstance(url, usr, pwd, driver) sql.execute(&quot;insert into table values ($foo, $bar)&quot;) sql.execute(&quot;insert into table values(?,?)&quot;, [a, b]) sql.eachRow(&quot;select * from EMPLOYEES&quot;) { print it.name } def list = sql.rows(&quot;select * from EMPLOYEES&quot;) </li></ul><ul><li>DataSet notion: poor-man ORM </li></ul><ul><li>def set = sql.dataSet(&quot;EMPLOYEES&quot;) set.add(name: &quot;Johnny&quot;, age: 33) set.each { user -> println user.name } set.findAll { it.age > 22 && it.age < 42 } // LINQ J </li></ul>
    15. 16. What is the magic? <ul><li>The dynamic part is coming from the: </li></ul><ul><ul><li>MOP: Meta Object Protocol </li></ul></ul><ul><li>At runtime, allows you to </li></ul><ul><ul><li>Intercept method calls (even non-existing ones) </li></ul></ul><ul><ul><li>Intercept property accesses </li></ul></ul><ul><ul><li>Inject new methods or new properties </li></ul></ul><ul><li>You can create Domain Specific Languages (DSL) </li></ul><ul><li>With the MOP, come the… builders </li></ul><ul><ul><li>Builder pattern at the syntax level! </li></ul></ul><ul><ul><li>MarkupBuilder, SwingBuilder, AntBuilder... </li></ul></ul>
    16. 17. Oracle and Groovy <ul><li>Manage your application server with Groovy </li></ul><ul><li>Groovy Editor for Jdeveloper </li></ul><ul><li>client = new OC4JClient() </li></ul><ul><li>client.connect(&quot;localhost&quot;,&quot;23791&quot;,&quot;oc4jadmin&quot;,&quot;welcome1&quot;) </li></ul><ul><li>defaultApp = client.helper.createGroovyMBean(client.helper.DEFAULTAPP_MBEAN_NAME); </li></ul><ul><li>println &quot; --> Create ConnectionPool&quot; </li></ul><ul><li>defaultApp.createJDBCConnectionPool(&quot;MyPool&quot;, </li></ul><ul><li>&quot;oracle.jdbc.pool.OracleDataSource&quot;, </li></ul><ul><li>&quot;scott&quot;, </li></ul><ul><li>&quot;tiger&quot;, </li></ul><ul><li>&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;) </li></ul>
    17. 18. Rapid Web Application Development with
    18. 19. Grails: Introduction <ul><li>Grails: MVC web framework inspired by: </li></ul><ul><ul><ul><li>Convention over Configuration </li></ul></ul></ul><ul><ul><ul><li>Don’t Repeat Yourself (DRY) </li></ul></ul></ul><ul><ul><ul><li>Ruby on Rails </li></ul></ul></ul><ul><li>Built on solid foundations: </li></ul><ul><ul><ul><li>Spring IoC, MVC and WebFlow </li></ul></ul></ul><ul><ul><ul><li>Hibernate </li></ul></ul></ul><ul><ul><ul><li>SiteMesh </li></ul></ul></ul>
    19. 20. Demonstration <ul><li>Create an application </li></ul>
    20. 21. Project Infrastructure + PROJECT_HOME + grails-app + conf + controllers + domain + i18n + services + taglib + views + lib + spring + hibernate + src + web-app
    21. 22. The Application Domain <ul><li>Domain classes hold state and implement behaviour </li></ul><ul><li>They are linked together via relationships (e.g. one-to-many) </li></ul><ul><li>In Java domain classes have traditionally been handled by Object-Relational Mapping (ORM) </li></ul><ul><li>Grails provides simple ORM built on Hibernate </li></ul>Source: Please add the source of your data here
    22. 23. Grails ORM (GORM) <ul><li>Extremely simple. No special class to extend or file to configure! </li></ul><ul><li>class ExpenseReport { </li></ul><ul><li>@Property Long id </li></ul><ul><li>@Property Long version </li></ul><ul><li>@Property relatesToMany = [ items :ExpenseItem] </li></ul><ul><li>@Property Set items </li></ul><ul><li>@Property Date submissionDate </li></ul><ul><li>@Property String employeeName </li></ul><ul><li>} </li></ul>
    23. 24. Grails ORM (GORM) <ul><li>We’ve got this far, so lets define the other side! </li></ul><ul><li>class ExpenseItem { </li></ul><ul><li>@Property Long id </li></ul><ul><li>@Property Long version </li></ul><ul><li>@Property belongsTo = ExpenseReport </li></ul><ul><li>@Property String type </li></ul><ul><li>@Property Currency currency </li></ul><ul><li>@Property Integer amount </li></ul><ul><li>} </li></ul>
    24. 25. Grails Constraints <ul><li>Validation constraints can be defined using the ‘constraints’ property </li></ul><ul><ul><ul><ul><li>class ExpenseItem { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>… </li></ul></ul></ul></ul><ul><ul><ul><ul><li>@Property constraints = { type (inList:['travel', 'accomodation']) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>amount (range:1..999) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
    25. 26. The Controller <ul><li>The controller and action name map to the URI: /expenseReport/list </li></ul><ul><li>class ExpenseReportController { </li></ul><ul><li>@Property list = { </li></ul><ul><li> [expenseReports : ExpenseReport.list()] </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    26. 27. Data Binding & Flow Control <ul><li>// save action </li></ul><ul><li>@Property save = { </li></ul><ul><li> def e = ExpenseItem.get(params.id) </li></ul><ul><li>e.properties = params </li></ul><ul><li>if(e. save ()){ redirect(action:show,id:e.id) </li></ul><ul><li>} else { </li></ul><ul><li>render( view: 'create', </li></ul><ul><li> model:[expenseItem:e] ) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    27. 28. Groovy Server Pages <ul><li>A view technology very similar to JSP, but with Groovy as the primary language </li></ul><ul><li>More expressive and concise with support for embedded GStrings & Tags </li></ul><ul><li>Layout support through integration with SiteMesh </li></ul><ul><li>Ability to define dynamic tag libraries </li></ul>Source: Please add the source of your data here
    28. 29. A GSP Example <ul><li>The GSP for the list action is named according to convention: grails-app/views/expenseItem/list.gsp </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><g:each in=&quot;${expenseItems}&quot;> </li></ul><ul><li><p> ${it.type} – amount: ${it.amount} </p> </li></ul><ul><li></g:each> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
    29. 30. Oracle and Grails <ul><li>Grails Application Running on OracleAS </li></ul><ul><li>Development team is working with Spring and Grails developers to integrate Grails and JPA </li></ul><ul><li>http://www.oracle.com/technology/tech/java/grails.html </li></ul>
    30. 31. A Q & Q U E S T I O N S A N S W E R S

    ×