Wiki source code of WebObjects with Scala
Version 400.1 by Ravi Mendis on 2010/01/17 22:07
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | === What is Scala? === | ||
| 2 | |||
| 3 | Scala is a language for concurrent computing. | ||
| 4 | In the day and age of multi-core processors, concurrent computing can't be ignored. | ||
| 5 | |||
| 6 | Many of Scala's features have been designed with concurrency in mind. | ||
| 7 | Some of these may not be unfamiliar to Objective-C or WebObjects developers. | ||
| 8 | |||
| 9 | Here's a quick summary: | ||
| 10 | |||
| 11 | |= |= Objective-C |= Java |= Scala | ||
| 12 | |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes | ||
| 13 | |= Closures | Blocks (//Extension//) | No | Anonymous Functions | ||
| 14 | |= Static variables | Yes | Yes | No | ||
| 15 | |= Static methods/functions | Yes | Yes | No | ||
| 16 | |= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//)| //Threads// |[[Actors>>http://en.wikipedia.org/wiki/Actor_model]] | ||
| 17 | |= |= Weakly Typed |=--Strongly Typed--|= Strongly Typed | ||
| 18 | |||
| 19 | Other notable features include: | ||
| 20 | |||
| 21 | |= |= Objective-C |= Java |= Scala | ||
| 22 | |= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)// | ||
| 23 | |= Class composition | Categories | Interfaces | Traits | ||
| 24 | |||
| 25 | A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]]. | ||
| 26 | |||
| 27 | === Why Use Scala? === | ||
| 28 | |||
| 29 | Scala is inherently thread-safe. | ||
| 30 | Because of the lack of static variables developers don't need to worry about synchronising access to mutable shared data. | ||
| 31 | It has concurrency that is effectively built-in to the language. | ||
| 32 | |||
| 33 | So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications. (In other words, Scala Actors can be used for problems that would have normally required threads). | ||
| 34 | |||
| 35 | === Can WebObjects be Programmed In Scala? === | ||
| 36 | |||
| 37 | Yes. It is very simple. | ||
| 38 | Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. | ||
| 39 | |||
| 40 | = WebObjects In Scala = | ||
| 41 | |||
| 42 | The following highlights some of the differences between Java and Scala in WebObjects: | ||
| 43 | |||
| 44 | == EOs in Scala == | ||
| 45 | |||
| 46 | === Thread-Safe Shared Vars === | ||
| 47 | |||
| 48 | Scala doesn't have static variables or methods. However, a class can have a //Companion Object// that will allow you to achieve something equivalent to static variables. | ||
| 49 | One of the advantages of this approach is that it is **thread-safe**, so you don't have to worry about synchronizing access to these fields in a concurrent application. | ||
| 50 | |||
| 51 | The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. | ||
| 52 | |||
| 53 | Java: | ||
| 54 | |||
| 55 | {{code value="java"}} | ||
| 56 | |||
| 57 | public class _Talent extends EOGenericRecord { | ||
| 58 | public static final String ENTITY_NAME = "Talent"; | ||
| 59 | |||
| 60 | {{/code}} | ||
| 61 | |||
| 62 | Scala: | ||
| 63 | |||
| 64 | {{code}} | ||
| 65 | |||
| 66 | object Talent extends EOGenericRecord { | ||
| 67 | val ENTITY_NAME = "Talent" | ||
| 68 | |||
| 69 | {{/code}} | ||
| 70 | |||
| 71 | ==== Compacted imports ==== | ||
| 72 | |||
| 73 | Two lines in Java are compacted into one in Scala. | ||
| 74 | |||
| 75 | In Java: | ||
| 76 | |||
| 77 | {{code value="java"}} | ||
| 78 | |||
| 79 | import com.webobjects.eocontrol.EOGenericRecord; | ||
| 80 | import com.webobjects.eocontrol.EORelationshipManipulation; | ||
| 81 | |||
| 82 | {{/code}} | ||
| 83 | |||
| 84 | In Scala: | ||
| 85 | |||
| 86 | {{code}} | ||
| 87 | |||
| 88 | import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation} | ||
| 89 | |||
| 90 | {{/code}} | ||
| 91 | |||
| 92 | == WOComponents in Scala == | ||
| 93 | |||
| 94 | ==== Compact Constructors ==== | ||
| 95 | |||
| 96 | Scala allows for simpler use of multi-valued constructors than Java. | ||
| 97 | |||
| 98 | In Java: | ||
| 99 | |||
| 100 | {{code value="java"}} | ||
| 101 | |||
| 102 | public class MenuHeader extends WOComponent { | ||
| 103 | |||
| 104 | public MenuHeader(WOContext aContext) { | ||
| 105 | super(aContext); | ||
| 106 | } | ||
| 107 | |||
| 108 | {{/code}} | ||
| 109 | |||
| 110 | In Scala: | ||
| 111 | |||
| 112 | {{code}} | ||
| 113 | |||
| 114 | class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) { | ||
| 115 | |||
| 116 | {{/code}} | ||
| 117 | |||
| 118 | ==== Simplified Exception Handling ==== | ||
| 119 | |||
| 120 | Scala doesn't force you to catch exceptions unlike in Java. | ||
| 121 | In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions. | ||
| 122 | |||
| 123 | In Java: | ||
| 124 | |||
| 125 | {{code value="java"}} | ||
| 126 | |||
| 127 | try { | ||
| 128 | EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session()); | ||
| 129 | epi.setNextPage(context().page()); | ||
| 130 | nextPage = (WOComponent) epi; | ||
| 131 | } catch (IllegalArgumentException e) { | ||
| 132 | ErrorPageInterface epf = D2W.factory().errorPage(session()); | ||
| 133 | epf.setMessage(e.toString()); | ||
| 134 | epf.setNextPage(context().page()); | ||
| 135 | nextPage = (WOComponent) epf; | ||
| 136 | } | ||
| 137 | |||
| 138 | {{/code}} | ||
| 139 | |||
| 140 | In Scala: | ||
| 141 | |||
| 142 | {{code}} | ||
| 143 | |||
| 144 | try { | ||
| 145 | var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session) | ||
| 146 | epi.setNextPage(context.page) | ||
| 147 | nextPage = epi.asInstanceOf[WOComponent] | ||
| 148 | } catch { | ||
| 149 | case e: IllegalArgumentException => { | ||
| 150 | var epf: ErrorPageInterface = D2W.factory.errorPage(session) | ||
| 151 | epf.setMessage(e.toString) | ||
| 152 | epf.setNextPage(context.page) | ||
| 153 | nextPage = epf.asInstanceOf[WOComponent] | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | {{/code}} | ||
| 158 | |||
| 159 | ==== Scala Annotations vs. Generic Accessors ==== | ||
| 160 | |||
| 161 | An example of accessing variables in WebObjects with the following languages: | ||
| 162 | |||
| 163 | |= |= Objective-C |= Java |= Scala | ||
| 164 | |= getter | ##object name## | ##object.name()## | ##object.name## | ||
| 165 | |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## | ||
| 166 | |||
| 167 | Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention. | ||
| 168 | In scala there is an additional convenience we may use to produce "get" and "set" methods in addition to the default Scala accessors - Scala Annotations. | ||
| 169 | |||
| 170 | E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods. | ||
| 171 | These variables can then be accessed via //KVC//. | ||
| 172 | |||
| 173 | {{code}} | ||
| 174 | |||
| 175 | @BeanProperty var username = new String() | ||
| 176 | @BeanProperty var password = new String() | ||
| 177 | @BeanProperty var isAssistantCheckboxVisible = false | ||
| 178 | |||
| 179 | {{/code}} | ||
| 180 | |||
| 181 | == How to Use Scala Collections with EOF == | ||
| 182 | |||
| 183 | One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map##, ##Seq## and ##Set##. | ||
| 184 | Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging. | ||
| 185 | |||
| 186 | But one may modify the EO templates to produce API such as: | ||
| 187 | |||
| 188 | {{code}} | ||
| 189 | |||
| 190 | def movies: NSArray[EOGenericRecord] = { | ||
| 191 | storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[EOGenericRecord]] | ||
| 192 | } | ||
| 193 | |||
| 194 | def moviesList: List[EOGenericRecord] = { | ||
| 195 | movies.objects.toList | ||
| 196 | } | ||
| 197 | |||
| 198 | {{/code}} | ||
| 199 | |||
| 200 | == How to Add Scala to a WO Project == | ||
| 201 | |||
| 202 | {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} | ||
| 203 | |||
| 204 | {{note title="Note"}} | ||
| 205 | |||
| 206 | This is for Eclipse/WOLips IDE | ||
| 207 | |||
| 208 | {{/note}} | ||
| 209 | |||
| 210 | == WO Scala Example == | ||
| 211 | |||
| 212 | The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app: | ||
| 213 | All the EO logic and WO components are in Scala. | ||
| 214 | Only the Application class is Java. | ||
| 215 | |||
| 216 | It is based on the D2W Movies example. | ||
| 217 | |||
| 218 | {{attachments patterns=".*zip"}}{{/attachments}} | ||
| 219 | |||
| 220 | === Setup === | ||
| 221 | |||
| 222 | 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]] | ||
| 223 | 1. Install and start the OpenBase OBMovies database. | ||
| 224 | 1. Right-click on Application.java and run as a WOApplication (as usual). | ||
| 225 | |||
| 226 | ==== EO Templates ==== | ||
| 227 | |||
| 228 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
| 229 | |||
| 230 | 1. Point to the local [[Scala versions>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]] of the .eotemplate files for ##Entity## and ##//Entity//## | ||
| 231 | 1. Change the File Names Extension to "scala" | ||
| 232 | 1. In Destination Paths set the Superclass Package (e.g: base) | ||
| 233 | 1. Uncheck Java under Options | ||
| 234 | |||
| 235 | == How to Build & Deploy a WebObjects Scala Project with Ant == | ||
| 236 | |||
| 237 | 1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala | ||
| 238 | 1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file | ||
| 239 | 1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file | ||
| 240 | 1. Run from the project directory: ##sudo ant clean install## |