Friday, July 31, 2009

Factory Pattern, Visitor pattern, Aspect oriented programming and spring framework

Today i read wikipedia articles about visitor pattern and factory pattern...

Factory pattern is useful when u don't know the type of object that you are invoking..
You separate the logic of identifying the object type and invoking the desired method on that object type in factory class.

Basically term factory method means any method that creates objects. Whenever u wish to invoke the object, u create the factory class object and pass the information to this class which figures out and invoke appropriate object.

Visitor pattern:
Suppose there are four components in the system and all of them have common method like print or run or act... so instead of doing a implementation of common method in each of the subclass, a new interface is defined which declare common method for each component. A new class which extend to this interface implements the component specific functionality in class code. This way we separate the code of common method from sub classes.

Aspect oriented programming:
The piece of code on wiki gives a wonderful insight into this. Suppose u have bank transaction code. Here a method transfer money from one account to another. For sake of security and system services like logging and database transaction, a lot of checks will be introduced in the code. This is bad because in future if logging functionality changes or security changes then transaction code will have to be changed. This leads to cross cutting concerns. cross cutting means the logging module is cutting across other module so it creates problems. To avoid this aspectJ should be used. In this all the cross cutting code is specified in a separate file as aspect. This file defined the join point and point cuts which helps in separating code.

Spring framework:
Powerful framework with lot of features. Yet to understand it's role in j2ee applications fully.

SJ

Reading and writing files in java

I always forget the exact syntax of lines in java for reading and writing files...

Reading files:
Convert the file to inputstream or outstream and then read or write to the file using streamReader or streamWriter...

Create the fileReader from the file and read that fileReader using bufferedReader

Use scanner to read file by line by line...

Thursday, July 30, 2009

Communication between JSP and Servlets

getServletConfig().getServletContext().getRequestDispatcher(”jspfilepathtoforward”).forward(request, response)

Deep copy vs Shallow copy

If the object is having any pointer then deep copy is preferred because in case of shallow copy, if pointer is changed or object is destroyed then dangling pointer problem may arise.

Wonderful post on pass by reference and pass by value confusion of Java
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Values of reference is passed by address and reference itself is passed by value.

Abstraction vs Interfaces

Abstraction: Default implementation of set of methods.
Compulsive use of extends variable and overriding of default functionality
Better to be used in application framework where a default implementation is required in case component does not implement their own. Like event handling service, Messaging service etc..
If variables changes often then use abstraction

Interfaces: Abstract methods and final variable
Provide conceptual structure to whole application.
Allow Multiple inheritance
If methods are changed frequently then use interfaces.

For example: Interfaces/ Need for multiple inheritance

Two kind of people: actor / director ..
for each of them we define one interface but now what if there is third kind for people who are both actors and directors....

In such scenario we need multiple inheritance...

Need fr abstraction:

Abstraction in big application framework to define default handling.


Inheritance vs. Abstraction
When functionality is to be imposed on unrelated classes then use interfaces...
When functionality is to be used on related classes then use abstraction..

Related classes: GenericList singlelinklist doublelinklist... all have the similar properties and methods...

Unrelated Classes: some function in interface which should be implemented by many component in the system.

Sunday, July 19, 2009

Tricky to see which process is using which port

http://www.mydigitallife.info/2008/12/03/how-to-check-and-identify-which-application-is-listening-or-opening-port-80-and-443-on-windows/

First run netstat -o -n -a
then find the process id and use this process id to find out process name and details...

Friday, July 17, 2009

To setup netbeans on Ubuntu

http://www.javadesign.info/SystemsHardware/OS/Ubuntu/install-netbeans-on-ubuntu

This page describes things wonderfully...
Hope this helps...

issues with my Eclipse and Jaxb setup

I was struggling a lot with my eclipse and jaxb setup.
Ultimately it turned out to be eclipse java settings on Ubuntu.

I was supposed to use sun java-6.0 but for some reason my eclipse configuration was picking up gcj-java.

In the process, i used following tricks...

Change eclipse java preferences in
/etc/eclipse/java_home

Change the by default installed JRE setting in eclipse>window>preferences...

Checked the installed vm of eclipse by looking into eclipse>help>about eclipse platform>configuration details

Changed the default JRE that needs to be included in the project while creating the project...

sudo update-alternatives --config java

and somehow it worked for me...

Saurabh

Monday, July 13, 2009

JSP notes

http://www.jsptut.com/Declarations.jsp

Understand the importance of
<%= %>
To embed single line expression in the code

<% %>
To write a code block

<%! %>
Declaration to enclose your declarations

<%@ %>----> Importing some java library
for example: <%@ page language="java" import="java.sql.*" %>

Variables should not be declared as global otherwise there would be synchronization issues and performance will be hurt. Ideally all the variables should go into request or session object.

Thursday, July 9, 2009

Jetty deployment

For deploying on Jetty there are several ways...
1. Download the binary zipped file from source forge...
2. Extract the file and start it using general instruction of
java -jar start.jar etc/jetty.xml &
3. Create the war file.
inside war file..
4. Add a context file in contexts folder for hot deployment
For static deployment--> check the jetty.xml page

This link should help...
http://docs.codehaus.org/display/JETTY/ContextDeployer

This is the directory structure while deploying servlet on any servlet container

base directory/ (this is the name of your war file)
JSP files
image files
HTML files
CSS files
any subdirectories containing additional JSP, HTML, images, and CSS
WEB-INF/
struts-config.xml
web.xml
TLD files for various tag libraries
lib/ (various .jar files for libraries you used)
classes/ (compiled .class files for your actions and action forms)
META-INF/ (this is part of the .war file)
MANIFEST.MF (the manifest file from the .war file)

Underneath the classes directory you would have compiled code for forms and actions. For example:

com/
johnmunsch/
strutstest/
actions/
LoginAction.class
SearchAction.class
forms/
LoginForm.class
SearchForm.class

This is a wonderful page which tells about the services described Tomcat file structure and installation stuff

http://linux-sxs.org/internet_serving/c292.html

Wonderful.. Wonderful

Jetty server installation and running details...

This is the Jetty server page...
Just download the jetty source from Jetty download page...
Read the instructions in README.txt for details...

Jetty is faster than tomcat or resin and easy to configure as well...

This page describe the things better...
http://helpme.morphexchange.com/jetty6/help/items/chapter_3_navigating_through_jetty6

Saurabh

Wednesday, July 8, 2009

Application server vs web app server

http://www.javaworld.com/javaqa/2002-08/01-qa-0823-appvswebserver.html?page=2

This page describes the best scenarios in which application server and web app server are most suitable.

Thanks
Saurabh

Tomcat and Jetty

Now days i am trying to get jetty-spring-hibernate framework running...
Here are the things that i find interesting....

Apache:
How to run apache on a different port?

1. By default Apache runs on port 80.
2. To change the port or add additional listening ports...

nano /etc/apache2/ports.conf

-- change
listen 80
-- to
listen 80
listen 8080

sudo /etc/init.d/apache2 restart
This will make apache listen on 80 and 8080. You can make it listen any other port that you want.

Tomcat:
How to change the port number on which tomcat runs?

Tomcat is a HTTP server and generally HTTP servers are related to port 80 so ideally tomcat should run on port 80 but default port for tomcat is 8080.
The reason for this is that Tomcat is good for handling Java and Jsp / Servlets code but it is not efficient for static page serving and other server side languages.

In some cases overhead of running tomcat on top of apache is poor so we can change default port of tomcat to 80.

To change the port.. We need to server.xml file
Also other way of port handling is to use kernel iptables to forward any request coming on port 80 to forward to port 8080.