Setup a local smtp server for JBoss

In my Seam application I needed a local smtp server for testing the email facilities Seam offers. After some research on the internet I came up with a configuration which uses Ubuntu’s Mail Transfer Agent postfix with Gmail as the so called smarthost. Postfix will act as an intermediate smtp server which will dispatch the mails it receives to Gmail‘s smtp server.
This blog will summarize the steps needed for this setup.

Postfix installation and configuration

First, let’s install postfix.

sudo apt-get install postfix

Next, add additional configuration.

sudo dpkg-reconfigure postfix

Choose the following settings. For settings not listed below, you can keep the default settings.

  • General type of mail configuration: Internet with smarthost
  • SMTP relay host: smtp.gmail.com:587
  • Internet protocols to use: all

Next add settings necessary for authentication against the Gmail smtp server.

sudo postconf -e 'smtp_use_tls = yes'
sudo postconf -e 'smtp_sasl_auth_enable = yes'
sudo postconf -e 'smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd'
sudo postconf -e 'smtp_sasl_security_options = noanonymous'
sudo postconf -e 'smtp_sasl_tls_security_options = noanonymous'

Next add the password file /etc/postfix/sasl_passwd containing the username and password of your Gmail account.

smtp.gmail.com username@gmail.com:PASSWORD

Now, hash the password file (this will create the hashed sasl_passwd.db file), throw away the original file and restart the postfix daemon.

sudo postmap /etc/postfix/sasl_passwd
sudo rm /etc/postfix/sasl_passwd
sudo /etc/init.d/postfix restart

Testing postfix

You can now test the postfix configuration via telnet.

telnet localhost 25
>mail from:noreply@gmail.com
>rcpt to:username@gmail.com
>data
>Testmail
>.
>quit

This should send a small email to username@gmail.com.

Troubleshooting

If the mail didn’t arrive, you can check a couple of things:
Check if it’s in the postfix queue:

sudo mailq

Flush the queue:

sudo postfix -f

Check the log file:

sudo tail /var/log/mail.log

JBoss configuration

Next we’re gonna add the mail server configuration to JBoss, so it van be accessed via JNDI. Edit the file $JBOSS_HOME/server/default/deploy/mail-service.xml.

<?xml version="1.0" encoding="UTF-8"?>
<server>
  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="Configuration">
      <configuration>
        <!-- Change to your mail server prototocol -->
        <property name="mail.transport.protocol" value="smtp"/>
        <!-- Change to the SMTP gateway server -->
        <property name="mail.smtp.host" value="localhost"/>
        <!-- The mail server port -->
        <property name="mail.smtp.port" value="25"/>
        <!-- Change to the address mail will be from  -->
        <property name="mail.from" value="noreply@gmail.com"/>
        <!-- Enable debugging output from the javamail classes -->
        <property name="mail.debug" value="false"/>
      </configuration>
    </attribute>
    <depends>jboss:service=Naming</depends>
  </mbean>
</server>

Seam configuration

Finally we need to configure seam so it can communicate with our new smtp server. This is done in the component descriptor components.xml.

<?xml version="1.0" encoding="UTF-8"?>
<components
  xmlns="http://jboss.com/products/seam/components"
  xmlns:mail="http://jboss.com/products/seam/mail"
  xsi:schemaLocation="http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
    http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
  ....
  <mail:mail-session session-jndi-name="java:/Mail" />
  ....
</components>

If you’re also using jBPM for business processes within your seam application and need email facilities, you can configure the smtp server in the jBPM configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<jbpm-configuration>
  ....
  <string name="jbpm.mail.smtp.host" value="localhost"/>
  <string name="jbpm.mail.from.address" value="noreply@open18.org"/>
</jbpm-configuration>
Advertisements

Seam-gen on Glassfish – part III

In this last part we’re gonna create the seam-gen project, import it in NetBeans, deploy it and at the end we have a running application.

1. Create the project

Creating the skeleton of the seam-gen project is done by issuing the following seam-gen command

$ ./seam create-project

If you stuck to the defaults there will be a project directory called open18 in your home directory.

2. Generate the JPA classes

Next we’ll generate the JPA classes from the tables in the open18 database into the new project. Again this is done by a seam-gen command

$ ./seam generate

3. Import in NetBeans

Now let’s import the project into NetBeans. It should look like the picture below

open18 project

The build targets for the NetBeans project are coupled to the default (i.e. JBoss) deploy targets. Let’s change them to their Glassfish counterparts. Right click on the open18 project and choose properties. Change the targets so they’ll look like this

open 18 build properties

4. Change build-glassfish.xml

This is the build file that contains all the Glassfish specific Ant targets. One thing it does, is copying all the Hibernate files to the lib/ext folder of the Glassfish domain (Glassfish uses EclipseLink as its default JPA provider).

We’re gonna make 2 changes to this file.

First we’re gonna make sure that the slf4j/log4j libraries also get copied to the Glassfish domain. If you omit this step, the application won’t deploy properly. Add the following xml stanza to the gf-deploy-hibernate target in the glassfish-build.xml file so that the copy task which copies the libraries to Glassfish resembles the example below.

        <copy todir="${glassfish.domains.dir}/${glassfish.domain}/lib/ext" overwrite="true">
            <fileset dir="${basedir}/lib">
                ....
                <!-- Include slf4j/log4j JARs below -->
                <include name="jcl-over-slf4j.jar"/>
                <include name="slf4j-api.jar"/>
                <include name="slf4j-log4j12.jar"/>
                <include name="log4j.jar"/>
            </fileset>
        </copy>

Next let’s fix a small bug in the deployment of the libraries to a Glassfish v2 server. If you don’t change this the asm.jar, asm-attrs.jar, cglib.jar and cglib-nodep.jar won’t be copied into the Glassfish domain.

Alter the lines in the gf-deploy-hibernate target that check whether the Glassfish server is a version 2  or 3.

<condition property="glassfish.v3" value="true" else="false">
    <available file="${glassfish.home}/glassfish" type="dir"/>
</condition>
<condition property="glassfish.domains.dir" value="${glassfish.home}/glassfish/domains" else="${glassfish.home}/domains">
    <istrue value="${glassfish.v3}"/>
</condition>

5. Prepare Glassfish for Hibernate

Now let’s copy all those library files to the Glassfish domain. Right click the build.xml file in the NetBeans project and choose Run Target > gf-prepare.

One last thing that needs to be taken care of, is fixing a bug in the Seam code regarding Hibernate and mysql. If you don’t do this, the facelet TeeList.xhtml (which corresponds with the Tee List menu item in the open18 application) will give you the following error message

/TeeList.xhtml: javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query

A nice workaround is provided here. In this workaround the flawed Seam component org.jboss.seam.persistence.persistenceProvider is overriden with the following Seam component (which has a higher precedence)

package org.open18.persistence;

import javax.annotation.PostConstruct;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.jboss.seam.persistence.HibernatePersistenceProvider;

/**
 * Addresses problem with HibernatePersistenceProvider by explicitly
 * calling HibernatePersistenceProvider.init() from
 * a PostConstruct method that actually gets called on postconstruct.
 *
 */
@Name("org.jboss.seam.persistence.persistenceProvider")
@Scope(ScopeType.STATELESS)
@BypassInterceptors
@Install(classDependencies={"org.hibernate.Session", "javax.persistence.EntityManager"})
public class MoBetterHibernatePersistenceProvider extends HibernatePersistenceProvider {

    private static Log log = Logging.getLog(MoBetterHibernatePersistenceProvider.class);

    @PostConstruct
    public void init()
    {
       log.debug("MoBetterPersistenceProvider init");
       super.init();
    }

}

6. Deploy the application

We’re almost there. Let’s deploy the application.

  • Right click the build.xml file and choose Run Target > gf-start to startup the Glassfish server;
  • Right click the open18 project and choose Clean and Build.

7. Run the application

Finally! You should be able to play with the open18 seam-gen app which runs on the following url: http://localhost:8080/open18.

Seam-gen on Glassfish – part II

In this second part we’re gonna setup an example database, prepare Glassfish for mysql and finally run seam setup to create the proper build.properties file for deployment to glassfish. I’m using the example schema provided by the excellent Seam in Action book by Manning. The setup scripts can be found in the zip file located here.

To recap I’m using the following file structure as recommended in the Seam in Action book:

  • ~/lib: this will contain the mysql driver, which will be copied by seam-gen to the Glassfish server;
  • ~/opt: this already contains jboss-seam and the Glassfish server;
  • ~/open18: this will contain the seam-gen generated project.

1. Installing the database schema

First download the seaminaction-20090413.zip file and extract it in your home directory.

Now login to the mysql database

$ mysql -u <user_name> -p

Now create a database and run the mysql scripts contained in the zip file

sql> CREATE DATABASE open18;
sql> USE open18;
sql> source ~/seaminaction/etc/schema/open18-initial-schema-mysql.sql
sql> source ~/seaminaction/etc/schema/open18-initial-seed-data-mysql.sql

2. Download and install the mysql driver

Next we’ll gonna provide seam-gen with a recent mysql driver

  • First, download the MySQL Connector/J zip file here.
  • Unzip this file and copy the mysql-connector-java-5.1.12-bin.jar to your ~/lib directory

The jar will be picked up by seam-gen later on and copied to the Glassfish domain. You can also choose to copy the driver file directly to the Glassfish server by copying the jar file to <glassfish_home>/domain/domain1/lib/ext (assuming you’ll be deploying the seam-gen app to this default domain).

3. Running seam-gen setup

Now it’s time to run seam-gen setup. This will create the proper build.properties file needed for the creation the the NetBeans project later on.

Switch to ~/opt/jboss-seam-2.2.1.CR1 and enter

$ ./seam setup

Answer the questions and verifiy the generated build.properties file in the ~/opt//jboss-seam-2.2.1.CR1/seam-gen directory. It should look like this:

seam-gen build.properties

Now all the hard work is done, in the last part of this blog series, we’re gonna create the seam-gen project and deploy it to our Glassfish domain!!

Seam-gen on Glassfish – part I

I recently managed to get a seam-gen app running on Glassfish. To share how this was done, i’ve divided this blog in 3 parts. This part will handle most of the prerequisite steps.

The complete setup

  • Ubuntu 9.10
  • Mysql server 5.1
  • Glassfish v2.1.1
  • Jboss Seam 2.2.1.CR1
  • Netbeans 6.8
  • Ant 1.7.1
  • Subversion 1.6.5

Jboss Seam and the Glassfish server will be installed under ~/opt.

1. Install JDK 6

First of all you need to install a recent JDK version

$ sudo apt-get update
$ sudo apt-get install sun-java6-jdk

Note: if you’re using ubuntu10 you need to enable the “partner” repository first. This is done via the System > Administration > Software menu.

2. Install Mysql Server 5.1

Next we’re gonna install mysql server on ubuntu

$sudo apt-get install mysql-server

3. Install Subversion (optional)

For versioning support we’ll be using subversion with http support

$ sudo apt-get install subversion
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-svn

3a. Create repository

Next we’re gonna create a svn repository

$ sudo svnadmin create /var/svn/repos

3b. Edit svn configuration

Edit the subversion configuration

$ sudo gedit /etc/apache2/mods-enabled/dav_svn.conf

Alter this so that effectively when ignoring all comments it’ll look like this

<Location /svn>
    DAV svn
    SVNPath /var/svn/repos
    AuthType Basic
    AuthName "Subversion Repository"
    AuthUserFile /etc/apache2/dav_svn.passwd
</Location>

3c. Create a svn user

We need a user to connect to the svn repository

$ sudo htpasswd -cm /etc/apache2/dav_svn.passwd <user_name>

When prompted enter a pasword.

3d. Restart Apache

$ sudo /etc/init.d/apache2 restart

3e. Change repository owner and rights

You need to change the owner and add some group rights on the repository files, or else you’ll get an error when commiting changes tot the repository over http.

$ sudo chown www-data:www-data  /var/svn/repos -R
$ sudo chmod g+w /var/svn/repos -R

Finally you can test if you can connect to your repository via the url http://127.0.0.1/svn

4. Install Netbeans 6.8

Download the installer file Netbeans

$ chmod +x <installer-file-name>.sh
$ sudo ./<installer-file-name>.sh

Accept the defaults. Netbeans will be installed in the /usr/local directory.

5. Install Glassfish 2.1.1

Download the installer file Glassfish v2 into a temporary directory

$ java -Xmx256m -jar <installer-file-name>.jar
$ mv glassfish ~/opt/glassfishv2
$ cd ~/opt/glassfishv2
$ chmod -R +x lib/ant/bin
$ ./lib/ant/bin/ant -f setup.xml

Note: To prevent future problems when the jdk version get updated make sure, that glassfish uses the symbolic link to the jdk installation.
The JDK home is set in the file <glassfish_home>/config/asenv.conf. It should read:

AS_JAVA="/usr/lib/jvm/java-6-sun/jre/.."

6. Install Jboss-seam

Download the latest seam distibution from the Seam website and unzip it into ~/opt/

7. Install Ant

$sudo apt-get install ant

If you’ve come this far, you”ll have all the components you need to get started with seam-gen!!

%d bloggers like this: