Category: Technical

  • Spring IoC, DI quick tutorial

    There has been an (evident) craze amongst Java community with contrived terms such as IoC (Inversion of Control), DI (Dependency Injection) mainly through Spring. I initially found the connotation “Don’t call me, I’ll call you” a bit difficult (yeah, it’s bending your head upside down) to understand, but after spending few hours around Spring documentation, I get the gist.

    To a layman (Java layman, of course) it is helpful to picture that each Java program is a set of one or more classes, which act together. Essentially, this means classes are “dependent” on some classes in order to be fully functional. Usually, the class requiring functionality of another class instantiates the class, and uses it. This is called coupling because class instantiates the object of required class. What if we always got an “instantiated” instance of required class, and our class did not have to worry of instantiation? – This is called IoC (Inversion of Control) principle in Spring, and it achieves this by providing ready-to-use instance (injecting dependency) to your class. This has some important uses, since now you don’t worry of creating connections to databases, loggers to log4j, or sessions for JMS queues. Spring will create these for you, and your class can focus on the actual purpose – using the pre-instantiated ready-to-use object.

    Get it? OK, to simplify it further, let us assume you have a class A having one field – log. You want to use log to log information, but your class nowhere has the logic to instantiate or initialize log. You accept a pre-instantiated log through constructor, or getter/setter methods, and you will have a ready-to-use log instance passed to your class via Spring!

    I won’t go into details of Spring further, since Spring’s own documentation on http://static.springsource.org/spring/docs/2.5.x/reference/index.html is the definitive source to look into.

  • Javascript sprite animations

    Javascript sprites weren’t as difficult as I thought. With a little bit of help, math, some css, js, images (of course) a decent animation can be shown without using any flash at all. I coupled it in a small class Animator, and wrote a small demo for anybody interested in using it.

    http://www.naiksblog.info/jsspriteanim/index.html

  • My first website – nostalgic

    I stumbled yesterday upon my first website I had created. This sure brings back memories on my old desktop PC, and keyboard sitting, and typing away at late nights 😉
    http://www.naiksblog.info/shantibhushan/default.html

    On another note, it does have some useful notes on Chasen (Japanese language morphological analysis tool), Mecab, Using Python with Chasen, Cygwin etc.

  • Outlook export to Thunderbird

    There is no straight (should I say “easy”) way to export Outlook mails to Thunderbird. Using Thunderbird’s import from Outlook did not work since quite a few emails came up as raw HTML, and I had to manually change Automatic encoding detection to OFF, Universal to see the contents each time.
    A sure-shot (well… which took me 99% of the) way was Outlook –> Import into Outlook Express –> Import into Thunderbird. This worked, but with one sad issue – non-English attachment names are not as original. If you can live with that, then this is the surest way to go !

    Few references which helped-
    Import .pst files – MozillaZine KB

  • Whois purely in SSI

    It was fun learning SSI, and tweaking around to get useful information such as detecting your IP adddress, and further WHOIS information.

    See below in action

    http://www.naiksblog.info/ip2country.shtml

    Impressive how quickly a page could be constructed using SSI. Courtesy to Apache’s mod_include, IP to Country database, and APNIC references.

  • Change VMWare Workstation language to English

    Rename folders present in C:\Program Files\VMware\VMware Workstation\messages, and restart VMWare Workstation. This will make VMWare start in English.

  • PHP in UTF-8

    To setup PHP for UTF-8

    データベース、スクリプトを記述するファイルの文字エンコーディングはPHPの文字エンコーディングを使用する方が設定が行い易いです。

    default_charset=”UTF-8″

    ダイナミックコンテンツの文字コードセットは必ず指定しなければならない。(セキュリティ上の理由。詳細は 2000年2月のCERTのXSSアドバイザリを参照)

    magic_quotes_gpc=off

    マルチバイト文字エンコーディング環境のみでなく、セキュリティ上も有害であるので必ずoffに設定する。ポータブルなスクリプトの場合、この設定 がonである場合にstrip_slashes()を全ての入力に適用するコードをスクリプトの開始時に実行する。稀に magic_quote_gpc=onである事を前提としているアプリケーションもある。そのようなアプリケーションは使わない方が良い。

    mbstring.input_encoding=”pass”

    現在のブラウザでHTMLを記述した文字エンコーディング以外で文字を送信してくるようなブラウザはない。(携帯などモバイル環境を除く)

    mbstring.internal_encoding=”UTF-8″

    ブラウザから送信される文字エンコーディングはcharsetと同じはず。プログラム側では必ず送信された文字 エンコーディングが正当なUTF-8エンコーディングであるか確認する事。

    mbstring.output_encoding=”pass”

    出力はinternal_encodingで行われる。つまりUTF-8。(携帯などモバイル環境を除く)

    mbstring.language=”japanese”

    言語環境を日本語に設定。mb_send_mail関数などの動作に影響する。

    mbstring.substitute_charactor=””

    入力に変換出来ない文字エンコーディングを含む場合、アプリケーションの実行を停止しなければならない。本来、セキュリティ上不正な文字を削除すべ きではないが、古いPHP(PHP 4.4.2, 5.1.2以下)では文字エンコーディングを確認する仕組みが無い。古いPHPで効率的に不正エンコーディングを検出するには文字列の長さの変化で確認す る。*1

    PHP 5.1.3, 4.3.3以降はmb_check_encoding関数を利用してスクリプトを実行の初期段階で不正エンコーディングを検出することが望ましい。

  • Racking brains – a Javascript string combination generator

    After racking my brains for almost 4 days (yeah I am a slow learner) I finally created a simple Javascript string combination generator

    See the demo http://naiksblog.info/stringcombinations.html

    I tried modeling the logic to how databases combine sets in a cross join. All rows from left side are combined with all rows from right side.

    The script is as below

    	function combine(a, b) {
    		var r= new Array();
    		for(var i= 0, k= 0; i < a.length; i++) {
    			for(var j= 0; j < b.length; j++) {
    				if(-1==a[i].indexOf(b[j])) r[k++]= a[i]+ b[j];
    			}
    		}
    
    		return(r);
    	}
    

    You can call this as below

    	function permute() {
    		var a= "abcd";
    		var p= new Array();
    
    		for(var i= 0; i< a.length; i++) p[i]= a.charAt(i);
    		var r= p;	// Input string as-is is first permutation
    		for(var i= 1; i< a.length; i++) r= combine(r, p);	// Get the permutations
    		// r.length - Get the combinations
    		// r contains all combinations as an array
    	}
    

    Some points worth noting

    • If any character is repeated, the combination does not happen successfully since the logic tries to remove same character matching elsewhere
    • The number of combinations increase by factorial of the number of characters, hence it will be a good idea to perform this on server side ideally otherwise javascript will hang for large strings
    • Logic could be optimized to generate combination in a different better way than using crossjoin strategy
  • Extremely useful tips for Java

    1. A .java class can contain only one top-level public class.  All other top level classes in same .java file cannot be private or protected or public, only default (package-level access) is allowed.
    2. If no package is specified for class, it belongs to unnamed package – which cannot be imported.
    3. Unlike Linux utilities, long parameters or short parameters to java(c) need just a space before parameter value. e.g. -classpath <classpath> or -cp <classpath> are same. There is no -classpath=<classpath> syntax in java(c) command line.
    4. 8 primitive types (byte, short, int, long, float, double, char, boolean), special type void, and base type for all objects – java.lang.Object.
    5. null is not a keyword, type. It is a value.
    6. String objects created using short-hand String A= "a string";
      syntax creates Strings on pool in heap. A is pointer (32-bit to max. 64-bit) to a String object “a string” on heap memory allocated to your program. Using short-hand syntax to “a string” on String B= "a string"; will point to same string object on heap.
    7. Object gets garbage-collected, not it’s reference.
    8. Garbage collection is indeterminate. You cannot guarantee freeing of memory by forcing System.gc()
    9. finalize method is invoked only once before being removed from memory. Always call super.finalize() if you override it.
    10. Parameters (primitive, references) are always passed-by-value. There is no pass-by-reference in Java. The references passed can be used to modify the object the reference points, but not the reference itself is not modified.
    11. Return values from function are also passed by value, never by reference.
    12. Compound operators (+=, -=, *= etc) automatically cast. Compiler never throws any error on data mismatch.
    13. & (and) results in 1 if both are 1, | (or) results in 0 if both are 0, ^ (xor) results in 1 if either operand is 1
    14. char supports increment, decrement operators.
    15. The default isequal() implementation of Object tests for reference equality, which is same as ==.
    16. When using operators, Java automatically promotes values to int or higher. For values below int – byte, short – an explicit cast is required.
    17. extends comes first then implements
    18. Java tokenizes your source into separators (tab or space), keywords, operators, literals, identifiers (variable names).
    19. new zeroes Object’s fields (instance variables), hence explicit initialization can be skipped.
    20. import static allows importing static variables like System.out into your program. Hence, you can simply use out without prefixing it with class name System.
    21. local variables must be initialized before using. Though method parameters are local variables too, since methods are called with parameters, they are initialized.
    22. arrays are fixed size data structures, where as collections are dynamically sized data structures.
    23. For multidimensional arrays, it is easier to imagine the first dimension as a single row containing the other dimensions.
    24. Array initialization always requires dimension to be provided when initializing through new (caught at compile time). If using array initializer, you can specify {} empty braces, but accessing any index at runtime will only result in index out of bounds exception.
    25. There is always a default constructor – empty body – available for your class only if you do not declare any constructor.
    26. super, this must be the first line in constructor. this() or super() both cannot be placed.
    27. Parent class constructor super() is always called. For this reason, you need to have an empty constructor always in parent class.
    28. Method signature is composed of method name and parameters. Modifiers, return type, exception list has nothing to do with it. Access-specifiers however control visibility.
    29. Javabean properties are get-, set- methods provided over a field in Javabean style.
    30. Variable length parameters always come at the end of the list, and they can be empty (meaning nothing was passed). Being at end of the list automatically implies that only variable length parameters is allowed. (Java 5.0)
    31. Overloading means providing different method signatures in same or inheriting class. Overriding means providing same return type, method signature in inheriting class.
    32. Covariant return types means overriding method returns a subclass of return type in parent class. (Java 5.0)
    33. Method hiding means overriding of static methods.
    34. Abstract method has no body. Interface methods are all abstract, while Abstract class can contain zero or more abstract methods.
    35. All abstract method of Interface are public, it’s fields are public, static and final. No static methods are allowed.
    36. Abstract method compiles with static modifier, but generates runtime error if directly accessed. If abstract static method is overridden in child class, child class works fine.
    37. Enum constructors are invoked for each element.
    38. Anonymous inner class can have only one instance. Local inner class can have multiple instances.
    39. Static nested class is similar to any top-level class.
    40. Static fields in class always need default value; otherwise code does not compile.
    41. false is boolean, 0 is not false, it is int zero
    42. switch…case does not allow case null, since a constant (and final) expression is required for case
    43. You can put break in default for switch
    44. enum classes can have main method
    45. for(;;) is a valid for-loop. It never finishes, since it is an infinite loop.
    46. In enhanced for loop, the variable used for iteration must be declared only in for loop, not outside it.
    47. Java complains if you have unreachable while loop code e.g while(false) is not allowed. However, contrarily if(false) is allowed!
    48. Variables declared inside do…while loop are not available in while to test for condition!
    49. It is a good idea to add labels to your loops to enhance readability
    50. Assertion is put in places in your code where you think it should be always true
    51. Though not a good design, but assertions do allow modification on variables while asserting them e.g. assert ++i
    52. try…catch – catch can never have an exception which is a subclass of previous catch clause. It throws compile error.
    53. Checked exceptions always derive from Exception, runtime exceptions derive from RuntimeException, errors derive from Error. Java enforces checked exceptions are either handled or thrown. For runtime, error there is no such rule, even if RuntimeException derives from Exception.
    54. try… must have either catch or finally atleast, otherwise compile gives error