Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, January 01, 2009

MyEclipse HQL

Though it has been few versions since MyEclipse had support for Hibernate and HQL, I only had a chance to play with it recently. It has a nice feature set and the HQL editor is tied Dynamic Query Translator which gives on-the-fly translation for HQL queries. This provides for an easy way to debug queries and validate them.

twitterclone

In order to open a HQL editor you need to create a file with .hql extension and also have an active hibernate configuration file. This took me a while to figure out. In order create an active Hibernate Configuration use File -> New -> Hibernate Configuration File and setup the connection parameters for the database.

Saturday, September 08, 2007

Auto-translate Resource Bundles using Google Translate

Internationalization is one of the common requirements in web applications. One of the challenges faced by a team is the non-availability of translated resource bundles during development (mostly due to logistical reasons). But it is important to test various scenarios during the application development.

In this post I will present a simple idea which utilizes Google Translate to auto-generate translated Java Resource Bundles for the language of choice. Just send a HTTP GET request with the text to be translated to Google Translate URL and read the translated text by parsing the response. In order to translate an entire resource bundle just pass a source bundle (For example, English) and read each property and write to target language resource bundle file.

Here is the URL for translating "welcome" from English to Spanish http://www.google.com/translate_t?langpair=en|es&text=welcome

Here is a snippet of Java code which utilizes Apache Commons HttpClient library :

String url = "http://www.google.com/translate_t? langpair=en|es&text=";
String text = "welcome";

HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url + text);

client.executeMethod(getMethod);
String xml = getMethod.getResponseBodyAsString();
xml = xml.substring(xml.lastIndexOf("<div id=result_box dir=ltr>"));
String translated_text = xml.substring(27, xml.indexOf("</div>"));
System.out.println(translated_text);

Technorati tags: , , ,

Sunday, August 12, 2007

Caching: Memcached and Terracotta

Applications are generally built with an expected user base but soon might be overwhelmed due to business demand. This is especially particularly true in the context of consumer facing applications. Caching is one of the most important aspect to improve application performance by storing object in Cache (memory) reducing database load.

Caching in a clustered environment requires a Distributed Caching solution which can support failover scenarios and data reliability. In this post I would like to explore the capabilities of Memcached and Terracotta as distributed caching solutions.

Memcached is a high-performance distributed object caching system with client APIs for Perl, PHP, Python, Ruby and Java. Here are some of its capabilities and limitations (using Java client API):

  • Requires objects to be Serializable
  • Object Identity is NOT preserved
  • Supports cache expiration
  • Does NOT handle failover scenarios
  • For a given object selects a server from a pool of cache server based on hash of the key
  • Easy to configure (through SockIOPool class)

Terracotta is an open-source Java based clustering solution for JVM. Distributed Caching can be achieved using Terracotta by using a java.util.HashMap or open-source caching solutions like EHCache, OSCache and JBoss TreeCache.

  • Preserves Object identity
  • Manages memory efficiently through Virtual Heap
  • Declarative requirement for lock support
  • Simple configuration file with Eclipse Tool support
  • Good documentation, support and active development
  • Due to the nature of its implementation certain classes are not Portable and hence cannot be used
  • Hard to determine which third-party classes are portable
  • Does NOT require classes to be serializable
  • Easy to configure and get started!
Technorati tags: ,

Thursday, August 09, 2007

Data Services in Spring

In today's web applications the need for returning model data from controllers in XML/JSON format is quite common. Mostly these requirements are met by writing custom code in controllers. In this post I will present technical approach that would enable your applications to return data model in XML/JSON or data format of your choice to the web tier using Spring MVC without modifying your applications.

Basically the idea is to return XML(or JSON, etc) whenever we encounter a request to the controller with parameter returnData=XML. This can be accomplished by writing a HandlerInterceptorAdapter with postHandle method to do the following:

void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
{

if ( request.getParamter("returnData") != null && request.getParamter("returnData")="XML" )
{

// add a XML Serializer to the model
// Find the model object names and add the list to model attribute "xmlModules"
// Set the view name to "xmlView"

}

}


In your xmlView (JSP or Velocity Template) for each attribute in xmlModules use the XML Serializer to generate XML and render it. There are few applications for this approach:

1. Enable front-end to readily use AJAX for existing controllers
2. Use this as a strategy to get a debug view of model data during development
3. Based on some additional key use this to get debug view from production for troubleshooting purpose

Resources:

1. XStream for XML Serialization

Tags: java spring ajax

Friday, February 24, 2006

Web Services- Using Axis/Spring/Castor

I was working on application architecture for building a document style web service this past week. I finally built the same using Axis, Spring and Castor frameworks. Spring also has their own web services framework(Spring-WS) but they dont quite have a stable release.

Basically the idea is to leverage the best of these two frameworks. As it is obvious Spring is used for hosting managed beans business logic and Axis for hosting the service. I leveraged Axis flexibilty to use custom Serializers and Deserializers and used Castor for OXM (Object-XML Mapping), so far it seems to work. As always, I would welcome any comments/suggestions/critic.

Disqus for techtalk