Upthrust

This is the weblog of Milinda Pathirage

jQuery Micro Optimizations

Long time ago Donal Knuth said,

Micro-optimizations are worth it while premature optimizations are the root of evil

And the difference between micro-optimizations and premature optimizations is that the first ones are driven by measurement while the second ones are driven by guesses, intuitions and hunches.

Corey Hart has analyzed jQuery code and came up with a fun post which list some of the micro-optimizations you can do when writing web applications using jQuery. And jQuery is one of the popular JS libraries and there are thousands of projects which use jQuery. So with the advancement of Web 2.0 and Ajax based cross-browser apps, I think it’s worth to consider these optimization tips when writing code which use jQuery. According to Corey you need to be careful when selecting the micro-optimizations he suggested.

The post is really interesting and here is the list of optimizations suggested by him.

  • jQuery.root : Internally, all selectors that don’t provide a context use jQuery( document ).find( selector ). Save yourself some ms, store the document root onto jQuery itself, and then run all global selectors off of that element.
  • Context sucks, use find: Don’t get me wrong, you should always run selections based on a context if possible. But passing in a context to the jQuery constructor creates an extra unneeded function call. Internally jQuery runs context.find( selector ) anyway, so skip that step
  • Live is terrible, delegate is awesome: The best part: delegate is live with a context. So why is live a bad idea? To use live, you first have to run a selection on the page, and then bind the live handler.
  • jQuery.data > jQuery.fn.data: If you are getting/setting single key values, always use jQuery.data over the jQuery.fn.data method. Here’s a common example and the fix for it.
  • Bind and Trigger, get used to it: Nothing better than going directly to the source. All the event methods, like clickfocusblursubmit, etc., are just short-hand methods mapping to bind and trigger, only with an extra function call. Getting used to using bindand trigger directly will not only reduce the number of function calls, but will also help you when working with larger applications that require namespacing your events.
  • Each is evil: Actually, each is a pretty awesome utility function. The problem is that there is only one true reason to use each, and that is when a closure is needed for each item. If you are just looping through an array, then the callback function gets triggered on every iteration. So using an array of 25 items, the callback gets triggered 25 times. That can really add up depending on the size of your array.
  • Classes over Styles: I would go so far as to say that using a class for a single style change is better than running through jQuery’s style module.
  • Object.length, use it: Every jQuery selection comes with a length property that defines how many elements were found. Always check to make sure that there is a set of elements in your object before running a chain of methods.

There are code examples in original post. So please go and have a look at it if you are really thinking about above micro-optimizations.

Webinar on Hybrid Cloud

The hybrid cloud option leverages the security of a private cloud solution and the elasticity/scalability of a public cloud. Architects designing hybrid cloud solutions need to reconcile between these competing goals. With WSO2’s Cloud Services Gateway organizations can now effectively mediate between their public and private clouds without compromising existing network firewall infrastructures.

Through this webinar you will be able to gain knowledge on WSO2 Cloud Service Gateway, it’s architecture and how to use it on real world environments.

For more information on this webinar please visit ‘Webinar: Makin the hybrid cloud a reality‘.

Webinar On SOA Solution Patterns In The Real World

In software engineering pattern is a reusable solution to a commonly occurring problem in software design.  Not only in conventional software design, patterns are their also for Service Oriented Architecture based designs.

Webinar from WSO2′s Lead Solutions Architect Asanka Abeysinghe will discuss software patterns and its role in SOA infrastructure while explaining how to incorporate SOA solutions patterns to your system design processes.

This webinar is well suited for architects and developers interested in SOA, SOA consultants and for those who are planning to adopt SOA in you enterprise IT systems.

For more information visit Webinar: Implementing SOA Solution Patterns in the Real World.

JPA Implementation Patterns

These days I am working on Human Task implementation for WSO2 Carbon platform, and decided to use JPA based persistence mechanism. While working on ‘Task Engine’ implementation, I found out that JPA is not just about implementing set of entities and persisting them using ‘Entity Manager’. I found out that, there are small but very important things that anyone should know to make their JPA based implementations successful. Without those you’ll stuck in the middle of some thing, deciding what is the best option. While googling I found series of articles from Vincent Partington on JPA implementation patterns which are very useful for anyone who new to JPA. In this post I am going to summarize each and every post from ‘JPA Implementation Patterns‘ article series.

Data Access Objects

This post starts with the background information about JPA and JPA implementations. After that Vincent tells us that there are more things than data access object and domain objects, like transaction handling, lazy loading and detach objects.

Then he comes to the main topic and first describes why we need DAOs(Data Access Objects) and how DAO layer which resides on top JPA can help you. After that he provides us with the information about type-safe generic DAO pattern, how to use DAO and advantages of type-safe generic DAO pattern.

In think this post is a must read for anyone who try to get started with JPA and this post help me a lot to understand issues in my JPA based implementation.

Bidirectional Associations

This post is about association between JPA entities. And it mainly describes a bidirectional association pattern used by developers who use JPA. Vincent start this post with a sample and then describes how bidirectional association can make developers life easier. After that he describes problems associated with bidirectional association and provide reader with a patter that can use to solve the issues. At last he describes the variations to the patter the introduced and what user must consider before using this pattern.

Saving (detached) Entities

This post talks about saving detached entities or merging changes done to the detached entities. First Vincent talked about how entities become detached and what  will happen when try to persist detached entity.

Then he describes about differences between Hibernate’s saveOrUpdate and JPA Entity Manager merge method.

In the last part of this post Vincent describes the problems with JPA merge and a pattern which every one can use to solve issues described with merge method.

Retrieving Entities

This post talks about two ways of retrieving an entity. One is using EntityManager.find and other is using JPQL queries. This post contains three example ways of retrieving entities by using EntityManager.find and JPQL queries.

Removing Entities

This post mainly focused on removing entities with bi-directional relationships. And Vincent has described pattern to overcome issue with removing entities when there are bi-directional associations.

Service Facades and Data Transfer Objects

This post is mainly about applying old school enterprise application architecture patterns to your JPA base implementations. First Vincent describes why we must bother about DAO’s DTO’s and Service Facades. Then he describes pros and cons of Data Transfer Objects(DTO) and pros and cons of Service Facades in JPA space. Finally he explain to you implications to your JPA architecture when you use DTO’s and Service Facades.

Lazy Loading

To use JPA to its full potential you must understand ‘Lazy Loading’ and how it works. Understanding lazy loading will allow you to utilize computing resources effectively and help you to design you entities and their relationships in way that it increase the efficiency.

First part of the post is about lazy loading and when lazy  loading occur. Then post has some information about lazy loading implementation mechanisms. After that the post describes “Runtime proxy based lazy loading” in hibernate and “Runtime bytecode instrumentation” in OpenJPA. Then there is a seperate section on differences between Hibernate and OpenJPA. Finally post contains a patter which help us to effectively use lazy loading.

Merging PDF Files In Linux Using PyPDF

PyPDF is a handy and valuable Python library for merging and splitting PDF files in Linux. It’s pure Python library built as a PDF toolkit. It is capable of:

  • extracting document information (title, author, …),
  • splitting documents page by page,
  • merging documents page by page,
  • cropping pages,
  • merging multiple pages into a single page,
  • encrypting and decrypting PDF files.

PyPDF is a great Python library use by many Python applications which handles PDF files directly. PDF-Shuffler is a one of the tools written based on PyPDF which you can use to merge PDF files easily in Linux. In Ubuntu you can install it using following command.

sudo apt-get install pdfshuffler

Here is a sample code that merge PDF files together using PyPDF library. In this code I have used PdfFileWriter and PdfFileReader classes from PyPDF module to read and append PDF files together. This sample doesn’t contain completed error handling logic for file handling.

# Copyright (C) 2010 Milinda Pathirage

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from pyPdf import PdfFileWriter, PdfFileReader

def mergePDFFiles(outputFile, filesToBeMerged):
    output = PdfFileWriter()
   
    if(len(filesToBeMerged) == 0):
        print 'Empty Input File List'
        return;
   
    for inFile in filesToBeMerged:
        print 'Adding file' + inFile + ' to the out put'
        # Read the input PDF file
        input = PdfFileReader(file(inFile, "rb"))
        # Add every page in input PDF file to output
        for page in input.pages:
            output.addPage(page)
    print 'Writing the final out put to file system'
    # Out put stream for output file        
    outputStream = file(outputFile, "wb")
    output.write(outputStream)
    outputStream.close()
   

if __name__ == '__main__':
    i = 0
    outputFile = ''
    inputFiles = []
    for arg in sys.argv:
        i = i + 1
       
        # Getting out file
        if arg == '-o':
            outputFile = sys.argv[i]
            print 'Output File: ' + outputFile
           
        # Extracting Input files
        if arg == '-i':
            outfileOptionPos = sys.argv.index('-o')
            if i < outfileOptionPos:
                inputFiles = sys.argv[i: outfileOptionPos]
                filesStr = ",".join(inputFiles).replace(",", " ")
                print 'Input Files: ' + filesStr
            else:    
                inputFiles = sys.argv[i:]
                filesStr = ",".join(inputFiles).replace(",", " ")
                print 'Input Files: ' + filesStr
               
    # Merging PDF files
    print 'Merging PDF Files......'            
    mergePDFFiles(outputFile, inputFiles)

You can get more understanding about usages of PyPDF if you explore more about open source projects which uses PyPDF. Here are some of the projects which use PyPDF.

Related Resources:

Data Services in SOA and WSO2 Data Services Server 2.2.0

Support for large XML outputsData Services are Web Services that encapsulate operations on key data entities of relevance to the enterprise, thus making data integration easy for business processes, mashups, gadgets, BI application and any service in general. According to David Linthicum

Most in the SOA community understand that data services provide controlled interfaces to underlying data, but typically don’t understand the strategic value of data services to the SOA.

He emphasized the fact that those who focus on the notion of a service as delivering functional behavior, neglect the need to manage the underlying data. He tells that

In many cases, data quality and consistency issues quickly arise, and the agility that SOA should provide is limited by the need to alter services directly after the underlying data has changed.

David argues that data services created and leveraged correctly within the context of a SOA, should provide a wide variety of features including data quality assurance, data governance, and, most importantly, the ability to support data abstractions.

Even though there are lot of benefits(David’s opinion is further elaborated by Ash Parikh), getting data service right is not an easy task. You need to consider about the design, implementation, technologies and tools you are going to use for this. You can find several guide lines and best practices in designing and implementing data services from articles “Introduction to Data Services“, “Incorporating Enterprise Data into SOA” and “How To Get Started with Data-Orientation – What Architects Told Me…“.

Even you get the concepts and your designs correct, you must find a tool which allows you to incorporate you design and best practices into the implementation in flexible and easy manner. There are so many tools out there which provide support for developing and deploying data services written following various standards. In this post I am going to introduce you to the WSO2 Data Services Server, which is a award winning product from WSO2.

WSO2′s award winning Data Services solution WSO2 Data Services Server is a data services solution that will help you to achieve your targets in data orientation.

With the recent 2.2 release WSO2 Data Services Server provides some major improvements to it’s industry leading data services solution. Following new features are included in this release.

  • Support for large XML outputs – The core engine has been modified to support XML streaming. This has resulted in two notable improvements.
    • Efficient use of server memory – No matter how large the payload is, server memory does not grow propotional to it. The streaming capabilies push data to client side as and when needed
    • Improved response time
  • Google Spreadsheets as a data source
  • Content filtering based on user roles
  • Support for named parameters
  • Ability to configure schema type for output elements
  • Mixing multiple data source types in nested queries
  • Excel 2007 support
  • Support for Oracle Ref Cursor – Oracle Ref cursor is a Data type. A variable created using this data type is usually called as a Cursor Variable. Some of the primary advantages of using a ref cursor are,
    • ability to pass resultset between sub programs (eg: functions, stored procedures)
    • dynamic queries
    • efficient memory utilization

For more tutorials on WSO2 Data Services Server visit wso2.org.

Golden Awards For Their Contribution To The Nation

Sri Lanka President Mahinda Rajapaksa yesterday honored three outstanding athletes of the country in a ceremony held at the Temple Trees.

President Rajapaksa presented awards to Sprinter Susanthika Jayasinghe and cricket legends Sanath Jayasuriya and Muttiah Muralitharan for their exemplary contribution to the country.

Susanthika Jayasinghe, 34, is the only Sri Lankan athlete to win an Olympic medal. She won a bronze in women’s 200m at the Sydney Olympics in 2000 but later awarded the silver after Marion Jones was stripped off the gold medal due to drug use.


Sanath Jayasuriya, 40, was honored for his 20 years of dedication to cricket since his debut on Dec 26, 1989 against Australia. He was part of the World Cup winning team in 1996.

Muttiah Muralitharan, 37, is the world’s highest wicket taker in both Test cricket and One-Day Internationals. He has been playing since 1992.

These people make us proud. They toiled for their country & brought us recognition & high regard in the world. They did not go around the world to destroy our country with lies. They did not betray our country. They did not try to hog the fame all for themselves. They were team players & acknowledged humbly the help & contribution of others. They did not ask for perks, for more & more & even more. To paraphrase, in the words of President John F. Kennedy – “They did not ask what their country could do for them, but went ahead & did what they could do for their country”. They silently & diligently went about their task & achieved success not only for themselves but for the whole country. Sanath, Muttiah, Susanthika – You are our heroes & today you are being felicitated by our National Saviour & Hero of the century, H.E. The President, Mahinda Rajapaksa. This is truly, an historic occasion, when the good people, who have worked for the welfare of our country meet. We, the people of Sri Lanka honour you all with deep affection & love for what you have so nobly done for our motherland & the history of our nation is so much the greater by being adorned by your achievements. May the Blessings of the Triple Gem, be upon you all always.

-  Comment by Sunil Jayanth Weliwitigoda in Adaderana.lk

Sources

A Happy New Year To The World!

Install Adobe Air 2 On Ubuntu 9.10 64-bit

Adobe AIR is a cross-operating system runtime that lets developers combine HTML, Ajax, Adobe Flash, and Adobe Flex technologies to deploy rich Internet applications (RIAs) on the desktop. The most recent version is Adobe Air 2 with more features than ever for the users as well as developers.

AIR 2 builds on the success of AIR 1 by giving developers new capabilities, and even tighter integration with the desktop. Some new features of AIR 2 include:

  • Support for the detection of mass storage devices.
  • Advanced networking capabilities like secure sockets, UDP support, and the ability to listen on sockets.
  • Support for native code integration.
  • The ability to open a file with its default application.
  • Multi-touch and gesture support.
  • New APIs for access to raw microphone data.
  • Webkit update with HTML5/CSS3 support.
  • Global error handling.
  • Improved cross-platform printing
  • Improved security and support for enterprise and government standards.

The beta release of Adobe Air 2 is available for download from Adobe site. But the problem is they don’t provide us with a 64-bit Linux version(for Ubuntu 9.10 64-bit). But there are some workarounds to this problem. You can install 32-bit version in 64-bit Ubuntu by installing some 32-bit libraries.

First, install ‘getlibs‘ tool from http://frozenfox.freehostia.com/cappy/getlibs-all.deb. Then install ‘libhal-storage.so.1′ library using following command.

sudo getlibs -l libhal-storage.so.1

After the above step please download the Adobe Air 2 beta version’s Linux *.bin file from Adobe, and install it by making it a executable and run the bin file in command line.

If every things went well, now you have Adobe Air 2 beta installed on your system. Please restart your browsers and try to navigate to some Adobe Air application sites and install your favorite Air applications from them.

Please note that I have ‘ia32-libs‘ installed in my Ubuntu 9.10 previously. If you have any problem running Adobe Air using above two steps, try to install the ‘ia32-libs’ using Ubuntu package manager. Also refer this knowledge base article from Adobe on installing Adobe Air 1.5 in 64-bit Linux including Ubuntu.

For more information Adobe Air 2:

Linux Utilities You Should Know About

Utility applications in Linux which are(most of them) originally created for Unix and ported to Linux can be used to make every Linux users life easier. It doesn’t matter whether you are a beginner or a hard core Linux geek, if you know the tools it’ll save you lot of time.

In this post I am going to give you introduction to several utilities available on Linux that originated from Unix based on the recent posts by Peteris Krumins. I am planning to update this post time to time once he add new articles about more tools.

Pipe Viewer

Pipe viewer (Written by Andrew Wood) or pv in short can be used inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

Default Ubuntu installation doesn’t come with this tool. You need to install it using ‘sudo apt-get install pv’ command.

To get start with pv, lets use this to monitor the progress of compressing large file containing giga bytes of information in to a small file. The normal way to do this using gzip is like following.

gzip -c x.avi &gt; x.avi.gz

But this command won’t tell you how much time it takes to compress this file or monitor the progress of compression.

By using pv you can precisely time how long it will take. Take a look at doing the same through pv:

pv x.avi | gzip &gt; x.avi.gz

69MB 0:00:04 [  15MB/s] [==================&gt;         ]   74%  ETA 0:00:01

Pipe viewer acts as “cat” here, except it also adds a progress bar. We can see that gzip processed 69MB of data in 4 seconds. It has processed 74% of all data and it will take 1 more seconds to finish.

There are several advance usage patterns of pv command and I am not going to cover those.Please refer the article from Peteris to explore more about pv command.

lsof(list open files)

Peteris called this tools as the Swiss Army Knife of Unix Debugging. lsof is a utility command that you can used to list information about files opened by  Unix/Linux processes. In Unix/Linux every things is a file: pipes are files, IP sockets are files, unix sockets are files, directories are files, devices are files, inodes are files… So you know the advantage of this kind of tools where every thing is a file.

Using lsof

Here are some common uage scenarios of lsof comand extracted from catonmat.net blog. You must have root permission to get the information about all the open files using lsof. Unless otherwise you’ll only get information set of files which you have permission to access them.

List all open files

Running lsof without any arguments lists all open files by all processes.

# lsof

Find who’s using a file

With an argument of a path to a file, lsof lists all the processes, which are using the file in some way.

# lsof /path/to/file

You may also specify several files, which lists all the processes, which are using all the files:

# lsof /path/to/file1 /path/to/file2

Find all open files in a directory recursively

With the 

+D

argument lsof finds all files in the specified directory and all the subdirectories.

# lsof +D /usr/lib

There more use cases of lsof command like getting open files by process, open files by users, list all network connections, list all TCP connections, list network activity by user and etc. Please refer blog post from Peteris for those usage scenarios. You can find some examples of lsof from this link also.