Tuesday, May 20, 2008

Controlling Module Dependencies in Eclipse

J2EE Module Dependencies is a critical issue in an enterprise application. You need your one web module to access some resources while prohibiting others. Similar is the case with EJB modules.

Let’s figure it out with Eclipse Web Tools Platform. I consider that you all have ability to create projects and modules in Eclipse; so skipping those steps. I have Eclipse 3.3 with WTP 2.0 M6.

I have created project with following details.

MyProject – Enterprise project

MyEJB_Module – Contains bean class

MyEJBClient_Module – Contains interfaces of EJB, VO, and BeanUtil.

MyWeb_Modules – Holds JSPs, servlets and all presentation tier stuffs.

MyJava_Module – Contains common classeslike constants, utilities.

Starting with MyProject, select properties from right click menu. In property dialog, select “J2EE Module Dependencies”. You will find all the modules. Select all as project is dependent on all modules.

Now you can configure dependency of individual module, let’s take an example of web module. In “J2EE Module Dependencies” window, you will find a radio button group which would has by default “Use EJB Client Jars” option selected. That radio button groups defines visibility of EJB jar.

As clients of any EJB should be isolated from bean classes, it is preferable not to select other options where you can define dependencies with EJB jar file. After all, EJB clients should not have any kind of dependencies with EJB jar.

Now here if I select MyEJB_ModuleClient.jar only, MyWeb_Module won’t able to access classes defined in MyJava_Module until you redefine module dependencies.

Similarity you can define dependencies with Utility project, specifically saying third party libraries. You want one module to access one jar and isolate others all you need to do is add library in main project (MyProject in my case) and then configure dependencies of other modules.

This feature takes care of dependencies on behalf of you and let you concentrate on other tasks.

Thursday, April 24, 2008

Java Web application cookie lost!

I got one new change to do in our java web application.I have not been in it since 2 or 4 months and forgot some of its settings.

First of all my jboss refuses to run from eclipse!i was very worried about it, but later i re-configured it as a "dynamic web application" project in eclipse.

So when jboss runs finally, i logged in it and a big blast! it redirected me to error page. I checked the logs but i can't find anything.I was thinking that even i have not chnaged the things in code,configuration etc. then also it stucks here?

But suddenly, a spark happned and i remembered that we set domain for cookies in most web applications.And that thing sucks in local environment.
cookie setting code:
Cookie c1 = new Cookie(name, value);
c1.setDomain(domainName);
So i just commented out c1.setDomain(domainName); and my application is ok.

Friday, April 4, 2008

Java Spring Framework : Multiple PropertyPlaceholderConfigurer configurtion

This is very small thing which can be very annoying while coding in spring framework. Of cource no one can directly understand the problem until he faced that.

At first, I had only one property file from which i am reading some values into spring applicationContext Xml configuration file.So when i needed to add second one for my new feature, i think it is just to add new propertyPlaceholderConfigurer bean with second prop file.

my first prop file 'default-prop.properties' is :
default-prop.custName=testing customer
default-prop.address=building1
and the bean definition in the applicationContext xml file for this is:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:default-prop.properties</value>
</property>
</bean>

so while adding second one, i have just added second bean for it like:
<bean id="propertyConfigurerNew"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:second-prop.properties</value>
</property>
</bean>
and in xml, prop has been read as,
<property name="customerName" value="${default-prop.custName}" />
But when i done this, i got error
"Could not resolve placeholder 'default-prop.custName' "
So i have to do some workaround and digg into spring references. And i found that we can define seperate placeHolderPrefix and suffix for each property configurer bean.

I have changed the bean definition as,
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:default-prop.properties</value>
</property>
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
</bean>

<bean id="propertyConfigurerNew"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:second-prop.properties</value>
</property>
<property name="placeholderPrefix" value="#[" />
<property name="placeholderSuffix" value="]" />
</bean>
it means, for first prop bean, we will use ${default-prop.custName} and for second bean we will use #[second-prop.secondName]

And voila! it solved my problem...

You can also use
<property name="ignoreUnresolvablePlaceholders" value="true" />
for each PropertyPlaceholderConfigurer bean defined. But, i have used the prefix-suffix one solution.
And my problem is resolved.

Give your comments on this problem and solution.