Wiki source code of WebObjects with Scala

Version 361.1 by Ravi Mendis on 2010/01/17 19:56

Hide last authors
Ravi Mendis 21.1 1 === What is Scala? ===
Ravi Mendis 195.1 2
Ravi Mendis 359.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 359.1 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.
Ravi Mendis 294.1 8
Ravi Mendis 359.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 338.1 29 Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.
Ravi Mendis 195.1 30
Ravi Mendis 359.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 359.1 34 So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications.
35 (In other words, Scala can be used for problems that would normally have required threads).
Ravi Mendis 338.1 36
Ravi Mendis 294.1 37 === Can WebObjects be Programmed In Scala? ===
Ravi Mendis 195.1 38
Ravi Mendis 288.1 39 Yes. It is very simple.
Ravi Mendis 318.1 40 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
Ravi Mendis 195.1 41
Ravi Mendis 294.1 42 = WebObjects In Scala =
Ravi Mendis 195.1 43
Ravi Mendis 294.1 44 The following highlights some of the differences between Java and Scala in WebObjects:
Ravi Mendis 195.1 45
Ravi Mendis 294.1 46 == EOs in Scala ==
47
Ravi Mendis 318.1 48 === Thread-Safe Shared Vars ===
Ravi Mendis 294.1 49
Ravi Mendis 318.1 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.
Ravi Mendis 294.1 52
Ravi Mendis 359.1 53 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 54
Ravi Mendis 355.1 55 Java:
56
Ravi Mendis 359.1 57 {{code value="java"}}
Ravi Mendis 308.1 58
Ravi Mendis 318.1 59 public class _Talent extends EOGenericRecord {
Ravi Mendis 294.1 60 public static final String ENTITY_NAME = "Talent";
61
62 {{/code}}
63
Ravi Mendis 355.1 64 Scala:
Ravi Mendis 294.1 65
66 {{code}}
67
Ravi Mendis 359.1 68 object Talent extends EOGenericRecord {
Ravi Mendis 294.1 69 val ENTITY_NAME = "Talent"
70
71 {{/code}}
72
Ravi Mendis 318.1 73 ==== Compacted imports ====
Ravi Mendis 308.1 74
Ravi Mendis 318.1 75 Two lines in Java are compacted into one in Scala.
Ravi Mendis 308.1 76
Ravi Mendis 294.1 77 In Java:
78
Ravi Mendis 359.1 79 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 359.1 102 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 312.1 116 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
Ravi Mendis 294.1 117
118 {{/code}}
119
120 ==== Simplified Exception Handling ====
121
122 Scala doesn't force you to catch exceptions unlike in Java.
Ravi Mendis 338.1 123 In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.
Ravi Mendis 294.1 124
125 In Java:
126
Ravi Mendis 359.1 127 {{code value="java"}}
Ravi Mendis 294.1 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
Ravi Mendis 359.1 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
Ravi Mendis 353.1 183 == How to Use Scala Collections with EOF ==
Ravi Mendis 351.1 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
Ravi Mendis 318.1 202 == How to Add Scala to a WO Project ==
Ravi Mendis 308.1 203
204 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
205
Ravi Mendis 338.1 206 {{note title="Note"}}
Ravi Mendis 318.1 207
208 This is for Eclipse/WOLips IDE
209
210 {{/note}}
211
Ravi Mendis 290.1 212 == WO Scala Example ==
213
Ravi Mendis 353.1 214 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
Ravi Mendis 292.1 215 All the EO logic and WO components are in Scala.
Ravi Mendis 318.1 216 Only the Application class is Java.
Ravi Mendis 292.1 217
Ravi Mendis 353.1 218 It is based on the D2W Movies example.
219
Ravi Mendis 290.1 220 {{attachments patterns=".*zip"}}{{/attachments}}
Ravi Mendis 294.1 221
222 === Setup ===
223
Ravi Mendis 318.1 224 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
Ravi Mendis 353.1 225 1. Install and start the OpenBase OBMovies database.
Ravi Mendis 294.1 226 1. Right-click on Application.java and run as a WOApplication (as usual).
227
Ravi Mendis 318.1 228 ==== EO Templates ====
Ravi Mendis 294.1 229
230 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
231
Ravi Mendis 357.1 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//##
Ravi Mendis 294.1 233 1. Change the File Names Extension to "scala"
Ravi Mendis 359.1 234 1. In Destination Paths set the Superclass Package (e.g: base)
Ravi Mendis 294.1 235 1. Uncheck Java under Options
Ravi Mendis 359.1 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##