Sie sind auf Seite 1von 8

Title: Developer Guidelines

Abstract:
This purpose of this document is to serve as a developer guide developing J2EE, Portal and Web
Service applications and for using Srest Common Framework/Principles based on most stable and
secure architectures framework, which make developing portal, j2ee, webservices and java applications
easy. The goal is to develop and use core framework like Srest Framework.

Table of Contents
1.0 Using Property File..............................................................................................................................2
2.0 Working with Database Connection.....................................................................................................2
3.0 Logging................................................................................................................................................4
3.1 Generic............................................................................................................................................4
3.2 DEBUG...........................................................................................................................................4
3.3 INFO................................................................................................................................................4
3.4 ERROR............................................................................................................................................5
3.5 FATAL.............................................................................................................................................5
4.0 Exception Handling..............................................................................................................................6
4.1 Rules........................................................................................................................................6
4.1 Error Codes..............................................................................................................................7
4.2 Exception Handling Flow........................................................................................................7

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

1.0 Using Property File

All property value should be read like


PropReader.getProperty("request_email", "SrestSite");
SrestSite - refer to property file which should be present in <tomcat home>/lib/property

Create your own application specific propery and place it there. Anything dynamic should be placed in
property files.

To read property from common srest property file - SRESTCommon.properties (which is also present
in <tomcat home>/lib/property directory) use
PropReader.getProperty("Property Name");

2.0 Working with Database Connection

Every database needs to have table called test. This table is used to test for stale connection.
create table test(no int(1));

Copy SrestFrame.jar, mysql-connector-java-5.0.3-bin.jar into $CATALINA_HOME/lib directory. Make


sure to reference SrestFrame.jar in your project. Using the J2EE_LIB variable defined above.

Copy property folder and log4j.properties in $CATALINA_HOME/lib directory.

in $CATALINA_HOME/conf/context.xml add the datasource entry as shown below

<Resource name="jdbc/srestus" auth="Container" type="javax.sql.DataSource"


maxActive="100" maxIdle="30" maxWait="10000"
username="srestus" password="secret" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/srestus?autoReconnect=true"/>

Get conection by calling ConnManager.getConnection - this method return auto commit connection.
# getTransactionConnection - this method return transaction connection. Make sure to call commit if
everything is ok. Call rollback if we get any exceptions.

# Always call close on database connection. Usually this is done in finnally method. Otherwise
connection leak happens and eventually server or applications will crash.

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

SAMPLE CODE ON HOW TO USE IT


Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null; // Well prepared statement is preferred way.
// in some cases Statement can be used.
try {
conn = ConnManager.getConnection("srestus", "userId");
// Userid is logged in user id. Pass SrestConstant.ANON for unauthenticated user.
// do your prccessing
if(logger.isDebugEnabled())logger.debug("Successfully got db connection");
}
catch( CommonException ce) {
throw ce;
}
catch (Throwable e) {
throw new CommonException(ErrorConstants.INTERNAL_ERROR, e);
// Only throw CommonException which should be dealt in front end.
// Potentially Controller Servlet/action classes.
}
finally {
DataUtils.closeAll(rs, stmt, conn); // null is allowed
// Do not use conn.close() directly
}

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

3.0 Logging
Copy property folder and log4j.properties in $CATALINA_HOME/lib directory.
Sometime it complains that log4j system was not initialized. Try putting the following the line in init of
your servlet.
PropertyConfigurator.configure(PropReader.getProperty("log4j_file"));.
Do not have local log4j.property. Do not try to change place of log or way the it is structured. Add your
module logger like

log4j.logger.srest.cashbook=DEBUG

3.1 Generic
Always use org.apache.log4j.Logger class for logging any message. Get an instance of the class using
its getLogger () method, pass in Module name or Group Name like (srest.zip, srest.admin etc). Never
use class name to get an instance of the logger. Never use System.out.println to log message. Define
constant s in ur module constant file.
Common utility kind of java classes should use SrestConstants.ROOT_LOGGER
While logging use the appropriate level (debug, info, error and fatal). If you have an exception objects
always pass that. Note exception stack trace should be only logged at the end and not in each layer.
Logging is recommended to be done at least at the entry (Entry) of each method, each exit (Exit) of a
method.
if(logger.isDebugEnabled())logger.debug(SrestConstants.ENTRY);
if(logger.isDebugEnabled())logger.debug(SrestConstants.EXIT);

3.2 DEBUG

The DEBUG Level designates fine-grained informational events that are most useful to debug an
application. Always check if debug level is enabled before the debug statement.
Ex:
if(logger.isDebugEnabled())logger.debug("debugging");

3.3 INFO

The INFO level designates informational messages that highlight the progress of the application at
http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748
Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

coarse-grained level. Check if info level is enabled before the info statement.
Ex:
if(logger.isInfoEnabled())logger.info("returned from WS call");

3.4 ERROR
The ERROR level designates error events that might still allow the application to continue running.
Ex:
logger.error("Got exception while trying to format", exception);

3.5 FATAL
The FATAL level designates very severe error events that will presumably lead the application to abort
or leave some portion of the system unavailable for effective use. Use this level if you want to notify
concerned parties that some serious error has happen. This level can be tied up with SMS or Email
notification or Automatic Support site update; as per the application requirements.
logger.fatal("Error connecting to Vignette Sub-System"); //never send exception stack trace
logger.error("Error connecting to Vignette Sub-System", exception);

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

4.0 Exception Handling

Currently only CommonException class is available for exception handling. Always pass error code.
Many error code are defined in ErrorConstants.

4.1 Rules
 Do not catch an exception unless it can be handled. Let the next level catch it. Do catch and
handle the exception at the lowest level possible.
 Do not throw exceptions from the finalize method.
 Never put a return in finally block
 Always have a catch block at the highest level and use a default catch clause in catch blocks at
the highest level.
 Catch blocks shouldn’t be empty. If you are simply ignoring an exception you should have a
comment explaining why and possibly log output.
 If you are dealing with the exception Log error using standard logging utility method (do not
use printStackTrace() or System.out.println()),.
 Use several small try-catch blocks to catch distinct exceptions. Wrap all non-trivial methods like
this:
try {
// method body
}
catch ( your specific exception here ){
// Log exception, handle it here
}
// Repeat above block for each specific exception to catch
catch (Throwable th) {
// Log catch all exception message here, handle it here
// logger.error(“messages”, th);
}

 Do not allow null object references to propagate if null is not expected; don’t assume
another method will handle it. Trap them, log an error, and gracefully return from the method.

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

4.1 Error Codes


All error codes are defined in the srest_error.properties file. Each entry in the file should be keyed by
the error code. There is also a file called ErrorConstants, this also need to be updated. Each
corresponding project may have it own equivalent of these two files plus the two common files.

4.2 Exception Handling Flow

1. A DAO catches a SQL exception/Error in case one is encountered.


2.The programmer determines the appropriate error code to use or defines a new one in the
xyz_error.properties file.
3.A new CommonException is thrown using the chosen error code and/or cause (Throwable type).
4.The service calling the DAO does catches the CommonException and throws it unless he has
better business case and wants to override the error code.
5.UI Java, Catches the exception, first logs it and then displays the error message. It gets the
message from property file using the error code and displays it in the browser.

Author: Parvin, Enterprise Architect at Srest Information Technology Pvt Ltd. He has more than 10
years of architecture experience building and architecturing mission critical applications for many
fortune 500 clients like Citibank, BlueCross, IBM, Cap Gemini Ernst Young and Morgan Stanley. He
can be reach at architect@srest.biz.

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed
Title: Developer Guidelines

http://srest.com/training Mandsaur: 07422404452, Mumbai: 022-29661118, US: 732.325.3748


Don't be a victim on economy be a torch bearer!! training@srest.in / training@srest.biz
Copyright @ Srest Information Technology Pvt. Ltd. - 2008 - http://srest.net/srest
All rights reserved. Distribution is allowed as long as no text, images or copyright message is removed

Das könnte Ihnen auch gefallen