Tuesday, December 29, 2009

Clear command in Cygwin

http://abhisanoujam.blogspot.com/2008/06/bash-cleaar-command-not-found-on-cygwin.html

Basically, install ncurses package from Util and Libs.

Setup OpenSSH on Windows

http://www.petri.co.il/setup-ssh-server-vista.htm

Basically, install cygwin with OpenSSH selected. Then run "ssh-host-config" to configure it. "net start sshd" to start it.

Very easy to use and install. Then you can use "ssh" and "scp"!

Monday, December 28, 2009

Android Dev Notes

Attach android source to Eclipse:



http://stuffthathappens.com/blog/2008/11/01/browsing-android-source-in-eclipse/

Basically, download source from google and copy frameworks/base/core/java/ to android-sdk/platforms/android-version/sources.

But it didn't work at first attempt. It turns out eclipse's cache is preventing source from showing up. Deleted some directories (don't remember exactly what I deleted :) under eclipse-workspace/.metadata/.plugins/org.eclipse.core.resources/.history and .projects directory. Then the source file begins to show up.

The link for downloading source code is like this: http://git.source.android.com/?p=platform/frameworks/base.git;a=snapshot;h=HEAD;sf=tgz (Thanks for http://geekycoder.wordpress.com/2009/07/03/how-to-download-latest-zip-android-source-code-and-using-it-in-intellij/) This link is for HEAD, if you want to download tagged version, such as 1.5, you have to find the correct value for "h" as in "h=HEAD". It can be found here: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=summary. Click any version you want, then in the URL, you can see the parameter value for "h".

Code Optimization for Dalvik VM



Accessing a local variable is much more efficient than accessing a field in the Dalvik VM. So creating a new local variable to refer to field variable will make the routine much more efficient. It is recommended that you use this optimization when possible.

Monday, December 21, 2009

Can not fix width for SPAN

SPAN is inline element, so you can not set the width for SPAN. Even you set it, it will be ignored by spec compatible browser. Use DIV instead.

Friday, September 25, 2009

Firefox cache executable jars

Just observed one weird behavior of FireFox when it downloads an executable jar file. If you select "open" jar directly, the file size of downloaded jar file may be not correct in the FF Downloads window. It could display the old same jar file size. It means if you open the jar directly, the file size on the Download window can be different from the size you just save the file. I think it's a bug that FF is getting the wrong file size if opening executable jar. The reason is that in the ${userhome}/Local Settings/Temp directory, each time opening the executable jar from FF, it will create another file with "-" and number as suffix to the original file. If the file downloaded is updated with a newer version and newer size, then it still getting the size from the very original file. For example, abc.jar is firstly downloaded and opened. Then abc-1.jar will be the name if you download and open jar again. If abc-1.jar has a newer size, the FF Download window will show the size from abc.jar

https://bugzilla.mozilla.org/show_bug.cgi?id=518850

Sunday, August 30, 2009

GWT Debug mode not working in Eclipse

It used to work fine. Recently I just came back to a project using GWT. Suddenly the Debugging is not working in hosted mode, even the most simple stockwatcher sample app. It was frustrating.

Google came to rescue. After some search, this blog (http://gbacon.blogspot.com/2009/06/gwt-not-hitting-breakpoints-in-hosted.html) is complaining the same issue and it turns out to be the "newer" JDK version (1.6.0_14) is the culprit. So either upgrade to latest 1.6.0_16 or downgrade to 1.6.0_13 fixed the problem!

Thursday, August 20, 2009

HTML position: absolute inside relative

If an absolute position is inside a relative positioned element, the absolute values are relative to the parent rather than to the browser window. This can be useful if you want to display popup relative to the mouse, but you don't need to calculate the mouse position.

Tuesday, July 14, 2009

http://www.dailyreckoning.com.au/austrian-keynesian-bastiat/2008/02/15/

Frédéric Bastiat pointed to one: the belief that the destruction of wealth fuels its creation. He explains this by means of an allegory that has come to be known as the story of the broken window. Most famously it was retold as the opening of Henry Hazlitt's Economics in One Lesson, which is probably the bestselling economics book of all time.

A kid throws a rock at a window and breaks it, and everyone standing around regrets the unfortunate state of affairs. But then up walks a man who purports to be wise and all-knowing. He points out that this is not a bad thing after all. The man fixing the window will get money for doing so. This will then be spent on a new suit, and the tailor too will get money. The tailor will spend money on other items and the circle of rising prosperity will expand without end.

What's wrong with this scenario? As Bastiat put it, "It is not seen that as our shopkeeper has spent six francs upon one thing, he cannot spend them upon another. It is not seen that if he had not had a window to replace, he would, perhaps, have replaced his old shoes, or added another book to his library. In short, he would have employed his six francs in some way which this accident has prevented."

You can see the absurdity of the position of the wise commentator when you take it to absurd extremes. If the broken window really produces wealth, why not break all windows up and down the whole city block? Indeed, why not break doors and walls? Why not tear down all houses so that they can be rebuilt? Why not bomb whole cities so construction firms can get busy rebuilding?

Tuesday, April 14, 2009

Groovy: No signature of method: java.lang.String.call()

Exception: groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types:

The first time I ran into this, took some time to figure out. Maybe because I haven't got used to this dynamic language.


def sendMail(mailhost, port, from, to) {
ant.mail(mailhost:"${mailhostname}", mailport:"${port}", subject:'Test Email') {
from( address:"${from}" )
to( address:"${to}" )
message(msg)
}
}


Code above will throw this exception, because groovy will use input parameter "from" and "to" to evaluate from, to function ant.mail closure.

Change to a different name.

Tuesday, April 7, 2009

HTTP keep-alive

Working on a download manager. If the download manager is generic to just work with any download url, how could it handle the download which requires user to login or accept some agreements/terms? Inside browser, such as FF's download manager, it will work since the cookie is sent each time to server.

1. Keep-alive (default 300 seconds) will make the URL still work for 5 mins. But after that, it will need cookie to make download work.
2. If resuming download from middle of the file, i.e. bytes=(value > 0), then the download works fine. The server only validates the download request for the initial request starting from byte 0. This makes sense. It will make resuming download still works since it's already authenticated.

Saturday, March 14, 2009

Hibernate key generator per model

Hibernate is designed to create a key generator instance for each model or each inheritance class tree. The logic is in the SessionFactoryImpl class:


while ( classes.hasNext() ) {
PersistentClass model = (PersistentClass) classes.next();
if ( !model.isInherited() ) {
IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(settings.getDialect(),
settings.getDefaultCatalogName(), settings.getDefaultSchemaName(),
(RootClass) model);
identifierGenerators.put( model.getEntityName(), generator );
}
}


This is quite flexible and customizable. Each table can configure a different sequence High Low key generator.

Same EntityKey for parent and child objects

This happens when modeling inheritance classes. If parent and child object which all extend same root class happens to use the same value for id, that's pretty fine since parent and child are different tables, but Hibernate will actually generate same entity key for them. That will lead to org.hibernate.WrongClassException.

After tracing into Hibernate, the reason looks to me is that Hibernate is using rootEntityName to generate hashcode for all subclasses. It will break if subclasses are using same id.

For example, Department (Dept) and Employee (Emp) class all extend from class Entity which has id attribute. When loading department and join fetching employees, if id in Dept table happens to have the same id in Emp table, that won't work. As of Hibernate 3.3.1 GA, in org.hibernate.engine.EntityKey:



private int generateHashCode() {
int result = 17;
result = 37 * result + rootEntityName.hashCode();
result = 37 * result + identifierType.getHashCode( identifier, entityMode, factory );
return result;
}



So the Dept and Emp are getting the same hashcode, and when loading Emp, Hibernate think it's already in cache, but that's Dept instance.

There is actually a ticket (not assigned yet) in Hibernate Jira: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3445

It's easy to patch, but not sure whether it will break other parts in Hibernate.

Friday, March 13, 2009

Duplicated resultsets by join fetch

Hibernate's join fetch is supposed to reduce resultset returned. But it returns duplicated results and behavior is not consistent between specifying join fetch in HQL and mapping.

For example, Department (Dept) has many employees (Emp) and sales deparment has 5 employees:
  • in HQL: from Dept d join fetch d.emps where d.name = 'sales' will return 5 exact same department records, each with totally initialized employees
  • in mapping: <set name="emps" table="EMP" inverse="true" fetch="join"> will only return 1 department with 5 initialized employees. This works as expected.
Hibernate has some suggestions: http://www.hibernate.org/117.html#A12

One possible workaround is using setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY). This works, although I feel they should fix it in the core.

Also see: http://glueclue.blogspot.com/2006/02/hibernate-duplicates-with-join-fetch.html

Sunday, March 8, 2009