Monthly Archives: July 2008

Replacing all occurances of a given pattern using Regex

Using Log4j with Maven Jetty plugin

No extra configuration is needed. If log4j.xml or log4j.properties is present in the classpath, it is picked up and logs are directed to the specified appender(s). I found http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin#MavenJettyPlugin-Logging overtly complicated and unnecessary. The logs can be redirected to a file by using a suitable appender in log4j.xml:

Running Jetty from a custom port through Maven

There are two options:

  • pom.xml:

  • command prompt: mvn -Djetty.port=8090 jetty:run

The thing to remember is the command line value will override the value in pom.xml.

Connecting to MsSql through JDBC

I was not able to connect to MsSql using JDBC. I was using the host with the port no. like:

jdbc:jtds:sqlserver//localhost:3683/TestDB

I would always get “Connection refused” exception.

Then I chucked out the port no. from the host name, and it works fine now.

jdbc:jtds:sqlserver://localhost/TestDB

How to pass an object from the controller to the view in SpringMVC?

The scenario is: I want to show a list of items on my view from the database. My controller picks up the data, passes it to the view which displays it. I am using a sub class of org.springframework.web.servlet.mvc.SimpleFormController:

@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView view = new ModelAndView(“complaintListings”);
List<String> strings = new ArrayList<String>();
strings.add(“AAAAAAAA”);
strings.add(“BBBBBBB”);
strings.add(“CCCCCCCC”);
strings.add(“EEEEEEEE”);
strings.add(“FFFFFFFFF”);

//set the object to view
view.addObject(“testStrings”, strings);

return view;

}

The view looks like this:

<c:forEach var=”string” items=”${testStrings}”>
<c:out value=”${string}”/>
<br />
</c:forEach>

Its that simple!

The cleanest cloning

Many a times we need to clone complex objects to promote re-using preset values while achieving perfect decoupling, as in, two identical objects but having different references to nested depths. The simplest and most elegant solution is serialization and de-serialization. Of course, there is a small catch here, the objects should be serializable. Moreover, there may be a slight performance glitch. This creates a deep-copy of the original object.

I am pasting the code below:

public Object createClone() throws Exception {

// serialize
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject(this);
oos.close();

// de-serialize
ByteArrayInputStream inputStream = new ByteArrayInputStream(
outputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(inputStream);

return ois.readObject();

}

Auto-start PostgreSQL in Linux

This post is outdated. Please see this instead:

http://palashray.com/fedora-getting-up-and-running-with-postgres-in-2-mins/

I had faced lot of problems starting Postgres when my system boots-up. The challenge is that the postmaster daemon should be started by the under-privileged user “postgres” and NOT “root”. This is what you must do:

In the source distribution of postgres, there is a file called linux in contrib/start-scripts/. Open this file and follow the instructions.
I will enumerate them as commands. Please note that you have to run these as root.

cp POSTGRES_SRC_DIST/contrib/start-scripts/linux /etc/rc.d/init.d/postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc0.d/K02postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc1.d/K02postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc2.d/K02postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc3.d/S98postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc4.d/S98postgresql
ln -s /etc/rc.d/init.d/postgresql /etc/rc.d/rc5.d/S98postgresql

You might also want to read these:

  • http://www.postgresql.org/docs/7.4/static/postmaster-start.html
  • http://www.postgresql.org/docs/8.3/static/server-start.html