Saturday, July 10, 2010

How to use different deployment locations in JBoss


Target Audience: Beginners
JBoss version 4.x

We normally would have a build system which builds the application, creates the distribution files ( war, sar ,ear...) and then copies the distribution files to server/xxx/deploy directory. In JBoss, We used to deploy our applications under server/xxx/deploy.
E.g: D:/Tools/jboss-4.2.0.GA/server/default/deploy
Alternatively we can tell( configure) JBoss to deploy the application from our preferred location ( somewhere from outside of the JBoss).

How can we do that?
There is a MBean with name “jboss.deployment:type=DeploymentScanner” has been defined in conf/jboss-service.xml. This MBean is responsible for hot deployments. This MBean has a attribute “URLs” which accepts comma separated values. We can add our values too along with the existing values. Suppose our distribution files are built under c:/myproject/dist, then we can configure the DeploymentScanner as given bellow.


Don’t forget to restart the JBoss server after the changes were made. The DeploymentScanner will scan the configured URLs and deploy the possible applications.

If you want to configure the location without restating the application as hot deployment then you would have to do it through the JMX-Console. Please find the details here

Hope this will be helpful.

Friday, July 9, 2010

Hibernate persistence lifecycle


Target Audience: Beginners
Hibernate has three states respect to its persistence lifecycle which are transient, persistent and detached. Hibernate persistent manager has some callback methods which involves in those persistent states. In a lifecycle, the object can transition from transient to persistent to detached states.

Transient objects
When an object is instantiated (with new operator), it’s not persistent immediately. It’s first goes to the transient state which is in memory. In this state it’s not related to any database table row. When that object is de-referenced it loses its state and goes to detached state which will be eligible to garbage collected.
The objects that are in transient state are not considered as transactional objects. Any modification made on those object can not be part of transaction. If a transient object need to be persist or in other words if an object need to changes its state from transient to persistent, then the save () method should be called to persistent manager or that object should refer to one which was created from already persisted.

Persistent objects
Persistent instance is which has a valid database entry set with a primary key identifier. In other words, it should refer a valid database table row.
Persistent object can be instantiated with a call of save () method of the persistent manager then it’s associated to that persistent manager. Or alternatively it can be instantiated with a persistent object which is already associated with that persistent manager.
Persistent object participated in a transaction will be synchronized with the database when the transaction manager commits the transaction which executes the SQL query. Some times, the synchronization will happen before the execution of the queries just to make sure that the queries aware of the changes done during the previous transaction.
When the transaction ends, all the persistent object will not be updated to the database. Hibernate checks the objects and finds which are modified then only it’ll update the database. This process is called dirty checking. Dirty objects are which has had modified and not synchronized with database. This process gains performance with some databases and loose performance with some databases. So hibernate allows user to set this with dynamic-update="true" in class mapping so it would generate SQL dynamically to update only the columns that are modified.
When the persistent manager class the delete () method the data will be removed from database and the persistent object will move fro persistent state to transient sate.

Detached objects
Detached object are which no longer guaranteed to be synchronized with database state.
The persistence instances will still exist even after the transaction completes. These instances will be detached when the close () the Session. But still those object holds the data , Hibernate can detain this objects out side of the transaction manager and persistent manager, with a new transaction manager and with a new persistent manager.
Persistent objects can not be detached explicitly. The evict () method of Session can be used to detach the object from session cache. All persistent objects will be detached when close () the session or the objects are serialized.

Thursday, July 8, 2010

Catching java.lang.Throwable


Target Audience: Beginners
Can we catch java.lang.Throwable? This is a hot question in java developers world. I would say “yes, you can but you should not”. Is it confusing? Ok. Let me explain

Technically we can catch it as java allows it.

try{
//some code
}catch (Throwable e) {
// TODO: handle exception
}
The java.lang.Exception and java.lang.Error are only the direct subclasses of java.lang.Throwable. As we know all Exceptions in our code or program should be caught and handled except RuntimeException. RuntimeExceptions are caught and handled some times and most of the time it is let to be thrown to the caller. When RuntimeException is caught and handled? Suppose we are developing a web application and we got a exception while writing to a database and the underlying API like JDBC or Hibernate throws a RuntimeException, then we catch it and show the web user with an appropriate error message. Erros are mostly unrecoverable like java.lang.OutOfMemoryError and ava.lang.StackOverflowError. We can not do anything if we even catch it. So the java world extremely suggest not catch java.lang.Error in other words java.lang.Throwable because if we catch Throwable then we catch Error too.

We can look at in a different view too. Suppose you are creating a API which will be used by other developers ( like an open source component) and your component uses other third party API ( some other open source component like apache beanutils API ). you might have created your component using a specific version( V1.0) of third party component. if a developer try to use your component and pick the wrong version(V.2.0) of the third party component which your component depend on, then he may run into some errors like java.lang.NoSuchMethodError because of different version of third party. So in your component’s code you can plan to catch java.lang.NoSuchMethodError , log it and throw it as your customized Exception saying “ Hello! you are probably using a different version of X API, please use Xv1.0 or any previous versions”. Here we catch the Errors.

So my point is there could be extremely rare situations where you might catch java.lang.Throwable. But much much better to avoid doing so.

Avoid catching java.lang.Throwable and be happy.

Reference:
A video tutorial explaining very basics of java exception handling

Tuesday, July 6, 2010

PageContext.getSession() returns null

javax.servlet.jsp.PageContext.getSession() will return the reference to the implicit session object that we define in the page
e.g:
<%... page session="false" %>
<html>
<%= pageContext.getSession() %>
</html>
This will return you a null as we defined ‘false’ for implicit session object.

And I think that PageContext.getSession() would only return the session object if it has been already created otherwise you will get a null.

I mean the bellow one might also get a null unless a session has been already created.
<%... page session="true" %>
<html>
<%= pageContext.getSession() %>
</html>
The following will work perfectly
<%... page session="true" %>
<html>
<% request.getSession(); %>
<%= pageContext.getSession() %>
</html>

Binding JBoss to specific IP / configuring IP to JBoss application server


Target Audience: Beginner, Intermediate
When we configure the JBoss application in a production environment, the server need to be bind to an IP address ( in most cases). There are several ways to do it.
1. Setting as a run parameter. When we run the JBoss server in a cmd prompt, run as
run -b127.0.0.1

2. Setting it as a system property. We can set a system property which is used internally by JBoss server.
run -Djboss.bind.address=127.0.0.1

3. If we want the configuration to be permanent, then we can place the configuration inside run.bat file. Open run.bat file and search for org.jboss.Main and place the binding configuration .
E.g: org.jboss.Main -b192.168.5.29


Note: We can specify 0.0.0.0 if we want all IP addresses to be mapped to JBoss Server.

Monday, July 5, 2010

What is java.home?


Target Audience: Beginners
When a java program runs, the java runtime environment (JRE- who runs any java program) sets some system properties for its own use and java.home is one of those system properties. The java.home is referencing the JRE home directory E.g: C:\Tools\jdk1.5.0_04\jre.

People tend to confuse with JAVA _HOME and java.home. They are different. JAVA _HOME is set by the user ( me or you) to reference JDK’s home directory. E,g: C:\Tools\jdk1.5.0_04\

Following are the system properties that java sets.
Key Description of Associated Value
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory


public class SystemExample {
public static void main(String[] args) {
System.out.println("java.home :" + System.getProperty("java.home"));
System.out.println("java.vm.version :" + System.getProperty("java.vm.version"));
System.out.println("os.name :" + System.getProperty("os.name"));
System.out.println("os.version :" + System.getProperty("os.version"));
System.out.println("user.name :" + System.getProperty("user.name"));
}
}

The output will be similar to this

java.home :C:\Tools\jdk1.5.0_04\jre
java.vm.version :1.5.0_04-b05
os.name :Windows XP
os.version :5.1
user.name :kannan

Sunday, July 4, 2010

Java Mail Examples


1.How to send a simple email in java
2.How to authenticate user and send email in java

1.How to send a simple email in java

package email;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailExample {

public static void main(String[] args) {

EmailExample.sendEmail("mail.test.com", "121",
"webmaster@test.com", "to@test.com",
"Wazz up!", "Testing...");
}

public static void sendEmail(String smtpHost, String smtpPort
, String from, String to, String subject, String body) {

Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
} catch (MessagingException ex) {
System.err.println("Opps!! could not send email. " + ex);
}
}
}

2.How to authenticate user and send email in java

package basics;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

/**
* This is a default mail sender instance, which is implemented based on
* javax.mail API. This handles the actual mail sending part.
* The caller should create a map of email parameters and pass it to
* the send(Map) method.
*
* Map<String, String> params = new HashMap<String, String>();
* params.put(Mail.SMTP_HOST, "smtp.blogspot.test.com"));
* params.put(Mail.FROM, "testz_apps@blogger.test.com");
* params.put(Mail.TO, "testz_apps@blogger.test.com");
* params.put(Mail.SUBJECT, "");
* params.put(Mail.BODY, "");
* mailSender.send(params);
*
* It supports only "text/plain" as body content. When there are multiple
* emails concatenated in one String for Mail.TO,Mail.CC or
* Mail.BCC, use ',' as a separator or use Mail.ADDRESS_SEPERATOR instead.
* If the credentials need to be authenticated, a Mail.AUTH parameter
* should be set in the parameters map with the value of "true" along
* with Mail.USER_NAME and Mail.PASSWORD params.
*
* Map<String, String> params = new HashMap<String, String>();
* params.put(Mail.SMTP_HOST, "smtp.blogspot.test.com"));
* params.put(Mail.FROM, "testz_apps@blogger.test.com");
* params.put(Mail.TO, "testz_apps@blogger.test.com");
* params.put(Mail.SUBJECT, "");
* params.put(Mail.BODY, "");
* params.put(AUTH, "true");
* params.put(USER_NAME, "kannan");
* params.put(PASSWORD, "password");
* mailSender.send(params);
*
* @author Kannan
* @version 1.0
*/
public class MailSender implements IMailSender {

// TODO Implement replyTo
// TODO Implement files attachment
// TODO Implement template content

/** The log handler. */
private Logger logger = Logger.getLogger(this.getClass().getName());

/**
* {@inheritDoc}
*/
public boolean send(Map mailParams) throws EmailSendException {
boolean result = false;
try {
// validate the mandatory parameters, and throws the EmailSendException.
validateParams(mailParams);

// Sends the mail if the validation is fine.
sendMail(mailParams);
result = true;
if (logger.isDebugEnabled()) {
logger.debug("mails sent successfully");
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new EmailSendException(e.getMessage());
}
return result;
}

/**
* Validates all parameters. The to address, from address, body, subject
* and smtp host address parameters are mandatory. if one of mandatory
* parameters is null or one of to address, from address and smtp host
* address is empty, then it throws the EmailSendException with a
* appropriate message.
*
* @param mailParams the mail parameters.
* @throws if one of mandatory parameters is null
* @throws EmailSendException the email send exception
*/
private void validateParams(Map mailParams) throws EmailSendException {
if (mailParams == null) {
logger.error("The mail parameter map is null");
throw new EmailSendException("The mail parameter map is null");
}

// validates the smtp host address, the smtp host can not be empty.
String smtpHost = (String) mailParams.get(SMTP_HOST);
if (smtpHost == null || smtpHost.equals("")) {
logger.error("The mail smtp host is null or empty");
throw new EmailSendException("The mail smtp host is null or empty");
}

// validates the from address, the from address can not be empty.
String from = (String) mailParams.get(FROM);
if (from == null || from.equals("")) {
logger.error("The mail from address is null or empty");
throw new EmailSendException("The mail from address is null or empty");
}

// validates the subject, the subject can be empty.
String subject = (String) mailParams.get(SUBJECT);
if (subject == null) {
logger.error("The mail subject is null");
throw new EmailSendException("The mail subject is null");
}

// validates the body, the body can be empty.
String body = (String) mailParams.get(BODY);
if (body == null) {
logger.error("The mail body is null");
throw new EmailSendException("The mail body is null");
}

// validates the to address, the to address can not be empty.
String to = (String) mailParams.get(TO);
if (to == null || to.equals("")) {
logger.error("The mail to address is null or empty");
throw new EmailSendException("The mail to address is null or empty");
}
}

/**
* Sends the email for given toAddress. The message body and subject will be
* messageBody and messageSubject.
*
* @param mailParams the mail params
* @throws AddressException throws when a wrongly formatted address is encountered.
* @throws MessagingException if there are any other unexpected exception is encountered
* @throws EmailSendException the email send exception
*/
private void sendMail(Map mailParams) throws EmailSendException {

String from = (String) mailParams.get(FROM);
String subject = (String) mailParams.get(SUBJECT);
String body = (String) mailParams.get(BODY);
String to = (String) mailParams.get(TO);
String cc = (String) mailParams.get(CC);
String bcc = (String) mailParams.get(BCC);
String smtpHost = (String) mailParams.get(SMTP_HOST);
String smtpPort = (String) mailParams.get(SMTP_PORT);
String auth = (String) mailParams.get(AUTH);

// Get system properties
Properties props = System.getProperties();

// Specify the desired SMTP server
props.put("mail.smtp.host", smtpHost);
if (smtpPort != null) {// if null, the default port 25 will be used instead
props.put("mail.smtp.port", smtpPort);
}

// Create a new Session object
Session session = null;
// If authentication is required
if (auth != null && auth.toLowerCase().equals("true")) {
String username = (String) mailParams.get(USER_NAME);
String password = (String) mailParams.get(PASSWORD);
SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
session = Session.getInstance(props, authenticator);
} else {
// If authentication is not required
session = Session.getInstance(props, null);
}

// Create a new MimeMessage object (using the Session created above)
Message message = new MimeMessage(session);

try {
// Setting the from address
message.setFrom(new InternetAddress(from));

// Setting the CC address if it's not null and empty
message.setRecipients(Message.RecipientType.TO, getInternetAddress(to));
if (cc != null && !cc.equals("")) {
message.setRecipients(Message.RecipientType.CC, getInternetAddress(cc));
}

// Setting the BCC address if it's not null and empty
if (bcc != null && !bcc.equals("")) {
message.setRecipients(Message.RecipientType.BCC, getInternetAddress(bcc));
}

// Setting the subject and body content. it supports only "text/plain" for now.
message.setSubject(subject);
message.setSentDate(new Date());
message.setContent(body, "text/plain");

if (logger.isDebugEnabled()) {
logger.debug("The mail is going to be sent using the following parameters");
logger.debug("To : " + to);
logger.debug("From : " + from);
logger.debug("Subject : " + subject);
logger.debug("Body : " + body);
logger.debug("Smtp host : " + smtpHost);
logger.debug("CC : " + cc);
logger.debug("BCC : " + bcc);
}
Transport.send(message);
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new EmailSendException(e.getMessage(), e);
}
}

/**
* Returns the array of InternetAddress , for given address. it splits the give
* address with the ADDRESS_SEPERATOR and convert it into a
* InternetAddress.
*
* @param address the address, can be a single address or multiple address separated by
* ADDRESS_SEPERATOR.
* @return the array of InternetAddress , for given address
* @throws EmailSendException if there are any AddressException, then it logs the
* error and wraps it with EmailSendException and then throws it.
*/
private InternetAddress[] getInternetAddress(String address) throws EmailSendException {
String addressArray[] = address.split(ADDRESS_SEPERATOR);
InternetAddress[] inetAddress = new InternetAddress[addressArray.length];
for (int i = 0; i < addressArray.length; i++) {
try {
inetAddress[i] = new InternetAddress(addressArray[i]);
} catch (AddressException e) {
logger.error(e.getMessage(), e);
throw new EmailSendException(e.getMessage(), e);
}
}
return inetAddress;
}

/**
* The Class SMTPAuthenticator.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {

/** The username. */
String username = null;

/** The password. */
String password = null;

/**
* Instantiates a new sMTP authenticator.
*
* @param username the username
* @param password the password
*/
SMTPAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}

}

/**
* The constant class holds the key value pair.
*
* @author Kannan
* @version 1.0
*/
public class Mail {

/** The email address separator */
public static final String ADDRESS_SEPERATOR = ",";

/** The key for email from address */
public static final String FROM = "FROM";

/** The key for email subject address */
public static final String SUBJECT = "SUBJECT";

/** The key for email body address */
public static final String BODY = "BODY";

/** The key for email SMTP address */
public static final String SMTP_HOST = "SMTP_HOST";

/** The key for email SMTP port */
public static final String SMTP_PORT = "SMTP_PORT";

/** The key for email to address */
public static final String TO = "TO";

/** The key for email CC address */
public static final String CC = "CC";

/** The key for email BCC address */
public static final String BCC = "BCC";

/** The key for username */
public static final String USER_NAME = "USER_NAME";

/** The key for password */
public static final String PASSWORD = "PASSWORD";

/** The key for email authentication indicator */
public static final String AUTH = "AUTH";
}

/**
* The interface define methods for sending mails.
*
* @author Kannan
* @version 1.0
*/
public interface IMailSender {

/**
* Send the mail according to the parameters that are set by the mailParams map.
*
* @param the mail parameters. the mail parameters can be set as follows. *
*
* Map<String, String> params = new HashMap<String, String>();
* params.put(Mail.SMTP_HOST, "smtp.blogspot.test.com"));
* params.put(Mail.FROM, "testz_apps@blogger.test.com");
* params.put(Mail.TO, "testz_apps@blogger.test.com");
* params.put(Mail.SUBJECT, "");
* params.put(Mail.BODY, "");
* mailSender.send(params);
*
* @throws EmailSendException, if there are any exceptions conditions, then it is wrapped with
* EmailSendException and thrown.
*/
boolean send(Map mailParams) throws EmailSendException;
}

Spring to enable multi-environment deployment without rebuilding

I would like to share this article which I came across. It gives a better idea of how spring enables loading environment specific property files. It’s really helpful as most of the projects will be tested on multiple environments.

for more read this

Java Abstract Class Examples


1. How to create an abstract Class and its sub classes
2. How to create a static method in abstract Class

1. How to create an abstract Class and its sub classes

package basics;

public abstract class AbstractClassExample {

public static void main(String[] args) {
Lion lion = new Lion("Mr Lion", 4);
lion.printName();
System.out.println(lion.canWalkWithTwoLegs());

Bear bear = new Bear("Mr Bear", 4);
bear.printName();
System.out.println(bear.canWalkWithTwoLegs());
}
}

abstract class Animal {
private String name = null;
private int numberOfLegs = 0;

public Animal(String name, int numberOfLegs) {
super();
this.name = name;
this.numberOfLegs = numberOfLegs;
}

public abstract boolean canWalkWithTwoLegs();

public void printName() {
System.out.println("Name is: " + name);
}
}

class Lion extends Animal {

public Lion(String name, int numberOfLegs) {
super(name, numberOfLegs);
}

public boolean canWalkWithTwoLegs() {
return false;
}
}

class Bear extends Animal {

public Bear(String name, int numberOfLegs) {
super(name, numberOfLegs);
}

public boolean canWalkWithTwoLegs() {
return true;
}
}

There will be a zip file created at
Name is: Mr Lion
false
Name is: Mr Bear
true


2. How to create a static method in abstract Class

package basics;

public abstract class AbstractClassExample {

public static void main(String[] args) {
AbstractClassExample.doSomeThing();
}

public static void doSomeThing() {
System.out.println("What can i do?");
}
}


There will be a zip file created at
What can i do?