Wednesday, March 17, 2010

Scala Unix file permissions DSL

Scala native DSL capabilities are astonishing. I just created some Scala object to mimic Unix file permission representation and "chmod" interaction, and I'm rather pleased of the result

Only user", "group" and "other" permission are managed - so no setuid, setgid or sticky bit.

Perm objects

They are simple immutable permission objects with a nice toString and an octal representation:
scala> val p:Perm = wx
scala> w.toString // "-wx"
scala> w.octal // 3
You can combine permission to obtain new permissions:
scala> w+r  // rw-
scala> rwx-w // r-w

FilePerms objects

They keep a group of three mutable permissions. You can create a new File permission object from octal values:
scala> val perms = FilePerms(777)

Or from Perm object:
scala> val perms = FilePerms(rw,rw,r)
Like Perm, they have nice string and octal representation:
scala> val perms = FilePerms(777)
scala> perms.toString //rwxrwxrwx
scala> perms.octal // 777

Missing values are initialized to "no permission":
scala> val perms = FilePerms(77) //rwxrwx---
scala> val perms = FilePerms(rw) //rw-------

And of course, you can change permissions:
scala> val perms = FilePerms(77) //rwxrwx---
scala> perms.g-wx  // rwxr----
scala> perms.ugo+x // rwxr-x--x
scala> perms.a-wx  // r--r-----
The binding of theses objects with Java IO with the goal to actually set file permissions is let as an exercise for the reader ;)
It's available on gist here: http://gist.github.com/335791. To test it, simply copy the file content, fire a Scala REPL, start an object declaration, past the content of the file, close you object, import its content, and play:
scala> object p { [enter]
  | [here, past the content of the file]
  .....
  | }
defined module p

scala> import p._
import p._

scala> FilePerms(644)
res0: Option[p.FilePerms] = Some(rw-r--r--)

Enjoy !


PS: if you have an idea about how to mimic chown, I would appreciate. For now, all I get is:
chmod( ug(_)+rw, filePerms)
Not really nice.

Wednesday, January 27, 2010

Scala 2.8 Beta 1 is out !

Today, the first official "Scala 2.8 Beta" was released. It was awaited, and for really good reasons !
Just look at the announcement summary to see all the goodness available: http://www.scala-lang.org/node/4587

I would like to thanks all the people at EPFL and all other contributors around the world to provide us with such a good language and awesome libraries/tools for our daily work - both production ready thanks to Java existing ecosystem and Start-up friendly (ok, no argument here, I just like that language :)

Downloads are available here: http://www.scala-lang.org/downloads

Wednesday, January 20, 2010

Scala and Spring 3 JavaConfig

Scala, Spring 3, Actor, REST, IoC configuration by code...
... and well, I should be able to put some other buzz words in that title, perhaps agile and Scrum ?

I wanted to test Spring 3 "configuration via code" new feature, and especially how well it works with Scala.

As a Tapestry 5 (former) user, I'm a great fan of IoC configuration done in Java (or Scala). From a developer point of view, it's so much more easy and robust (especially refactoring prone) to have access to a real, type safe language in place of an ersatz like XML... and most of the time, it's also much less verbose.

The application
I needed a pretext and decided to build a trivial web application that allows to upload files to some URL, and post-process them - a rather usual need in a web-application. That will allow to test Spring 3 new "REST" features, a bit of Expression Language, and put in a little Scala Actor to let the (long) processing be done asynchronously.
I also used maven, and Slf4j with NoCommonsLogging to be able to use Slf4j with Spring.

What exactly does the application:
- wait uploads on a REST endpoint URL;
- when an upload comes, save it into a temporary file;
- signal the file's availability to a "file processor". That processing may take a looooooong time to process (for example, it's a big XML report, with a lot of parsing, input validation, graphs generation, etc), and so, the processing should be done in a side process.

Results
Everything comes along really well, and the result is available on github.

To test it, you will need a JVM and Maven (if you haven't done it yet and are not limited by production constrains, just go download the last 1.6 JVM (1.6.0_18), the performances improvements with the last two releases are impressive)

% git clone git://github.com/fanf/scala-spring3-upload.git
% cd scala-spring3-upload
% mvn jetty:run
You should have a Jetty server up and running on localhost.
Now, you can upload a file to http://localhost:8080/upload, and see that the upload is processed and HTTP code returned, and then an asynchronous process is launched to process the file - well, actually it just waits 10 seconds and delete it.
On Linux, you can use Curl to post files:

% curl -F FileToUploadName=@/path/to/the/file/to/upload http://localhost:8080/upload/


Details about the project
Here comes the different files and their purpose:

src/main/webapp/WEB-INF/

It contains usual servlet configuration files, in XML :
- web.xml : standard Java servlet config file, nothing to see here. with a reference to the annotation based context loader and the AppConfig.scala file.

- upload-servlet.xml : Spring servlet config file. It's this one that is Spring default enter point, and it contains an entry point to the Scala file used for bean configuration, and to the prope


I just don't understand why this last file is needed. It's where you feel that JavaConfig was an afterthought in Spring... Couldn't have we a convention in place of that ? Something like "put your master configuration code in that package" ? All in all, it's small pain to have to write that file, but it also bring really little information. 


UPDATE: as Chris Beams shows me, I should have RTFM more carefully. It's now possible (I do believed it wasn't in first RCs), and I updated the sources to have a full ScalaBased configuration.

src/main/scala/org/test/upload

That package contains the code to receive file (UploadEndpoint.scala) and process them (FileProcessor.scala).
As you can see, there is very little code, and most of it is self-explanatory.

src/main/scala/org/test/upload/config

That last package only contains the AppConfig.scala file, the place where Spring IoC configuration is done.
I really like the cleaness of that file, compared to what may have been the equivalent XML one.
The default sleepTime value is especially cool, typically a things hard to do when you only have XML

Conclusion
This little project could be a good starting-point if you want to use Spring 3 with Scala, or Slf4j with Spring.
I'm also rather impressed by how much little code is needed with Spring 3 to configured the REST endpoint.
And building little asynchronous services thanks to actor is just too simple (OK, here it's a toy, but if you want serious business with actor, look at AKKA).

Saturday, January 9, 2010

Happy new year 2010

For me, year 2010 will be a big switch in my life... hopefully for the best !
So, I wish you all the best things for this last year of the first third millennium's decade !

Happy new year !

Thursday, November 12, 2009

Bootstrap GWT2 with maven, UiBinder and i18n

I'm setting up a boostrap project for GWT 2 witch would use maven 2, UiBinder and i18n.
The idea is to centralize resources about UiBinder and test GWT 2, the result being used as a template project for real GWT 2 projects.

I'm quite new to GWT (2 days old...), so it is also a learning project. If it could be of any use for you, don't hesitate to use it - or enhance it, or make comments :)

For now, it's based on GWT 2.0.0-ms2, and some configuration may be specific to my environment (Linux&Firefox 3.5).

It's available here: http://github.com/fanf/gwt2-mvn-bootstrap

  © Blogger template 'Minimalist G' by Ourblogtemplates.com 2008

Back to TOP