Wednesday, February 24, 2010

Creating log4j (logging) for a EJB\WEB project




If we have a big project with both Web and EJB projects and a EAR project to link them and if we want to implement logging, in this case here is the approval we need to take.

In the EAR project, under META-INF folder, we need to create a folder called "APP-INF" under this folder, we need to place the log4j.jar file.

Once we add the jar file, we need to include a reference to this jar file from Web and EJB project.

Right click on the project "OCEDecisionEJB" and "OCEDecisionWeb", properties, Java build path, Libraries tab, click "Add JARs" button and select the log4j.jar from EAR project.

As we can see in the snapshot, the log4j.jar in the EJB project refers to the EAR project. (OCEDecisionEAR/META-INF/APP-INF).

Once this is done, we need to open the META-INF\MANIFEST.MF file in both EJB and WEB projects. For Web project it should be under WebContent\META-INF\MANIFEST.MF

MANIFEST.MF = This is basically a "Jar Dependency Editor". We edit this file and add
this line.

Class-Path: META-INF/APP-INF/log4j.jar

This means add the log4j jar file in the classpath.

Once this done, we can verify the details by right click on EJB and Web projects, "J2EE Module Dependencies". We can see the dependency defined here.

Creating jar (war) - Command

This sun link will he help us to understand the basics of creating a jar file and the commands involved.


When we open a directory in C drive, say folder name is myProject, and we have look of files in it.

c:>cd myProject;

We can see 2 things. A single dot (.) and a double dot (..) The single dot means its the current directory and double dot means parent directory.

c:\myProject> cd .

With this command, it is pointing to the current directory and nothing will happen.

c:\myProject> cd ..

With this command, it is pointing to the parent directory and it will move one level up and reach its parent directory.

c:\>

With that understanding we can now look at creating a war file.

Web projects are packaged as war while EJB projects are packaged as ear.

Web Project = myProject.war
EJB Project = myProject.ear

c:\myProject\webProject\Webcontent\jar -cvf oce.war .

jar command

c = create
v = verbose (like logger, which prints all the details on the command prompt)
f = File name (Here all the files will packed under the name we give, ex: oce.war)
dot (.) = This dot means, pack everything under this folder.

There is one important thing we should note here. We should pack (creating .war file) things within the "Webcontent" folder rather "WebProject" folder.

Wednesday, September 02, 2009

Websphere Server Ports

Default (Host) listening port of webshpere is

9081, 80, 9444, 5060, 5061, 443

http://localhost:9081/<context-root>

Admin (Host) listening port of webshpere is

9061, 9044

Weblogic Server Ports

The default listening port of weblogic is 7001.

http://localhost:7001/<context-root>

Example:

http://localhost:7001/GTEMWeb/

Where to find the Context Root?

Context Root can be found in META-INF folder "application.xml" file.

For deploying the application locally
http://localhost:7001/console/login/LoginForm.jsp

Tuesday, April 01, 2008

Spring framework tutorial

Here is a link to learn about Spring framework.

Friday, January 04, 2008

Log4j Properties File

# The possible values here are debug, info, warn, error, fatal

log4j.rootLogger=info,R
log4j.logger.com.abc.xyz.projectName.action=debug,A1
log4j.logger.com.abc.xyz.projectName.bean=debug,A2
log4j.logger.com.abc.xyz.projectName.form=debug,A3
log4j.logger.com.abc.xyz.projectName.dao=debug,A4
log4j.logger.com.abc.xyz.projectName.dto=debug,A5
log4j.logger.com.abc.xyz.projectName.interfaces=debug,A6
log4j.logger.com.abc.xyz.projectName.services=debug,A7
log4j.logger.com.abc.xyz.projectName.servlets=debug,A8
log4j.logger.com.abc.xyz.projectName.utils=debug,A9

##ConsoleAppender (writes to system console).

log4j.appender.R=org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A3=org.apache.log4j.ConsoleAppender
log4j.appender.A4=org.apache.log4j.ConsoleAppender
log4j.appender.A5=org.apache.log4j.ConsoleAppender
log4j.appender.A6=org.apache.log4j.ConsoleAppender
log4j.appender.A7=org.apache.log4j.ConsoleAppender
log4j.appender.A8=org.apache.log4j.ConsoleAppender
log4j.appender.A9=org.apache.log4j.ConsoleAppender

# Uses PatternLayout.Dont change this

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.layout=org.apache.log4j.PatternLayout
log4j.appender.A4.layout=org.apache.log4j.PatternLayout
log4j.appender.A5.layout=org.apache.log4j.PatternLayout
log4j.appender.A6.layout=org.apache.log4j.PatternLayout
log4j.appender.A7.layout=org.apache.log4j.PatternLayout
log4j.appender.A8.layout=org.apache.log4j.PatternLayout
log4j.appender.A9.layout=org.apache.log4j.PatternLayout

##Defines the way comments are written to the log file, for e.g. %d tells the logger to write the date
# for a full list see the log4j manual. %p is the priority (debug, info etc as defined above)

log4j.appender.R.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L %m%n
log4j.appender.A1.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Action ---> %m%n
log4j.appender.A2.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Bean ---> %m%n
log4j.appender.A3.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Form ---> %m%n
log4j.appender.A4.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Dao ---> %m%n
log4j.appender.A5.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Dto ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Interfaces ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Services ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Servlets ---> %m%n
log4j.appender.A6.layout.ConversionPattern=%d{MM-dd HH:mm:ss} %-5p %c{1}:%L GTEM Utils ---> %m%n


# Additivity Flag

log4j.additivity.com.abc.xyz.projectName.action=false
log4j.additivity.com.abc.xyz.projectName.form=false
log4j.additivity.com.abc.xyz.projectName.bean=false
log4j.additivity.com.abc.xyz.projectName.dao=false
log4j.additivity.com.abc.xyz.projectName.dto=false
log4j.additivity.com.abc.xyz.projectName.interfaces=false
log4j.additivity.com.abc.xyz.projectName.services=false
log4j.additivity.com.abc.xyz.projectName.servlets=false
log4j.additivity.com.abc.xyz.projectName.utils=false
log4j.additivity.jsp_servlet=false

Logger Implementation

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class ApplicationNameLogger {

Example:
// public class ABCLogger {


private static Logger logger;

public static boolean TRACING = false;
public static final String DEBUG = "DEBUG";
public static final String INFO = "INFO";
public static final String WARNING = "WARNING";
public static final String ERROR = "ERROR";

public static void ApplicationNameLog(Class className, String logMsg, String priority)
{
logger = Logger.getLogger(className);
if(priority.equalsIgnoreCase(DEBUG) && TRACING)
{
logger.debug(logMsg);
}
else if (priority.equalsIgnoreCase(INFO) && TRACING)
{
logger.info(logMsg);
}
else if (priority.equalsIgnoreCase(WARNING) && TRACING)
{
logger.warn(logMsg);
}
else if (priority.equalsIgnoreCase(ERROR) && TRACING)
{
logger.error(logMsg);
}
}

public static void setTracing(boolean tracingValue) {
TRACING = tracingValue;
}

}


Invoking the Logger from Java classes.


public class ABCAction extends Action {

public static final Class CLASS_NAME = ABCAction.class;

public void getCountryName {

ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, " Enter into getCountryName () ", ApplicationNameLogger.INFO);

try {

---
--
--
} catch(Exception exp) {
ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, " In getCountryName ()->Exception: "
+ exp.getMessage(), ApplicationNameLogger.ERROR);
}
ApplicationNameLogger. ApplicationNameLog(CLASS_NAME, "Exit from getCountryName ()",
ApplicationNameLogger.INFO);
}

Loading log4j properties file on application start-up.

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;

public class ApplicationNameLoggerServlet extends HttpServlet {
public void init() {
try {
InputStream is = this.getClass().getResourceAsStream(
"log4j.properties");
Properties p = new Properties();
p.load(is);
PropertyConfigurator.configure(p);
} catch (Exception ex) {
System.out.println("Exception occurred during log4j initialization");
}
}
}

Web.xml mapping for loading the logger Servlet on application start-up.

<servlet>
<servlet-name> ABCLoggerServlet </servlet-name>
<servlet-class>com.abc.xyz.mno.applicationName.servlets.ABCLoggerServlet</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>

Monday, October 29, 2007

web.xml

This is how a typical web.xml file will look like for a standard J2EE application which has been designed and constructed on a struts framework

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>ABC Application</display-name>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml,
/WEB-INF/struts-config_module1.xml
/WEB-INF/struts-config_module2.xml
</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>LoadApplicationServlet</servlet-name>
<servlet-class> com.abc.xyz.projectName.servlets. LoadApplicationDataServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ApplicationLoggerServlet</servlet-name>
<servlet-class> com.abc.xyz.projectName.servlets. ApplicationLoggerServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>



<filter>
<filter-name>security</filter-name>
<filter-class> com.abc.xyz.projectName.security.SecurityFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>security</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>


<!-- ======================================================== -->
<!-- The Welcome File List. -->
<!-- ======================================================== -->
<welcome-file-list>
<welcome-file>home.do</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/tld/struts/taglib.tld</taglib-uri>
<taglib-location>/WEB-INF/taglib.tld</taglib-location>
</taglib>

</jsp-config>
</web-app>

Monday, July 17, 2006

JDBC Drivers

Types of JDBC technology drivers

JDBC technology drivers fit into one of four categories:

1. A JDBC-ODBC bridge provides JDBC API access via one or more ODBC drivers. Note that some ODBC native code and in many cases native database client code must be loaded on each client machine that uses this type of driver. Hence, this kind of driver is generally most appropriate when automatic installation and downloading of a Java technology application is not important. For information on the JDBC-ODBC bridge driver provided by Sun, see JDBC-ODBC Bridge Driver.
2. A native-API partly Java technology-enabled driver converts JDBC calls into calls on the client API for Oracle, Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this style of driver requires that some binary code be loaded on each client machine.
3. A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products.
4. A native-protocol fully Java technology-enabled driver converts JDBC technology calls into the network protocol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. Since many of these protocols are proprietary the database vendors themselves will be the primary source for this style of driver. Several database vendors have these in progress.

Tuesday, June 27, 2006

JUnit Notes and Tutorial:

Home Page

Using JUnit With Eclipse IDE

JUnit FAQ (Has everything you need to undertand on Unit Tesing)

JUnit Test Infected: Programmers Love Writing Tests

Documentation

Cooks Tour

JUnit Articles



CONTACT

p> You can reach me @ My EMail ID

Powered by Blogger