Archive for the ‘Java’ Category

Google API References

Sunday, December 11th, 2011

Java: C3P0 Maven (and hibernate c3p0 maven) and quick start

Saturday, May 21st, 2011
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("swaldman");
cpds.setPassword("test-password");                                  

// the settings below are optional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);

// The DataSource cpds is now a fully configured and usable pooled DataSource

...

Cleaning up on app shutdown

DataSources.destroy( cpds );

c3p0 Doc

For Hibernate, the maven dist is

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
  <version>3.5.1-Final</version>
</dependency>

Java: slf4j log factory initialization

Saturday, May 21st, 2011
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(AnyClass.class);

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

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 ).

Publishing Artifacts to Central Repository

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