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

Friday, 11 September 2009

Accessing static constants in EL

Ever wanted to compare a value to a constant using Expression language in your UI ? Usually I would use the string representation hard-coded in the JSP which , if you have 1000 references in your UI code and you want to change the static constant value, you have a lot of work to do.

So you could

a) add accessors to your class for your contsants, which will work but is not very elegant when you have methods such as EG getAPPROVAL_STATUS_PENDING .

b) have a utility class which does the same as in a) and/or put all your constants in a map and import this class to the EL context.

I was not really happy with any of thse methods as they seemed clunky and unelegant. So I  did what all people do in this situation and used Google.

I came up with a Tag library from Jakarta called "unstandard"  which provided the functionality I needed. However it appears to be deprecated and only available as source code in one of their more obscure repository locations.
http://svn.apache.org/repos/asf/jakarta/taglibs/sandbox/unstandard/trunk

Since we have our own custom tag library on the project and I didnt really need all the functionality I took the description for the tag "useConstant" from the tld and integrated it with our Tag library descriptor  and tooks the classes
- org.apache.taglibs.unstandard.TagUtils
- org.apache.taglibs.unstandard.ClassUtils
- org.apache.taglibs.unstandard.UseConstantsTag

and put them in our tag code library.

Now I can access constants in UI expression language context in the following way. On the jsp page I wish to use the constants I add the declaration:

<mytaglibrary_prefix:useConstants classname="com.mypackagename.MyClass" var="MyClass" />



Then I can access my classes static constants using standard exrpession language syntax:
${MyClass.A_STATIC_FINAL_CONSTANT_VALUE}


I quite like this as its nice and clean, negates the need for string literals in the code corresponding to constant values , and will work seamlessly if constant values change.

A thank you to the Jakarta taglib team, my question is why is this not in the standard library and why do these utils appear deprecated?

Wednesday, 5 November 2008

To shard or not to shard

I remember back in the day it was simple, HTML --> Server Side Scripting --> DB. Thats it. It was easy.

It seems that as with the gaming industry driving the development of hardware to handle the increased requirements of the gaming software, data is driving the development of frameworks and designs to solve one issue;

"How do we create reliable architecture to scale with increased storage and retrieval requirements". That to me is the bottom line. To further segment those requirements with proposed solutions

Storage
  1. More Disk Space
  2. More Servers
  3. Sharding
  4. Archiving
Retrieval
  1. In Memory caches
  2. Indexing(DB)
  3. Indexing(Application)
  4. Sharding
There are more but these seem to be the most prevelant. The performance and scalablity gains are paid for in complexity, which in turn excacerbates maintenance issues, but I suppose that is the price of progress.
In our teams latest prototype, we have created a prototype which works extremely well using a java suite of framworkes.
Briefly if uses Spring MCV and IOC,Hibernate Core,Hibernate Search,Memcached and MYSQL.
We have a DAO design pattern with Hibernate implementations, A single Hibernate Search(Lucene Index) as well as a Memcached object cache with an indexed mysql DB being used fro persistent storage.
After hammering the web application using JMETER, I have come to the conclusion that it performs very well.
Now enter Hibernate Sharding and the wheels are coming off!

My first blow came from my extensive use of DetachedCriteria.

My design all stems from a base object with a shardId in all object hierarchies, and a constraint ensuring all objects descended from that base object will be in the same shard. My ShardSelectionStrategy takes care of that based on entity type
So I subclassed DetachedCriteria and using a factory based on entity type I can retrieve the shard I need, which is restrictive but works for me.

I am now busy with the FullTextSession and I am running into big trouble.

Basically EventSource isnt supported by shards and a ShardedSessionImplementor and a SessionImplementor are quite different. I can created shard aware classes for all the main players.
  1. FullTextSession
  2. FullTextQuery
  3. AbstractQuery
In a nutshell, I have got to point, as expected where it returns the correct hits from the index, but since I needed to reference a single shard(The master shard) in order to do this, it will only return the objects on that shard. So my result size may be 3 but my list of objects will only contain the objects on that shard.
Now without rewriting the whole implementation, and going through that pain, how do I solve the issue of implementing a distributed fetch?