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