Tuesday, July 29, 2014

Reloadble Properties using Apache Commons Configuration

import java.io.File;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
public class ConfigUtil {
    private static Logger log = Logger.getLogger(ConfigUtil.class);
    private static PropertiesConfiguration configuration = null;
    private static final String PROPERTY_FILENAME = "config.properties";
    private static String RELOADBLE_PROPERTY_FILENAME = null;
    private static PropertiesConfiguration reloadConfiguration = null;
    static
    {
        try {
            configuration = new PropertiesConfiguration(PROPERTY_FILENAME);
            log.info("Loading property file from classpath :");
            RELOADBLE_PROPERTY_FILENAME = ConfigUtil.getProperty("RELOAD_PROERTY_FILENAME");
             if(null != RELOADBLE_PROPERTY_FILENAME){
                File f = new File(RELOADBLE_PROPERTY_FILENAME);
                if(f.exists()){
                    reloadConfiguration = new PropertiesConfiguration(RELOADBLE_PROPERTY_FILENAME);
                    reloadConfiguration.setReloadingStrategy(new FileChangedReloadingStrategy());
                    log.info("Loading reloadble property file from path : "  + RELOADBLE_PROPERTY_FILENAME);
                }else{
                    log.info("Error: reloadble property file not Found : "  + RELOADBLE_PROPERTY_FILENAME);
                    }
            } // end of if
          } catch (ConfigurationException e) {
          //  e.printStackTrace();
              log.debug(e.getStackTrace().toString());
        }
    }
    public static synchronized String getProperty(final String key)
    {
        if(reloadConfiguration != null){
            if(getReloadableProperty(key) != null){
                return getReloadableProperty(key);
            }
            else
                return  configuration.getString(key);
        }
        else
            return  configuration.getString(key);
    }
    private static synchronized String getReloadableProperty(final String key)
    {
        return  reloadConfiguration.getString(key);
    }
    public static void main(String[] args) {
        System.out.println(RELOADBLE_PROPERTY_FILENAME);
    }
}
=========================================
RELOAD_PROERTY_FILENAME=C:/config.properties
#DB Properties
db-url=jdbc:db2://127.0.0.1:3306/test
db-username=jini
db-password=123
=========================================
import com.jini.util.ConfigUtil;
public class zzZZTestReloading {
    public static void main(String[] args)
    {
        while(true)
        {
            try
            {
                Thread.sleep(2000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
                System.out.println(ConfigUtil.getProperty("DATASOURCE_ENABLE=TRUE"));
        }
    }
}
 

LOG4JDBC (JDBC proxy driver for logging SQL)

# Click => Here <= for help
########################################
# log4jdbc-log4j
# jdbc.driverClassName=net.sf.log4jdbc.DriverSpy
# Add to DB url=jdbc:log4
########################################
# Below lines in log4jdbc.properties file
log4jdbc.drivers=com.ibm.db2.jcc.DB2Driver
log4jdbc.auto.load.popular.drivers=false
log4jdbc.dump.booleanastruefalse=true
log4jdbc.trim.sql.extrablanklines=false
log4jdbc.dump.sql.addsemicolon=true
log4jdbc.dump.sql.select=true
log4jdbc.dump.sql.insert=true
log4jdbc.dump.sql.update=true
log4jdbc.dump.sql.delete=true
log4jdbc.dump.sql.create=true
log4jdbc.debug.stack.prefix=com.jini.dao
########################################
# log4jdbc-log4j2
# jdbc.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy 
# Below line in log4jdbc.log4j2.properties
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
########################################
# Below line in log4j.properties 
log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO,sql,stdout
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
log4j.logger.jdbc.resultsettable=INFO,sql,stdout

log4j.additivity.jdbc.sqlonly=false
log4j.additivity.jdbc.sqltiming=false
log4j.additivity.jdbc.audidt=false
log4j.additivity.jdbc.resultset=false
log4j.additivity.jdbc.connection=false
log4j.additivity.jdbc.resultsettable=false

## Daily rolling file appender for JDBC Log
#the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sql.File=logs/sql.log
llog4j.appender.sql.DatePattern='.'dd-MM-yyyy
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=%-5p [%d{DATE} %c]: %m%n
log4j.additivity.sql=false
log4j.appender.sql.Append=false
########################################

LOG4J

# Root logger option
log4j.rootLogger=INFO,stdout,file

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -  %m%n`
#log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#log4j.appender.stdout.layout.ConversionPattern=%p %t %c - %m%n

# Daily rolling file appender for PROJECT
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/logs.log
log4j.appender.file.DatePattern='.'dd-MM-yyyy
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n
#log4j.appender.file.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.jdbc.sqlonly=OFF
log4j.logger.jdbc.sqltiming=INFO,sql,stdout
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
log4j.logger.jdbc.resultsettable=INFO,sql,stdout

log4j.additivity.jdbc.sqlonly=false
log4j.additivity.jdbc.sqltiming=false
log4j.additivity.jdbc.audidt=false
log4j.additivity.jdbc.resultset=false
log4j.additivity.jdbc.connection=false
log4j.additivity.jdbc.resultsettable=false

## Daily rolling file appender for SQL Log
#the appender used for the JDBC API layer call logging above, sql only
log4j.appender.sql=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sql.File=logs/sql.log
llog4j.appender.sql.DatePattern='.'dd-MM-yyyy
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=%-5p [%d{DATE} %c]: %m%n
log4j.additivity.sql=false
log4j.appender.sql.Append=false

## Spring JDBC logging
#log4j.logger.org.springframework.jdbc=DEBUG
#log4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUG, file
##log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE, file

## Spring Transaction logging
#log4j.logger.org.springframework.transaction=INFO
#log4j.logger.org.springframework.transaction.interceptor=TRACE
##log4j.logger.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG
##log4j.logger.org.springframework.orm.jpa=INFO

## Hibernate Transaction logging
#log4j.logger.org.hibernate.SQL=DEBUG
#log4j.logger.org.hibernate.engine.transaction=DEBUG
#log4j.logger.org.springframework.orm.hibernate3.HibernateTransactionManager=DEBUG

# BoneCP Connection Pooling logging
#log4j.category.com.jolbox=DEBUG,stdout

Websphere URL

Websphere Datasource Tracing

Disable & Enabling tracing using properties file
#  Click => Here  <= for details
# -Ddb2.jcc.propertiesFile=C:/jcc.properties
db2.jcc.traceFile=C:/EMD/jcc.log
db2.jcc.traceOption=1
db2.jcc.traceLevel=3
db2.jcc.traceFileSize=20971520
db2.jcc.traceFileCount=4
db2.jcc.traceFileAppend=true

Using JDBC URL:
jdbc:db2://localhost:50000/sample:traceDirectory=/tmp;traceFile=jcctrace.log;traceFileCount=5;traceFileSize=20971520;traceLevel=-1;

XML Encoder and Decoder

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class OXMUtil {
    public static void main(String args[]) {
        ArrayList testArrayList = new ArrayList();
        testArrayList.add(new String("Test"));
        testArrayList.add(new String("Test1"));
        String testString = objectToXML(testArrayList);
        System.out.println("XML String .....");
        System.out.println(testString);
        ArrayList finalArrayList = (ArrayList) XMLToObject(new String(
                testString));
        System.out.println("Actual Object.....");
        for (int i = 0; i < finalArrayList.size(); i++)
            System.out.println(finalArrayList.get(i).toString());
    }
    public static String objectToXML(Object voObj) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        XMLEncoder xmlEncoder = null;
        try {
            xmlEncoder = new XMLEncoder(new BufferedOutputStream(stream));
            xmlEncoder.writeObject(voObj);
            xmlEncoder.close();
            return stream.toString("UTF-8");
        } catch (Exception e) {
            System.out.println("Error while Converting from object to XML : "
                    + e.getMessage());
            xmlEncoder.close();
        }
        return null;
    }
    public static Object XMLToObject(String dataXML) {
        XMLDecoder xMLDecoder = null;
        try {
            xMLDecoder = new XMLDecoder(new ByteArrayInputStream(
                    dataXML.getBytes("UTF-8")));
            Object voObj = xMLDecoder.readObject();
            xMLDecoder.close();
            return voObj;
        } catch (Exception e) {
            System.out.println("Error while Converting XML to Object : "
                    + e.getMessage());
            xMLDecoder.close();
        }
        return null;
    }
}