Wiki source code of WebObjects with Scala

Version 399.1 by Ravi Mendis on 2010/01/17 22:07

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.
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 399.1 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.
Ravi Mendis 195.1 32
Ravi Mendis 399.1 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).
Ravi Mendis 318.1 34
Ravi Mendis 294.1 35 === Can WebObjects be Programmed In Scala? ===
Ravi Mendis 195.1 36
Ravi Mendis 288.1 37 Yes. It is very simple.
Ravi Mendis 318.1 38 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
Ravi Mendis 195.1 39
Ravi Mendis 294.1 40 = WebObjects In Scala =
Ravi Mendis 195.1 41
Ravi Mendis 294.1 42 The following highlights some of the differences between Java and Scala in WebObjects:
Ravi Mendis 195.1 43
Ravi Mendis 294.1 44 == EOs in Scala ==
45
Ravi Mendis 318.1 46 === Thread-Safe Shared Vars ===
Ravi Mendis 294.1 47
Ravi Mendis 318.1 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.
Ravi Mendis 294.1 50
Ravi Mendis 359.1 51 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 52
Ravi Mendis 355.1 53 Java:
54
Ravi Mendis 393.1 55 {{code value="java"}}
Ravi Mendis 308.1 56
Ravi Mendis 369.1 57 public class _Talent extends EOGenericRecord {
Ravi Mendis 294.1 58 public static final String ENTITY_NAME = "Talent";
59
60 {{/code}}
61
Ravi Mendis 355.1 62 Scala:
Ravi Mendis 294.1 63
64 {{code}}
65
Ravi Mendis 369.1 66 object Talent extends EOGenericRecord {
Ravi Mendis 294.1 67 val ENTITY_NAME = "Talent"
68
69 {{/code}}
70
Ravi Mendis 318.1 71 ==== Compacted imports ====
Ravi Mendis 308.1 72
Ravi Mendis 318.1 73 Two lines in Java are compacted into one in Scala.
Ravi Mendis 308.1 74
Ravi Mendis 294.1 75 In Java:
76
Ravi Mendis 393.1 77 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 393.1 100 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 312.1 114 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
Ravi Mendis 294.1 115
116 {{/code}}
117
118 ==== Simplified Exception Handling ====
119
120 Scala doesn't force you to catch exceptions unlike in Java.
Ravi Mendis 395.1 121 In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.
Ravi Mendis 294.1 122
123 In Java:
124
Ravi Mendis 393.1 125 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 381.1 159 ==== Scala Annotations vs. Generic Accessors ====
160
Ravi Mendis 385.1 161 An example of accessing variables in WebObjects with the following languages:
Ravi Mendis 381.1 162
Ravi Mendis 385.1 163 |= |= Objective-C |= Java |= Scala
164 |= getter | ##object name## | ##object.name()## | ##object.name##
165 |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName##
Ravi Mendis 381.1 166
Ravi Mendis 385.1 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
Ravi Mendis 353.1 181 == How to Use Scala Collections with EOF ==
Ravi Mendis 351.1 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
Ravi Mendis 318.1 200 == How to Add Scala to a WO Project ==
Ravi Mendis 308.1 201
202 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
203
Ravi Mendis 338.1 204 {{note title="Note"}}
Ravi Mendis 318.1 205
206 This is for Eclipse/WOLips IDE
207
208 {{/note}}
209
Ravi Mendis 290.1 210 == WO Scala Example ==
211
Ravi Mendis 353.1 212 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
Ravi Mendis 292.1 213 All the EO logic and WO components are in Scala.
Ravi Mendis 318.1 214 Only the Application class is Java.
Ravi Mendis 292.1 215
Ravi Mendis 353.1 216 It is based on the D2W Movies example.
217
Ravi Mendis 290.1 218 {{attachments patterns=".*zip"}}{{/attachments}}
Ravi Mendis 294.1 219
220 === Setup ===
221
Ravi Mendis 318.1 222 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
Ravi Mendis 353.1 223 1. Install and start the OpenBase OBMovies database.
Ravi Mendis 294.1 224 1. Right-click on Application.java and run as a WOApplication (as usual).
225
Ravi Mendis 318.1 226 ==== EO Templates ====
Ravi Mendis 294.1 227
228 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
229
Ravi Mendis 357.1 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//##
Ravi Mendis 294.1 231 1. Change the File Names Extension to "scala"
Ravi Mendis 367.1 232 1. In Destination Paths set the Superclass Package (e.g: base)
Ravi Mendis 294.1 233 1. Uncheck Java under Options
Ravi Mendis 385.1 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
Ravi Mendis 393.1 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##