Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, September 29, 2010

Axis2 Java Web Services with NetBeans deployed on Tomcat

As we discussed earlier (previous post) you should set axis2 in to your tomcat web server.

Apply axis2 plugin

First you need to install "Axis2 Support" plugin to the net beans. Open the netbeans IDE and navigate to Tools -> Plugins. The plugins list will be displayed. If "Axis2 Support" is not showing there, please update the list.Then click the install button below and continue with installing...

Add apache web server to IDE

Ok, now our next task is to add apache to the netbeans IDE. To do that, go to the Services tab (or Window -> Services). Right click on servers and click "Add Server". It will popup following window. Select Tomcat 6.0 (according to your sever version) and continue.
You should navigate the folder which you have been placed Tomcat server. Add username and password (in my case I just used admin/admin). Then you will appear new server like this, You can simply start server by right click on tomcat and selecting start option.

Setup .aar file creating directory

Go to Tools -> Options. And now you will be able to see an separate tab for Axis2 with its logo. Click on that.
Now browse to your axis2.war file for "Target Location for axis2 .aar files:" field. And tick on use tomcat manager for deployment, enter username and password which you set earlier (in my case admin/admin). Then click ok.

Creating Web Services

Open a new project (File -> New Project). Select Java and Java Application. Add a desired project name (I used "CardValidator") and complete creating project. Then right click on newly created project and select New -> Axis2 Service From Java...
Then you will get popup window. There is nothing to change, in this case lets take this as Create Empty Web Service and click next. Then following step will visible. Give any name you wish as class name (in my case "MyClassValidator") and select package from list (in my case "cardvalidator"). And Finish it. Now its done. You will find a sample service call hello:String.
Important:You should right click on Axis2 Web Services and Deploy to Server, before test the sample program.
After that you can right click on hello:String and Test operation in browser. It will give following browser opening for your request.
Writing new methods is not a big deal.
Let's write a method to accept a card number and check whether it is valid one or not. If its starts with "1111" then its a valid card and should return true. Else should return false.
Simply write following java method on MyCardValidator class (which contains method hello as well)
public boolean validateCard(String cardNumber){

if(cardNumber.startsWith("1111")){
return true;
}else{
return false;
}
}

And now right click on MyCardValidator and deploy to server. Then your new service method will appear as a service. You can right click on it and test it.
Then your browser will open with link like this "http://localhost:8080/axis2/services/MyCardValidator/validateCard?cardNumber=XYZ". By changing XYZ you can check whether your service works properly.
Note: If your default browser is IE or Firefox, then the output will looks like a XML message. If you used chrome, the output will just be text values.

By placing the axis2.war file in any tomcat server after you creating it you can access to the web services in above manner.
Hope you got it correctly. If you got any problems pleas feel free to contact me by mailing or commenting here.
Your comments will be very useful for further writings.... :)
Thank you for visiting. :)

Sunday, November 22, 2009

Log4j - Logging package

Log4j is a package which supports to do logging very easily. This logging outputs can be taken in many ways.

There are many more types can be used,
AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender, TelnetAppender, DailyRollingFileAppender, RollingFileAppender.
To begin with log4j, you should first download the log4j package. Here is a sample link to download it.

Log Levels

There are some log levels defined in log4j.
  1. all - All levels including custom levels
  2. trace (since log4j 1.2.12) - Developing only, can be used to follow the program execution.
  3. debug - Developing only, for debugging purpose
  4. info - Production optionally, Course grained (rarely written information), I use it to print that a configuration is initialized, a long running import job is starting and ending.
  5. warn - Production, simple application error or unexpected behaviour. Application can continue. I warn for example in case of bad logging attempts, unexpected data during import jobs
  6. error - Production, application error/exception but
    application can continue. Part of the application is probably not working
  7. fatal - Production, fatal application error/exception, application cannot continue, for example database is down.
  8. no - Do not log at all.

A simple example

First you better add downloaded jar package to the project using your IDE. Then you have to setup the log4j properties through property file or, xml file.... In this case, let's check with log4j.properties file. In your IDE, as other property files add your log4j properties file. For example create new file called "log4j.properties" It should be look like this.
log4j.rootLogger=debug, file
log4j.appender.file=org.apache.log4j.RollingFileAppender #logging Type
log4j.appender.file.maxFileSize=100KB #Max file size of a log file
log4j.appender.file.maxBackupIndex=5 #keeping backups of
log4j.appender.file.File=C://myprogram_logs/test.log #Location which saving log file
log4j.appender.file.threshold=debug #Log Level
log4j.appender.file.layout=org.apache.log4j.PatternLayout #Layout of the log file
log4j.appender.file.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n #Layout of the log file
I am using RollingFileAppender here. Max file size is 100KB, kind of all the properties related to Log4j is stated here.
At your java file, load the property file and put it into the PropertyConfigurator on log4j package. Example code snipped here,
Improte packages

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import java.util.Properties;
On the relevant class, define the Logger type global variable(in my case "logger").
private static Logger logger = Logger.getLogger(MyProgram.class);
On main method,
   public static void main(String[] args){
Properties properties = new Properties();
InputStream inputStream = MyProgram.class.getClassLoader().getResourceAsStream("log4j.properties");
properties.load(inputStream);
PropertyConfigurator.configure(properties);
logger.info("My program Begins");
}
When you catch an exception, write it on log as follows
     try {
//Your code here
} catch (Exception e) {
logger.error("Found the exception here");
}

Now you will find logs on C://myprogram_logs/test.log location file.
When you need to use it in ubuntu, you just have to change the file location only from the properties file (i.e. /var/myprogram_logs/test.log).
See how easy logging you programming status with Log4j........
Here is a nice pdf to follow.
Cheers......