Wiki source code of WebObjects with Scala

Version 411.1 by Ravi Mendis on 2010/01/14 22:59

Hide last authors
Ravi Mendis 21.1 1 === What is Scala? ===
Ravi Mendis 195.1 2
Ravi Mendis 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.
Ravi Mendis 195.1 6
Ravi Mendis 409.1 7 Many of its features and paradigms favor multi-threading and concurrency.
Ravi Mendis 411.1 8 It could be said that Scala was designed from the ground up for concurrency.
Ravi Mendis 294.1 9
Ravi Mendis 411.1 10 Some of these features may not be unfamiliar to Objective-C or WebObjects developers. Here's a summary:
Ravi Mendis 393.1 11
Ravi Mendis 338.1 12 |= |= Objective-C |= Java |= Scala
13 |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes
Ravi Mendis 349.1 14 |= Closures | Blocks (//Extension//) | No | Anonymous Functions
Ravi Mendis 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
Ravi Mendis 294.1 19
20 Other notable features include:
21
Ravi Mendis 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
Ravi Mendis 294.1 25
Ravi Mendis 338.1 26 A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]].
27
Ravi Mendis 195.1 28 === Why Use Scala? ===
29
Ravi Mendis 411.1 30 With Web 2.0, building concurrent WebObjects applications is a must.
Ravi Mendis 401.1 31 Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.
Ravi Mendis 195.1 32
Ravi Mendis 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.
Ravi Mendis 318.1 35
Ravi Mendis 401.1 36 So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications.
37
Ravi Mendis 294.1 38 === Can WebObjects be Programmed In Scala? ===
Ravi Mendis 195.1 39
Ravi Mendis 288.1 40 Yes. It is very simple.
Ravi Mendis 318.1 41 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
Ravi Mendis 195.1 42
Ravi Mendis 294.1 43 = WebObjects In Scala =
Ravi Mendis 195.1 44
Ravi Mendis 294.1 45 The following highlights some of the differences between Java and Scala in WebObjects:
Ravi Mendis 195.1 46
Ravi Mendis 294.1 47 == EOs in Scala ==
48
Ravi Mendis 318.1 49 === Thread-Safe Shared Vars ===
Ravi Mendis 294.1 50
Ravi Mendis 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.
Ravi Mendis 294.1 53
Ravi Mendis 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.
Ravi Mendis 294.1 55
Ravi Mendis 355.1 56 Java:
57
Ravi Mendis 393.1 58 {{code value="java"}}
Ravi Mendis 308.1 59
Ravi Mendis 369.1 60 public class _Talent extends EOGenericRecord {
Ravi Mendis 294.1 61 public static final String ENTITY_NAME = "Talent";
62
63 {{/code}}
64
Ravi Mendis 355.1 65 Scala:
Ravi Mendis 294.1 66
67 {{code}}
68
Ravi Mendis 369.1 69 object Talent extends EOGenericRecord {
Ravi Mendis 294.1 70 val ENTITY_NAME = "Talent"
71
72 {{/code}}
73
Ravi Mendis 318.1 74 ==== Compacted imports ====
Ravi Mendis 308.1 75
Ravi Mendis 318.1 76 Two lines in Java are compacted into one in Scala.
Ravi Mendis 308.1 77
Ravi Mendis 294.1 78 In Java:
79
Ravi Mendis 393.1 80 {{code value="java"}}
Ravi Mendis 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
Ravi Mendis 393.1 103 {{code value="java"}}
Ravi Mendis 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
Ravi Mendis 312.1 117 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
Ravi Mendis 294.1 118
119 {{/code}}
120
121 ==== Simplified Exception Handling ====
122
123 Scala doesn't force you to catch exceptions unlike in Java.
Ravi Mendis 395.1 124 In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.
Ravi Mendis 294.1 125
126 In Java:
127
Ravi Mendis 393.1 128 {{code value="java"}}
Ravi Mendis 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
Ravi Mendis 381.1 162 ==== Scala Annotations vs. Generic Accessors ====
163
Ravi Mendis 385.1 164 An example of accessing variables in WebObjects with the following languages:
Ravi Mendis 381.1 165
Ravi Mendis 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##
Ravi Mendis 381.1 169
Ravi Mendis 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
Ravi Mendis 353.1 184 == How to Use Scala Collections with EOF ==
Ravi Mendis 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
Ravi Mendis 318.1 203 == How to Add Scala to a WO Project ==
Ravi Mendis 308.1 204
205 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
206
Ravi Mendis 338.1 207 {{note title="Note"}}
Ravi Mendis 318.1 208
209 This is for Eclipse/WOLips IDE
210
211 {{/note}}
212
Ravi Mendis 290.1 213 == WO Scala Example ==
214
Ravi Mendis 353.1 215 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
Ravi Mendis 292.1 216 All the EO logic and WO components are in Scala.
Ravi Mendis 318.1 217 Only the Application class is Java.
Ravi Mendis 292.1 218
Ravi Mendis 353.1 219 It is based on the D2W Movies example.
220
Ravi Mendis 290.1 221 {{attachments patterns=".*zip"}}{{/attachments}}
Ravi Mendis 294.1 222
223 === Setup ===
224
Ravi Mendis 318.1 225 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
Ravi Mendis 353.1 226 1. Install and start the OpenBase OBMovies database.
Ravi Mendis 294.1 227 1. Right-click on Application.java and run as a WOApplication (as usual).
228
Ravi Mendis 318.1 229 ==== EO Templates ====
Ravi Mendis 294.1 230
231 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
232
Ravi Mendis 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//##
Ravi Mendis 294.1 234 1. Change the File Names Extension to "scala"
Ravi Mendis 367.1 235 1. In Destination Paths set the Superclass Package (e.g: base)
Ravi Mendis 294.1 236 1. Uncheck Java under Options
Ravi Mendis 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
Ravi Mendis 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##