Thursday, September 25, 2008

Apache commons usage

There are many good ready to use commons libraries from apache. These should have been in JDK. Often we fail to use their feature and spend time and effort to reinvent the wheel. I make an effort to brief the practical usage of these libraries here: 1. commons-lang public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Institution)) return false; Institution rhs = (Institution) obj; return new EqualsBuilder() .appendSuper(super.equals(obj)) .append(this.getSubscriberId(), rhs.getSubscriberId()) .isEquals(); } public int hashCode() { // Don't be tempted to use reflection here - Hibernate throws a major wobbly :( return new HashCodeBuilder() .appendSuper(super.hashCode()) .append(getSubscriberId()) .toHashCode(); } public String toString() { // Don't be tempted to use reflection here - Hibernate throws a major wobbly :( return new ToStringBuilder(this) .appendSuper(super.toString()) .append(getSubscriberId()) .toString(); } 2. commons-digester -This provides a wrapper to parse XML. Usage: Digester articleDigester = new Digester(); articleDigester.addObjectCreate("art", ArticleXMLTO.class); articleDigester.addCallMethod("art/fm/bibl/source", "setSource", 0); //setSource is a setter in ArticleXMLTO articleDigester.addCallMethod("art/fm/cpyrt/year", "setCopyrightYear", 0); //for publication date. articleDigester.addCallMethod("art/fm/history/pub/date/day", "setTempPublicationDay", 0); //For passing params to methods.. trialDigester.addCallMethod("MRCT_RECORD/CONTENTS/TITLE/DATA", "addField",2 ); trialDigester.addObjectParam("MRCT_RECORD/CONTENTS/TITLE/DATA", 0, "FIELD-NAME"); trialDigester.addCallParam("MRCT_RECORD/CONTENTS/TITLE/DATA", 1); //For encapsulated objects articleDigester.addObjectCreate("art/fm/bibl/aug/au", AuthXMLTO.class); articleDigester.addCallMethod("art/fm/bibl/aug/au/fnm", "setFName", 0); articleDigester.addCallMethod("art/fm/bibl/aug/au/mi", "setMName", 0); //for a set of objects articleDigester.addSetNext("art/fm/bibl/aug/au", "addAuth"); //addAuth method adds the object to the list. articleXMLTO = (ArticleXMLTO) articleDigester.parse(xml); Simpler than dealing with DOM or SAXing isn't it. 3.commons-configurator Please refer here

JDBC essentials

Good to know on how to build jdbc URL here

More to come .....