This was originally posted on my dev2dev blog February 6th, 2008.
At the end of my previous post about the hype/buzz for alternative programming languages I promised an entry on my experimenting with Scala in WebLogic. I have only spent a few hours of weekend time playing around, so I can't say much yet, but I'll document some of my first impressions and experiences in this post. This is not meant to be a post on learning Scala, For that I recommend an excellent podcast from Bill Venners that goes into great detail about Scala and why you might use it. I highly recommend it, it is well-worth the listen. If you prefer reading, then start with The busy Java developer's guide to Scala: Functional programming for the object oriented. This is a post that documents my experience of using Scala with tooling and an application server familiar to java developers, namely Eclipse and WebLogic Server.
After restarting Eclipse because of the new plug-ins, I didn't have to set any paths to configure the Scala Plugin as the set of 5 scala plugins installed include the Scala SDK. Simply follow the instructions at the bottom of the Scala Plugin for Eclipse page to create your first HelloWorld application to make sure that everything is wired up correctly. I did not encounter a single snag with HelloWorld at all and my console printed "HelloWorld" in no time flat.
The scalac compiler takes .scala files and builds .class files that have a dependency on the scala-library.jar. Go check out the bin directory in the Resource perspective and see for yourself. Therefore, the only thing you need to invoke and run them from java is to have the .class files and the scala-library.jar on your classpath. The converse is also true; Scala objects can just as easily invoke java objects by including them on the scala classpath.
Now this is where the developer experience is not quite ideal with respect to the packaging scala artifacts to be used easily in Dynamic Web Projects for iterative development with WebLogic Server. First of all, I could not find a non-custom way of exporting the Scala .class files into a jar file. In order to package up my Scala Project into a jar with the IDE I had to:
Right click on the project -> Export... -> General -> Archive File -> and select appropriate values as shown in multiple places.
Is this really that big of a deal? Well, it depends on how you use your IDE to develop. In the normal process of doing Java web development in the IDE, I find it convenient to have a project with Java code, say a Utility project, which is automatically included in your Dynamic Web Project's classpath and packaged up in the EAR as a jar when I do an export or a deployment. It doesn't appear that such a nice arrangement is possible using the default tooling. Of course you can always create a custom ant builder yourself and attach this to your project's build activity, but there is some work involved. Certainly this will be addressed as the Scala Plugin for Eclipse matures.
Trying out some various alternatives, I was able to modify the .project xml file for the Scala project and add a Java nature and a java builder so that both .java source and .scala source were compiled into the same bin directory. This seemed a little fragile and the build order had to be just right in order for both sets of .class files to be in the bin directory after the build. I suspect that when the build order was reversed that the bin directory was being cleaned by one of the builders. Here is the .project file if you feel like taking this approach.
<?xml version="1.0" encoding="UTF-8"?>
After deciding not to spend more time on the packaging issues I moved on to another basic concept, simply calling a Scala object from a JSP. In this case there is a suitable example addressbook.scala on the Scala site that spits out an addressbook in Xhtml. This example also showcases one of the Scala languages handy features of native XML support. Check out the source. In order to invoke this quickly from a JSP, I moved it to a package called examples and created a new function named toXhtml to the AddressBook class to return the Xhtml string since the example was meant to be called from a main method and needed the function to be callable from the JSP.
def toXhtml(): String = page.toString
Here is the index.jsp
<%= addressbook.toXhtml() %>
The steps are:
Here is the end result:
There is nothing really WebLogic Server specific to this, I could have just as easily deployed this to Tomcat., or WebLogic Event Server. The point is that Scala actually is fairly easy to integrate into both tools and runtimes that most java developers will already be familiar with. If you have thoughts on Scala, better integration with java IDE's, etc, drop a comment.