Lars Nielsen's Discoveries

January 19, 2013

JavaScript Patterns book

Filed under: Architecture,Development,SharePoint — Lars Nielsen @ 2:00 pm

With SharePoint 2013 and HTML 5 it looks like the future is Javascript.  I’m personally not convinced that the demise of Flash will lead to improved security – many exploits are actually injected through Javascript and things like federated authentication provide new scope for exploits (just Google “Facebook fake logon” to find out all about it!).   But anyway clearly Javascript is becoming more and more important and I found this great book, JavaScript Patterns, which demonstrates more than a few features of the Javascript language.  Reading this book you really begin to see how, despite being syntactically similar to C# or Java, Javascript is truly different.

Here’s an interesting example :

var city = "Washington";

function alertCity()
   var city;
   city = "New York";

If you try this code in a page you find that the first alert call outputs not (as you might expect) “Washington” but “Undefined”.   The second one outputs “New York”.  This is because the var city statement inside the alertCity function is hoisted up to the beginning of the function, as if it were declared at the beginning of the function.  It hides the global variable city and it becomes an uninitialised variable.  Later it’s initialised, and so the second alert behaves as you might expect.  It’s a good example of why you should always declare variables with a var statement at the beginning of a function.

Reading through this book I’ve come to realise that JavaScript’s treatment of functions as objects is in many ways closer to functional programming than many other of the “C-like” languages like Java or C#.  Most of the time I must admit I tend to write Javascript as if it was C# without types, and I guess many .NET or Java developers do the same. But it’s worth knowing that when working in Javascript, you can do clever things like curry functions which, in the C# world for example, have only relatively recently become possible with the introduction of lambda expressions.

December 16, 2010

Virtual Box and Xen Server

Filed under: Architecture,SharePoint — Lars Nielsen @ 9:12 pm
Tags: , ,

We had some fun (not really) recently getting Sun Virtual Box to run on Citrix Xen Server.

Virtual Box is a powerful virtual machine environment which I was converted to by some colleagues who told me it’s better than Virtual PC which is what I always used to use for SharePoint development.   Initially I was sceptical but I did find it faster and more stable, less prone to crash and suffer corruption on the virtual hard disk.  The biggest problem I had in setting it all up was that there is no obvious way to set up differencing disks.   Differencing disks are a really useful feature of Virtual PC that make it possible to have multiple machines based on the same base image.  I find this really useful.  You can even create a separate virtual machine for every project, and leave each one in a saved state ready to fire up with Visual Studio open with the project code already loaded in.

It took a while to work out how to get Virtual Box to do the same thing.  Virtual Box has snapshots, but these aren’t quite the same thing because you can’t branch off multiple machines off the same base.  I finally found out how to do it by following these steps.    I set the base virtual machine hard disk image with all the basic applications that I need, updated with all the service packs etc.  Then I set it to be immutable (read-only) and actually physically set the VDI file to be read-only as well, so I know it’s always going to be intact.  Then I create separate machines each with their own differencing disks.

Well this was all well and good on a desktop machine, but in a corporate environment, it’s obviously a disk to have  critical files on your desktop (especially if it’s a 15GB VDI file!).   But at the same time I can imagine how Virtual Box would run very slowly if its hard disk files were located on a network share, so the VDI files really have to be on the hard disk.  So we tried to set up a developer server on Citrix Xen Server, the idea being that I’d run Virtual Box on that server instead of the desktop.

Virtual Box installed no problem as a 64 bit app on the 64 bit server.  Trying to fire it up, however, was a different matter.  The Virtual Box application ran fine but at the first attempt to open up a completely empty virtual disk file (with no operating system on it) the server CPU spiked up to 100% and stayed there, freezing the server until it was rebooted.  It seems it’s not easy to get Virtual Box  to run inside another virtualization platform – there’s a discussion about this on the Virtual Box forum.   There are plenty of people complaining about this problem but none of the solutions I found seemed to work.  I tried disabling USB support, allocating more video RAM, disabling the clipboard sharing, disabling IOAPIC, giving the Virtual Box process a lower priority and setting the processor affinity to one core.  No joy – I still got the same behaviour even with a completely blank disk.

So for now I’m forced to conclude that Virtual Box is not happy running inside a Xen Server VM, but if anyone has got around this problem please post your answer here!

(You might reasonably ask why should you want to run a virtual machine on a virtual server, why not just forget about Virtual Box and use Xen as the virtualization platform.  The answer is that in many organisations the infrastructure team looks after the virtual servers, and developers want complete control over the virtual machines – to be able to branch off multiple machines, take snapshots, etc. – without having to ask the server team who are usually hard pressed looking after customers).

Blog at