As logging required in every project.  Log4j provided by Apache to add logging to your system in best way.  I also required logging to my application and decided to use Log4j but could find any comprehensive tutorial about logging as somewhere basic were forund and advance use on other site. the way you want to add logging into your application is also vary to normal system sometime. thats why i had to read many tutorial to implement loggin as my requirements. here i will cover applying logging in different and hope it will cover your requirement of logging. here goes my comprehensive tutorial for Log4j.

I will cover following thing in this Post/tutorial

-Simple logging tutorial

- Logging on console and File

- How to manage logging in different files and size

- How to do logging on different file for different package or for different functional functionality withing the application.

let me first introduce you LOG4J

Log4j is a Reliable, Fast and Flexible Logging Framework (APIs) written in Java which is distributed under the Apache Software License.

Log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

Log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog etc.

Log4j has three main components:

what you will achieve here

- Console loggign

- File logging

- define multiple log files within application

you can do it by two way.

3.1 -Define multipal log files

- for each log file  define package of the application(mean “first.log”  is for  ”com.mypackage1 and come.mypakage2″  etc)

to implement this functionality yoz need to change log4j propertiues files and in the code intilization of the logger.

log4j.properties file:

# Define the root logger with appender file
log4j.rootLogger = stdout ,FILE, EXPORT

log4j.appender.stdout=org.apache.log4j.ConsoleAppender // Console logging

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

# Define the file appender

log4j.appender.FILE=org.apache.log4j.RollingFileAppender //file logging

log4j.appender.FILE.File=first.log //log file name

log4j.appender.FILE.MaxFileSize=2MB //log file size, if size increase to this then new file will be created

log4j.appender.FILE.MaxBackupIndex=2 //backup will be persist until two files
# Define the layout for file appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.conversionPattern=%m%n

log4j.logger.com.mypackage1=info, EXPORT

log4j.logger.com.mypackage2=info, EXPORT

log4j.logger.com.mypackage1=info, EXPORT
# Define the file appender

log4j.appender.EXPORT=org.apache.log4j.RollingFileAppender

log4j.appender.EXPORT.Threshold=fatal

log4j.appender.EXPORT.File=second.log // second log file

log4j.appender.EXPORT.MaxFileSize=2MB

log4j.appender.EXPORT.MaxBackupIndex=2
# Define the layout for file appender

log4j.appender.EXPORT.layout=org.apache.log4j.PatternLayoutlog4j.appender.EXPORT.layout.conversionPattern=%m%n

to implement logging to specific to package  is marked as RED .

now initialize the logger like this.

static Logger log =Logger.getLogger(“FILE”); for  first log file , use this intialization in the code where you wanted to perform logging in first file

static Logger log =Logger.getLogger(“EXPORT”); for  second log file , use this intialization in the code where you wanted to perform logging in second file

logging statements in the code you dont need to change(except you change the logging level)

3.2- Define different debug level for different files.

- for first.log  define “fatal” logging level like this “log4j.appender.FIRST.Threshold=fatal”

- for second.log  define “info” logging level like this “log4j.appender.SECOND.Threshold=debug”

how it helps, where you wnated to write limited log or related to some functionality use first logger becouse its lowses loggign level so its only print the

log.fatal(…..)  statements log in the file.

for the rest of the application used second logger because its logging level is debug and its cover almost all logging level, therefor its write all log statement of application in log file.

log.debug(…..)

log.info(…..) etc you can check in above colorfull table how much logging level debug covers.

how you can implement in your application.  for this you have to Chane your log4j. properties file, logging initialization and log statement in the code

log4j.properties :

# Define the root logger with appender file
log4j.rootLogger = stdout ,FILE, EXPORT

log4j.appender.stdout=org.apache.log4j.ConsoleAppender // Console logging

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

# Define the file appender

log4j.appender.FILE=org.apache.log4j.RollingFileAppender //file logging

log4j.appender.FILE.File=first.log //log file name

log4j.appender.FILE.MaxFileSize=2MB //log file size, if size increase to this then new file will be created

log4j.appender.FILE.MaxBackupIndex=2 //backup will be persist until two files
# Define the layout for file appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.conversionPattern=%m%n
# Define the file appender

log4j.appender.EXPORT=org.apache.log4j.RollingFileAppender

log4j.appender.EXPORT.Threshold=fatal

log4j.appender.EXPORT.File=second.log // second log file

log4j.appender.EXPORT.MaxFileSize=2MB

log4j.appender.EXPORT.MaxBackupIndex=2
# Define the layout for file appender

log4j.appender.EXPORT.layout=org.apache.log4j.PatternLayoutlog4j.appender.EXPORT.layout.conversionPattern=%m%n

here you can see two log file.

what you have to change to intialize loggign in code. see below

static Logger log =Logger.getLogger(“FILE“); for  first log file , use this intialization in the code where you wanted to perform logging in first file

static Logger log =Logger.getLogger(“EXPORT“); for  second log file , use this intialization in the code where you wanted to perform logging in second file

becouse the second file we have define logging leve is FATAL, functionality in second filespecificthis line to perform logging for some write

log.fatal(“………”);

for the fist logging you can use any statement like log.debug, log.info because first logging level is debug which covers all.

Hope it helps you!!! if you like this post please comment and share with other.

I am working on web service from last couple of years  before i was only know about Axis1/Axis2 and later i get to know more webservice API and framworks for JAVA(some of them given by Apache and SUN). Therefore first thing to know is whats the difference between all these APIs and framworsk. here is my try to make it simple to cleare confusion b/w ways to make webservices.  here is littel defination part;

- Apache SOAP,JAX-RPC, Axis1 and Axis2.

Soap, JAX-RPC  and Axis1 is obsolete now. JAX-RPC is a pecification/API for java developer to develop SOAP based interoperatable webservices . This API is now obsolete and may be dropped from  next J2EE version. same is the case with Axis1 because Axis1 is a web service framework and based on JAX_RPC ansd SAAJ and this turned out not be good approach because JAX-RPC constrained the internal design of the Axis code and contributed to both performance and flexibility issues. therefor Axis1 is also now obsolete now. Axis2 replace the JAX-RPC with  JAX-WS and  also support SAAJ. so Axis2 was designed to be flexible enough to  implement support for the replacement web service standard on top of base framwork one of the reason is based on completely new architecture and has been written from scratch. Recent version of the Axis2 have implemented support for both the JAXB 2.x java XML data binding standard and JAX-WS 2.x java web service standard that replace java JAX-RPC.

- SAAJ  is another API/specification for using SOAP envelops with or without attachment. It operates on lower level then JAX-RPC or JAX-WS, both of which will use SOAP envelopes based on SAAJ if needed.

- CXF is an open source service framwork . It comes up from a fusion of XFire and Ionas Celtix project. CXF was developed with the intention to integrate it into other systems. This is reflected in the CXF API and the use of the Spring framework. Therefore it is simple to integrate CXF into existing systems. CXF helps you to develop and build services using frontend programming APIs, like JAX-WS and JAX-RS. these  services can speak veriety of protocols such as SOAP, XML/HTTP, RestFul Http, CORBA and work over veritey of transports such  as Http, JMS or JBI. CSF implemnts  the JAX-Ws APIs. CXF  JAX-WS  support includes some extension to the standard that make it significantly easy to use, compared to the reference implementation: it will automatically generate code for request and response bean classes  and dose not requires WSDL in the simple case. It also include “simple front-end ” which allow creation of client and endpoint without annotation. It support both Generating WSDL from Java classes and generating Java classes from WSDL.

- JAX-WS The Java API for XML based Web Services is the successor of the JAX-RPC specification. JAX-WS respectively its predecessor is message based and supports asynchronous communication. The Configuration is managed by annotations therefore Java 5 or higher is required. JAX-WS isn’t downwardly compatible to its predecessor JAX-RPC. With JAX-WS it is pretty easy to write and consume Web Services. The default values of numerous parameters are comfortable for the programmer, so that simple Pojos declared with a @WebService annotation can be used as a Service

- More recent SOAP offerings from Apache―the competing Apache Axis2 and Apache CXF projects―both implement JAX-WS. So far as I can tell, neither of them implements the older JAX-RPC API. The CXF User Guide recommends JAX-WS as a primary programming framework. Axis2 advocates its own APIs, and at the time of writing the JAX-WS interface was sparsely documented. But it does exist.

The JAX-WS Reference Implementation (JAX-WS-RI) is developed within the GlassFish Community. It is another Open Source implementation of JAX-WS. So these three leading candidates for “modern” Java SOAP implementations all implement JAX-WS, and none of them implements JAX-RPC. If the implementers are right, JAX-WS seems to be the way of the future.

A snapshot

JAX-WS is a Specification (provided by Sun, now Oracle) to defining Web Services by use of Annotations.

Apache CXF is a framework that implements JAX-WS to generate Web Services Definitions and Java Proxy (from a Web Service Definitions)

Apache Axis 2 is also a framework that now supports JAX-WS (up to JAX-WS 2). It’s an improvement from Apache Axis. CXF supports up to JAX-WS 1.2.

Hope this Artical help you to understand the difference /relationship between webservice API and frameworks.

P.S if you have question ask under this blog and if you like this then support me to write more by showing your likeness.

When you installed mysql first time all goes well but if due to some reason you need to uninstall then install mysql agian follwoing problem always occure at configuring it

Processing configuration…

 

/ Prepare configuration

/ Write configuration file (c:\Program … \my.ini)

/ Start service

x Apply Security settings

 

the last step (Apply security setting) always get fail. I also had the same problem  and it took so much time to me to solve the issue. on the internet you found lots of solution

related to properly uninstalling mysql like

 

-Unintsall by Add/Remove program(In windows XP)

- delete Mysql folder in program Files(c:progme Files etc) directory

-remove entry from registry(regedit)

- remove entry from system configration (msconfig)

- check by (Alt+ctr+del), that my sql service is runnign uder proccess, if it is then stop it.

- better to restar your system(optional)

- Now reisntall  and configure it agian.

 

If this solution works then its greate, otherwise i have another solution for your problem

 

for the 2nd solution you have to dlelete  some enrty from mysql tables to congfiure mysql easily.

so follow it as following

 

1 -stop if mysql service running(offcourse its running after installation), use (Alt+ctr+del) to stop form procsess

or use it http://dev.mysql.com/doc/refman/5.1/en/windows-start-service.html

 

2 -Open command promt (in windows Menu>Start>Run>CMD )

“C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld.exe” –skip-grant-tables

3 – Let this window open , open another command promt

Menu>Start>Run>CMD

“C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe” -u root

4 -Set new password for root user (or your user that given it another name)

 

You’re now connect to MySQL, you have to modified the root account in the date of MySQL

 

mysql> use mysql;

mysql> UPDATE user SET password=PASSWORD(‘motdepasse’) WHERE user=”root”;

mysql> flush privileges;

mysql> exit;

 

5- Restart with MySQL Commant Line Client (mean restart the mysql service).

 

Thats all you have to do!!!

 

Leave your comments, if this solution works for you (offcourse it will work, as it work in my case) :D

I had gone through lost of taturial about how to configure axis1 or axis2 then add your projetcs to Axisp.but there is no taturial that tells about “how to add Axis1 into running projetc” instead of adding projetc to Axis..so i have made this short taturial for it..its v simple

Simply follow these smal steps..

1-Downlaod Axis1.*  and put somewhere in your computer.

2-copy all jar files from Axis lib folder to your application lib folder(fir example in my case its HOMEPATH/axis-1_4\lib, the real Path is     D:\axis-1_4\lib)

3-copy All servcila mappings from web.xml of Axis1.4(D:\axis-1_4\webapps\axis\WEB-INF\web.xl) to your projetc web.xml

4-set Folwing path in your seysetn Enviorment Veriables.

AXIS_HOME=D:\axis-1_4

AXIS_LIB=%AXIS_HOME%\lib

AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar

5-Then Simple open command prompt and us any of follwing command to depoly the web servcie

 

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -1http://localhost:8081/HyperCupSample/servlet/AxisServlet deploy.wsdd

or

D:\axis-1_4\lib>java -cp activation.jar:axis-ant.jar:axis.jar:axis-schema.jar:commons-discovery-0.2.jar:commons-logging-1.0.4.jar:jaxrpc.jar:log4j-1.2.8.jar:saaj.jar:wsdl4j-1.5.1.jar:. org.apache.axis.client.AdminClient -1http://localhost:8080/HyperCupSample/servlet/AxisServlet deploy.wsdd

 

Enjoyyyyyyyyyyyyyy!!!!

if you find some problem then simply email me and if your like this short taturail then please comment something.thanks

Javascript calendar

Posted: September 17, 2008 in Uncategorized

i have used this javascript calendare on my web pag..its good

check this link to follow

http://ui.jquery.com/repository/latest/demos/functional/#ui.datepicker

http://docs.jquery.com/UI/Datepicker

if you want to get path where calss is located ..here is code

URL url =getClass().getProtectionDomain().getCodeSource().getLocation();

hop its helpfull

write here if you feel its helpful

cherrrrrrrrrrzzzzzzzzzzzzz

How to add veriable/enviorment variabls in web application.these are the variable you can access anywhere in project

here is solution

addfollwing tag to web xm…and given values of your own choics

<env-entry>
    <env-entry-name>webmasterEmail</env-entry-name>
    <env-entry-value>admin@domain.com</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

you can access in a code by this

// Get a handle to the JNDI environment naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");
// Get a single value
String webmasterEmail = (String)env.lookup("webmasterEmail");

thats It ...enjoyyyyyyyy

here is the helping uRL

http://wiki.metawerx.net/wiki/Web.xml.EnvEntry

comment here if it is useful for you ;) 

get Server Context Path

Posted: September 1, 2008 in Uncategorized

Q:how take server(tomcat, Jboss)context path for any work e:g file upload.

A:here is the solution
make a class of any name and implements ServletContextListener(its servlet calss)

write body of contextInitialized(ServletContextEvent event) method

like this

public void contextInitialized(ServletContextEvent event) {

yourUtilitiesClass.FILES_PATH = event.getServletContext().getRealPath(””) + File.separator + “files” + File.separator;
yourUtilitiesClass.IMAGES_FILES_PATH = event.getServletContext().getRealPath(””) + File.separator + “images” + File.separator;
yourUtilitiesClass.MY_RATES_XML_PATH = event.getServletContext().getRealPath(””) + File.separator + “flash” + File.separator;
}

add follwing tag in the web.xml of you web application and give your class name in the listener-class tag (that you write above)

<listener>
<listener-class>com.myproject.portal.utilities.ContextListener</listener-class>
</listener>

now what happend
on every event on your web application server.xml call the listen and set the paths in your static(calss level) variable so you can use any where in the project.

this is the sample calss

import java.io.File;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextListener implements ServletContextListener {

public ContextListener() {
super();
}

public void contextInitialized(ServletContextEvent event) {
yourUtilities.FILES_PATH = event.getServletContext().getRealPath(“”) + File.separator + “files” + File.separator;
yourUtilities.IMAGES_FILES_PATH = event.getServletContext().getRealPath(“”) + File.separator + “images” + File.separator;
yourUtilities.VOPIUM_RATES_XML_PATH = event.getServletContext().getRealPath(“”) + File.separator + “flash” + File.separator;
}

public void contextDestroyed(ServletContextEvent event) {

}

}

cheeerrrrrrrrs!!!

DBCp connection!

Posted: August 13, 2008 in Uncategorized

1.create your own context.xml file and put in Yourapp/META-INF/ location

its contents should be
<Context docBase=”Tracker” path=”/Tracker”>

<Resource name=”jdbc/tracker” scope=”Shareable” type=”javax.sql.DataSource”
factory=”org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory”
url=”jdbc:mysql://localhost:3306/tracker?autoReconnect=true”
driverClassName=”com.mysql.jdbc.Driver”
username=”root”
password=”admin”
maxIdle=”5″
maxActive=”50″

/>
2.Then make entry in web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/tracker</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

3.Then place mysql-connector-java-3.1.13.jar in TOMCAT_HOME/common/lib folder
4.place jstl.jar.standar,jar in TOMCAT_HOME/common/lib folder/

or

in you webInf/lib folder(this is optional stepp if u using jstl)

5. use any where with code
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup(“java:comp/env/jdbc/tracker”);
connection = ds.getConnection();

Note:

-download Connector/J 3.0.11-stable (the official JDBC Driver)

-Downlaod jstl lib(standard.jar, jstl.jar agian this is optional)

you can email me for jdbc connector and JStl lib at aijaza.b@gmail.com

for refrence also check that link

http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

Q.how to send an configration SMS(like mobile operators send GPRS setting, its an configration SMS)

A.configration SMS is not a simple text sms.you have to send your message using  Over The Air(OTA) or Open Mobile Alaince (OMA).

you can download OTA spacification document from there but first you have to make a login to nokia forum.open the link make you registration the download the pdf file

http://www.forum.nokia.com/info/sw.nokia.com/id/3377eb33-cb1d-490e-a1cb-8735d785e1d3/OTA_settings_general_7_0.pdf.html

you can also download OMA specification

http://www.forum.nokia.com/info/sw.nokia.com/id/8d85ad81-5a9b-40fe-bf2a-d567dfd755ec/Series_40_DP_2_0_OMA_Client_Provisioning_v1_3_en.pdf.html

-i wanna send configration sms for synch settings . for that i read the these above spacification(parameters required).
-then i create an xml according to OMA/OTA spacification.
-and concert this xml to wbxml

how convert xml to wbxml
there is two solution for that, you can find two APIs on the internet
-wbxml this is c++ library
can be downlaoded for here http://libwbxml.aymerick.com/
-kxml this is java library
can be donwlaod from here http://kxmlrpc.objectweb.org/software/downloads/

i converted my xml to wbxml using kxml api.here is code

package test.kxml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.kxml.io.AbstractXmlWriter;
import org.kxml.kdom.Document;
import org.kxml.parser.XmlParser;
import org.kxml.wap.WbxmlWriter;

public class Converter {

public static void main(String args[]){

byte[] bytes=null;

try {
File file = new File(“D:\\xml\\OMA2.xml”);

InputStream is = new FileInputStream(file);

// Get the size of the file
long length = file.length();

// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}

// Create the byte array to hold the data
bytes = new byte[(int)length];

// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}

} catch (Exception e) {
// TODO: handle exception
}

try {
FileOutputStream fOutStream = new FileOutputStream(“D:\\xml\\OMA2.wbxml”);
// Construct an InputStreamReader on ByterArrayInputStream.
InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(bytes));

// Make parser using this Reader.
XmlParser parser = new XmlParser(in);

// Construct a DOM Document to parse XML.
Document document = new Document ();
document.parse(parser);
in.close();

// Make an OutputStream on which WbxmlWriter can write.
ByteArrayOutputStream out = new ByteArrayOutputStream();
//AbstractXmlWriter writer = new WbxmlWriter(out);

AbstractXmlWriter writer = new WbxmlWriter(fOutStream);
document.write(writer);
writer.close();

System.out.println(“————-”+out.toString());
//out.writeTo(fOutStream);
// Get WBXML from this stream.
bytes = out.toString().getBytes();

out.close();

}//try
catch (Exception e) { e.printStackTrace(); }

}//End of MAin

}

i also have the exe file of wbxml lib(complied and convert to exe). you can run it on command promt.
place the xml file and related dll in a folder the open the command promt and point to that folder and the write that command
xml2wbxml.exe
then write this command
xml2wbxml -o output.wbxml input.xml
xml2wbxml -k -n -v 1.1 -o output.wbxml input.xml

then your file convertd to wbxml. for wbxml exe file you can email me at aijaz.a.b@gmail.com

wbxml is short form of xml mean all xml data converted to binary so file size get commpressed. when you convert the xml to wbxml you will see all tags are converted into binary form but the text will remain the same.
then convert it to Hexadecimal form and send it to your mobile.

downlaod hex-editor form that link http://www.brothersoft.com/free-hex-editor-download-40299.html, it will convert wbxml to hex.

how to send to the mobile you have to read your gate spacificatio. i am using unwire gateway so for that i set the “type” varibale to “raw” in the url and set the header.i didnt able to get sms on mobile therefor i contacted to my gateway provider but these all step really help to send the configration SMS.

the sms you will send it will have two parts
first is UDH head
what is UDH head read this link

http://dev.mobi/article/binary-sms-sending-rich-content-devices-using-sms

the samlple i send like this
(UDH header)Header1: 0B0504C34FC0020003B90201
(hexdecimal form of wbxml)Body1: 010604039481EA00010045C60601871245018713110331302E392E382E37000187146101872111032B333637303730303039323700018722700187231103766F6461776170000187241103766F646177617000018728730187296C0101C606018712410187131103323232000187146001871A11032B33363730393939363530
Header2: 0B0504C34FC0020003B90202

hope this all will help

also read this link .might be it will be helpful

http://dev.mobi/article/email-configuration-sms

this is the example that i tryied with some gatway

i used this url of my gateway provider to send sms on mobile

http://mobile.unwire.dk/java/servlet/smspush?user=test&password=test&to=92322111111&smsc=dk.tdc&udh=0605040B840000&text=02056A0045C60C037777772E6465762E6D6F62692F69735F66756E2E68746D6C0011033635333200070103446576446F744D6F62692069732046756E2021000101&phoneno=92322111111&mediacode=ijazcompnay&appnr=321&from=ijax&type=raw&

see type variable

cheeeeeeeeeeeeeeerz