Thursday, May 21, 2009

XSLT caching Transformers

 The usage of cached transformer objects is recommended here

A sample implementation of CachingTransformerFactory is here

The above code abstracts the caching of Transformer objects using HashMap's.

In brief it says to override the TransformerFactoryImpl and cache the transformer objects. The xsl updates are dealt by checking the timestamp on the XSL file.
Hence no need of a web service to update XSL transformer cache!!

Using TransformerFactory.newInstance() ,
there will be absolutely no code change. The Services API will look for a classname in the file META-INF/services/javax.xml.transform.TransformerFactory in jars available to the runtime. More here...






Thursday, May 14, 2009

Cron jobs

Cron actually an UNIX tool.

Cron expressions:
* * * * * *
seconds minutes hours "day of month" month "day of week" year

Except year rest all are mandatory.
The details are here



Wednesday, May 13, 2009

JSP implicit objects printer

copy paste the below and include it using <%@ include file="jspPrinter.jsp" %>

<%@ page
errorPage="ErrorPage.jsp"
import="java.io.*"
import="java.util.*"
%>

<%
Enumeration enames;
Map map;
String title;

// Print the request headers

map = new TreeMap();
enames = request.getHeaderNames();
while (enames.hasMoreElements()) {
String name = (String) enames.nextElement();
String value = request.getHeader(name);
map.put(name, value);
}
out.println(createTable(map, "Request Headers"));

// Print the session attributes

map = new TreeMap();
enames = session.getAttributeNames();
while (enames.hasMoreElements()) {
String name = (String) enames.nextElement();
String value = "" + session.getAttribute(name);
map.put(name, value);
}
out.println(createTable(map, "Session Attributes"));

map = new TreeMap();
enames = request.getAttributeNames();
while (enames.hasMoreElements()) {
String name = (String) enames.nextElement();
String value = "" + session.getAttribute(name);
map.put(name, value);
}
out.println(createTable(map, "Request Attributes"));

%>



<%-- Define a method to create an HTML table --%>

<%!
private static String createTable(Map map, String title)
{
StringBuffer sb = new StringBuffer();

// Generate the header lines

sb.append("");
sb.append("");
sb.append("");
sb.append("");

// Generate the table rows

Iterator imap = map.entrySet().iterator();
while (imap.hasNext()) {
Map.Entry entry = (Map.Entry) imap.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
sb.append("");
sb.append("");
sb.append("");
sb.append("");
}

// Generate the footer lines

sb.append("
");
sb.append(title);
sb.append("
");
sb.append(key);
sb.append("
");
sb.append(value);
sb.append("

");

// Return the generated HTML

return sb.toString();
}
%>

Wednesday, April 22, 2009

And the name was with DNS

Find out dns server ip address under:

Windows  C:\>ipconfig /all

Linux:  $ cat /etc/resolv.conf

Wildcard DNS: refer this

Wildcard DNS and apache virtual host setup here


Tuesday, April 21, 2009

Rewrite rules in apache and IIS

Well we can control how the server serves stuff to clients by defining rewrite rules.

As servers are dumb, its important to explain well about the rewrite rules. For example you should explicity say when a rule matches a URL pattern, no need to seek for further rules('L' flag).

The most interesting part is the regular expressions used for rewrite rules. This I will discuss sometime later...But to do some hit and try use me.

Okie, first with apache:

The rewrite rules are handled by module mod_rewrite. This reads the rewrite conf file configured in httpd.conf.

Include the rewrite rule path in httpd.conf >> VirtualHost >>
    Include /path/to/rewrite.conf

Define a rewrite rule file /path/to/rewrite.conf
All set. Ok now the details on rewrite rules:

#This says that rewrite engine is on. This can be configured in httpd.conf as well.
RewriteEngine   On

#for debugging only
RewriteLog logs/rewrite.log
RewriteLogLevel 9

#check if a condition is true:
RewriteCond %{HTTP_HOST} ^local.xyz.com$

#if true above then only apply the below rule(s).
RewriteRule    ^/?$          /journal/index.html [PT]

#RewriteRule    has the syntax RewriteRule
Pattern Substitution [flags]

The important flags are:

L - Stop the rewriting process here and don't apply any more
rewriting rules
R - Redirect. Must have 'L' flag usually with this(to stop processing further)
N - Re-run the rewriting process
P -This flag forces the substitution part to be internally
forced as a proxy request and immediately
NC - No case .This makes the Pattern case-insensitive
PT - pass through to next handler . Works with alias..Used rarely??.

Lots more on Configuration Directives for apache rewrite rules go here:

Gotcha : To apply a rewrite rule based on the request parameter you *have* to use
RewriteCond  %{QUERY_STRING} ^param=value
By default the RewriteRule does not apply for request parameter

On ISAPI rewrite rules for IIS:


Though it is supposed to be a clone of apache mod_rewrite to help IIS do the redirections there are some differences. There are more ISAPI_Rewrite directives for IIS like RewriteHeader ,etc
More or less the syntax match (thank god) and should be easy if u know apache rewrites.

More on IIS rewrite rules here



Monday, March 30, 2009

SMTP mail from windows using telnet

Reference here

To summarize:
telnet host port

helo myMachineName

mail from: testFrom@test.com

rcpt to: testTo@test.com

data
This is a sample mail.  [Press . and enter to complete message.]

quit


Friday, March 13, 2009

Liferay - web 2.0 made simple!

Like Drupal or Joomla CMS/portal solutions in the PHP world we have few good open source java solutions. Liferay is one of them.
This product was chosen by my previous employers client( a large online media publishing house of USA). This is when I got a chance to play around with liferay for around 1 year. 
This client wanted to upgrade their media sites to latest technology but with minimum costs and the so called web2.0. Liferay was evaluated and given a choice by our onsite/offshore Archtects.
"Brian Chan, Chief Software Architect and founder, began development on Liferay Portal in 2000 to provide nonprofit organizations with an open source solution to facilitate collaboration on the Internet. He has since steered Liferay to become a leader in innovative open source enterprise solutions. With a strong foundation in software architecture and economics, Brian has solidified open source as a low-cost, high performance solution for the enterprise. His expertise in portal architecture and design has garnered him a seat on the JSR-286 portlet specification committee "
What liferay has is - a well organized and fusion of the best java/open source technologies.  
Most organizations end up reinventing the wheel- doing a portal after spending time in evaluating, building and testing different open source technologies. For example web/service/persistence tiers, RSS feed, blogs, theme design, etc would need enough effort . A complete lifecycle is required to get things going. If they are not well organized they end up with maintenance nightmares or issues when trying to extend it features.. 
The technicalities:
You name a buzzword in Java/J2ee (No EJB please) and they have it
 http://www.liferay.com/web/guest/products/portal/techspecs
To list a few java buzzwords what Liferay Portal has:
    Web Services,
         REST, WebDAV
    Architecture:
          SSO(CAS), ESB support, modular, pluggable
    Performance and scalability:
          Clustering, Caching (ehcache), page/portlet caching.
    Supported standards:
        AJAX, JSR-168/286(for portlets)
    Uses best of the breed open source technologies
        To name a few - jquery, lucene, spring, hibernate, struts, velocity, ehcache
   
    SOA, SSO, Web2.0 features - wiki, blog
    Most databases supported.(Persistence tier uses hibernate)
Content Management:
    Document Library - JSR-170  
    Versioning/workflow/webdav/image gallery,etc.
    SEO/Site map, rich text editors, friendly urls
Themes and layout:
    jQuery standardized
    hot deployable
    velocity based.
More here
A video to create a journal article on a site so easily is here
A few numbers to show its popularity:
Google returns
    363,000 results for "Liferay portal" (exact search including quotes)
    1,710,000 for liferay
Started in 2000 9th year of development.
Liferay Community forums:
# of Categories: 41
# of Posts: 77,263
# of Participants: 8,524
Wiki:
300+ articles
Pluses
  • This open source product is being used by many users. Hence well tested and best practises incorporated. Also there is a large active community support.
  • Most importantly by embracing an open source product like liferay, it would keep the organisation up to date with latest technologies. It is just a matter of upgrading to never version.
  • Also most common problems like single sign on, RSS, indexing/searching, etc are already solved by applying the best practises. With such a readily available integrated technology stack, migrating to newer technologies becomes much more easier.
The worries:
  • As it is bundled with lots of technologies, it becomes heavy weight. But with liferay's a modular architecture, this can be customized and hence minimized. 
  • Like any new technology, a steeper learning curve is involved. Once a transition happens it should be worth the investment of time and money.
  • Customizing liferay to the organizational needs would take time as we need to match the companies infrastructure to liferay's architecture.Eg: The database schema is designed for liferay. How do we customize it? This is where you got to burn your midnight oil...
  • Liferay has virtual hosting support. But to integrate with the existing infrastructure, it would take enough time and research.
  • "Its a black box with lot of complex things going in it. It may go out of control as we dont know what is happening inside.. " As it is opensource it is transparent and you are free to fix it for yourself.
Some good links:
Videos : 
http://www.liferay.com/web/guest/community/documentation/5_1
An issue tracking system : issues.liferay.com
 

Saturday, February 28, 2009

Jooming with Joomla

The amount of work needed to get a site up and running especially the front end XHTML design part i A LOT.
I came across one excellent CMS solution which can very well be used to develop plain vanilla websites.
The results are really amazing. I feel it is much better than Drupal in terms of ease of use and its rich admin features..
It has millions of plugins and themes to choose from. Once you get the knack of placing the plugins and
using themes into your site, virtually you can achieve amazing results with very less coding effort. Here are a few notes for Joomla 1.5: Positioning in CSS is the key for each module. You need to understand the positioning after reading the CSS. The module name is the key in css that needs to be mentioned in the new module's postion attribute. Eg: left, top, right, user3, use4 , etc To add a search box in home page Extensions --> Module Manager -->new--> search To add a menu Extensions -->Module manager --> new --> menu. Be careful to give the position of the menu as the one in HTML theme...eg: News in home page Define a section news, then a category for news. Add article for this category. Extensions--> Module manager --> new --> news flash Choose the category which holds articles for news..thats it. Contact us Menus --> new --> Internal link--contacts Gallery Download a plugin extension like MorfeoShow. Add this module and configure.. Watch out for this space. more to come....joom jooom joooooomla

Wednesday, February 25, 2009

Spring MVC

Spring has many controllers.
SimplefFormController is an interesting one to work with.
I try to disect this in this blog posting.

TODO: More to come here:

A good source for understanding is available at
http://raibledesigns.com/wiki/Wiki.jsp?page=SpringControllers

http://www.springify.com/



Wednesday, February 4, 2009

Lucene

Lucene is an excellent text search engine. Solr a sub project of lucene provides web based interface handling XML requests and executing XML commands . "Solr is more of a general-purpose search server, and it assumes you already have structured data (like catalog data, music collections,etc)." Nutch again a sub project of lucene is an excellent web crawler. "Nutch is more like an open-source google... it's for crawling, converting, indexing, and searching websites." Assuming you have an existing J2ee application with struts and hibernate. The following major components/classes would be required if we consider to use lucene or solr: 1. Search Index writer: This will use existing hibernate methods/APIs to read the records (which need to be searched) and create lucene indeces. The index can be stored on the disk or in a database via JDBC. Lucene calls these index records as documents and will have relevant information to display in search result.
2. Search index reader: This will read the index stored(on disk or database by step 1.) and return the search result. There is no hibernate method calls involved as the lucene index is separate from database. To display the search result, the struts action classes would need to be altered/added. However on click of the search result for details of the record, the existing struts/hibernate(if it is available) functionality will be used to display the details of a record.
Basically you can think of it as a search engine implementation(like google) where you index the records(like websites) and search results will contain only the basic information. The detailed information is delivered on click of a link on search result.

Coming to Solr, this is basically a web service wrapper on top of lucene. Under the hood it also builds lucene index. The advantage of Solr being it is web service based so it is easy to sync index in a distributed environment. Also it provides caching, index syncing etc out of the box.

Gotchas and Tips: Sorting: Need to maintain a duplicate field which is NOT_ANALYZED. **** sorting is case sensitive ***** sorting field value can be having a fixed max length say 20 ... this improves performance. Indexing: Analyzer used for indexing and searching should be the same. StandardAnalyzer can be extended to have HTMLStripReader and ISOLatin1AccentFilter. Links: Syntax supported: http://lucene.apache.org/java/1_4_3/queryparsersyntax.html Solr + Jquery sample: http://solrjs.solrstuff.org/test/reuters/ http://www.theserverside.com/news/thread.tss?thread_id=43617 http://www.xml.com/pub/a/2006/08/09/solr-indexing-xml-with-lucene-andrest.html http://www.ibm.com/developerworks/java/library/j-solr-update/?S_TACT=105AGX01&S_CMP=HP

Friday, December 5, 2008

Spring method interceptors

An excellent feature of spring AOP
http://www.javalobby.org/java/forums/t44746.html

Excellent podcast spanning from spring framework, AJAX to cloud and grid computing. Must see
http://skillsmatter.com/podcast/java-jee

Saturday, October 25, 2008

Fedora 9 on lenovo 3000 N200

My lenovo 3000 N200 was sick last week.
I have a dual boot win xp and fedora 9 setup. It worked for a few months..Then the agony...
The laptop would shutdown suddenly, or while booting and sometimes it even would wake up by itself although it was shutdown (like a zombie). Scary !!.

After some initial struggle and making dozens of calls to Lenovo support centre, a techie told me that N200 has a problem with BIOS and I had to reset it to defaults.

Well I did it. Guess what - my windows xp BSOD started showing up on startup.
Fedora 9 also was crashing now.

Luckily I had fedora 9 setup cd and after a re-install things were normal with fedora. However the windows BSOD persists.
Tried to get hold of a windows XP cd, but unfortunately though I am able to but up with win xp, it simply says setup is now checking.... and simply hangs with Black screen...Seems like it's not able to detect HDD

Anyway I got fedora 9 with internet working fine.
However I had a problem with sound. As per a forum i setup sound and it worked like a charm. The fix was:
In /etc/modprobe.d/alsa-base add a line
options snd-hda-intel model=lenovo.
Restart, voila sound's good.

Next with fedora, i was not able to play mp3,
After some searching around, I got a cool tool for fedora 9, it is named easylife - an opensource project. This installs all the required software on fedora at one go. Like skype, adobe, flash, etc. Everything is taken care under the hood.
next flash on fedora was not working..
Had to checkout if it was 64 bit installation using $uname -a
Yes it was for me and hence followed below instructions:

Installation on Fedora 64-bit

The following steps are required for Fedora 64-bit users.

First install the Adobe YUM repository, as stated above:

[mirandam@charon Download]$ sudo mkdir -p /usr/lib/mozilla/plugins
[mirandam@charon Download]$ sudo yum install nspluginwrapper.{i386,x86_64} pulseaudio-libs.i386 libflashsupport.i386
[mirandam@charon Download]$ sudo yum install flash-plugin

[mirandam@charon Download]$ sudo mozilla-plugin-config -i -g -v

This was adapted from the Fedora 9 Release Notes.

NOTE - PulseAudio - I was able to hear sound properly in both Gnome and KDE in Firefox when PulseAudio was enabled.

I'm yet to try this link to revive win xp.. http://forums.lenovo.com/lnv/board/message?board.id=N_Series_Lenovo_3000&thread.id=269

Friday, October 10, 2008

Jquery

It is :
* simple (include a javascript into ur existing page and you start playing with Jquery),
* light weight(core is just 20KB), pluggable/ extensible(thousands of plugins) and
* easy to use (implicit event handling, expression evaluation and function chaining to name a few).

Client-side
Also a jquery-form plugin( http://malsup.com/jquery/form/ ) makes HTTP form post much more easier.
This takes care of marshalling the HTML input fields automatically , make a form post and deal with the HTTP response.

Server-side
There is some work done already on (spring MVC + jquery) http://www.infoq.com/articles/First-Cup-Web-2.0-Joel-Confino
Tried a prototype using this and jquery-form. Actually it works well with Xstream and Jettison mapped driver .
Also converting the existing spring MVC controller to have AJAX capability is just a matter of setting a custom Json View instead of existing Velocity
view.

Data can be transferred from browser to server in either of the format:
JSON(Java script object notation) or XML.
JSON is simple as it gives you a java like object graph and is more popular in open source community.
However it lacks XML flexibility (like having attributes,etc)

Obviously, having an IDE for jQuery would make development easier. Searched around for an IDE with JS code completion and debugging capabilities.
There are few of them to evaluate - Aptana and Spket IDE.

JQuery... Ahh kids play, is it really? find out yourself http://www.youtube.com/watch?v=8mwKq7_JlS8
How it works : http://docs.jquery.com/How_jQuery_Works
Best practises
http://www.smashingmagazine.com/2008/09/16/jquery-examples-and-best-practices/
Examples http://www.noupe.com/jquery/50-amazing-jquery-examples-part1.html

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 .....

Friday, August 29, 2008

Java Classpath issues

The most difficult problem to solve from a distance... But if we look closely its a piece of cake.
Many developers spend so much time in fixing this issue. Lot of frustration and anger. What they dont spend time on is finding how the classpath and class loading is done in java. Once you know it, you are done.

I herewith try to log a list of java classpath issues:

1. Junit cannot load classpath resource:
The ideal solution as I know is to put the resource along with the junit class(unless it is not reused across). Then load the resource using MyClassTest.class.getResourceAsStream("articles.xml");

This is a simple and elegant solution rather than hardcoding or putting a relative path , which are bound to fail in different environments.

Sunday, August 10, 2008

AJAX - What javascript can do..

AJAX is asynchronous javascript and XML. AJAX in it raw form is difficult to build rich applications. Many frameworks are developed hence. As javascript is object oriented, building a framework on it is easy. Something like a JDK with packages can be built and maintained easily.. Though AJAX was for XML, HTTP responses and JSON(kind of protocol) are supported. The following are in the market:
  1. GWT -by google. Java to javascript compiler makes it easy for java developer.
  2. Zk -very powerful and simple.
  3. DWR -simple
  4. Dojo - Excellent.
  5. Yahoo kit - dont know
  6. Ext JS
  7. Jquery
  8. More.....
Setting up the environment for these AJAX development is always a pain. Will write more in future on this. Links: Excellent gwt components here : http://www.gwt-ext.com/demo Jquery slider for select boxes : http://www.filamentgroup.com/lab/progressive_enhancement_convert_select_box_to_accessible_jquery_ui_slider/
This guy is 12 yr old.. and he knows so much of  jquery :
http://uk.youtube.com/watch?v=8mwKq7_JlS8

jquery with jsp
http://www.infoq.com/articles/First-Cup-Web-2.0-Joel-Confino

Automatic form post jquery plugin:

http://malsup.com/jquery/form/

Yes it is infact simple (include a javascript into ur existing page and
you start playing with Jquery), light weight(core is just 20KB),
pluggable/ extensible(thousands of plugins) and easy to use (implicit
event handling, expression evaluation and function chaining).





Also its jquery-form plugin( http://malsup.com/jquery/form/ )  makes
things much more easier. This takes care of marshalling the HTML input
fields automatically at the browser end.



There is some work done already on (spring MVC  + jquery) http://www.infoq.com/articles/First-Cup-Web-2.0-Joel-Confino

Tried a prototype using this and actually it works well with Xstream and Jettison mapped driver .

Also converting the existing spring MVC controller to have AJAX
capability is just a matter of setting a custom Json View instead of
Velocity view.



Data can be transferred from browser to server in either of the format:

JSON(Java script object notation) or XML.

   JSON is simple as it gives you a java like object graph and is more popular in open source community.      

   However it lacks XML flexibility (like having attributes,etc)



Obviously, having an IDE for jQuery would make development easier
especially with code completion  and debugging capabilities. There are
few of them to evaluate - Aptana and Spket IDE.

Jquery Editor:
http://spket.com/jquery.html

Tuesday, August 5, 2008

Oracle tips

A sneeky tool in oracle SQL developer to help u visually generate sql query is here


Printing a tree hierarchy using SQL connect by:

select
lpad('-', (level)*2,' ' ) || HIERARCHY_cat_short_name as padded_name, HIERARCHY_cat_id, HIERARCHY_cat_parent_id, level
from HIERARCHY_category
connect by prior HIERARCHY_cat_id =HIERARCHY_cat_parent_id
start with
HIERARCHY_cat_id in (select HIERARCHY_cat_id from HIERARCHY_category where
HIERARCHY_cat_parent_id is null)

quick reference here :http://philip.greenspun.com/sql/trees.html

Oracle CONTAINS function for XML based search
select count(*) from ATABLE where contains(xml_COLUMN, 'HASPATH(//filed="12568") ') >0

Extractvalue from xml column
SELECT distinct EXTRACTVALUE( xmltype(column_meta_xml), '//field',    'xmlns:ora="http://xmlns.oracle.com/xdb"') from atable





Friday, August 1, 2008

Textpad tricks

Textpad tricks

Find and replace 

Input : 66 Urology & Nephrology 737 Spermatogenesis
Replace : the bold text in the above input

Find Pattern : ^\([0-9]+\)\(.*[a-z \t]+\)\([0-9]+\)
Replace pattern: \1 \3

output: 66 737 Spermatogenesis


Gotcha's

Always escape group by characters ( or ) with \ [backslash] like this \( \)

^ is the beginning of the line

$ is the end of the line

\n is new line.