Thursday, January 13, 2011

Iterate over JAVA HashMap in FreeMarker

Java HashMap can be iterated in FreeMarker by using a <#list> tag as follows:

<#list hashMap?keys as key>
        ${key}=${hashMap[key]}
<#list>


If you are using the above code with default FreeMarker Configuration then there would be some different behavior during iteration. For example if the HashMap is:

HashMap hashMap = new HashMap();
hashMap.put("1","one");
hashMap.put("2","two"); 

By iterating on hashMap with default Configuration it will output following keys in result:

put
remove
entrySet
hashCode
productElement
clear
isEmpty
1
values
empty
underlying
productElements
copy
getClass
get
copy$default$1
equals
productPrefix
class
canEqual
keySet
size
2
containsKey
productArity
containsValue
productIterator
toString
putAll        


This result shows that the iteration is done on all the available hashMap properties.

To get rid of this you have to set the FreeMarker Configuration as follows:


Configuration configuration = new Configuration();
BeansWrapper wrapper = (BeansWrapper) this.configuration.getObjectWrapper();
wrapper.setSimpleMapWrapper(true);
this.configuration.setObjectWrapper(wrapper);

Here the BeanWrapper's simpleMapWrapper property is set as 'true'. After setting this configuration the results over iterating on hashMap will give only the defined key-value pair.

rizzz86

Wednesday, January 12, 2011

MD5 Hashing in J2ME

MD5 (Message Digest) is a hashing algorithm that can be used to ensure data integrity, save and protect passwords/codes, generate unique keys of limited length against a long literal etc. It takes input as String and generates the Hash of that string.

In JAVA there is a built-in MD5 support in its Standard Edition (J2SE). But if we talk about its Micro Edition (J2ME) their is no support for MD5. The package of JAVA Standard Edition can also be used in Micro Edition if and only if the platform supports the SATSA-Crypto Optional Package (How to know that your platform supports SATSA-Crypto).

I have found and tested the Third-Party implementation that can generate MD5 hash and can be used in JAVA Micro Edition. You can get the MD5 source files from here.

rizzz86