What is Sling?

posted 08:15PM Jan 08, 2008 with tags javascript jcr opensource osgi rest sling software by Lars Trieloff

I had a short e-mail exchange with Peter Svenson yesterday, who has some really interesting ideas on web development, Javascript and frameworks and wanted to introduce Sling to him. Unfortunately the Sling website is a bit terse on "big picture" introductions, so I though I share my introduction with my blog readers.
What is Sling? This is a fairly complex issue, because Sling combines many aspects. The main idea is that Sling is a web framework that uses a JCR container such as Apache Jackrabbit as repository. The interesting part here is JCR, which is a storage API that allows you to store nodes which can have child nodes and properties, so the best way is to imagine a JCR repository as a giant storage tree or as a file system on stereoids, as it supports typing, transactions, versioning, full-text-search, oberservation and so on. The main role of Sling is being a translator between the storage repository and the client application and this means representing the Nodes in the JCR tree (that have a path) as Resources (as in REST) that have a URI. ;:A resource can have different representations, for instance a blog can have the HTML representation (blog.html) and the Atom feed representation (blog.atom). Representations are not limited to the resource type, there could be another HTML representation, for example the archive page (blog.archive.html). Representations of a resource are generated on GET requests and there are two high-level ways to do it: Servlets (the classical, heavy-weight Java way) or Scripts and Templates. Among the script engines currently supported are Rhino (Javascript), Ruby, Velocity and Freemarker. Basically, there is a script file or servlet for each representation (in our example html.js, html.archive.js and atom.js could be imaginable). If you want to create a new resource type, e.g. blog entry, you just have to create a new folder and put the appropriately named scripts into this folder. Secondly, a resource can have different behaviors. Again, behaviours are implemented either via Servlets or Scripts. The default behavior is to create a node when posting content to a collection, replacing a node when sending a PUT to a resource and deleting a node when sending a DELETE request. You are, however able to override this default behavior by placing scripts into the right folder. These scripts could for instance validate comment contents before creating the node. This is basically where server side scripting comes into play.
Microjax is based on a clever default handling of certain requests. This means representing resources as JSON objects on GET request and processing JSON request entities for PUT and POST. Additionally, Sling and Microjax introduce two Javascript-based templating mechanisms. Sling comes with the built-in ESP, server-side templating mechanism that will allow you to mash up HTML with embedded Javascript that will be executed server-side, e.g.
<ul>
<% foreach (child in node.children) {
 %><a href="${child.path}"><%= child.properties.name %></a><%
} %>
</ul>
Microsling extends this pattern by introducing EST, which allows you to write the same script, which will then be transformed by the server into a proper Javascript with lots of document.write(...) expressions that can be executed on the client-side.

To sum it up: Take a look at Sling, it is an interesting way to build content-centric applications.

| Comments[3]

Count Characters, Words and Pages with a simple bookmarket

posted 03:11PM Jan 07, 2008 with tags bookmarklet javascript tips by Lars Trieloff

If you are writing texts online, you may come across forms that require a specific minimum or maximum number of pages, words or characters in a text field or textarea. The Count Words bookmarklet is designed to help you with meeting this requirement. As with all bookmarks, all you have to do is pulling the bookmarklet into your bookmarks bar, when you need it, click the bookmarklet and it will display how many characters, words and pages you have written for each individual text field or textarea.

For the curious: I've used Dead Edward's Packer to compress the bookmarklet, the original source code looks like this:

function f() {
  var D=document,i,f,j,e;
  
  function S(e) {
    if(!e.N) {
      var x=D.createElement("span");
      var s=x.style;
      s.color="#333333";
      s.border="1px solid #aaaaaa";
      s.background="#cccccc";
      s.font="10pt sans-serif";
      s.verticalAlign="top";
      //s.float="left";
      //s.clear="left";
      e.parentNode.insertBefore(x,e.nextSibling);
      function u() {
        x.innerHTML=e.value.split(" ").length + " words, " + e.value.length + " 
characters, " + (Math.floor(e.value.length / (1800 / 2))/2) + " pages";
      }
      u();
      e.onchange=u;
      e.onkeyup=u;
      e.oninput=u;
      e.N=x;
    } else {
      e.parentNode.removeChild(e.N);
      e.N=0;
    }
  }
  
  for(i=0;f=D.forms[i];++i) {
    for(j=0;e=f[j];++j)
      if(e.type=="text"||e.type=="password"||e.tagName.toLowerCase()=="textarea"
)
        S(e);
  }
}

f();

Observerations of the day

posted 05:58PM Oct 22, 2007 with tags farming java javascript jcr softwaredevlopment by Lars Trieloff

  • despite the name, Java is not made for Java Content Repository API, you need something more dynamic
  • Is there something like JSDoc for Rhino, that allows me to generate documentation for my Scriptable implementations automatically?
  • Farm Fetish
There are four times as many Americans living in urban than rural areas. There are four times as many people sucking back coffee in New York city alone than make a living farming. According to the Burea of Labor, there are just as many people employed in Architecture and Engineering as farming, hell, 3 million people working in Computer and Mathematical jobs. But when one of these "What does America think about culture" pieces comes on, do I ever see a mid-30's software engineer onscreen bitching about having to download BitTorrents of "The IT Crowd"?
  • In the web office market, Google docs and Google spreadsheets hold a market share of 92%. The report calls these "online collaborative productivity tools". Interesting segmentation.

ESP (Javascript Template Language) syntax highlighting for jEdit

posted 12:48PM Oct 19, 2007 with tags esp javascript jedit jste microsling by Lars Trieloff

I started playing around with Bertrand's microsling (P.S. do not google for the name of the project), which is a minimalized version of the Apache Sling (incubating) framework. Microsling supports two templating languages, Velocity and ESP which is a JSTE-inspired JSP-like syntax for Javascript. As I am a fan of server-side Javascript, I opted for the ESP template language and noted there is no syntax highlighting support in my editor.

With jEdit this is no problem, however. I just copied and adjusted the JSP syntax highlighting definition file, adopted it to ESP and got fine syntax highlighting for ESP files.

http://weblogs.goshaky.com/weblogs/lars/resource/esp-syntax.png

All you need to do is download esp.xml, put it into your jEdit-modes directory (either in the jEdit application folder or in $HOME/.jedit/), and add following line to the catalog file in the same directory:

<MODE NAME="esp" FILE="esp.xml" FILE_NAME_GLOB="*.esp" />

Update: As I learned from Felix, ESP is not inspired by JSTE, it is just very similar. In fact ESP is being used since 2001 by Day. And as I learned from Betrand, it is not Bertrand's microsling, he was just the first to write about it.

| Comments[3]

DAX presentation from CocoonGT

posted 12:30PM Oct 05, 2007 with tags cocoon cocoongt2007 conference dax java javascript opensource rome xslt by Lars Trieloff

I just had my first presentation at this year's Cocoon GetTogether in the Rome Bioparco about DAX - Declarative API for XML. I've uploaded the slides to slideshare.net and would invite all other presenters to do the same and add their presentations to the newly created Cocoon group at slideshare.net

Phobos: Another Server-side Javascript framework I did not know

posted 10:42PM Jul 08, 2007 with tags javascript web20 webdevelopment by Lars Trieloff

Javascript seems to be the hottest language on the web these days. Not on the client side, but on the server side. Today I came across Phobos, a server-side Javascript framework with good integration into the Netbeans IDE.

Some other interesting frameworks with server-side Javascript support are:

The advantage of server-side Javascript frameworks: You can use one language for much of the web programming on the client and on the server-side, Javascript is easy to learn, yet powerful and incredibly flexible and as most of these frameworks are powered by Rhino, you have access to all libraries running in the JVM, and that is a lot.

Blogging from Linux Tag: Javascript for Web 2.0

posted 11:49AM Jun 02, 2007 with tags javascript linuxtag by Lars Trieloff

Christian Wenz: Grüße aus der Gruft: Javascript in Zeiten des Web 2.0. Things I did not know:

Everything I learned about Javascript, I learned on the web

posted 10:39AM May 22, 2007 with tags javascript softwaredevelopment webdevelopment by Lars Trieloff

Yesterday, during an interview I said regarding the fact that, according to Ohloh our applications is written mostly in Javascript: "Javascript is an impressive elegant, but often misunderstood language.". The applicant answed that this was the first time he heard someone saying that, but recent signs show I am not alone with my opinion:

The interesting thing is: I have been developing in Javascript for nearly all of my programmer's life, but have never possessed a single Javascript book. This is part due to a misunderstanding of the language ("It's just a toy language for scripting web pages" - which it is not) and part due to the great resources on the web for developing Javascript.

With more and more applications built unpon Javascript, server-side, client side in web sites, in rich internet applications based on Firefox, Thunderbird or XULRunner or Actionscript, developers will have to learn about the beauty of Javascript, but also about the dark sides.

| Comments[1]

Software Development Trends 2007

posted 09:58AM Jan 02, 2007 with tags 2007 cocoon javascript jcr spring by Lars Trieloff

Happy new year to all my readers. This morning, a pice by Carlos E. Perez caught my eye:Software Development Trends for 2007. Some of the trends are already catched up by Mindquarry, e.g.
  • Dynamic Programming Languages - Mindquarry's core functionality is written in dynamic languages like Javascript and declarative programming languages like XSLT and JX
  • Java Content Repository - Mindquarry uses Apache Jackrabbit, because "Organizations will begin to worry about their creaky poorly supported CMS and begin towards safeguarding their content assets by migrating to a JCR implementation."
  • Life after EJB - Mindquarry uses Cocoon 2.2 which in turn uses Spring as a component framework and we are very happy with it.

Firebug 1.0 beta

posted 08:08PM Dec 06, 2006 with tags css debug firebug firefox javascript opensource by Lars Trieloff

Firebug 1.0 beta the ultimate Firefox extension for web developers that helps with CSS tweaking, Javascript debugging and load time optimization (new in Firebug 1.0) has been released. If you are already using Firebug, I recommend thinking a minute or two about the invaluable help Firebug is and has been and donate to this project to keep it up and running.