Archive for the ‘Java’ Category

Tomcat: Session Timeout

Thursday, August 5th, 2010

In the Web.xml

 <web-app>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
...
 </web-app>

ant: delete, copy, jar, mkdir tasks

Tuesday, July 27th, 2010

Ant Task Overview

Delete

<delete>
   <fileset dir="WEB-INF/lib" includes="**/some*.jar" excludes="**/some-other*.jar" />
</delete>

Ant Delete Doc

Copy

<copy todir="../new/dir">
    <fileset dir="src_dir"/>
</copy>

<copy todir="../dest/dir">
    <fileset dir="src_dir">
      <exclude name="**/*.java"/>
    </fileset>
</copy>

<copy todir="../dest/dir">
    <fileset dir="src_dir" excludes="**/*.java"/>
</copy>

Ant Copy Doc

Jetty: Configuration, Web Application, Virtual Hosting

Thursday, July 22nd, 2010

Set a WebApp directly from directory (without .war file)

    <!-- =========================================================== -->
    <!-- Set handler Collection Structure                            -->
    <!-- =========================================================== -->
    <Set name="handler">
      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
           <Item>
             <New id="akuiContext" class="org.eclipse.jetty.webapp.WebAppContext">
                <Set name="contextPath">/mywebapp</Set>
                <Set name="resourceBase">/path/to/single/mywebapp</Set>
             </New>
           </Item>
         </Array>
        </Set>
      </New>
    </Set>

Remote Debuging

#format: -agentlib:jdwp=name1[=value1],name2[=value2]...
java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n start.jar

help: prints a brief message on how to use it and exits the VM.

server: (”n” or “y”) If “y,” listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address.

address: transport address for the connection. If server=n, attempt to attach to a debugger application at this address. If server=y, listen for a connection at this address.

timeout: If server=y, this specifies the timeout, in milliseconds, to wait for the debugger to attach. If server=n, this specifies the timeout, in milliseconds, to use when attaching to the debugger.

suspend: If “y,” JVM suspends the execution until a debugger connects to the debuggee JVM.

More info: Techrepublic and Source: StackOverflow

Other links

Maven: Maven Tips & Quick Start & Cheat Sheet

Monday, July 19th, 2010

Skip Test

mvn -Dmaven.test.skip=true package

Official JBoss Repository for Hibernate and more

<repository>
 <id>jboss-repository-group</id>
 <name>jboss-repository-group</name>
 <url>https://repository.jboss.org/nexus/content/groups/public/</url>
 <layout>default</layout>
</repository>

Note: the “http://repository.jboss.org/maven2/” is deprecated. Use the above one (Announcement, Getting Started Doc ).

Generate Source

mvn source:jar

maven source doc

Generate Source

mvn javadoc:jar

maven javadoc doc

Publishing Artifacts to Central Repository

Maven Sonatype/Maven Central commands

Publish Snapshots

mvn clean deploy

Or better with source

mvn clean source:jar javadoc:jar deploy

Stage the release

# prepare it (will create the new tag in sonatype SVN, automatically checking in on your behalf).
mvn release:clean
mvn release:prepare
#stage the release
mvn release:perform

Maven will checkout the tag you just prepared, then build and deploy it into Nexus staging repository.

release it

Other Links

Java: Open CSV Parser

Monday, April 12th, 2010

Parsing CSV is not as simple at it seems. Not hard, but cumbersome.

Here is a simple Lib that does the job: Open CSV (download)

CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
	System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
}

Java: Validation JSR303, Hibernate, JavaScript

Thursday, April 1st, 2010

Java: Move a File to a new Directory (Folder)

Saturday, February 6th, 2010

Yes, File.renameTo is used to move file.

// File (or directory) to be moved
    File file = new File("filename");

    // Destination directory
    File dir = new File("directoryname");

    // Move file to new directory
    boolean success = file.renameTo(new File(dir, file.getName()));
    if (!success) {
        // File was not successfully moved
    }

source

Java: Image IO, JAI

Friday, August 21st, 2009

Docs

Sample code links:

  • The Perils of Image.getScaledInstance() (Best algorigthm to scale down/up images with ImageIO)
    • “… be aware that if you try to downscale an image by a factor of more than two (i.e., the scaled instance is less than half the size of the original), and you are using the BILINEAR or BICUBIC hint, the quality of the scaled instance may not be as smooth as you might like…. To combat this issue, you can use a multi-step approach when downscaling by more than two times; this helps prevent the information loss issue and produces a much higher quality result that is visually quite close to that produced by Image.SCALE_AREA_AVERAGING.”
  • JAI Tutorial (Sun)
  • Resizing with JAI
  • Resizing with JAI (Subsample for best result)

Other Samples

Java: Sorting List with Comparator

Tuesday, August 18th, 2009
class EmpSortByName implements Comparator{

    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

....
List employees = ...;
Collections.sort(employees , new EmpSortByName());

java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.

  1. positive – this object is greater than o1
  2. zero – this object equals to o1
  3. negative – this object is less than o1

java.lang.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.

  1. positive – o1 is greater than o2
  2. zero – o1 equals to o2
  3. negative – o1 is less than o1

Source: Java Sorting: Comparator vs Comparable Tutorial

Java: BeanUtils Enum Support - generic Enum converter

Monday, July 6th, 2009

By default, apache commons BeanUtils does not support string value to their targeted Enum. You can write a converter for each enum, but this quickly become unmanageable.

Here is a quick fix to automatically convert the string value to the targeted enum.

BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean(){
            @Override
            public Object convert(String value, Class clazz) {
                  if (clazz.isEnum()){
                       return Enum.valueOf(clazz, value);
                  }else{
                       return super.convert(value, clazz);
                  }
           }
        });

Now, you can call beanUtilsBean.populate(bean,map) even for enum values.