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: ,

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: , , ,

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: , ,

Friday, January 12, 2007

Active Record Java

Impressed by the ActiveRecord concept in Rails/Martin Fowler, I tried to develop something of the same kind in Java called "Active Record Java".

The things that I liked are:->
  1. No XML requirement for mapping of classes and tables.
  2. Dynamic nature for agile development.
  3. Shell prompt to evaluate pieces of code fast (irb).
  4. Class file clean for business logic (validation code though present I find it clean).
Till now what I have accomplished is R of CRUD. I used Innodb type tables In MySql not done in any other DB.

You have to name your classfile same as your table name.
eg: Parent => parent table.

You have to give just the database name and you are ready.You don't need to define variables like "private String name" , during startup metadata is read from the database and also the relationships between tables of type 1..*.With metadata I keep the track of all the variables and their datatype.
eg:

parent table:->
id | int(11) | PRI
name | varchar(15)
address | varchar(100)

The class Parent will have

public class Parent extends ActiveRecordJava{

public Parent(){}

}


the methods that I have been able to implement are

static find

Parent parent = (Parent)find("parent","1");

this will take string "parent" the table name and the next id i.e 1
and return instance of Parent class.

to access fields you have to make a call like......

parent.get("name");
parent.get("address");

There is 1..* relation between parent and child table.
This you dont need to define anywhere in class file.
Both Parent and Child class file are clean.

Child child = (Child)find("child", "1");
Parent p=(Parent)child.belongsTo("parent");

to get the other side i.e all children of parent.
p.listOfAll("child") will return arraylist of all values.

Only thing that you have to write is for *..* as it doesn't makes sense for detecting this kind of relation from metadata.You will have to initialize the relation in constructor.

public Products() {
markMany("customers","through:customers_products");
}

to get arraylist of customers

Products products = (Products)find("products","1");
products.hasMany("customers");

usage:->
ArrayList<Customers> cst = products.hasMany("customers");
Customers c1 = cst.get(0);


till now I have implemented these things and trying for many other things hope so i'll finish CRUD fast.

Labels: , ,