Wednesday, September 29, 2010

Confguring Java mail with JBoss AS 5 and gmail

I've been reading the excellent book: JBoss AS 5 Development and the Example in Chapter 4 creates a Mailer EJB, however the book gives no indication on how to configure Java Mail in JBoss, so I thought I'd share.

  1. Navigate to:

  2. $JBOSS_HOME/server/default/deploy

  3. Open the file: mail-service.xml in your favorite text editor
  4. Edit it as follows:


  5. <server>

    <mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">${username}@gmail.com</attribute>
    <attribute name="Password">${password}</attribute>
    <attribute name="Configuration">
    <!-- A test configuration -->
    <configuration>
    <!-- Change to your mail server prototocol -->
    <property name="mail.store.protocol" value="pop3" />
    <property name="mail.transport.protocol" value="smtp" />

    <!-- Change to the user who will receive mail -->
    <property name="mail.user" value="${username}@gmail.com" />

    <!-- Change to the mail server -->
    <property name="mail.pop3.host" value="pop.gmail.com" />

    <!-- Change to the SMTP gateway server -->
    <property name="mail.smtp.host" value="smtp.gmail.com" />
    <property name="mail.smtp.auth" value="true" />
    <property name="mail.smtp.user" value="${username}@gmail.com" />
    <property name="mail.smtp.password" value="${password}" />
    <property name="mail.smtp.ssl.enable" value="true" />
    <property name="mail.smtp.starttls.enable" value="true" />
    <property name="mail.smtp.socketFactory.class"
    value="javax.net.ssl.SSLSocketFactory" />

    <!-- The mail server port -->
    <property name="mail.smtp.port" value="465" />

    <!-- Change the default address mail will be from -->
    <property name="mail.from" value="${username}@gmail.com" />

    <!-- Enable debugging output from the javamail classes -->
    <property name="mail.debug" value="false" />
    </configuration>
    </attribute>
    <depends>jboss:service=Naming</depends>
    </mbean>
    </server>

NOTE: If you are using Google Apps, simply change the gmail.com extension to your-domain.com.

Friday, June 4, 2010

Testing for a number in the ksh

Quick script for testing for a number in the ksh
#!/bin/ksh

if [[ $1 = ?([+-])+([0-9]) ]]; then
echo "true";
else
echo "false";
fi

Wednesday, May 19, 2010

Sending a mail with an attachment

I always end up googling this; so thought I'd add it to my blog - leaving me only one place to look!

To mail a single attachment (with no message):

uuencode filename.txt.gz filename.txt.gz | mailx -s subject email_address

To mail a single attachment with a message:

(cat message.txt; uuencode filename.txt.gz filename.txt.gz) | mailx -s subject email_address

To mail multiple attachments:

(cat message.txt; uuencode filename01.txt.gz filename02.txt.gz && uuencode filename02.txt.gz filename02.txt.gz) | mailx -s subject email_address

Friday, September 25, 2009

Hacking the Trac Database

Unfortunately my Trac Server at work has been down for a number of days and I've had to manually log tickets in an Excel spreadsheet. The network team have finally fixed the issue and I've just finished importing the tickets using the excellent csv2trac.py Python program.

Unfortunately I need to adjust some of the dates which can be done easily using sqlite. Essentially log on the the trac database using the following command:
sqlite3 /path/to/trac/project/db

All dates and times in sqlite are stored as the number of seconds since 01-Jan-1970, therefore to subtract 5-days from the creation date you would run:
UPDATE ticket
SET time = time - (5*24*60*60)
WHERE id = n

Wednesday, August 26, 2009

Configuring Hibernate 3 with c3p0

Thought I'd share a little tip that doesn't (currently) appear in the documentation for configuring c3p0 connection pooling with Hibernate 3.

Apart from setting the properties below (which are documented):
<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->

You must also set the following (un-documented) property:
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

If you do not set the provider_class property hibernate will continue to us the built-in connection pool!

Tuesday, June 16, 2009

Custom String Values for an Enum in Java

The default String value that Java returns for an enum is the actual name of the enum constant in use. For example given the following enum:

public enum DayOfWeek {

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

}
This simple program:
public class DayOfWeekTest {

public static void main(String[] args) {
DayOfWeek dow = DayOfWeek.MONDAY;
System.out.println(dow.toString());
}
}
Will return:
$ java DayOfWeekTest
MONDAY
But in the program above what I really want is an abbreviated version of the week day in Title Case; For example Mon. Now I could override the toString method of the enum class as follows:
public enum DayOfWeek {

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

public String toString() {
String dayOfWeek = name().toString();
dayOfWeek = dayOfWeek.charAt(0) + dayOfWeek.substring(1,3).toLowerCase();
return dayOfWeek;
}
}
Which does meet my needs, but it’s not the most obvious or readable piece of code. What Java allows you to do is provide a constructor for the enumeration as follows:
public enum DayOfWeek {

MONDAY("Mon"), TUESDAY("Tue"), WEDNESDAY("Wed"), THURSDAY("Thu"), FRIDAY("Fri"), SATURDAY("Sat"), SUNDAY("Sun");

private String dayOfWeek;

private DayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}

public String toString() {
return this.dayOfWeek;
}
}
Which I think we can all agree is a far simpler piece of code.

Now to make this enum really useful we should provide a method to convert a String back to an enum. If we simply used the supplied valueOf method in the DayOfWeekTest as follows:
public class DayOfWeekTest {

public static void main(String[] args) {
DayOfWeek mon = DayOfWeek.MONDAY;
System.out.println(mon.toString());
DayOfWeek tue = DayOfWeek.valueOf("Tue");
System.out.println(tue.toString());
}
}
A java.lang.IllegalArgumentException: No enum const class DayOfWeek.Tue is thrown as the valueOf method expects the full name of the enum constant; For example TUESDAY. A new method is needed, therefore out final enum becomes:
public enum DayOfWeek {

MONDAY("Mon"), TUESDAY("Tue"), WEDNESDAY("Wed"), THURSDAY("Thu"), FRIDAY("Fri"), SATURDAY("Sat"), SUNDAY("Sun");

private String dayOfWeek;

private DayOfWeek(String dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}

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

public static DayOfWeek getValue(String dayOfWeek) {
for (DayOfWeek dow : DayOfWeek.values()) {
// Use equalsIgnoreCase to make the getValue method a little more robust
if (dow.toString().equalsIgnoreCase(dayOfWeek)) {
return dow;
}
}
return null;
}
}

We then change the valueOf method in the DayOfWeekTest to the getValue method as follows:
public class DayOfWeekTest {

public static void main(String[] args) {
DayOfWeek mon = DayOfWeek.MONDAY;
System.out.println(mon.toString());
DayOfWeek tue = DayOfWeek.getValue("Tue");
System.out.println(tue.toString());
}
}
Run the program and Voila!
$ java DayOfWeekTest
Mon
Tue

Wednesday, June 10, 2009

Mimicing "break" in a PL/SQL FOR Loop

It seems a major omission in the Oracle PL/SQL Programming language that the BREAK keyword cannot be used in FOR loops (or WHILE loops) to skip to the next iteration.

Here is a (trivial) example of how you can implement "break" functionality using exceptions:
DECLARE
e_skip_row EXCEPTION;
l_total PLS_INTEGER := 0;
BEGIN
--
dbms_output.enable(10000);
--
FOR l_index IN 1..100 LOOP
BEGIN
--
-- If the row is even then skip
IF MOD(l_index,2) = 0 THEN
RAISE e_skip_row;
END IF;
--
l_total := l_total + l_index;
--
EXCEPTION
WHEN e_skip_row THEN NULL;
END;
END LOOP;
--
dbms_output.put_line('Total is: '||l_total);
--
END;