Archive

Archive for July, 2008

How to create excessive log for Tomcat?

July 15, 2008 Leave a comment

1. create log4j.properties and place it under $CATALINA_HOME/lib.

  log4j.rootLogger=DEBUG, stdout, R 
  log4j.appender.R=org.apache.log4j.RollingFileAppender
  log4j.appender.R.File=${catalina.home}/logs/tomcat.log
  log4j.appender.R.MaxFileSize=10MB
  log4j.appender.R.MaxBackupIndex=10
  log4j.appender.R.layout=org.apache.log4j.PatternLayout
  log4j.appender.R.layout.ConversionPattern=%p %t %c – %m%n

  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  log4j.appender.stdout.layout.ConversionPattern=%p %t %c – %m%n

2. place log4j-1.2.8.jar in $CATALINA_HOME/lib.  (Search in your computer, you may find one)

3. Build the additional component commons-logging (This is not regular commons-logging from apache commons project) . To build it, you require ant build script named extras.xml which is there in Tomcat source bundle.

Replace $CATALINA_HOME/bin/tomcat-juli.jar with output/extras/tomcat-juli.jar.  (which is in the bin folder)

Place output/extras/tomcat-juli-adapters.jar in $CATALINA_HOME/lib.

Start Tomcat
3. Add the following properites build.properties.default
proxy.use=on
proxy.host=cbp-ca-1.link.net
proxy.port=8080

4. CREATe a folder named “classes” under “apache-tomcat-6.0.14-src\output\”

5. log4j-1.2.8.jar under lib folder

6. log4j.properties placed under lib

Categories: Uncategorized

Hibernate :=> java.sql.SQLException: ORA-00907: missing right parenthesis

July 15, 2008 Leave a comment

 java.sql.SQLException: ORA-00907: missing right parenthesis

If you ever encountered the above exception using Hiberate… Better remove the catalog (database name) information from your hibernate.cfg.xml.

At least it solved my issue.

Categories: Uncategorized

POJO – Hibernate Code generate using Hibernate Tools.

July 15, 2008 Leave a comment

Download and place (unzip) the following in the same folder.

1. hibernate-3.2.5.ga.tar.gz
2. HibernateTools-3.2.2.Beta1.zip
3. middlegen-2.1 (Optional – middlegen is unstable, 2.2 still in CVS – No Maintenance)

Unzip all the above in the same directory and use the following ant build.xml and run “ant -v hiberateTool” – If your ant is already setup properly, then create hibernate.properties and change hibernate-reverse-engineering.xml’s schema and catalog.

Here catalog was oracle’s database name.

4. Create build.xml as following (hope your already familiar with ANT”)

____________________________________________________

<project name=”Hibernate3″ default=”jar” basedir=”.”>

<property file=”build.properties”/>

    <!– fileset definition for the lib directory –>
    <fileset id=”fs.lib” dir=”${dir.lib}”>
        <include name=”**/*.jar” />
    </fileset>
    <fileset id=”ora.lib” dir=”${oracle.driver.dir}”>
        <include name=”**/*.jar” />
    </fileset>
    <fileset id=”hibe.lib” dir=”${hib.dir}”>
        <include name=”**/*.jar” />
    </fileset>
    <path id=”path.lib”>
        <fileset refid=”fs.lib” />
  <fileset refid=”ora.lib” />
  <fileset refid=”hibe.lib” />
 </path>

<taskdef name=”hibernatetool”
         classname=”org.hibernate.tool.ant.HibernateToolTask”
         classpathref=”path.lib” />
  
<target name=”hibernateTool” description=”Generate Hibernate mapping files” >
<!–
  Configuration is not working for Code Gernation
  <hibernatetool destdir=”${build.dir}/generated”>
  <configuration
     configurationfile=”hibernate.oracle.cfg.xml”/>
   <hbmtemplate
     templateprefix=”pojo/”
     template=”pojo/Pojo.ftl”
     filepattern=”{package-name}/{class-name}.java”>
    <property key=”jdk5″ value=”true” />
    <property key=”ejb3″ value=”true” />
   </hbmtemplate>
  </hibernatetool>
–>
 <hibernatetool destdir=”${build.dir}/generated”>
   <classpath>
   <path location=”${build.dir}/classes”/>
   </classpath>
   <jdbcconfiguration propertyfile=”hibernate.properties”  revengfile=”hibernate.reveng.xml” />
  <!– hbm2ddl –>
   <hbm2dao/>  
   <hbm2java/>  
   <hbmtemplate
    filepattern=”{package-name}/I{class-name}Constants.java”
    templatepath=”${etc.dir}/customtemplates”
    template=”myconstants.vm” 
   />
    <hbm2java jdk5=”true”/>
 </hibernatetool>
 
</target>
  
<taskdef name=”hbm2java” classname=”net.sf.hibernate.tool.hbm2java.Hbm2JavaTask”>
 <classpath refid=”path.lib” />
</taskdef>

<taskdef name=”middlegen” classname=”middlegen.MiddlegenTask”>
 <classpath refid=”path.lib” />
</taskdef>

<target name=”hbm2java” description=”Generate Java Pojos from the Hibernate mapping files”>
 <hbm2java output=”${source.generated}” config=”hibernate.cfg.xml”>
  <fileset dir=”${hibernate.source}”>
   <include name=”**/*.hbm.xml” />
  </fileset>
 </hbm2java>
</target>

<!— There were a blog claims that CVS middlegen code works better than middlegne2.1 – so avoid MiddleGen->
<target name=”middlegen” description=”Generate Hibernate mapping files” unless=”middlegen.skip” >
 <middlegen appname=”Hibernate-Spring” prefsdir=”${middlegen.temp}” gui=”false” databaseurl=”${jdbc.url}” driver=”${jdbc.driver}” username=”${database.username}” password=”${database.password}” schema=”${database.schema}” catalog=”${database.catalog}”>
  <table name=”DW_SECURITIES” pktable=”FORUM_KEYS” singular=”forum” plural=”fora”/>
  <hibernate destination=”${source.generated}” package=”${package.name}” javaTypeMapper=”middlegen.plugins.hibernate.HibernateJavaTypeMapper” />
 </middlegen>
</target>

</project>

____________________________________________________

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC “-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd” >

<hibernate-reverse-engineering>

 <schema-selection match-catalog=”databasenameOfOracle”  match-schema=”SchemaNameOfOracle” />

 <!– BIN$ is recycle bin tables in Oracle –>
 <table-filter match-name=”BIN$.*” exclude=”true” />

 <!– Exclude DoNotWantIt from all catalogs/schemas –>
 <table-filter match-name=”DoNotWantIt” exclude=”true” />

</hibernate-reverse-engineering>

____________________________________________________

#Hibernate.properties

## Oracle
hibernate.dialect org.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username db_user
hibernate.connection.password db_user_pwd
hibernate.connection.url jdbc:oracle:thin:@127.12.127.127:1521:db
hibernate.default_schema SCHEMANAME
hibernate.default_catalog DBNAME

____________________________________________________

Create build.properties for build.xml

dir.lib=value
hib.dir=value
oracle.driver.dir=value
build.dir=value
etc.dir=value
# MSJava, Hibernate, Middlegen
hibernate.home=value
hibernate.extensions.home=value
middlegen.home=value
middlegen.temp=value

# Source Folders/Packages
source.generated=value
hibernate.source=value
package.name=value

# Database connection
jdbc.url=value
jdbc.driver=value
database.username=value
database.password=value
database.schema=value
database.catalog=value

Categories: Uncategorized

ThreadPool Example

July 15, 2008 Leave a comment

package org.nikias.concurrent.util.test;

import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorSample {

 private Executor executorEngine;

 public ExecutorSample() {
  executorEngine = new ThreadPoolExecutor(5, 10, 100,
    TimeUnit.MICROSECONDS, new SynchronousQueue<Runnable>());
 }

 public void add(Runnable task) {
  executorEngine.execute(task);
 }

 public static void main(String args[]) {
  ExecutorSample executor = new ExecutorSample();
  for (int i = 0; i < 10; i++) {
   Runnable task = getRunnableTask();
   executor.add(task);
  }
 }

 private static Runnable getRunnableTask() {
  Runnable task = new Runnable() {
   public void run() {
    long d = (long) (Math.random() * 10000);
    System.out.println(“Thread (”
      + Thread.currentThread().getName() + “) Id is : ”
      + +Thread.currentThread().getId()
      + ”  :: Generated a random : ” + d);
    try {
     Thread.sleep(d);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    System.out.println(“Thread (”
      + Thread.currentThread().getName() + “) Id is : ”
      + +Thread.currentThread().getId()
      + ” :: Completes the task”);
   }
  };
  return task;
 }
}

Categories: Uncategorized