Java N Coffee

Monday, June 01, 2009

The fickle properties file.

This is one area where serious thinking needs to be done. I have seen lot of people cringe over this but yet they keep going on. Biggest problem I find in a property file is APP VERSION and APP CONSTANTS.

Your mate comes gives you a version and you start out code, after few months you have a small functionality to develop and you have to use the same constants. Then few months down the line you have another HOT NEWS, part of the code you have to add into some other part of code and that new code too has got a VERSION. Only thing you can do is bang your head to a wall.

Another annoying thing people do, property.getProperty(AppConstants.SOME_VALUE) lying all around the code. Then they complain if a constant changes due to TYPO or wrong constant name selection, compile the whole thing and build again as compiler puts the String constants all over the place.

Peace!!!!!!!!

To limit the causality of my brain cells, I got close to , make a single (may be singleton, yes yes I said singleton dont frown on me) class file with getters to all properties right there with string constants encapsulated well in that class itself. So if i need a property from properties file, I'll rather say

AppProperties.instance.getRepositoryLocation()

with all the property file stuff encapsulated in that AppProperties class.

One thing I cringe for is AppVersion, all the time we talk about reuse, seniors spend most of time dealing with it in the end what we have is AppVersion, at the max we could have ComponentVersion.

Labels: ,

Annotation Vs. Xml

People talk about putting meta information about a POJO through annotations, but it makes me wonder, how closely related my meta information should be?

Taking dip into Servlet 3.0 specification, annotations here seems to add value to Servlet classes because as responsibility of a servlet class is to process request and send response, it is also close to server end. Many things I'll write in a servlet will be close to container. Even if I write it in annotations or external XML files, I am not concerned about polluting my class with annotation that I'll have to carry everywhere I move my classes.

Now, in hibernate or JPA for instance, my POJO gets polluted with all the meta information that well suits in an external XML file. It keeps my POJO's clean from annotations. If I have to use a portion of the designed model somewhere else, I not bothered what goes in annotatons, its clean and meta information is externalized.

Percenatage of me reusing a servlet code to a domain object is more and makes life easier to keep POJO devoid of all meta information that can cause problem in future.

This is my view, it keeps things cleaner for me to work with and the usuall rant, makes unit testing easy without carrying framewrok burden.

To conclude there should be balance between how much annotation to use when and where.

Labels: , ,

Wednesday, May 06, 2009

old evil new

Well its been long I have written in the Java space. Lately I realized, I had an old foe disguised as a true friend lurking by my back, making mockery of my work :X. This friend of mine is "new", surprised, hmmm well how?

Every time I used his service, I had to pay the price. Let me show you.

public interface Sexy{
String wink();
}

public class Handsome implements Sexy {

// properties

String wink(){
BeautyFinder(this).getBeauty().sendWink();
}
}

Somewhere in the middle of heat.... working hard

class DateMaker{

Sexy sexy;

public void setSexy(Sexy sexy){
this.sexy = sexy;
}

public Sexy getSexy(){
return this.sexy;
}

public boolean fixDate(){
//complex logic for finding a beauty ;)
}

}

// the MAIN or core business process logic

public static void main(String[] args){
Sexy me = new Handsome();
DateMaker dateLord = new DateMaker();
dateLord.setSexy(me);
if(dateLord.fixDate()){
me.wink();
}
}


The problem might not be evident, but as the job of DateMaker increases coz of popularity ;), need arises to handle different types of sexy beings..living .. non living any thing..The creation of relevant object becomes akin to feeding data for information.

How long can we rely on new XXX(); all over code base, and the famous cliche pain in ass "change is the only constant" sooner or latter it will become hell. A part of it can be solved by using patterns like facotry and abstract factory pattern, but not long so.

If we can move out this creation burden from our code base and delegate it to some other source. We can achieve much better flexibility and change requests.

Needless to say for Java world there is Spring and another cool framewrok Guice for the dependency injection.

Labels: , ,

Thursday, July 03, 2008

Heap error with jira attachement web service call

I blogged recently about custom error logger, in which I had to do some extra exception handling after logger logs it. One task was to attach log file to jira issue being created. Problem was the client I was using used axis 1.4 which dint support soap with attachments well and was giving heap space error. When googled about it I found that it was not a problem with jira service but the client being created.

As it was not feasible to go to axis2 which does support soap with attachments well, I ended up using apache's httpclient for creating post request to attach the file.

Below is the link which helped me
http://jira.atlassian.com/browse/JRA-11693

Hope it helps someone .... :)





Blogged with the Flock Browser

Labels: , ,

Monday, June 23, 2008

Batch/Shell Scripts

I wanted to execute a shell script which had input values with space eg: "Test Client".I was baffled for sometime by using wrong method, the method with which I succeeded was


Runtime.getRuntime().exec(String cmdarray[])

providing shell script and the input parameters in cmdarray[]

Labels: , , ,

Custom Error Logging

Recently I was given job of making error manager, which is like putting your feet on the axe. Whenever an error is thrown it should be logged in jira, wow perhaps now you would have realized why the axe stuff. No escapes, had to do and it helps though at times ;-).

Now issue being how to go about it, oops into rescue as usual. I extended one of the log4j's class and added customization in the "public void append(LoggingEvent event)"
which helped the cause.

In a gist,

public class CustomLog4j extends DailyRollingFileAppender {

private static final String COMMA = ",";
private static final String ERROR = "ERROR";
private String level;

@Override
public void append(LoggingEvent event) {

super.append(event);
String[] stackTrace = null;
this.level = event.getLevel().toString();
if (level.equals(ERROR)) {
if (event != null) {
LocationInfo locationInfo
= event.getLocationInformation();
if (shouldContinueForThisClass(
locationInfo.getClassName())){
/*Custom code*/
}
}
}
}


public void setLevel(String level) {
this.level = level;
}

public String getLevel() {
return level;
}

public boolean shouldContinueForThisClass(String clazz) {
String[] clazzez = Props.getErrorProperties().getProperty(
ErrorConstants.CLASS_FILTER).split(COMMA);
for (String filter : clazzez) {
if (clazz.indexOf(filter) != -1) {
return false;
}
}
return true;
}

}

shouldContinueForThisClass method is there to avoid some classes error being registered,I have a properties file where I define class names/package keywords which I fetch by

String[] clazzez = Props.getErrorProperties().getProperty(
ErrorConstants.CLASS_FILTER).split(COMMA);

this is also to avoid Error Manager going in loops, in case my CustomLog4j crashes :(

So this way I achieved the task, the moment any error happens, it gets logged in jira and mail comes to the person assigned, most of the time its me :(

Labels: , ,

Tuesday, May 08, 2007

Spring


Recently started with Spring framework.
Initially found it difficult to find some good tutorial on it but found few which I am refering right now.



Hope this helps someone starting of with Spring.In case you have few links,tell me too :).

Of these articles,article by Martin Fowler is a good read before you start of with Spring (My Recommendation ;)).

Labels: , , ,

Active Record Java - Base

I have completed a basic implementation of the idea Activerecord Java.
It is hosted at Active Record Java@ Sourceforge.

It is in its basic stage so there are not many features.
Its in a nascent stage, I have given a release just for demonstration purpose.
People interested check it out and let me know your views what you all think of it.

Labels: , ,

Friday, April 27, 2007

J2ee Design Patterns

Recently started to link my project on active record java in a j2ee environment.Did few examples, till now success.While doing this I stumbled upon few good links.

http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
and
http://java.sun.com/blueprints/patterns/catalog.html

Enjoy in case these links are new to you. :)

Labels: ,

Saturday, February 03, 2007

Active Record Java - II

Did few more things
Going at slow pace :) right now

Well I have added few methods.....

find_all("parent","condition:class= ? and name = ?,limit:0..10","3","cool dude")
this method will give me ArrayList of Objects of Class Parent which maps to parent table and where class is 3 ,name is "cool dude" and will limit the resultset to top 10.

a variant of the above

find_all("parent","limit:0..10,condition:class= ?,order:? desc","1","name")
here i have brought limit ahead of condition and also we can have order by.


find_all("parent")
this method will give me list of all the rows in table parent.


Though working on creates I have accomplished so far.......

Parent p = new Parent();
p.set("id",1);
p.set("name", "John");
p.set("address", "Pune");
p.set("class", 3);
p.save();

will create a new row with above details.

these are the few things I have accomplished lately.


Labels: , ,