7/16/2010

String.contains and String.indexOf

To check if a string contains a substring, I usually use:

if(s.indexOf(sub) >= 0)
For JDK 5 or later, a contains method can also be used, which seems to be a little more readable:
if(s.contains(sub))
The signature of contains method is:
public boolean contains(java.lang.CharSequence s);
Note that CharSequence is a super-interface of String, StringBuffer, StringBuilder, java.nio.CharBuffer, javax.swing.text.Segment. So you can pass any of the 5 types to contains method.

The current implementation of contains method just convert the param to String and calls indexOf.

7/14/2010

Java debug options in ant build.xml

To add debug options to java (Sun's JDK) command line:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9009
If the java process runs and terminates itself, change suspend=y, so you can have a chance to attach the remote debugger.

To add the same debug options to ant build.xml java task:
<target name="run">
 <java fork="on"
       failonerror="true"
       classpath="xxx"
       classname="xxx">
     <jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009" />
     <arg line="--arg1 arg2 --arg3 arg4"/>
  </java>
</target>
Or, with the newer -agentlib option:
<target name="test" depends="compile">
    <junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="true">
      <formatter type="plain" usefile="false"/>
      <formatter type="xml"/>
      <test name="FooBeanTest"
          todir="xxx"/>
      <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />
      <classpath>
        <path refid="test.compile.classpath"/>
        <path refid="javaee.classpath"/>
        <pathelement path="${build.dir}"/>
        <pathelement path="${junit.jar}"/>
      </classpath>
    </junit>
  </target>
Note: <arg> and <jvmarg> take either line or value attribute, but value attributes are treated as one single argument that may contain spaces. So in our case line attribute is used to specify multiple arguments separated by spaces. If value attribute was used instead of line attribute, the debug options will not take effect.
The same element can also be used on ant testng task to debug test run:
<testng mode="testng" dumpCommand="true" workingDir="." failureProperty="tests.failed" outputdir="results">
    <classpath>
        <fileset dir="lib">
            <include name="*.jar" />
        </fileset>
    </classpath>
    <xmlfileset dir="." includes="testng-suite.xml" />
    <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />
</testng>

7/04/2010

Some basic steps using Derby 10.5

The following is some basic steps of using derby bundled with GlassFish.

1. start derby database server:

$GLASSFISH_HOME/bin/asadmin start-database
or start it on a non-default port number:

$GLASSFISH_HOME/bin/asadmin start-database --dbport 1528
2. make sure environment variable DERBY_HOME is set:
$ echo $DERBY_HOME
/Users/root/v3/glassfish/../javadb
3. start derby ij (or ij.bat), connect to derby server:
$ $DERBY_HOME/bin/ij
ij version 10.5
ij> connect 'jdbc:derby://localhost:1527/sun-appserv-samples;user=APP;password=APP;create=true';

ij> select count(*) COUNT from sys.systables;
COUNT
-----------
24

1 row selected

ij> select OWNERID from EJB__TIMER__TBL;
ij> delete from EJB__TIMER__TBL;

ij> show tables;
TABLE_SCHEM |TABLE_NAME |REMARKS
-------------------------------------------
SYS |SYSALIASES |
SYS |SYSCHECKS |
SYS |SYSCOLPERMS |
SYS |SYSCOLUMNS |
SYS |SYSCONGLOMERATES |
SYS |SYSCONSTRAINTS |
SYS |SYSDEPENDS |
SYS |SYSFILES |
SYS |SYSFOREIGNKEYS |
SYS |SYSKEYS |
SYS |SYSROLES |
SYS |SYSROUTINEPERMS |
SYS |SYSSCHEMAS |
SYS |SYSSTATEMENTS |
SYS |SYSSTATISTICS |
SYS |SYSTABLEPERMS |
SYS |SYSTABLES |
SYS |SYSTRIGGERS |
SYS |SYSVIEWS |
SYSIBM |SYSDUMMY1 |
APP |LEAGUE |
APP |PLAYER |
APP |TEAM |
APP |TEAMPLAYER |

24 rows selected

ij> describe player;
COLUMN_NAME |TYPE_NAME|DEC&|NUM&|COLUM&|COLUMN_DEF|CHAR_OCTE&|IS_NULL&
------------------------------------------------------------------------------
PLAYER_ID |VARCHAR |NULL|NULL|255 |NULL |510 |NO
NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES
POSITION |VARCHAR |NULL|NULL|255 |NULL |510 |YES
SALARY |DOUBLE |NULL|2 |52 |NULL |NULL |NO

4 rows selected

ij> CREATE TABLE FOO (FOO_ID BIGINT, FOO_NAME VARCHAR(25), DESCRIPTION VARCHAR(64));
0 rows inserted/updated/deleted

ij> insert into foo values(1, 'foo1', 'foo1, that is.');
1 row inserted/updated/deleted

ij> select * from foo;
FOO_ID |FOO_NAME |DESCRIPTION
---------------------------------------------------------------------------------------------------------------
1 |foo1 |foo1, that is.

1 row selected

ij> update foo set foo_name='foo1 new name' where foo_id=1;
1 row inserted/updated/deleted

ij> delete from foo where foo_id=1;
1 row inserted/updated/deleted

ij> select count(*) count from foo;
COUNT
-----------
0

1 row selected

ij> drop table foo;
0 rows inserted/updated/deleted

To start ij and connect to a database in single step:
java -classpath $GLASSFISH_HOME/../javadb/lib/derbyrun.jar -Dij.connection.sample="jdbc:derby://localhost:1527/sun-appserv-samples;user=APP;password=APP;create=true" org.apache.derby.tools.ij
When starting derby with GlassFish "asadmin start-database" command, the default host is localhost, the default port number is 1527, and the default derby system home is set to $GLASSFISH_HOME/databases. This is also the directory where derby.log and database files will be stored. To overwrite the defaults with the following options:
asadmin start-database --dbhost dell-01 --dbport 3333 --terse=true --dbhome $HOME/derby

A sample web.xml (servlet 3.0) with ejb-ref and ejb-local-ref

This is a sample web.xml based on servlet 3.0 (part of Java EE 6).

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<servlet>
<display-name>Servlet1</display-name>
<servlet-name>Servlet1</servlet-name>
<servlet-class>test.Servlet1</servlet-class>
<init-param>
<param-name>sleep-time-in-seconds</param-name>
<param-value>10</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>

<ejb-ref>
<ejb-ref-name>ejb/fooremote</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<remote>test.FooRemoteIF</remote>
</ejb-ref>

<ejb-local-ref>
<ejb-ref-name>ejb/foo</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>test.FooIF</local>
<ejb-link>FooBean</ejb-link>
</ejb-local-ref>
</web-app>