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