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