poornerd

my thoughts on programming and other nerdy stuff

May 15, 2013
by poornerd
0 comments

Proof that Java, Scala and Play! Framework Developers do NOT use Internet Explorer!

browser_statsI was (almost) shocked when I looked at the sta­tis­tics of which Browsers are being used to access my blog. In absolute num­bers Inter­net Explorer ends up being about 1%. Webkit is almost 75% if you add Chrome + Safari + Mobile Safari. Even Mobile Safari is used more than Inter­net Exploder (sic)!

I am bas­ing this on Google Ana­lyt­ics, and the fact that 90% of what I blog about is Play! Frame­work, or Java related.

If you have read this far, you may as well fol­low me on Twit­ter:

May 13, 2013
by poornerd
1 Comment

How to spend less time waiting for Play! Framework to compile while coding

A col­league just pointed this cool tip out to me:

play ~run

If you enter “play help”, it says: “~ Executes the specified command whenever source files change.

So what this does — and how it saves you time — is: it com­piles when you save, so that by the time you hit reload in the browser, if you are lucky, it will have already compiled.

This saves me a few sec­onds each time I switch to the browser to check out my most recent changes.

If you have read this far, you may as well fol­low me on Twit­ter:

May 8, 2013
by poornerd
2 Comments

How to Edit Messages in the Browser While Developing Play! Framework Templates

As a fol­lowup to my recent exper­i­ments to inline edit­ing of con­tent blocks while devel­op­ing Play! Frame­work tem­plates, I decided to focus on Mes­sages this week.  Mes­sages are basi­cally Resource Bun­dles with Texts to be used in the Application.

This is what I wanted to accomplish:

  1. No changes required to the Play! Frame­work Scala Templates
  2. Use an inline HTML5 edi­tor to edit the mes­sages directly in the ren­dered tem­plate in the browser
  3. Save (update) the mes­sage file directly in the conf direc­tory of the application

I mod­i­fied my play-aloha project on github (https://​github​.com/​p​o​o​r​n​e​r​d​/​p​l​a​y​-​a​l​oha ) for the first require­ment, by adding a Mes­sages tem­plate, so that the typ­i­cal @Messages("label.title") tags used in the tem­plates would be ren­dered by my tag instead.  If the play-aloha.admin=true is set in the application.conf, then the Messages.scala.html tem­plate ren­ders a <span> around the mes­sage with an id that con­tains "message." + the key of the message.

@(key:String)
@if(Application.isAlohaOn()) {
  <span id="message.@key" class="editable">
}
  @play.api.i18n.Messages(key)
@if(Application.isAlohaOn()) {
  </span>
}

I also added the class="editable" so that the Aloha Edi­tor which I already inte­grated into the project would auto­mat­i­cally make the <span> editable.

So for exam­ple this code in the tem­plate would make the mes­sages with the given keys editable:

<h2 class="featurette-heading">
  @Messages("label.featurette1") 
  <span class="muted">@Messages("label.featurette1Mute")</span>
</h2>

message-editing

 

In the con­troller method which han­dles sav­ing the edited con­tent from the Aloha Edi­tor via an AJAX Request, I check the id to see if it starts with "message.".

        if (contentId.startsWith("message.")) {
            // save as message
            saveMessages(contentId.substring(8), content);
        } else {
            // save in Template
            if (templateFilename != null) {
                saveToTemplate(fullTemplateFilename, pageId, contentId, content);
            }
        }

If it does, then I use the rest of the id (which is the mes­sage) key, to lookup and save the new mes­sage in the mes­sages file.

#test
label.featurette1Mute=Google is cool!
label.featurette1=Chrome
If you have read this far, you may as well fol­low me on Twit­ter:

May 3, 2013
by poornerd
0 comments

Wysiwyg Editing of Play! Framework Templates with the Aloha Editor

aloha-savesOk, so it is only par­tial edit­ing, but still this is an inter­est­ing step in the “no CMS” direc­tion. What I have done is mod­i­fied the exam­ple code which I posted on github last week for inline edit­ing of HTML5 ele­ments with the Aloha WYSIWYG Edi­tor to actu­ally save the new con­tent back in to the Scala Template.

I find this use­ful for allow­ing non-techie col­leagues to edit and opti­mize text and word­ings in more con­tent ori­ented tem­plates. Of course it does directly mod­ify the source, so in the case of a live web­site you would want to do this in the devel­op­ment envi­ron­ment and then com­mit, push, deploy the new ver­sion — hence “no CMS”.

In my exam­ple I have lim­ited the fron­tend code to a block in the footer tem­plate. It is only ren­dered into the page if the fol­low­ing value is set to true in the application.conf:

play-aloha.admin=true

One of the non-elegant parts of the code is decid­ing which tem­plate file to look for the HTML to replace. Right now it is hard coded. I sup­posed the most prag­matic way would be to cre­ate a hashtable map­ping URL’s to filenames.

Here you can see the changed HTML in the scala template:

git-diff

 

The code is in github here: https://​github​.com/​p​o​o​r​n​e​r​d​/​p​l​a​y​-​a​l​oha

If you have read this far, you may as well fol­low me on Twit­ter:

April 26, 2013
by poornerd
4 Comments

How to integrate the Aloha Wysiwyg Editor with Play! Framework

UPDATE: code checked in on github: https://​github​.com/​p​o​o​r​n​e​r​d​/​p​l​a​y​-​a​l​oha

play-alohaI am con­stantly need­ing to update sta­tic con­tent in Tem­plates in my Play! Frame­work project, so I decided to inte­grate the Aloha Edi­tor, an HTML5 WYSIWYG Editor.

It is free to use, and they offer an exam­ple of inte­gra­tion an sav­ing with a POST to the server on this page: http://​www​.aloha​-edi​tor​.org/​h​o​w​t​o​-​s​t​o​r​e​-​d​a​t​a​-​w​i​t​h​-​a​l​o​h​a​-​e​d​i​t​o​r​-​a​j​a​x​.​php

Basi­cally all I had to do in the fron­tend was inte­grate the edi­tor as described, and add a class “editable” to the ele­ment that should be editable.

<h1 class="editable" id="c1title">Example headline.</h1>

Then I had to cre­ate a POST action in my controller:

public static Result aloha() {
   final Map<String, String[]> values = request().body().asFormUrlEncoded();
   final String content = values.get("content")[0];
   final String contentId = values.get("contentId")[0];
   final String pageId = values.get("pageId")[0];
   System.out.println("pageId:" + pageId);
   System.out.println("contentId:" + contentId);
   System.out.println("Content:" + content);
   // save the content or whatever here...
   return ok();
}

Then I mod­i­fied the aloha-save.js from the Aloha Exam­ple (which I copied from the above men­tioned web­site), to post to the route for my action instead of save.php.

That was pretty much it. Make sure you add an id to the HTML ele­ments so you can tell which on is which. The pageId ends up being the URL of the page, so that is not always helpful.

If you want to find out what I am doing with this, keep tuned to this blog or fol­low me on twitter!

If you have read this far, you may as well fol­low me on Twit­ter:

April 19, 2013
by poornerd
0 comments

CRUD for Play! Framework 2

Update: yes, those are ques­tions inter­spersed in the arti­cle… please answer in the com­ments!
crudOne of the main things I missed from Play 1.x is the CRUD Mod­ule. After read­ing posts on the forum about that one or more are in the works, I decided to check them out today.

I found a few projects — all which have key differences:

Am I miss­ing any projects? Let me know in the comments…

Right now I like how easy it is to get fully auto­matic CRUD Screens using “play2-crud” from njin, but I wish that I was able to build on that more like in the CRUD Mod­ule from Play 1.x.

The other “play2-crud” from Hakan Dilek on the other hand has a good basis for writ­ing your own CRUD screens and DAOs, but for a quick start it requires too much coding.

Which of these — from a generic per­spec­tive — is the best? Why? Let me know in the comments…

My wish list:

  • Para­me­ter­ize­able (is that a word) Templates/Tags for the var­i­ous screens (Search Form, List, Edit, View) which default to the stan­dard view, but where you can spec­ify which fields or columns to show.  These could be included in exist­ing templates.
  • Boot­strap inte­gra­tion with the abil­ity to use the projects Boot­strap / Theme for the auto­matic screens, as well as turn on/off Bootstrap.
  • Tools for “reverse data­base gen­er­a­tion” of Beans (Ebeans, JPA, etc.)
  • Optional JSON inter­faces, with Swagger
  • Optional Secu­rity with ?? and Deadbolt

What fea­tures do you want? Let me know in the comments…

Maybe it is time to join forces and work on CRUD mod­ule that many would find use­ful and use.

If you have read this far, you may as well fol­low me on Twit­ter:

April 8, 2013
by poornerd
0 comments

How deploy Play! Framework Apps with different configurations to Cloudbees

cloudbees-playHave you ever wanted to deploy the same Appli­ca­tion with mul­ti­ple con­fig­u­ra­tions to Cloud­bees, with­out chang­ing your envi­ron­ment vari­ables each time while using the com­mand line?

I spent a ton of time try­ing to fig­ure it out, and it ended up being a punc­tu­a­tion problem.

So basi­cally, this is it:

How­ever, if you want to script it, like I did, you want to be able to start the deploy­ment process from the shell as well.

The doc­u­men­ta­tion said:
cloudbees-deploy-config
Deploy a configuration of your app to a Run@Cloud app id. Arguments are:
(1) the base name of a conf file in your project's conf directory, defaulting to "application"/
(2) Optional. The application id to which this configuration should deploy. You can omit this
arg if you have either set cloudbees.applicationId in the config file from the first
arg or have set the project ID in your PlayProject.
Example usage: `> cloudbees-deploy-config live`, where live.conf exists in the project's conf/
directory and contains a key cloudbees.applicationId.

So I tried: play cloudbees-deploy-config xxxxx

That did not work — I got an error like this:
[success] Total time: 110 s, completed Mar 15, 2013 9:08:36 PM
[error] Not a valid command: xxxxx (similar: run)
[error] Expected '/'
[error] Expected ':'
[error] Not a valid key: xxxxx (similar: run)
[error] xxxxx

Thanks to help on stack­over­flow, I dis­cov­ered that it was just miss­ing quotes!

play "cloudbees-deploy-config xxxxx"

Here is the link to the Stack­over­flow thread if you want more details:  http://​stack​over​flow​.com/​q​u​e​s​t​i​o​n​s​/​1​5​4​4​1​6​2​8​/​h​o​w​-​c​a​n​-​i​-​s​p​e​c​i​f​y​-​w​h​i​c​h​-​a​p​p​l​i​c​a​t​i​o​n​-​c​o​n​f​-​t​o​-​u​s​e​r​-​w​h​e​n​-​d​e​p​l​o​y​i​n​g​-​o​n​-​c​l​o​u​d​b​ees

If you have read this far, you may as well fol­low me on Twit­ter:

March 27, 2013
by poornerd
0 comments

How to quickly start a Play! Framework Project and Deploy it in the Cloud

playstartjavaJump­start your Play! Frame­work 2.1 Java project, by check­ing out my project scaf­fold from GitHub: https://​github​.com/​p​o​o​r​n​e​r​d​/​p​l​a​y​s​t​a​r​t​j​ava

Demo run­ning @cloudbees : http://​playstart​java​.poornerd​.cloud​bees​.net/

The Project Includes:

Fol­low the instruc­tions on the Cloud­bees link after you signup and install the Cloud­bees SDK and then just do this to deploy:
play cloudbees-deploy

Thanks to @jaliss for all his help with Secure­So­cial, and @_JamesWard for adding some libs to Web­Jars for me!

If you have read this far, you may as well fol­low me on Twit­ter:

March 5, 2013
by poornerd
0 comments

How to switch back and forth between Play! Framework Versions

When I first read about the Play Ver­sion Man­ager I first thought it was some sort of tool for versioning.

When Play! Frame­work 2.1.0 came out, I finally real­ized that it was a tool for switch­ing back and forth between var­i­ous Play! Frame­work versions.

So down­load the Play Ver­sion Man­ager from github: https://​github​.com/​k​a​i​i​n​k​i​n​e​n​/​pvm

You can do things like “pvm use 2.1.0″ or “pvm use 2.0.4″.

Here are some of the sup­ported com­mands:


$ pvm [tab][tab]
alias          copy-packages  help           
list           run            uninstall      
version        clear-cache    deactivate     
install        ls             unalias        
use

So, down­load it, and give it a try.

If you have read this far, you may as well fol­low me on Twit­ter:

March 1, 2013
by poornerd
1 Comment

Our Energy Saving Server Farm

Ok, so 3 VMWare ESXi Servers run­ning 20–30 Vir­tual Machines only slightly qual­i­fies as a “Server Farm”, but we will be sav­ing a ton of elec­tric­ity (sorry Stadtwerke München ) at Site­Force.

IMG_8056Our new setup:

  • 3 Mac Mini Server 2,0 GHz Quad Core with 16 GB each run­ning VMWare ESXi 5.0 which peak at just over 60 Watt each (max 180 Watt) — all the old servers, includ­ing mail have been moved to vir­tual machines, except the Open Direc­tory which is on a QNAP NAS Server
  • 2 QNAP NAS Servers (with iSCSI for VMWare) at about 45 Watt each  (sec­ond one is used for redun­dancy and backups)
  • Tele­phone Sys­tem will be vir­tu­al­ized with Sip­gate
  • 2 low-power, fan­less Cisco Switches ( < 40 Watt each)
  • new Fire­wall Appli­ance (< 50 Watt)
  • 1 KVM
  • 1 older Mac Mini as a Backup Server
  • NO MORE air conditioning

IMG_8057We are mov­ing from:

  • 2 x VMWare ESXi Servers which are Dual Quad Core XEON (2,2 GHz) with 24 GB RAM, and dual power sup­plies which peak at 650 Watt each (that is 4 power sup­plies x 650 Watt!!!)
  • Mac Pro Dual G5 - with Open Direc­tory and our Mail Server (now on a vir­tual machine) — with (I am guess­ing) 450+ Watt 
  • Fire­wall — old PC with 400 Watt 
  • 2 x Quan­tum Snap Server ( 2 x 400+ Watt)
  • QNAP NAS (50 Watt)
  • Air Con­di­tioner (300–500 Watt)
  • 2 x 5+ year old rack mount switches (200 Watt ?)
  • 2 x old PC’s as Servers (2 x 300 Watt?)
  • Siemens HiCom Tele­phone Sys­tem ( 200 Watt?)

Of course we needed that air con­di­tion­ing to keep the temp down in the room with all that equip­ment, and I am sure that some of it is energy effi­cient and did not always draw full power, but it is safe to say that we had any­where from 3000–6000 Watt when you add the KVM and Mon­i­tor in the rack!

Aside from a few annoy­ing hums from the QNAP NAS Servers, it could almost sit on the desk next to me.  Now we are talk­ing some­thing along the lines of 300–475 Watt!!!

That is a HUGE dif­fer­ence.  I real­ize that this is not a very sci­en­tific or accu­rate cal­cu­la­tion, but the sav­ings are obvious.

We have a lit­tle less CPU Power, but we were already over­pow­ered for our nec­es­sary vir­tual machines, and if we need more CPU for a project now, we just go cloud.

If you want to know more about our MacMini Server VMWare ESXi setup with iSCSI then stay tuned, I’ll write that up one of these days, too.

If you have read this far, you may as well fol­low me on Twit­ter: