Wiki source code of WebObjects with Scala

Version 414.1 by Ravi Mendis on 2010/01/14 23:00

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