Tuesday, October 19, 2010

Spring security core build path problem

First time trying Grails 1.3.5, creating a new test project. Installing spring-security-core plugin (following the tutorial) and then importing the project to STS 2.5M1 and letting STS know that it's a Grail project (Configure->Convert to a Grails project... something like this). I was hoping for an easy ride here, expecting no errors since I haven't add anything, all are auto generated.

But as usual, with programming world, there ain't no such things as a bug/error free codes. I got compile errors like this in my LoginController.groovy.



I know that I'm missing org.springframework.security.core-3.0.3.RELEASE.jar release in this case and the shortcut to solve this is just to add the jar to the project build path. But, is this going to happen for each plugin jar that I'm going to use? and anyway I don't like the solution to put the jar manually on the build path, it does look an ugly solution.

Fortunately I find a menu to refresh Grails' dependencies and the good news is it works for me, no more compile errors. Here's the context menu that pops up when I right click on my project:



I think this is caused by me adding the plugin from command prompt and then importing the project in STS. Imho, if the plugin is added thru STS' grails command prompt it should works without refreshing.

Hmm... just remember that this is similar to when I add a new file to my project using my file manager. Eclipse can't see the file, until I refresh the project.

Tuesday, September 14, 2010

Using hibernate generated collection in h:dataTable

It's a bit difficult to pick the correct title for the thing that I'm going to write :) It's about having your POJO generated by hibernate and then feed it to h:dataTable.

When your object has a collection properties and the elements contained are unique within the collection, hibernate will create a Set to contains it. The thing with a Set is h:dataTable just can't accept it. The simplest solution is to change Set to Collection object and h:dataTable can accept it and Hibernate won't complain about it :)

Rolling back transaction (Seam managed transaction)

JSF+Seam is what I'm learning at the moment. And the case is I need to rollback the transaction due to business exception but I don't want to not catch the transaction (when a runtime exception is uncaught the transaction will be rollback automatically). The business exception here is a new exception class extending RuntimeException.

At first try, I add ApplicationException annotation with rollback = true in my BusinessException class. The controller class which invokes the business service class and under certain condition an exception of BusinessException type is thrown. In my scenario my transaction should be rolled back, the thing is... it was not rolled back.

This forced me to dive into seam's code (that's why I love open source) and find out how to handle the transaction. And I came into Transaction class, which I can use to do what I want.
Here's how to use it to mark the transaction as rollback:

Transaction.instance().rollback();

Actually all I wish is when the exception is thrown by the service class (already registered to seam) the transaction will be rolled back automatically, unfortunately for me it doesn't work :(

Entity inheritance-single table per class hierarchy strategy with JPA + Hibernate

I happened to have the chance to implement entity inheritance using single table per class hierarchy strategy. At first I though I would sail smooth and arrive at the promised land easily :) but as usual with programming, it is a rare thing a code would sail smooth at the first time.
Persisting entity works like charm, retrieving it makes me speechless. Here's an example:

The base class (ItemReference)


Subclass#1 (ItemReferenceAge)


Subclass#2 (ItemReferenceSex)


DDL


One class that I don't include here is the class which has a collection of ItemReferenceSex and a collection of ItemReferenceAge, let's call this class Wrapper.

The scenario is I add 2 instances of ItemReferenceAge to wrapper collection and persisted it. What happened when I try to load the same wrapper instance is both collections are having elements in them. Remember, I only add ItemReferenceAge instance which means the collection of ItemReferenceSex should be empty.
Looking at the query generated shows that the discriminator column is not used in the where clause of the select query. What I can think of the design reasoning is that it is assumed that there will not be two or more type of instances loaded in the same parent object which in my case it is.
Luckily for me, there's a solution for this and unfortunately it's hibernate specific annotation (is it maybe because of JPA doesn't state how to handle my case or is it hibernate implementation?). All I need to do is to add ForceDiscriminator annotation in my base class and the problem is solved!

Thursday, August 19, 2010

Eclipse fails to start?

Your eclipse fails to start? actually we could say your workspace fails to start. I've gone through it several times and my solution was to create a new workspace and import the existing projects into the new workspace.
However, this time I'm trying to solve it once for all (as though it's possible :). Googled around and I found, imho, a rough solution that works for me. It's as simple as deleting org.eclipse.core.resources folder inside {workspace}/.metadata/.plugins and then run eclipse -clean.
The result is eclipse is working but I still have to import the existing projects back. The good thing is I don't lost all the preferences and server setting (though I could just import the preferences file I once exported).

Check the link on the title and also this one, tools from eclipse team that maybe could help with the broken workspace (I never tried it yet... my workspace already fixed :)

Tuesday, August 17, 2010

A bit on classloader

At first glance, I thought java could face the same problem as dll was (dll hell). But then classloader scoping comes to the rescue. And by the way, I won't explain the theory of classloading here. By having classloader scope set to the application, it will allow each application to bring their own libraries without worrying of library conflict.

For example, I built a library of common utilities and there are already 2 versions of it. And 2 of my web applications are using the library, however each with a different versions from the other. If the flat classloading is used, only one of the 2 versions of the library will be used and the impact is the web application which used the version not loaded will have strange errors such as ClassCastException, IllegalAccessErrors and possibly many others. Having classloader scoping will allow each of my web application to use its respective library.

One thing cross my mind, how could I have a smaller deployment size for a web application and still having the classloader scoping. Smaller deployment size here refers to my classes only without the whole bunch of libraries. Usually when people delivers a patch for the web application, all is packed in war file which size is quite large. On my case, the library size are around 80% of the war file size. Up to now, all I can do is to deploy the application as exploded. Thus allowing me to deploy patches by replacing the content/libraries. Not an elegant solution but still a viable one, until I can find a nicer solution.....

Sunday, August 15, 2010

Initialize static List and Map

If you intend to create a List or Map for your constants, here's how you do it.

List

public static String CONSTANT_1 = "1";
public static String CONSTANT_2 = "2";

public static List CONSTANTS = new ArrayList() { {
add(CONSTANT_1);
add(CONSTANT_2);
}};


Map


public static Map CONSTANTS = new HashMap() { {
put("key1", "value1");
put("key2", "value2");
}};


Even better is to wrap the ArrayList/HashMap by passing them to Collections.unmodifiableList or Collections.unmodifiableMap. Thus making sure that it is truly a constant list/map.

Wednesday, July 28, 2010

How to search for column reference in oracle

Imagine that you need to find out which code update a column in a table. Some developer tools could point out who refers to the table but so far I couldn't find the one that could answer my need.

All_source comes to the rescue, I'd say that it is a special view that holds all the database objects accessible by the user.

Here is the description on the view's columns:
OWNER VARCHAR2(30) NOT NULL Owner of the object
NAME VARCHAR2(30) NOT NULL Name of the object
TYPE VARCHAR2(12)
Type of object: FUNCTION, JAVA SOURCE, PACKAGE, PACKAGE BODY, PROCEDURE, TRIGGER, TYPE, TYPE BODY
LINE NUMBER NOT NULL Line number of this line of source
TEXT VARCHAR2(4000)
Text source of the stored object

Example:
I need to find out who refers to column FK_USER.

select * from all_source where text like '%FK_USER%'

And I will get all the places which have reference to FK_USER.

If you would take it further, you could utilize it for searching almost anything (e.g. comments in the code, TODO tag, exception).

... wondering if there're more "magical" view like this.

Saturday, July 24, 2010

Retrieving generic's Class type

Quite few times I've been using generic and I needed the Class type. I found out that I've to pass the class type instead of extracting the class type from the type parameter. This story comes to an end when I found out a way to extract the type, thanks to Type interface :)

Here's the recipe:
- First extract the genericSuperclass
- Retrieve the actual type arguments

.. and the code is:
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
Class classType = (Class) type.getActualTypeArguments()[0];

ParameterizedType represents an invocation of a generic class or interface, better explanation could be read in the javadoc.

That's all... :)

Saturday, June 12, 2010

Knowing the exact location of error in your jsp

Actually it's been several times I have to deal with error happening during jsp rendering. To be more exact, the error doesn't lie in the jsp page but in the content prepared before rendering the jsp. Usually what I do is just removing part by part of the jsp page until I can locate the part that caused the rendering error. I keep on doing it until I remember that during page rendering a buffer is used and the buffer size itself is configurable.

Here's in short of how to do it:
- Insert the following page direction to the top of the jsp page
<%@ page buffer="none" %>
- Try to open the page again in the browser and view the source
- The last position of the source page will show exactly the position of the error, i.e. the position before the source of the error
- After you've managed to fix the error, don't forget to remove the page directive. It will certainly degrade the performance if you leave it there

Tuesday, April 27, 2010

Configuring static IP in Fedora

If all you have is a CLI, then you might find this to be of benefit. Otherwise you could just use the Network Device Control GUI.

Here are the steps:
  1. Execute ifconfig and find out your ethernet configuration name (e.g. eth0)
  2. Go to /etc/sysconfig/network-script
  3. edit ifcfg-eth0 (depends on your ethernet)
  4. Update the file as follow (set the IPADDR, BROADCAST according to your desired configuration)

  5. Restart the network service
    service network restart

Here's a brief explanation on the configuration:
  • BOOTPROTO : telling the network that we're configuring it as static ip
  • ONBOOT : the configuration will be set at boot time
  • BROADCAST : your gateway
  • NETMASK : your netmask


.

Saturday, December 12, 2009

Opera Turbo

Someone asks me if is it safe browsing with Opera mini (which defaulted to use Turbo)? The safe here mentioned refer to the data sent being stored by Opera Turbo (remember, all data traffic goes thru Opera Turbo so that it could compressed the content and sent the result to the browser). This question is answered on the following link:
http://labs.opera.com/news/2009/03/13/

If you are using ssl, Opera Turbo is bypassed and we're communicating with the SSL site directly. Which implies that turbo is turned "off" and you're back to the old way of browsing. Opera Turbo also declares that it doesn't store any user's information.

The conclusion is if we're looking for a cheaper way to browse, which is the case where the price mostly depends on the data being transferred (usually per KB), Opera Mini will be a great choice. Bear in mind, the statement declares that no user's data is stored. If you don't trust it then it simply mean don't use Opera.

Before I finished, I read one of the comments on this blog. It mentioned that Opera is a Norwegian company, which means that it follow the Personal Data Act (http://www.datatilsynet.no/templates/Page____194.aspx). And it is extremely strict.

Wednesday, December 09, 2009

Agile Manifesto

The thought of using agile methodology started about 3 years ago. Never been involved in a project using agile, I was looking for one to be involved in or have "my" project using it.
It seems to be challenging enough to use it, to know the advantage and of course the weaknesses. To find out the know how-when to use agile or waterfall. I'm not all for agile, but I have to admit agile excites me more. Adopting one of agile practice in one of the projects I'm involved in, which is daily meeting (face to face communication), opened my eyes to realize that the team starts to become more alive. Everyone starts to involve themselves more, discussion on the requirements become more often. As opposed to developer works only as they're told to.

Reading on agile manifesto, quoted below

We are uncovering better ways of developing software by doing it and helping others do it. Through this work we have come to value:

  • Individuals and interactions over processes and tools
  • Working software over comprehensive documentation
  • Customer collaboration over contract negotiation
  • Responding to change over following a plan
That is, while there is value in the items on the right, we value the items on the left more.


quite confirms that agile might be the one we should use for the development. But still I'm not all for agile, there're still some factors that drives me to choose the old methodology. Such as large complex projects, the number of developers involved. Maybe it's me not knowing in depth on how agile methodology really works.

After all, it fall back to one step at a time. Welcome agile, I'm looking forward to really understand you more :)

Tuesday, July 07, 2009

Connect to wireless network using netsh

If you kinda tired of constantly changing your wireless network when you're at home or office, batch file could be a great help for you. A single click is enough to setup your wireless connection. I was using Net Profiles and I'm a satisfied user, but this single click idea beats Net Profiles.
Of course you've to create a batch file for each connection you want to setup, but that's not too much to do (imho).

FYI, I'm using Vista. For XP, the command could be a bit different but the idea is still the same (c'mon improvise).

Now, here's the simple command line to connect to a wireless lan:
netsh wlan connect name="network name" interface="Connection name under Manage Network Connection" ssid="the ssid"

example:
netsh wlan connect name="hpsetup" interface="Wireless Network Connection" ssid="hpsetup"

in case you use static ip in your office or home, the command goes like this:
netsh interface ipv4 set address "Connection name under Manage Network Connection" static the_static_ip mask the_gateway

example:
netsh interface ipv4 set address "Wireless Network Connection" static 192.168.1.102 255.255.255.0 192.168.1.103


One more thing, running under Vista you need to run the batch file as administrator.

After all, it's only one click away.....

Tuesday, June 16, 2009

Virtual Serial Port

How did I start to play around with it? It all began when a friend of mine was asking me for a favour to help him develop a simple software to communicate to another software thru serial port. My laptop doesn't have a serial port (of course, unless it was a very very old laptop) and I only have one usb to serial converter.

My first idea is to buy another usb to serial converter and a null modem cable to connect 2 laptops, thereby I can test the serial communication. Another idea is to use my old desktop (I have one and lent it to someone and actually forgot about it until I need it) but I don't think it is easy to do.
Then today at the office, asking my friend and he gave me one brilliant solution called VIRTUAL SERIAL PORT. I never heard it until today and my oh my... it is the solution to my problem (Thanks Adhi).

Googling around and I found one free driver (http://com0com.sourceforge.net/). Easy to install and I think I don't need to setup anything more, unless I need to configure another serial port pair. One thing lacking is the GUI to configure the ports. Anyway, it's free and open source, I couldn't be happier.

Friday, June 05, 2009

Turning Toshiba Satellite illumination led off

The first time I got my Toshiba M300 (actually it's not mine, it's my office's), I love to see all the LEDs light up the laptop.



Not until a week that it already bored me to see my laptop lights up as if it was a festival. Then to my dismay, I found out that turning off the LEDs wasn't easy (I did manage to turn it off though).
My colleague buying the same laptop as mine, also find it difficult and he did ask me how.

Today, someone asked me (she's using toshiba also) how to turn the LED off and gladly I told her and write in this post on the how to.

1. Press fn button and click on the rightmost button


2. Click on the HWSetup button


3. Go to Illumination tab and select Off and click OK. You're done.

MD5 hash

This morning I read a blog discussing about how to keep a secure password. The usual way to store a password is first to hash it using MD5 hash and store it in a db. The benefit of using MD5 is you cannot do a reverse-hash.

Here's how you code it in Java :

MessageDigest md = java.security.MessageDigest.getInstance("MD5");
md.update("your password here");
byte[] hashed= md.digest();


To convert it in hex, here's the code (actually I got it from here):



One more tips, for better security, you might consider to add salt before hashing the password to make it less vulnerable. The changes looks like this :

md.update("The salt" + "your password here");

"The salt" here should be created dynamically and stored it along with the hashed password to be used later.


An excellent post on secure password scheme could be found here.

Friday, May 22, 2009

Being critical with your own capacity

This post I think only relates to small company where employees are less than 30 ppl or where there're no strict structure applied.
It's about how people responses when they're given task while they're still working on one.

By far, I've seen 3 ways of ppl managing their capacity:
  • "Yes man" (or "can do guy")
    This guy will say yes and confidently say I can to all the tasks given to him. Usually this type of guy is a smart guy, able to work everything fast and done as it seems. This guy is willing to sacrifice everything for the sake of finishing all the tasks. One thing I notice though, shortcuts were applied everywhere to get things done. The simple reason for this is there are a lot of tasks in hand, and as quickly as possible he wants to get rid of them all one by one.

  • Rational guy
    This type of guy is the rational thinking one, when given a new task he will look at his capacity first and dare to say "I can't" if he's asked to finish the task using the time allocated for other task. Mostly guys with this type are a bit perfectionist, they have their argument that task should be finished in a right way, with few shortcuts (or none if possible).

  • Quiet guy
    Given tasks beyond his capacity, he will keep quiet. He will keep on working on the tasks one by one, sadly without minding the time limit he has. The result is this guy will work overtime at most and doesn't meet the schedule. The quality of his work might not matter for him, he will keep on working and someday when he is tired enough, he will resign and start over in other place.

So which type is the best?
I would say a small company only needs 1 or 2 "Yes man" and a lot of "rational guy".
There are times where resources are limited and adding more resource is not the best solution. This is where "Yes man" is very helpful, however having too many "Yes man" won't be good either considering the quality of the work.
A growing company is a company which also learn to realize quality does come with price.

One caution for "rational guy", sometimes he might become too rational that he start to reject the task for the sake of task dislike using the current task as an excuse.

Thursday, March 19, 2009

Scheduling a project correctly? Almost impossible!

Scheduling a software project, I can't say that it's enjoyable. To say that it's complicated, risky and very unreliable task is agreeable for me. All the task durations set are all guestimate, basically based on hunches. After all, who can measure software development correctly. And to imagine that everything would go smooth is one thought that should never cross your mind.

The latest schedule that I arranged was a schedule for documentation project, actually it was a product reverse engineereed and documented. We have a client bought the product and they're interested in buying the source code along with the complete documentation.

At first, I thought for this project I was a bit lucky, no development needed. The thought that I forbid to cross my mind actually entered in. Thinking that documentation are all measureable, I began to arrange the schedule. The project started and after a month passed by, I begin to notice that problems start to creep in along with their surprises :
  1. New things to be documented start to pop up, things that I didn't see in the first place.
  2. Tools that we used sometimes stressed us up, even M$Word! The documentation has reached almost 500 pages, when my team tried to format the doc sometimes it stopped working. And still not to mention other tools we used. Are we picking a wrong tools here? Nope, I don't think so. Problems definitely will be met whatever the tools we picked.
  3. Human resources. When one member of the team quits, it left a hole that couldn't be filled straight away.
  4. The skills required. Not everybody has the skill to document, create diagrams, read existing code.
  5. Interrupt. I have to help other project and what can I do but to assign my time to help.

I hate the idea that working over hours is the only solution to finish the project on time. I prefer working smart, which are :
  • Finding patterns repeatedly used in the documentation. I'm lucky enough to have a teammate who is also able to recognize patterns repeatedly used in the documentation. Create/automate it once and solve it for all.
  • Start measuring the time spent for a task and focus on working faster for the same task either by optimizing the way we work, finding the common things (i.e. pattern) or even using copy paste :)
  • Spend less time on the normal working hours for other things we usually do (e.g. reading mails, newspaper, etc).

The project is still on its way, let's see whether we could finish the project on time or not :)
and whether the smart way will prevail over working over hours.

Before I finish, some books that I read also mentioned that software projects tend to not meet it's schedule, which I hope I could disagree with. These two books I would like to recommend : Secrets of Success Software and Mythical Man Month.

Tuesday, February 17, 2009

Running grails app in eclipse

Running grails app in eclipse actually is quite simple if... you know how.

Here's how :
- Import your grails app as existing project in eclipse
- Find {app-name}.launch in the project root and right click select Run As and the .launch configuration name
running-grails-app
- There's all to it :)

If you want to know how to debug grails app, see my previous post.