Wiki source code of WebObjects with Scala
Version 407.1 by Ravi Mendis on 2010/01/17 19:19
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | === What is Scala? === | ||
2 | |||
3 | Scala is a language for concurrent computing. | ||
4 | In the day and age of multi-core processors, concurrent computing can't be ignored. | ||
5 | |||
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. | ||
8 | |||
9 | Here's a quick summary: | ||
10 | |||
11 | |= |= Objective-C |= Java |= Scala | ||
12 | |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes | ||
13 | |= Closures | Blocks (//Extension//) | No | Anonymous Functions | ||
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 | ||
18 | |||
19 | Other notable features include: | ||
20 | |||
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 | ||
24 | |||
25 | A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]]. | ||
26 | |||
27 | === Why Use Scala? === | ||
28 | |||
29 | Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging. | ||
30 | |||
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. | ||
33 | |||
34 | So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications. | ||
35 | |||
36 | === Can WebObjects be Programmed In Scala? === | ||
37 | |||
38 | Yes. It is very simple. | ||
39 | Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. | ||
40 | |||
41 | = WebObjects In Scala = | ||
42 | |||
43 | The following highlights some of the differences between Java and Scala in WebObjects: | ||
44 | |||
45 | == EOs in Scala == | ||
46 | |||
47 | === Thread-Safe Shared Vars === | ||
48 | |||
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. | ||
51 | |||
52 | The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. | ||
53 | |||
54 | Java: | ||
55 | |||
56 | {{code value="java"}} | ||
57 | |||
58 | public class _Talent extends EOGenericRecord { | ||
59 | public static final String ENTITY_NAME = "Talent"; | ||
60 | |||
61 | {{/code}} | ||
62 | |||
63 | Scala: | ||
64 | |||
65 | {{code}} | ||
66 | |||
67 | object Talent extends EOGenericRecord { | ||
68 | val ENTITY_NAME = "Talent" | ||
69 | |||
70 | {{/code}} | ||
71 | |||
72 | ==== Compacted imports ==== | ||
73 | |||
74 | Two lines in Java are compacted into one in Scala. | ||
75 | |||
76 | In Java: | ||
77 | |||
78 | {{code value="java"}} | ||
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 | |||
101 | {{code value="java"}} | ||
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 | |||
115 | class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) { | ||
116 | |||
117 | {{/code}} | ||
118 | |||
119 | ==== Simplified Exception Handling ==== | ||
120 | |||
121 | Scala doesn't force you to catch exceptions unlike in Java. | ||
122 | In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions. | ||
123 | |||
124 | In Java: | ||
125 | |||
126 | {{code value="java"}} | ||
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 | |||
160 | ==== Scala Annotations vs. Generic Accessors ==== | ||
161 | |||
162 | An example of accessing variables in WebObjects with the following languages: | ||
163 | |||
164 | |= |= Objective-C |= Java |= Scala | ||
165 | |= getter | ##object name## | ##object.name()## | ##object.name## | ||
166 | |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## | ||
167 | |||
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 | |||
182 | == How to Use Scala Collections with EOF == | ||
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 | |||
201 | == How to Add Scala to a WO Project == | ||
202 | |||
203 | {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} | ||
204 | |||
205 | {{note title="Note"}} | ||
206 | |||
207 | This is for Eclipse/WOLips IDE | ||
208 | |||
209 | {{/note}} | ||
210 | |||
211 | == WO Scala Example == | ||
212 | |||
213 | The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app: | ||
214 | All the EO logic and WO components are in Scala. | ||
215 | Only the Application class is Java. | ||
216 | |||
217 | It is based on the D2W Movies example. | ||
218 | |||
219 | {{attachments patterns=".*zip"}}{{/attachments}} | ||
220 | |||
221 | === Setup === | ||
222 | |||
223 | 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]] | ||
224 | 1. Install and start the OpenBase OBMovies database. | ||
225 | 1. Right-click on Application.java and run as a WOApplication (as usual). | ||
226 | |||
227 | ==== EO Templates ==== | ||
228 | |||
229 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
230 | |||
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//## | ||
232 | 1. Change the File Names Extension to "scala" | ||
233 | 1. In Destination Paths set the Superclass Package (e.g: base) | ||
234 | 1. Uncheck Java under Options | ||
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 | ||
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## |