Monday, May 14, 2012

Configuring Custom Session identifier

Lets assume customer has a policy that each web application must have an alternative session identifier than that the default JSESSIONID. In this case they wanted to set it to applicationname_sessionid.

It is possible that they perhaps have in there infrasturucture some other applications to enforce certian security restrictions , such as a requests has to have a specific session identfier.

1. To configure custom session identifier in weblogic, we need to modifiy the application related WEB-INF\weblogic.xml file. We need to add the cookieName parameter and value applicationname_sessionid under tags session-param


CookieName applicationname_sessionid


1. To configure custom session identifier in websphere, we need to add the custom property SessionRewriteIdentifier at web container level.

To specify custom properties for session management, use the following steps:

1.In the administrative console click Servers > Application Servers > server_name > Web Container Settings > Web container.
2.Under Additional Properties select Custom Properties.
3.On the Custom Properties page, click New.
4.On the settings page, enter the property that you want to configure in the Name field (SessionRewriteIdentifier ) and the value that you want to set it to in the Value field (applicationname_sessionid).
5.Click Apply or OK.
6.Click Save on the console task bar to save your configuration changes.
7.Restart the server.

Thursday, February 10, 2011

How to make class Immutable like String

1. make your class final, so that no other class can be inherited
2. Make sure that all variables are final and private.
3. No setter methods.
4. Provide a constructor which can take values to these variables.

Example:

final class MyImmutableClass
{
// instance var are made private & final to restrict the access

private final int count;
private final double value;

// Constructor where we can provide the constant value
public MyImmutableClass(int paramCount,double paramValue)
{
count = paramCount;
value = paramValue;
}

// provide only methods which return the instance var
// & not change the values

public int getCount()
{
return count;
}

public double getValue()
{
return value;
}
}

// class TestImmutable
public class TestImmutable
{
public static void main(String[] args)
{
MyImmutableClass obj1 = new MyImmutableClass(3,5);

System.out.println(obj1.getCount());
System.out.println(obj1.getValue());

// there is no way to change the values of count & value-
// no method to call besides getXX, no subclassing, no public access to var -> Immutable
}
}

Monday, January 17, 2011

When providing a user defined class to HashMap or HashTable what methods needs to be overridden?

You should override the equals() and hashCode() methods from the Object class. The default implementation of the equals() and hashcode(), which are inherited from the java.lang.Object uses an object instance’s memory location (e.g. MyEmployeeObject@8d52g38h). This can cause problems when two instances of the Employee objects have the
same id but the inherited equals() will return false because it uses the memory location, which is different for the two instances. Also the toString() method can be overridden to provide a proper string representation of your
object. Points to consider:

• If a class overrides equals(), it must override hashCode().
• If 2 objects are equal, then their hashCode values must be equal as well.
• If a field is not used in equals(), then it must not be used in hashCode().
• If it is accessed often, hashCode() is a candidate for caching to enhance performance.

Vector Vs ArrayList

Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization
will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList.


Data growth


Internally, both the ArrayList and Vector hold onto their contents using an Array. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance
hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program
will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later.
If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.


Usage patterns


Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end
of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements
at index i and higher over by one element. So what does this all mean?


It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.

When to use an abstract class and interface

When to use an abstract class?:

In case where you want to use implementation inheritance then it is usually
provided by an abstract base class. Abstract classes let you define some default behaviour and force subclasses to provide any specific behaviour.

When to use an interface?: For polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently,

you should prefer using interface to abstract. Justification for using interfaces
is that they solve the ‘diamond problem’ of traditional multiple inheritance.

What is the difference between aggregation and composition?

Aggregation is an association in which one class belongs to a collection. This is a part of a whole relationship where a part can exist without a whole.

For example a line item is a whole and product is a part. If a line item is deleted then corresponding product need not be deleted. So aggregation has a weaker relationship.

Ex:

Class employee {

private Department dept;

//setter and getter methods
}

dept can exists even if we delete employee class.since dept can be a member of another class.

Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.

For example An order is a whole and line items are parts. If an order deleted then all corresponding line items for that order should be deleted. So composition has a stronger relationship.

Ex:

class Employee {

private Department dept = new Department();

}

If employee is deleted, department also deleted.

Difference between NoClassDefFoundException and ClassNotFoundException

A NoClassDefFoundException is thrown if a class is referenced with Java’s “new” operator (i.e. static loading) but the runtime system cannot find the referenced class.

Ex: Employee e = new Employee();


A ClassNotFoundException is thrown when an application tries to load in a
class through its string name using the following methods but no definition for the
class with the specified name could be found:

1. The forName(..) method in class - Class.
2. The findSystemClass(..) method in class - ClassLoader.
3. The loadClass(..) method in class - ClassLoader

Ex: Class employeeClass = Class.forName("com.samples.Employee") ;