The Answer to Life, the Universe, and Everything

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, August 18, 2008

Log4J Log rotate

It's better to log rotate by Java side than using Linux/Unix logrotate.
This is the example for log4J v1.2x (The v1.3 development is stopped by the way.) series , "log4j.xml" file.

By using DailyRollingFileAppender, you can log rotate as the date base.


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--Definition of the File Appender -->
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<!-- Date pattern policy -->
<param name="DatePattern" value=".yyyy-ww" />

<param name="File" value="logs/hibernate.log"/>
<!-- Log append -->
<param name="Append" value="true" />

<!-- Log layout -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %c{1} - %m%n" />
</layout>

<!-- Default log level -->
<param name="threshold" value="info"/>

</appender>

<!--Console log appender -->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<!-- Default log level -->
<param name="threshold" value="info"/>
<!-- log layout -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %c{1} - %m%n"/>
</layout>
</appender>

<root>
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>

Sunday, December 09, 2007

maven2 java version

You might need to specify the compile java version by using generics etc in pom.xml file.

<project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

Friday, November 23, 2007

Eclipse Heap Memory

This is how you can change the memory for Eclipse to use.

#vim $ECLIPSE_HOME/eclipse.ini
--
-vmargs
-Xms64m
-Xmx256m

Tomcat Heap Memory

Here is the how we can change the setting of the memory used for tomcat.

set CATALINA_OPTS="-Xms512m -Xmx512m"  (Windows)
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash)
setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh)

Java:Loading the file

The dev environment and the actual service server are different and often face the problem of path to the config file with FileNotFound Exception or so. Here is a solution.

URL propXML = this.getClass().getResource(RESFILE);
Configuration config = new XMLConfiguration(propXML);


or

ClassLoader loader = Thread.currentThread().getContextClassLoader();
DOMConfigurator.configure(loader.getResource("log4j.xml"));

Monday, August 13, 2007

Formats decimal numbers by Java

DecimalFormat df = new DecimalFormat("0000");
df.format(77);
#OUTPUT: 0077


DecimalFormat df = new DecimalFormat("####");
df.format(77);
#OUTPUT: 77


DecimalFormat df = new DecimalFormat("####%");
df.format(77);
#OUTPUT: 77%


DecimalFormat df = new DecimalFormat("'AA'####%");
df.format(77);
#OUTPUT: AA77%


DecimalFormat df = new DecimalFormat("\u00A5###,###");
df.format(7777);
#OUTPUT: \7,777

Sunday, June 17, 2007

Ubuntu Java configuration

In case you need to change the version of java runtime environment which has been installed on you machine. It's simple as this,
$  sudo /usr/sbin/update-alternatives  --config   java

Tuesday, June 12, 2007

Maven2 with Hibernate

Maven cannot solve the dependency regarding jta as it is released by Sun and based upon their binary license it cannot be deployed on the maven repository.

You will see something like this below.
1) javax.transaction:jta:jar:1.0.1B
Try downloading the file manually from:
http://java.sun.com/products/jta
Then, install it using the command:
mvn install:install-file -DgroupId=javax.transaction -DartifactId=jta \
-Dversion=1.0.1B -Dpackaging=jar -Dfile=/path/to/file
Path to dependency:
1) RssUpdater:RssUpdater:package:0.0.1
2) org.hibernate:hibernate:jar:3.2.4.ga
3) javax.transaction:jta:jar:1.0.1B
1 required artifact is missing.
for artifact:
RssUpdater:RssUpdater:package:0.0.1
from the specified remote repositories:
codehaus.org (http://snapshots.repository.codehaus.org/org/codehaus/mojo/),
maven.seasar.org (http://maven.seasar.org/maven2),
central (http://repo1.maven.org/maven2)



In order to avoid this, download jta and deploy it onto your local maven repos.

mvn install:install-file \
-Dfile=./jta-1_0_1B-classes.zip \
-DgroupId=javax.transaction \
-DartifactId=jta -Dversion=1.0.1B \
-Dpackaging=jar

Thursday, May 17, 2007

Commons Digester Variable

The variable in xml data can be converted with Digester with the easy following codes.
Digester digester = DigesterLoader.createDigester(getURL(ruleFile));
Map vars = new HashMap();
vars.put("hello","Czesc!");
MultiVariableExpander expander = new MultiVariableExpander();
expander.addSource("$", vars);
Substitutor substitutor = new VariableSubstitutor(expander);
digester.setSubstitutor(substitutor);

Tuesday, May 15, 2007

Commons Lang toString()

Jakarta Commons Lang support the reflection for toString() which need to synchronize with the changes made for the object model.

public String toString(){
return ReflectionToStringBuilder.toString(this);
}


For the customization of the output,
public String toString(){
return ReflectionToStringBuilder.toString(this,ToStringStyle.MULTI_LINE_STYLE)
.append("lastName",lastName).append("firstName",firstName).toString();
}

Monday, March 19, 2007

Tomcat JkShmFile

Under the default config, you might get error when you boot up the tomcat saying,

jk_child_init::mod_jk.c (2317): Attachning shm:/etc/apache2/logs/jk-runtime-status errno=2

Write the following in /etc/apache2/mod-available/jk.load
JkShmFile /var/log/apache2/jk-runtime-status

Monday, November 06, 2006

Regex

Small sample to replace the regular expression..
Using Core API
String body = "\n";
String out;
Pattern pattern = Pattern.compile("class=\".*?\"");
Matcher matcher = pattern.matcher(body);
out = matcher.replaceAll("");
System.out.println(out);


Using Jakarta ORO
String body = "\n";
String out;
Perl5Util util = new Perl5Util();
out = util.substitute("s/ class=\".*?\"//g", body);
System.out.println(out);