Tuesday, January 27, 2009

Extracting excel sheet index

First, this post has nothing to do with Java. It only serves as a reminder for me.
I was in the middle of documenting things where I have to use Excel with many sheets. The need for me is to have automatic numbering on each sheet, where the index depends on the sheet index (fortunate for me). Excel doesn't provide a function for retrieving index, I tried to put the index on the sheet name and further extract it. It works with the 1st excel file (because the other sheets were copied from the 1st sheet and excel voluntarily add index to the new copy's name), but with the 2nd excel file I don't want to put the index on the sheet name myself. The macro below (see this link) helps me on extracting the sheet index :



  1. Type above code.
  2. In Excel press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save workbook before any other changes.
To use it you could simply type "=SheetNum()".

Thursday, January 22, 2009

Javascript root objects available for webscript

In this post I'm quoting the wiki content for the sake of ease to find it later.

One link that might be of interest : Javascript API cookbook

The following JavaScript root objects are available to all Web Scripts, regardless of their hosting environment.

args
an associative array of all URI parameters. See example.
argsM
an associative array of all URI parameters (where each key is an argument name and each value is an array containing all respective argument values, even if only one is supplied). See example.
headers
an associative array of all request headers.
headersM
an associative array of all request headers (where each key is an header name and each value is an array containing all respective header values, even if only one is supplied). See example.
url
provides access to the URI (or parts of the URI) that triggered the web script.
model
An empty associative array which may be populated by the JavaScript. Values placed into this array are available as root objects in Web Script response templates.
status
provides ability to control response status codes.
cache
provides ability to control caching of response.
config
provides access to the Web Script configuration.
msg
provides access to Web Script localized messages.
format
provides information on the response format to be rendered.
guest
A boolean indicating whether the web script is executing as "Guest".
webscript
An associative array of meta-data properties describing the Web Script.
server
An associative array of meta-data properties describing the server hosting the Web Script.
jsonUtils
A host object for parsing and generating JSON objects.
atom
A host object for parsing and generating Atom (Publishing) documents.
logger
A host object providing access to console logging facilities for debugging of scripts. See the Logging API.

Web Scripts hosted within the Alfresco Repository tier also have access to the following root objects which provide direct access to Repository services and content.

roothome
the repository root node (only available if authenticated)
companyhome
the company home folder (only available if authenticated)
person
the person node of the currently authenticated user (only available if authenticated)
userhome
the user home folder (only available if authenticated)
search
A host object providing access to Lucene and Saved Search results. See the Search API.
people
A host object providing access to Alfresco people and groups. See the People API.
actions
A host object providing invocation of registered Alfresco Actions. See the Actions API.
session
Session related information such as the current authentication ticket. See the Session API.
classification
Access to the root elements of the classification API. See the Classification API.
utils
Access to a library of useful helper functions not provided as part of generic JavaScript. See the Utility Functions.
avm
Access to WCM objects such as AVM paths and searching within AVM stores and web projects. See the AVM API.
crossRepoCopy
Cross repository copy support. See the Cross Repository Copy.
workflow
Start workflows and access, control in-flight workflows. See the JavaScript API#Workflow API.

Web Scripts hosted within the presentation tier (i.e. within Alfresco SURF) have their own extras as described in the Surf Platform - Freemarker Template and JavaScript API.

Wednesday, January 21, 2009

Thread safe singleton implementation

Singleton pattern is used in many projects I've gone through and I implemented it the same way in all the projects just like others did. Luckily for me the many projects could be safely assumed as thread safe projects. Thread safe project for me is a project where no thread programming is required. Before continuing, below is how I implemented singleton pattern.

public class SingletonClassic {
static SingletonClassic instance;

public static SingletonClassic getInstance() {
if (instance == null) {
instance = new SingletonClassic();
}
return instance;
}
}


I guess the code above should be familiar to programmers. Now, we delve into the reason of why I spend my time writing this blog. The code above is actually not thread safe, let's consider for example we have 2 threads running. Both are trying to access the singleton above (assume that the singleton isn't accessed yet), the 1st thread already pass the "if (instance == null) {" statement and the thread scheduler thinks that it's the 2nd thread time to run so the 1st thread got paused and the 2nd thread runs. 2nd thread arrive at the "if (instance == null) {" statement, the instance isn't instantiated yet so it enters into the if clause and create an instance and returns the instance. 2nd thread sleeps and 1st thread wakes up and continue where it left before which is creating another instance.
So, we have a Singleton which is not so Singleton. At this point we see no harm done but if you're a perfectionist, the problem above will annoy you :)
To ease you, here's the solution that I read in some article which unfortunately I forgot (very very unfortunate for me not to keep the link) :

public class SingletonClass {

public static SingletonClass getInstance() {
return SingletonHolder.getInstance();
}

static class SingletonHolder {
static SingletonClass instance;

static {
instance = new SingletonClass();
}

static SingletonClass getInstance() {
return instance;
}
}
}


I just go straight to the point here, the reason why the new code above is thread safe is because the static initializer is guaranteed to be thread safe in the jvm specification. It will be initialized only once when the class is loaded.

still confused? let me break it down :
1. SingletonClass.getInstance() is called
2. which in turn will call SingletonHolder.getInstance()
3. at this point static initializer in SingletonHolder will be executed where SingletonClass instance is initialized
4. the new SingletonClass instance is returned, the instance will not be instantiated twice. Guaranteed!


If I ever meet the article again, I will write the name of the guy who found this method here.

Problem with subclipse and tortoisesvn

If you use subclipse and tortoisesvn on the same checkout folder on your machine, you might got this exception while working with subclipse :

"svn: This client is too old to work with working copy"

The reason is your tortoisesvn is using the latest svn version to perform some actions on the checkout folder.
The solution is to replace libsvnjavahl-1.dll in your [eclipse-root]/plugins/org.tigris.subversion.javahl.win32_1.1.0 with the latest one found in the zip file on this link.

I guess that's all that I did on my machine.