Wiki source code of WebObjects with Scala

Version 407.1 by Ravi Mendis on 2010/01/17 19:19

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