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



Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 0 (more)