Thursday, February 2, 2012

Scala : Accessing Trait Inner Class

For example you have:-


package test

trait A {
  class B {

  }
}


You can use test.A#B to access class B in other class, for example:-


import test.A
type B = A#B
val b = new B

Friday, May 27, 2011

i7's Turbo-Boost in Action

The following steps in Ubuntu Natty show turbo-boost in action on my new laptop, which is quadcore 2.0GHz, boost-able to 2.8/2.9GHz:-

sudo apt-get install acpidump
sudo modprobe msr
sudo turbostat

Turbostat shows the following stat while my laptop is not doing heavy thing:-

CPU GHz TSC
avg 0.80 2.00
0 0.80 2.00
1 0.80 2.00
2 0.80 2.00
3 0.80 2.00
4 0.81 2.00
5 0.80 2.00
6 0.80 2.00
7 0.80 2.00

Let's make the processor's cores busy, open another terminal and enter:-

while :; do :; done

Now the stat shows:-

CPU GHz TSC
avg 2.86 2.00
0 2.77 2.00
1 2.87 2.00
2 2.70 2.00
3 2.74 2.00
4 2.77 2.00
5 2.81 2.00
6 2.76 2.00
7 2.77 2.00

Cores are working hard, enjoy!

Sunday, May 8, 2011

Learning Scala - Code Update without Redeployment via JRebel

Scala Developer entitles to free license from JRebel, just request it from ZeroTurnaround (http://sales.zeroturnaround.com/), the license will be emailed to you in minutes. You'll need to download and install JRebel as well.

Having done so, you are only 2 steps away from code update without redeployment.

Step 1
Changed your sbt launch script, I have changed mine to:-


#!/bin/sh
java -Xmx1512M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=512m -noverify -javaagent:/opt/ZeroTurnaround/JRebel/jrebel.jar -jar /opt/sbt/sbt-launch-0.7.5.jar "$@"


Note that I installed my sbt jar in /opt/sbt/ and JRebel in /opt/ZeroTurnaround/JRebel/.

Step 2
Change your Web Project build class:-


import sbt._
class WebAppBuild(info: ProjectInfo) extends DefaultWebProject(info) {

val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test"
val servletApi = "javax.servlet" % "servlet-api" % "2.5"

override def jettyWebappPath = webappPath
override def scanDirectories = Nil
}


That's all for the setup, now you can launch sbt in your project directory and test:-


jetty-run
~ prepare-webapp


Go ahead and change your Servlet source file, upon saving, you can refresh your web browser to see your changes immediately, without the need to redeploy the whole web app.

Learning Scala - Continuous Redeployment with jetty-run

To make jetty-run to auto-detect and redeploy the web app, run the following commands in sbt:-


jetty-run
~ prepare-webapp


Now you can change your servlet source code and save, the web app should get compiled and redeployed automatcally.

Saturday, May 7, 2011

Learning Scala : Hello World Web App with SBT

Creating Project:-


mkdir HelloWorld
cd HelloWorld
sbt


You will be prompted to create new project, type 'y' and enter project name, organization, version etc.:-


Name: HelloWorld
Organization: QQ Enterprise
Version [1.0]:
Scala version [2.8.1]:
sbt version [0.7.5]:


Required Scala libraries will be downloaded automatically, make sure you are connected to internet.

Create WebAppBuild.scala build in project/build/ (create build directory if it's not there):-


import sbt._
class WebAppBuild(info: ProjectInfo) extends DefaultWebProject(info) {
val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test"
val servletApi = "javax.servlet" % "servlet-api" % "2.5"
}


In the SBT console, enter:-


reload
update


Create HelloWorldServlet.scala in src/main/scala directory:-


import javax.servlet.http._
class HelloWorldServlet extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
resp.getWriter().print("Hello World!")
}
}


Create webapp directory in src/main, and consequently create WEB-INF directory and web.xml:-


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>


In SBT, run:-


reload
jetty-run


Open your web browser and go to http://localhost:8080, you should see the Hello World! message being print out from your simple servlet written in Scala.

Monday, March 28, 2011

Rip Movie from DVD iso to mpg

Using mencoder:-

mencoder input.iso -of mpeg -ovc lavc -lavcopts vcodec=mpeg1video -oac mp3lame -aid 0 -o output.mpg

Wednesday, November 3, 2010

Get Domain Class's Error Message in Grails Controller

The following code snippet can be used to get domain class's error messages in Grails (1.2.2) Controller:-


def errMsgList = domainClassObj.errors.allErrors.collect{g.message([error : it])}

Reset Netbeans JDK Home

I always need to reset Netbeans JDK Home whenever I install new JDK version, this serves as my own note to remember:-

To reset, change 'netbeans_jdkhome' entry in {NETBEANS_INSTALL_FOLDER}/etc/netbeans.conf.

Sunday, September 5, 2010

Integrating BlazeDS to Grails

Sebastien has written a very good Grails plugin for using BlazeDS in Grails project:-

Grails BlazeDS 4 Integration Plugin

Grails/BlazeDS/Flex/iPhone Full Stack Part1/3

Grails/BlazeDS/Flex/iPhone Full Stack Part 2/3

If you are like me, who try to integrate BlazeDS by hand (by downloading BlazeDS separately and putting in jars and configuration files into existing Grails project), you must use 'run-war' instead of 'run-app' to run your project or you might get 'Type xxxxx not found' error when invoking a BlazeDS service. I solved this after reading notes in Sebastien's blog:-

Run your Grails application using “grails run-war” instead of “grails run-app”. Once again this is a known limitation: Flash Builder BlazeDS data connection plugin relies on a classical web app layout and doesn’t understand Grails dynamic layout (that is until someone manages to create a Grails data connection wizard for Flash Builder 4)

Monday, August 16, 2010

Converting .ogv to .flv

I used GTK-recordMyDesktop to record step by step guide. The new recordMyDesktop produce .ogv as the output of the recording session. I tried to convert it to .flv using ffmpeg to the outcome is unusable. Finally, I use combination of mencoder and ffmpeg:-

mencoder -oac mp3lame -ovc lavc input.ogv -o middle.avi
ffmpeg -i middle.avi output.flv

This does what I want.

Tuesday, August 10, 2010

Enabling Logitech C250 Webcam in Skype 2.1 Beta (Ubuntu 10.04)

My Logitech C250 Webcam works with guvcviewer, but does not working directly with my Skype 2.1 Beta. When tested on the Video Device it just gave me a blank screen.

To make it work, I perform the following steps:-


cd /usr/bin
sudo mv skype skype.real
sudo touch skype
sudo gedit skype


And in the new skype file, type in the followings and saved:-


#!/bin/bash
LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so XLIB_SKIP_ARGB_VISUALS=1 skype.real


close gedit and change the new skype file access:-


sudo chmod 755 skype


Restart your Skype and test again with Video Device.

Friday, August 6, 2010

Including Styles & Resources in Multi-Modules Flex Application

I have been developing KZLite http://www.kzlite.com which handle modularization with LoaderInfo class. The purpose of the modularization is simple, a module (a swf file which contains multiple class definition) will be loaded only when necessary. We need that as we have a lot of modules in HRMS software.

One of the confusing bumper when loading external swf with LoaderInfo is that control's styles and resource bundles information is not loaded in the main application. For example, my main application use RSL for the flex framework, the RSL will load class definition for ComboBox, DateField, but not their styles and resource bundles information (such as the format string), unless you used the particular component in the main application itself. This is because when compiling the main application, the Flex compiler will find and include only styles and resource bundles information for controls that are used in the main application. However, in case where a control is used in a module but not in the main application (e.g. my main application is just a place holder to load module, and my module may use a DateField control), it will cause a lot of funny runtime errors, something like 'One of the properties is invalid' or 'cannot operate on null' etc.

To solve this, I usually have a IncludeClasses.as in my main application that include all control classes that my other modules will use. The following is my example IncludeClasses.as : -


package
{
import mx.charts.BarChart;
import mx.charts.PieChart;
import mx.controls.Button;
import mx.controls.CheckBox;
import mx.controls.ColorPicker;
import mx.controls.ComboBox;
import mx.controls.DataGrid;
import mx.controls.DateField;
import mx.controls.HSlider;
import mx.controls.HorizontalList;
import mx.controls.Image;
import mx.controls.LinkButton;
import mx.controls.List;
import mx.controls.MenuBar;
import mx.controls.NumericStepper;
import mx.controls.ProgressBar;
import mx.controls.RadioButton;
import mx.controls.TextArea;
import mx.controls.TextInput;
import mx.controls.TileList;
import mx.controls.Tree;
import mx.controls.VSlider;

internal class IncludeClasses
{
internal var button:Button;
internal var checkBox:CheckBox;
internal var colorPicker:ColorPicker;
internal var comboBox:ComboBox;
internal var dataGrid:DataGrid;
internal var dateField:DateField;
internal var linkButton:LinkButton;
internal var horizontalList:HorizontalList;
internal var hSlider:HSlider;
internal var image:Image;
internal var progressBar:ProgressBar;
internal var list:List;
internal var menuBar:MenuBar;
internal var numericStepper:NumericStepper;
internal var radioButton:RadioButton;
internal var textArea:TextArea;
internal var textInput:TextInput;
internal var tileList:TileList;
internal var tree:Tree;
internal var vSlider:VSlider;

internal var pieChart:PieChart;
internal var barChart:BarChart;

public function IncludeClasses()
{

}
}
}


And in the init() of the Application, add in this line:-

new IncludeClasses();


This will make the Flex compiler to think that those classes are in use and include their styles and resource bundle information.

Monday, August 2, 2010

Configuring ${DOCUMENTS} variable in Flash (Flex) Builder

When adding library reference in FB, if the particular .swc file is within the workspace, the FB will use ${DOCUMENTS} variable to refer to the targeted .swc file.

I had a problem when I created a maintenance branch in my SVN, being lazy to check out from the branch and re-setup my projects, I copied over my current working copy of trunk and do a quick switch. Every things work well except the ${DOCUMENT} variable is still referring to the old workspace directory.

Fortunately, I soon read from here that the ${DOCUMENTS} variable is actually set in the Workspace Preferences:-

Window -> Preferences -> General -> Workspace -> Linked Resources

You can also configure your custom variable there.

KZLite Payroll : The Quick Start Guides

KZLite Payroll comes with a set of Quick Start Guide (QSG) to assist the user to setup and maintain KZLite according to their own company requirements. The following lists the (QSG) available:-

Initial System Setup
-Company Setup
-Payroll Setup

Staff Record
-New Staff Joining
-Staff Resignation
-Staff Confirmation
-Staff Promotion / Transfer / Salary Adjustment

Payroll Process
-Create Payout Record
-Pay Salary Twice a Month (Mid Month & Month End Payroll Process)
-Payroll Statutory Export & Report (Monthly)
-Salary Crediting - Autopay & Report
-Advance Salary Processing
-Bonus Processing

When opened, a QSG shows a booklet style window that will guide you through the process step by step:-



For each step, they normally will have a 'Open Screen' link that will open the related screen. This convenient navigation way can greatly improve user's productivity when dealing with complex HR processes.

To try it, you can logon to our Online Demo.

Tuesday, July 27, 2010

KZLite Payroll is now Available

It has been a while since I updated this blog, I have been working full-force to reach a release point for the HRMS project written in Flex-Java.

The final project name is now fixed : KZLite for the suite name, and KZLite Payroll is currently available.

The website URL is http://www.kzlite.com.

There's a Online Demo version available (http://www.kzlite.com/demo), or you can download the installer to try it out on your local machine.

KZLite Payroll handles Malaysian Payroll Statutory requirements, and has ported many features from our Enterprise version of HRMS system (http://www.kzensolutions.com).

I will write about some useful tips in coming blogs.

Saturday, April 11, 2009

Learning Scala 1 : A Hello World

Scala is a powerful and interesting language to learn. Personally, I like it as the next generation of my daily written Java programming language.

To getting started with Scala is easy. Get a Scala distribution from http://www.scala-lang.org and unzip it to, for say C:\scala-2.7.3.final and set C:\scala-2.7.3\bin into your PATH environment variable. It requires Java runtime environment version 1.5 or later installed on your machine, I am having Sun JDK 6 Update 13 installed on my Windows XP machine.

After the above setup, you can start Scala Interpreter by entering 'scala' in command prompt:-


C:\>scala


Alternatively, you can start the Scala Interpreter also by double clicking 'scala.bat' under the 'bin' folder. You should after the Scala Interpreter waiting for your command now:-


scala>


Now, let's type 3 + 5 into it:-


scala> 3 + 5
res0: Int = 8


The 'res0' is a generated name to refer to the calculated value. You can try to print it out by using println():-


scala> println(res0)
8


You can declare 'val' (value) or 'var' (variable) in Scala, 'val' works similar like 'final' in Java, which reassignment is not allowed. You can try reassign the 'res0' to something else and you should get an error:-


scala> res0 = 9
:5: error: reassignment to val
res0 = 9


Now, let's declare a value call 'myValue':-


scala> val myValue = "Hello World"
myValue: java.lang.String = Hello World


You can see that I do not declare any type for myValue, the Scala compiler is smart enough to infer the value type to String, optionally, you can write it in the following way:-


val myValue:String = "Hello World"


Now, let's print it out:-


scala> println(myValue)
Hello World


That's complete my 'Hello World' in Scala!. ;-)

Friday, March 27, 2009

ACE for Flex 3 with AIR Exam Cleared

After a week of intensive reading after working hours, finally I got it through today with 92%. The preparation process was tough for me as the scope seems to be very wide. I studied mainly using the official materials from Adobe, which include:

Adobe Flex 3 Developer Guide
Developing Adobe AIR Applications With Adobe Flex 3
BLAZEDS Developer Guide

I didn't study for ActionScript programming as I am coding it in my daily job. ;-)

Looking forward to have a great weekend, Cheers!

Friday, January 9, 2009

'Work In Progress' Animated GIF Generator

I am looking for an nice icon to indicate 'Work in Progress' GIF today and found a nice site which let you customize and generate 'Work in Progress' animated GIF:-

http://www.ajaxload.info

Customize indicator type, colors and transparency.








Generated GIF with 'Bar' indicator:

Monday, December 22, 2008

Solving Table Name Case Sensitive Problem of Mysql on Linux

Table name case sensitivity issue is a common problem to hit when you are trying to port an application to use Mysql on Linux. You can set the Mysql to always store table name in lower case by setting the startup parameter lower_case_table_names=1.

You can type the following to check if the parameter is set or not:-

mysqladmin -u root -p variables


If it is not set, you can set it by editing /etc/mysql/my.cnf:

sudo gedit /etc/mysql/my.cnf

In my.cnf, it should have portions like this:-

[mysqld]
#
# * Basic Settings
#

#
# * IMPORTANT
# If you make changes to these settings and your system uses apparmor, you may
# also need to also adjust /etc/apparmor.d/usr.sbin.mysqld.
#

lower_case_table_names=1

user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
language = /usr/share/mysql/english
skip-external-locking


Then, restart the server:-

mysqladmin -u root -p shutdown
sudo mysqld

Thursday, December 18, 2008

Email, How Reliable is it?

Email is one of the most important application in Internet today beside Web. However, it has become a channel where hackers target to steal confidential information such as login info from user. One of the most popular trick is to masquerade and send a fake email, saying the user account is going to expire and ask the user to login with a fake hyperlink, which link to a fake page with similar design to the real login page.

From technical point of view, how easy to send a fake email? The answer to this question is : 'It's very simple'.

You do not need to write a C program, BackTrack 3 or any hacker tool. You can easily do it with high level language such as Java. It depends on how your email provider securing their SMTP server, but in my case, most of the companies that I have worked with is having their SMTP that you can easily send a fake mail.

At very basic level, these few lines of Java code using Spring Framework can masquerade as 'Your Boss' and send out an email to your colleague saying that he is fired:-

JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("mail.yourcompany.com");
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom("Boss <yourboss@yourcompany.com>");
helper.setTo("your_colleague@yourcompany.com");
helper.setSubject("You're Fired!");
helper.setText("I am very unhappy with your face, you're fired!!!");


You still will be able to find out which machine this email is sent from if you examine the email's properties. We joked with one of our colleague years ago with this. She was stunned and we quickly clarified with her before any bad thing happens next, it is sin. However, this shows that most people 'TRUST' email, and they believe it.

Do you believe your email?